blob: 20e1620f7c63169f00eb501ae7424cb4291faa30 [file] [log] [blame]
velandy9d1a08d2016-06-27 16:48:41 +02001#!/bin/bash
Philip Joseph15b76cb2016-07-08 01:42:08 +05302
3############################################################################
4# Copyright 2016 RIFT.io Inc #
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17############################################################################
18
Austin Cormier3aeed122016-05-23 19:12:00 -040019#
20# This shell script is used to create a descriptor package
21# The main functions of this script include:
22# - Generate checksums.txt file
23# - Generate a tar.gz file
Philip Joseph15b76cb2016-07-08 01:42:08 +053024# This script can be used to create the required folders for
25# a descriptor package and a template descriptor
Austin Cormier3aeed122016-05-23 19:12:00 -040026
Philip Joseph15b76cb2016-07-08 01:42:08 +053027# Usage: generate_descriptor_pkg.sh <base-directory> <package-directory>
Austin Cormier3aeed122016-05-23 19:12:00 -040028
Philip Joseph15b76cb2016-07-08 01:42:08 +053029# Descriptor names should be
30# - (nsd|vnfd).(yaml|yml|json|xml)
31# - *_(nsd|vnfd).(yaml|yml|json|xml)
32# - *_(nsd|vnfd)_*.(yaml|yml|json|xml)
33# - (nsd|vnfd)/*.(yaml|yml|json|xml)
34#
Austin Cormier3aeed122016-05-23 19:12:00 -040035
Philip Joseph15b76cb2016-07-08 01:42:08 +053036SCRIPTNAME=`basename $0`
Austin Cormier3aeed122016-05-23 19:12:00 -040037
Philip Joseph15b76cb2016-07-08 01:42:08 +053038# From https://osm.etsi.org/wikipub/index.php/Release_0_Data_Model_Details
39# Supported folders for VNFD
40# cloud_init - Rel 4.3, not yet part of OSM
41VNFD_FOLDERS=(images scripts icons charms cloud_init)
42
43# Supported folders for NSD
44# OSM document specifies (ns|vnf)-config folder, while Rel 4.3
45# is using (ns|vnf)_config.
46NSD_FOLDERS=(scripts icons ns_config vnf_config)
47
48# Other files allowed in the descriptor base directory
49ALLOWED_FILES=(README)
50
51DESC_TYPES=(vnfd nsd)
52DESC_EXTN=(yml yaml json xml)
53CHKSUM='checksums.txt'
54
55VERBOSE=false
56DRY_RUN=false
57CREATE=false
58RM="--remove-files"
59DEBUG=false
60
Philip Joseph1c36f4a2016-09-14 07:05:33 +000061ARCHIVE=false
62CREATE_NSD=false
63VENDOR='OSM'
64INTF_TYPE='VIRTIO'
65VCPU=2
66MEMORY=4096
67STORAGE=10
Philip Joseph8f0826d2016-09-28 21:40:39 +053068INTERFACES=1
Philip Joseph1c36f4a2016-09-14 07:05:33 +000069
Philip Joseph15b76cb2016-07-08 01:42:08 +053070function usage() {
71 cat <<EOF
72Usage:
Philip Joseph1c36f4a2016-09-14 07:05:33 +000073 $SCRIPTNAME [-t <type>] [-N] [-c] [base-directory] <package-name>
Philip Joseph15b76cb2016-07-08 01:42:08 +053074
75 -h|--help : show this message
76
77 -t|--package-type <nsd|vnfd> : Descriptor package type
78 is NSD or VNFD. Script will try to
79 determine the type if not provided.
80 Default is vnfd for create-folders.
81
82 -d|--destination-dir <destination directory>: Directory to create the
83 archived file.
Philip Joseph1c36f4a2016-09-14 07:05:33 +000084 Default is base-directory
Philip Joseph15b76cb2016-07-08 01:42:08 +053085
86 -N|--no-remove-files : Do not remove the package files after creating
87 archive
88
Philip Joseph1c36f4a2016-09-14 07:05:33 +000089 Options specifc for create descriptor:
Philip Joseph15b76cb2016-07-08 01:42:08 +053090
Philip Joseph1c36f4a2016-09-14 07:05:33 +000091 -c|--create-folder : Create folder with the structure for the
92 package type using the base-dir and package-dir
93 and a descriptor template
94
95 -a|--archive: Create package for the descriptor
96
97 --nsd : Generate NSD descriptor package also.
98
99 --vendor : Vendor name for descriptor. Default OSM
100
Philip Joseph89c5ea02016-09-28 00:35:59 +0530101 --interface-type : Interface type [VIRTIO|SR-IOV|PCI-PASSTHROUGH|E1000]
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000102 Default VIRTIO
103
104 VM Flavour options:
105
106 --vcpu : Virtual CPU count. Default 2
107
108 --memory : Memory for VM in MB. Default 4096MB
109
110 --storage : Storage size for VM in GB. Default 10GB
111
112 VDU Parameters:
113
114 --image : Location URI of the image
115
116 --cloud-init-file : Cloud init file
117
118 --cloud-init : Cloud init script. Will be ignored if
119 cloud-init-file is specified
120
Philip Joseph8f0826d2016-09-28 21:40:39 +0530121 --interfaces : Number of external interfaces in additon to OM-MGMT. Default 1.
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000122
123 End of create descriptor specific options
124
125 -v| --verbose : Generate progress details
Philip Joseph15b76cb2016-07-08 01:42:08 +0530126
127 -n| --dry-run : Validate the package dir
128
129 base-dir : Directory where the archive file or folders are created,
130 if destination directory is not specified.
131 Default is current directory
132
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000133 package-name : The descriptor name (full path if base-dir not specified)
Philip Joseph15b76cb2016-07-08 01:42:08 +0530134EOF
135}
136
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000137CP_TYPE='VPORT'
138function get_cp_type() {
139 case ${INTF_TYPE} in
140 VIRTIO ) CP_TYPE='VPORT';;
141 SR-IOV ) CP_TYPE='VPORT';;
142 PCI-PASSTHROUGH ) CP_TYPE='VPORT';;
143 OM-MGMT ) CP_TYPE='VPORT';;
144 E1000 ) CP_TYPE='VPORT';;
145 * ) echo "ERROR: Unknown interface type ${INTF_TYPE}"; exit 1;;
146 esac
147}
148
149# Get pci number starting from 0x0a
150get_pci() {
151 printf '%02x' $((10 + $1)) | tr '[:upper:]' '[:lower:]'
152}
153
Philip Joseph92b42602016-09-30 20:28:42 +0530154function write_readme() {
155 dir=$1
156 file=${dir}/README
157 date=$(date)
158
159 cat >$file <<EOF
160Descriptor created by OSM descriptor package generated
161Created on $date
162EOF
163
164}
165
Philip Joseph15b76cb2016-07-08 01:42:08 +0530166function write_vnfd_tmpl() {
167 name=$(basename $1)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000168 desc_file="${name}.yaml"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530169
170 cat >$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530171vnfd:vnfd-catalog:
172 vnfd:
173 - id: ${name}
174 name: ${name}
175 short-name: ${name}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000176 description: Generated by OSM pacakage generator
177 vendor: ${VENDOR}
178 version: '1.0'
Philip Joseph15b76cb2016-07-08 01:42:08 +0530179
180 # Place the logo as png in icons directory and provide the name here
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000181 # logo: <update, optional>
Philip Joseph15b76cb2016-07-08 01:42:08 +0530182
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000183 # Management interface
Philip Joseph15b76cb2016-07-08 01:42:08 +0530184 mgmt-interface:
Philip Joseph15b76cb2016-07-08 01:42:08 +0530185 vdu-id: ${name}-VM
Philip Joseph15b76cb2016-07-08 01:42:08 +0530186
187 # Atleast one VDU need to be specified
188 vdu:
Philip Joseph92b42602016-09-30 20:28:42 +0530189 # Additional VDUs can be created by copying the
190 # VDU descriptor below
Philip Joseph15b76cb2016-07-08 01:42:08 +0530191 - id: ${name}-VM
192 name: ${name}-VM
193 description: ${name}-VM
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000194 count: 1
Philip Joseph15b76cb2016-07-08 01:42:08 +0530195
196 # Flavour of the VM to be instantiated for the VDU
197 vm-flavor:
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000198 vcpu-count: ${VCPU}
199 memory-mb: ${MEMORY}
200 storage-gb: ${STORAGE}
Philip Joseph15b76cb2016-07-08 01:42:08 +0530201
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000202 # Image including the full path
203 image: '${IMAGE}'
204
205EOF
206
207 # Add the cloud init file or script
208 if [[ -n ${CLOUD_INIT_FILE} ]]; then
209 cif=$(basename ${CLOUD_INIT_FILE})
210 cat >>$desc_file <<EOF
211 # Cloud init file
212 cloud-init-file: '${cif}'
213EOF
Philip Josephc737a092016-09-27 12:34:32 +0530214 elif [[ -n ${CLOUD_INIT} ]]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000215 cat >>$desc_file <<EOF
216 # Cloud init to use
217 cloud-init: '${CLOUD_INIT}'
218EOF
219 fi
220
221 # Add external interfaces
222 cat >>$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530223 external-interface:
224 # Specify the external interfaces
225 # There can be multiple interfaces defined
Philip Joseph15b76cb2016-07-08 01:42:08 +0530226EOF
227
Philip Joseph89c5ea02016-09-28 00:35:59 +0530228 # Add mgmt interface
229 cat >>$desc_file <<EOF
230 - name: eth0
231 virtual-interface:
232 type: OM-MGMT
233 bandwidth: '0'
234 vpci: 0000:00:0a.0
235 vnfd-connection-point-ref: eth0
236EOF
237
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000238 # Add external interfaces
Philip Joseph8f0826d2016-09-28 21:40:39 +0530239 for i in `seq 1 ${INTERFACES}`; do
240 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000241 pci=$(get_pci $eth)
242 cat >>$desc_file <<EOF
243 - name: eth${eth}
244 virtual-interface:
245 type: ${INTF_TYPE}
246 bandwidth: '0'
247 vpci: 0000:00:${pci}.0
248 vnfd-connection-point-ref: eth${eth}
249EOF
250 done
251
252 # Add connection points
253 cat >>$desc_file <<EOF
254
255 connection-point:
256EOF
257
Philip Joseph8f0826d2016-09-28 21:40:39 +0530258 for i in `seq 0 ${INTERFACES}`; do
259 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000260 cat >>$desc_file <<EOF
261 - name: eth${eth}
262 type: ${CP_TYPE}
263EOF
264 done
265
Philip Joseph92b42602016-09-30 20:28:42 +0530266 cat >>$desc_file <<EOF
267
268 # Uncomment and update below to enable juju
269 # charm configuration for the VNF
270 # vnf-configuration:
271 # juju:
272 # charm: <charm name>
273 # service-primitive:
274 # - name: config
275 # parameter:
276 # - name: <config parameter>
277 # data-type: [STRING|INTEGER]
278 # mandatory: [true|false]
279 # default-value: <value>
280 # - name: <action name>
281 # parameter:
282 # - name: <action parameter>
283 # data-type: [STRING|INTEGER]
284 # mandatory: [true|false]
285 # default-value: <value>
286 # initial-config-primitive:
287 # - name: config
288 # parameter:
289 # - name: <config name>
290 # value: <value>
291 # - name: <action name>
292 # parameter:
293 # - name: <action parameter>
294 # value: <value>
295EOF
296
Philip Joseph15b76cb2016-07-08 01:42:08 +0530297 if [ $VERBOSE == true ]; then
298 echo "INFO: Created $desc_file"
299 fi
300}
301
302function write_nsd_tmpl() {
303 name=$(basename $1)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000304 vnfd=$2
305 desc_file="${name}.yaml"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530306
307 cat >$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530308nsd:nsd-catalog:
309 nsd:
310 - id: ${name}
311 name: ${name}
312 short-name: ${name}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000313 description: Generated by OSM pacakage generator
314 vendor: ${VENDOR}
315 version: '1.0'
Philip Joseph15b76cb2016-07-08 01:42:08 +0530316
317 # Place the logo as png in icons directory and provide the name here
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000318 # logo: <update, optional>
Philip Joseph15b76cb2016-07-08 01:42:08 +0530319
320 # Specify the VNFDs that are part of this NSD
321 constituent-vnfd:
322 # The member-vnf-index needs to be unique, starting from 1
323 # vnfd-id-ref is the id of the VNFD
324 # Multiple constituent VNFDs can be specified
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000325 - member-vnf-index: 1
326 vnfd-id-ref: ${vnfd}
Philip Joseph15b76cb2016-07-08 01:42:08 +0530327
Philip Joseph15b76cb2016-07-08 01:42:08 +0530328EOF
329
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000330 cat >>$desc_file <<EOF
331 vld:
332 # Networks for the VNFs
333EOF
334
Philip Joseph89c5ea02016-09-28 00:35:59 +0530335 # Add management VLD
336 cat >>$desc_file <<EOF
Philip Joseph8f0826d2016-09-28 21:40:39 +0530337 - id: ${name}_vld0
Philip Joseph89c5ea02016-09-28 00:35:59 +0530338 name: management
339 short-name: management
340 type: ELAN
341 # vim-network-name: <update>
342 # provider-network:
343 # overlay-type: VLAN
344 # segmentation_id: <update>
345 vnfd-connection-point-ref:
346 # Specify the constituent VNFs
347 # member-vnf-index-ref - entry from constituent vnf
348 # vnfd-id-ref - VNFD id
349 # vnfd-connection-point-ref - connection point name in the VNFD
350 - nsd:member-vnf-index-ref: 1
351 nsd:vnfd-id-ref: ${vnfd}
352 # NOTE: Validate the entry below
353 nsd:vnfd-connection-point-ref: eth0
354EOF
355
356 # Add rest of VLDs
Philip Joseph8f0826d2016-09-28 21:40:39 +0530357 for i in `seq 1 ${INTERFACES}`; do
358 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000359 cat >>$desc_file <<EOF
360 - id: ${name}_vld${i}
361 name: ${name}_vld${i}
Philip Joseph89c5ea02016-09-28 00:35:59 +0530362 short-name: ${name}_vld${i}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000363 type: ELAN
Philip Joseph89c5ea02016-09-28 00:35:59 +0530364 # vim-network-name: <update>
Philip Joseph14060ca2016-09-14 18:30:57 +0000365 # provider-network:
366 # overlay-type: VLAN
Philip Joseph14060ca2016-09-14 18:30:57 +0000367 # segmentation_id: <update>
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000368 vnfd-connection-point-ref:
369 # Specify the constituent VNFs
370 # member-vnf-index-ref - entry from constituent vnf
371 # vnfd-id-ref - VNFD id
372 # vnfd-connection-point-ref - connection point name in the VNFD
373 - nsd:member-vnf-index-ref: 1
374 nsd:vnfd-id-ref: ${vnfd}
Philip Joseph14060ca2016-09-14 18:30:57 +0000375 # NOTE: Validate the entry below
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000376 nsd:vnfd-connection-point-ref: eth${eth}
377EOF
378 done
379
Philip Joseph15b76cb2016-07-08 01:42:08 +0530380 if [ $VERBOSE == true ]; then
381 echo "INFO: Created $desc_file"
382 fi
383}
384
385function write_nsd_config_tmpl() {
386 name=$(basename $1)
387 cfg_file="ns_config/$name.yaml"
388
389 cat >$cfg_file <<EOF
390
391EOF
392
393 if [ $VERBOSE == true ]; then
394 echo "INFO: Created $cfg_file"
395 fi
396}
397
Philip Joseph15b76cb2016-07-08 01:42:08 +0530398cur_dir=`pwd`
399
400# Check if the array contains a specific value
401# Taken from
402# http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
403function contains() {
404 local n=$#
405 local value=${!n}
406 for ((i=1;i < $#;i++)); do
407 if [ "${!i}" == "${value}" ]; then
408 echo "y"
409 return 0
410 fi
Austin Cormier3aeed122016-05-23 19:12:00 -0400411 done
Philip Joseph15b76cb2016-07-08 01:42:08 +0530412 echo "n"
413 return 1
414}
415
416function check_type() {
417 type=$1
418 if [ $(contains "${DESC_TYPES[@]}" $type) == "y" ]; then
419 TYPE=$type
420 else
421 echo "ERROR: Unknown descriptor type $type!" >&2
422 exit 1
423 fi
424}
425
426function get_expr(){
Philip Joseph15b76cb2016-07-08 01:42:08 +0530427 # First argument is to specify if this is a negative match or not
428 # Rest are filename expressions without extension
429
430 local regex=" "
431 local n=$#
432 local neg="${1}"
433 local first="true"
434 for ((i=2;i <= $#;i++)); do
435 for extn in "${DESC_EXTN[@]}"; do
436 if [ $first == true ]; then
437 if [ $neg == true ]; then
438 subexpr='! -name'
439 else
440 subexpr='-name'
441 fi
442 first=false
443 else
444 if [ $neg == true ]; then
445 subexpr=' -a ! -name'
446 else
447 subexpr=' -o -name'
448 fi
449 fi
450
451 regex="$regex $subexpr ${!i}.$extn"
452 done
453 done
454
455 echo "$regex"
456}
457
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000458function generate_package(){
459 type=$1
460 name="${2}_${type}"
461 vnfd="${2}_vnfd" # Required for NSD
462 dest_dir=$3
463
464 dir="${dest_dir}/${name}"
465
466 # Create the folders for the descriptor
467 if [ $VERBOSE == true ]; then
468 echo "INFO: Creating folders for $PKG in $dest_dir"
469 fi
470
471 # Remove any existing directory
472 if [ -d $dir ]; then
473 rm -rf $dir >/dev/null 2>&1
474 fi
475
476 mkdir -p $dir && cd $dir
477 if [ $? -ne 0 ]; then
478 rc=$?
479 echo "ERROR: creating directory $dir ($rc)" >&2
480 exit $rc
481 fi
482
483 if [ $type == 'nsd' ]; then
484 folders=("${NSD_FOLDERS[@]}")
485 else
486 folders=("${VNFD_FOLDERS[@]}")
487 fi
488
489 for d in ${folders[@]}; do
490 mkdir -p $dir/$d
491 if [ $? -ne 0 ]; then
492 rc=$?
493 echo "ERROR: creating directory $dir/$d ($rc)" >&2
494 exit $rc
495 fi
496 if [ $VERBOSE == true ]; then
497 echo "Created folder $d in $dir"
498 fi
499 done
500
501 if [ $VERBOSE == true ]; then
502 echo "INFO: Created folders for in $dir"
503 fi
504
505 # Write a descriptor template file
506 if [ $type == 'vnfd' ]; then
507
508 # Copy cloud init file to correct folder
509 if [[ -n ${CLOUD_INIT_FILE} ]]; then
510 if [[ -e ${CLOUD_INIT_FILE} ]]; then
511 cp ${CLOUD_INIT_FILE} $dir/cloud_init
512 else
513 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
514 exit 1
515 fi
516 fi
517
518 write_vnfd_tmpl $dir
519 else
520 write_nsd_tmpl $dir $vnfd
521 fi
522
Philip Joseph92b42602016-09-30 20:28:42 +0530523 write_readme $dir
524
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000525 if [ $ARCHIVE == true ]; then
526 # Create archive of the package
527 cd $dest_dir
528 if [ $VERBOSE == true ]; then
529 tar zcvf ${name}.tar.gz ${name}
530 echo "Created package ${name}.tar.gz in $dest_dir"
531 else
532 tar zcvf ${name}.tar.gz ${name} >/dev/null 2>&1
533 fi
534
535 if [ $? -ne 0 ]; then
536 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
537 exit 1
538 fi
539
540 echo "$dest_dir/${name}.tar.gz" >&2
541
542 if [ $RM == true ]; then
543 rm -rf ${name}
544 fi
545 fi
546}
547
548OPTS=`getopt -o vhnt:d:caN --long verbose,dry-run,help,package-type:,destination-dir,create-folder,no-remove-files,archive,nsd,vendor:,interface-type:,vcpu:,memory:,storage:,image:,cloud-init-file:,cloud-init:,interfaces:,debug -n $SCRIPTNAME -- "$@"`
549
550if [ $? != 0 ] ; then
551 echo "ERROR: Failed parsing options ($?)." >&2
552 usage
553 exit 1
554fi
555
556echo "$OPTS"
557eval set -- "$OPTS >/dev/null 2>&1"
558
Philip Joseph15b76cb2016-07-08 01:42:08 +0530559while true; do
560 case "$1" in
561 -v | --verbose ) VERBOSE=true; shift ;;
562 -h | --help ) usage; exit 0; shift ;;
563 -n | --dry-run ) DRY_RUN=true; shift ;;
564 -t | --package-type ) check_type "$2"; shift; shift ;;
565 -d | --destination-dir ) DEST_DIR=$2; shift; shift;;
566 -c | --create-folder ) CREATE=true; shift;;
567 -N | --no-remove-files ) RM=''; shift;;
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000568 -a | --archive ) ARCHIVE=true; shift;;
569 --nsd ) CREATE_NSD=true; shift;;
570 --vendor ) VENDOR=$2; shift; shift;;
571 --interface-type ) INTF_TYPE=$2; shift; shift;;
572 --vcpu ) VCPU=$2; shift; shift;;
573 --memory ) MEMORY=$2; shift; shift;;
574 --storage ) STORAGE=$2; shift; shift;;
575 --image ) IMAGE=$2; shift; shift;;
576 --cloud-init ) CLOUD_INIT=$2; shift; shift;;
577 --cloud-init-file ) CLOUD_INIT_FILE=$2; shift; shift;;
578 --interfaces ) INTERFACES=$2; shift; shift;;
Philip Joseph15b76cb2016-07-08 01:42:08 +0530579 --debug ) DEBUG=true; shift;;
580 -- ) shift; break ;;
581 * ) break ;;
582 esac
583done
584
585if [ $DEBUG == true ]; then
586 echo "INFO: Debugging ON"
587 set -x
588fi
589
590if [ $VERBOSE == true ]; then
591 echo "INFO: Descriptor type: $TYPE"
592fi
593
594# Dry run is to validate existing descriptor folders
595if [ $DRY_RUN == true ] && [ $CREATE == true ]; then
596 echo "ERROR: Option dry-run with create-folders not supported!" >&2
597 exit 1
598fi
599
600if [ $# -gt 1 ]; then
601 BASE_DIR=$1
602 PKG=$(basename $2)
603else
604 BASE_DIR=$(dirname $1)
605 PKG=$(basename $1)
606fi
607
608if [ $VERBOSE == true ]; then
609 echo "INFO: Using base dir: $BASE_DIR"
610fi
611
612if [ $VERBOSE == true ]; then
613 echo "INFO: Using package: $PKG"
614fi
615
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000616if [[ -z "$PKG" ]]; then
617 echo "ERROR: Need to specify the package name" >&2
Philip Joseph15b76cb2016-07-08 01:42:08 +0530618 usage >&2
619 exit 1
620fi
621
Philip Joseph14060ca2016-09-14 18:30:57 +0000622if [[ ! -d $BASE_DIR ]]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000623 if [ $CREATE == true ]; then
624 mkdir -p $BASE_DIR
625 if [ $? -ne 0 ]; then
626 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
627 exit 1
628 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000629 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530630fi
Philip Joseph14060ca2016-09-14 18:30:57 +0000631cd $BASE_DIR
632if [ $? -ne 0 ]; then
633 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
634 exit 1
635fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530636
637# Get full base dir path
638BASE_DIR=`pwd`
639cd $cur_dir
640
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000641if [[ -z $DEST_DIR ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530642 DEST_DIR=$BASE_DIR # Default to base directory
643fi
644
645mkdir -p $DEST_DIR
646
647cd $DEST_DIR
648if [ $? -ne 0 ]; then
649 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
650 exit 1
651fi
652
653# Get the full destination dir path
654DEST_DIR=`pwd`
655cd $cur_dir
656
657dir=${BASE_DIR}/${PKG}
658
659function add_chksum() {
660 if [ $VERBOSE == true ]; then
661 echo "INFO: Add file $1 to $CHKSUM"
662 fi
663
664 md5sum $1 >> $CHKSUM
665}
666
667if [ $CREATE == false ]; then
668 if [ ! -d $dir ]; then
669 echo "INFO: Package folder $dir not found!" >&2
670 exit 1
671 fi
672
673 cd $dir
674 if [ $? -ne 0 ]; then
675 rc=$?
676 echo "ERROR: changing directory to $dir ($rc)" >&2
677 exit $rc
678 fi
679
680 # Remove checksum file, if present
681 rm -f $CHKSUM
682
683 # Check if the descriptor file is present
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000684 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530685 # Desc type not specified, look for the desc file and guess the type
686 # Required for backward compatibility
687 for ty in ${DESC_TYPES[@]}; do
688 re=$(get_expr false "$ty" "*_$ty" "*_${ty}_*")
689 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
690
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000691 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530692 # Check the vnfd|nsd folder
693 if [ ! -d $ty ]; then
694 continue
695 fi
696 re=$(get_expr false "*")
697 desc=$(find $ty/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000698 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530699 continue
700 elif [ ${#desc[@]} -gt 1 ]; then
701 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
702 exit 1
703 fi
704 # Descriptor sub directory
705 desc_sub_dir=$ty
706 fi
707
708 TYPE=$ty
709 if [ $TYPE == 'nsd' ]; then
710 folders=("${NSD_FOLDERS[@]}")
711 else
712 folders=("${VNFD_FOLDERS[@]}")
713 fi
714
715 if [ $VERBOSE == true ]; then
716 echo "INFO: Determined descriptor is of type $TYPE"
717 fi
718 break
719 done
720
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000721 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530722 echo "ERROR: Unable to determine the descriptor type!" >&2
723 exit 1
724 fi
725 else
726 if [ $TYPE == 'nsd' ]; then
727 folders=("${NSD_FOLDERS[@]}")
728 else
729 folders=("${VNFD_FOLDERS[@]}")
730 fi
731
732 # Check for descriptor of type provided on command line
733 re=$(get_expr false "$TYPE" "*_${TYPE}" "*_${TYPE}_*")
734 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
735
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000736 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530737 # Check if it is under vnfd/nsd subdirectory
738 # Backward compatibility support
739 re=$(get_expr false "*")
740 desc=$(find $TYPE/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000741 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530742 echo "ERROR: Did not find descriptor file of type $TYPE" \
743 " in $dir" >&2
744 exit 1
745 fi
746 desc_sub_dir=$ty
747 fi
748 fi
749
750 if [ ${#desc[@]} -gt 1 ]; then
751 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
752 exit 1
753 fi
754
755 descriptor=${desc[0]}
756
757 # Check if there are files not supported
758 files=$(find * -maxdepth 0 -type f ! -name $descriptor 2>/dev/null)
759
760 for f in ${files[@]}; do
761 if [ $(contains "${ALLOWED_FILES[@]}" $f) == "n" ]; then
762 echo "WARN: Unsupported file $f found"
763 fi
764 done
765
766 if [ $VERBOSE == true ]; then
767 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
768 fi
769
770 if [ $DRY_RUN == false ]; then
771 add_chksum ${descriptor}
772 fi
773
774 # Check the folders are supported ones
775 dirs=$( find * -maxdepth 0 -type d )
776
777 for d in ${dirs[@]}; do
778 if [ $(contains "${folders[@]}" $d) == "y" ]; then
779 if [ $DRY_RUN == false ]; then
780 find $d/* -type f 2>/dev/null|
781 while read file; do
782 add_chksum $file
783 done
784 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000785 elif [[ -z $desc_sub_dir ]] || [ $d != $desc_sub_dir ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530786 echo "WARN: $d is not part of standard folders " \
787 "for descriptor type $TYPE in $PKG"
788 fi
789 done
790
791 if [ $VERBOSE == true ]; then
792 echo "INFO: Creating archive for $PKG"
793 fi
794
795 cd $BASE_DIR
796 if [ $DRY_RUN == false ]; then
797 tar zcvf "$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
798 if [ $? -ne 0 ]; then
799 rc=$?
800 echo "ERROR: creating archive for $PKG ($rc)" >&2
801 exit $rc
802 fi
803 fi
804else
Philip Joseph15b76cb2016-07-08 01:42:08 +0530805 # Create, default to VNFD if no type is defined
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000806 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530807 TYPE=vnfd
Philip Joseph15b76cb2016-07-08 01:42:08 +0530808 if [ $VERBOSE == true ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000809 echo "WARNING: Defaulting to descriptor type $TYPE"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530810 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530811 fi
812
Philip Joseph15b76cb2016-07-08 01:42:08 +0530813 if [ $TYPE == 'vnfd' ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000814 if [[ -z $IMAGE ]]; then
815 echo "ERROR: Image file need to be specified for VNF"
816 exit 1
817 fi
818 generate_package vnfd $PKG $DEST_DIR
819 fi
820
821 if [ $TYPE == 'nsd' -o $CREATE_NSD == true ]; then
822 generate_package nsd $PKG $DEST_DIR
Philip Joseph15b76cb2016-07-08 01:42:08 +0530823 fi
824
825fi
826
827cd $cur_dir