blob: 8edb06feaf4052dec45456c0907b2dd2f36a6c34 [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 Joseph15b76cb2016-07-08 01:42:08 +0530154function write_vnfd_tmpl() {
155 name=$(basename $1)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000156 desc_file="${name}.yaml"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530157
158 cat >$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530159vnfd:vnfd-catalog:
160 vnfd:
161 - id: ${name}
162 name: ${name}
163 short-name: ${name}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000164 description: Generated by OSM pacakage generator
165 vendor: ${VENDOR}
166 version: '1.0'
Philip Joseph15b76cb2016-07-08 01:42:08 +0530167
168 # Place the logo as png in icons directory and provide the name here
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000169 # logo: <update, optional>
Philip Joseph15b76cb2016-07-08 01:42:08 +0530170
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000171 # Management interface
Philip Joseph15b76cb2016-07-08 01:42:08 +0530172 mgmt-interface:
Philip Joseph15b76cb2016-07-08 01:42:08 +0530173 vdu-id: ${name}-VM
Philip Joseph15b76cb2016-07-08 01:42:08 +0530174
175 # Atleast one VDU need to be specified
176 vdu:
177 - id: ${name}-VM
178 name: ${name}-VM
179 description: ${name}-VM
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000180 count: 1
Philip Joseph15b76cb2016-07-08 01:42:08 +0530181
182 # Flavour of the VM to be instantiated for the VDU
183 vm-flavor:
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000184 vcpu-count: ${VCPU}
185 memory-mb: ${MEMORY}
186 storage-gb: ${STORAGE}
Philip Joseph15b76cb2016-07-08 01:42:08 +0530187
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000188 # Image including the full path
189 image: '${IMAGE}'
190
191EOF
192
193 # Add the cloud init file or script
194 if [[ -n ${CLOUD_INIT_FILE} ]]; then
195 cif=$(basename ${CLOUD_INIT_FILE})
196 cat >>$desc_file <<EOF
197 # Cloud init file
198 cloud-init-file: '${cif}'
199EOF
Philip Josephc737a092016-09-27 12:34:32 +0530200 elif [[ -n ${CLOUD_INIT} ]]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000201 cat >>$desc_file <<EOF
202 # Cloud init to use
203 cloud-init: '${CLOUD_INIT}'
204EOF
205 fi
206
207 # Add external interfaces
208 cat >>$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530209 external-interface:
210 # Specify the external interfaces
211 # There can be multiple interfaces defined
Philip Joseph15b76cb2016-07-08 01:42:08 +0530212EOF
213
Philip Joseph89c5ea02016-09-28 00:35:59 +0530214 # Add mgmt interface
215 cat >>$desc_file <<EOF
216 - name: eth0
217 virtual-interface:
218 type: OM-MGMT
219 bandwidth: '0'
220 vpci: 0000:00:0a.0
221 vnfd-connection-point-ref: eth0
222EOF
223
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000224 # Add external interfaces
Philip Joseph8f0826d2016-09-28 21:40:39 +0530225 for i in `seq 1 ${INTERFACES}`; do
226 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000227 pci=$(get_pci $eth)
228 cat >>$desc_file <<EOF
229 - name: eth${eth}
230 virtual-interface:
231 type: ${INTF_TYPE}
232 bandwidth: '0'
233 vpci: 0000:00:${pci}.0
234 vnfd-connection-point-ref: eth${eth}
235EOF
236 done
237
238 # Add connection points
239 cat >>$desc_file <<EOF
240
241 connection-point:
242EOF
243
Philip Joseph8f0826d2016-09-28 21:40:39 +0530244 for i in `seq 0 ${INTERFACES}`; do
245 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000246 cat >>$desc_file <<EOF
247 - name: eth${eth}
248 type: ${CP_TYPE}
249EOF
250 done
251
Philip Joseph15b76cb2016-07-08 01:42:08 +0530252 if [ $VERBOSE == true ]; then
253 echo "INFO: Created $desc_file"
254 fi
255}
256
257function write_nsd_tmpl() {
258 name=$(basename $1)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000259 vnfd=$2
260 desc_file="${name}.yaml"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530261
262 cat >$desc_file <<EOF
Philip Joseph15b76cb2016-07-08 01:42:08 +0530263nsd:nsd-catalog:
264 nsd:
265 - id: ${name}
266 name: ${name}
267 short-name: ${name}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000268 description: Generated by OSM pacakage generator
269 vendor: ${VENDOR}
270 version: '1.0'
Philip Joseph15b76cb2016-07-08 01:42:08 +0530271
272 # Place the logo as png in icons directory and provide the name here
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000273 # logo: <update, optional>
Philip Joseph15b76cb2016-07-08 01:42:08 +0530274
275 # Specify the VNFDs that are part of this NSD
276 constituent-vnfd:
277 # The member-vnf-index needs to be unique, starting from 1
278 # vnfd-id-ref is the id of the VNFD
279 # Multiple constituent VNFDs can be specified
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000280 - member-vnf-index: 1
281 vnfd-id-ref: ${vnfd}
Philip Joseph15b76cb2016-07-08 01:42:08 +0530282
Philip Joseph15b76cb2016-07-08 01:42:08 +0530283EOF
284
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000285 cat >>$desc_file <<EOF
286 vld:
287 # Networks for the VNFs
288EOF
289
Philip Joseph89c5ea02016-09-28 00:35:59 +0530290 # Add management VLD
291 cat >>$desc_file <<EOF
Philip Joseph8f0826d2016-09-28 21:40:39 +0530292 - id: ${name}_vld0
Philip Joseph89c5ea02016-09-28 00:35:59 +0530293 name: management
294 short-name: management
295 type: ELAN
296 # vim-network-name: <update>
297 # provider-network:
298 # overlay-type: VLAN
299 # segmentation_id: <update>
300 vnfd-connection-point-ref:
301 # Specify the constituent VNFs
302 # member-vnf-index-ref - entry from constituent vnf
303 # vnfd-id-ref - VNFD id
304 # vnfd-connection-point-ref - connection point name in the VNFD
305 - nsd:member-vnf-index-ref: 1
306 nsd:vnfd-id-ref: ${vnfd}
307 # NOTE: Validate the entry below
308 nsd:vnfd-connection-point-ref: eth0
309EOF
310
311 # Add rest of VLDs
Philip Joseph8f0826d2016-09-28 21:40:39 +0530312 for i in `seq 1 ${INTERFACES}`; do
313 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000314 cat >>$desc_file <<EOF
315 - id: ${name}_vld${i}
316 name: ${name}_vld${i}
Philip Joseph89c5ea02016-09-28 00:35:59 +0530317 short-name: ${name}_vld${i}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000318 type: ELAN
Philip Joseph89c5ea02016-09-28 00:35:59 +0530319 # vim-network-name: <update>
Philip Joseph14060ca2016-09-14 18:30:57 +0000320 # provider-network:
321 # overlay-type: VLAN
Philip Joseph14060ca2016-09-14 18:30:57 +0000322 # segmentation_id: <update>
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000323 vnfd-connection-point-ref:
324 # Specify the constituent VNFs
325 # member-vnf-index-ref - entry from constituent vnf
326 # vnfd-id-ref - VNFD id
327 # vnfd-connection-point-ref - connection point name in the VNFD
328 - nsd:member-vnf-index-ref: 1
329 nsd:vnfd-id-ref: ${vnfd}
Philip Joseph14060ca2016-09-14 18:30:57 +0000330 # NOTE: Validate the entry below
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000331 nsd:vnfd-connection-point-ref: eth${eth}
332EOF
333 done
334
Philip Joseph15b76cb2016-07-08 01:42:08 +0530335 if [ $VERBOSE == true ]; then
336 echo "INFO: Created $desc_file"
337 fi
338}
339
340function write_nsd_config_tmpl() {
341 name=$(basename $1)
342 cfg_file="ns_config/$name.yaml"
343
344 cat >$cfg_file <<EOF
345
346EOF
347
348 if [ $VERBOSE == true ]; then
349 echo "INFO: Created $cfg_file"
350 fi
351}
352
Philip Joseph15b76cb2016-07-08 01:42:08 +0530353cur_dir=`pwd`
354
355# Check if the array contains a specific value
356# Taken from
357# http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
358function contains() {
359 local n=$#
360 local value=${!n}
361 for ((i=1;i < $#;i++)); do
362 if [ "${!i}" == "${value}" ]; then
363 echo "y"
364 return 0
365 fi
Austin Cormier3aeed122016-05-23 19:12:00 -0400366 done
Philip Joseph15b76cb2016-07-08 01:42:08 +0530367 echo "n"
368 return 1
369}
370
371function check_type() {
372 type=$1
373 if [ $(contains "${DESC_TYPES[@]}" $type) == "y" ]; then
374 TYPE=$type
375 else
376 echo "ERROR: Unknown descriptor type $type!" >&2
377 exit 1
378 fi
379}
380
381function get_expr(){
Philip Joseph15b76cb2016-07-08 01:42:08 +0530382 # First argument is to specify if this is a negative match or not
383 # Rest are filename expressions without extension
384
385 local regex=" "
386 local n=$#
387 local neg="${1}"
388 local first="true"
389 for ((i=2;i <= $#;i++)); do
390 for extn in "${DESC_EXTN[@]}"; do
391 if [ $first == true ]; then
392 if [ $neg == true ]; then
393 subexpr='! -name'
394 else
395 subexpr='-name'
396 fi
397 first=false
398 else
399 if [ $neg == true ]; then
400 subexpr=' -a ! -name'
401 else
402 subexpr=' -o -name'
403 fi
404 fi
405
406 regex="$regex $subexpr ${!i}.$extn"
407 done
408 done
409
410 echo "$regex"
411}
412
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000413function generate_package(){
414 type=$1
415 name="${2}_${type}"
416 vnfd="${2}_vnfd" # Required for NSD
417 dest_dir=$3
418
419 dir="${dest_dir}/${name}"
420
421 # Create the folders for the descriptor
422 if [ $VERBOSE == true ]; then
423 echo "INFO: Creating folders for $PKG in $dest_dir"
424 fi
425
426 # Remove any existing directory
427 if [ -d $dir ]; then
428 rm -rf $dir >/dev/null 2>&1
429 fi
430
431 mkdir -p $dir && cd $dir
432 if [ $? -ne 0 ]; then
433 rc=$?
434 echo "ERROR: creating directory $dir ($rc)" >&2
435 exit $rc
436 fi
437
438 if [ $type == 'nsd' ]; then
439 folders=("${NSD_FOLDERS[@]}")
440 else
441 folders=("${VNFD_FOLDERS[@]}")
442 fi
443
444 for d in ${folders[@]}; do
445 mkdir -p $dir/$d
446 if [ $? -ne 0 ]; then
447 rc=$?
448 echo "ERROR: creating directory $dir/$d ($rc)" >&2
449 exit $rc
450 fi
451 if [ $VERBOSE == true ]; then
452 echo "Created folder $d in $dir"
453 fi
454 done
455
456 if [ $VERBOSE == true ]; then
457 echo "INFO: Created folders for in $dir"
458 fi
459
460 # Write a descriptor template file
461 if [ $type == 'vnfd' ]; then
462
463 # Copy cloud init file to correct folder
464 if [[ -n ${CLOUD_INIT_FILE} ]]; then
465 if [[ -e ${CLOUD_INIT_FILE} ]]; then
466 cp ${CLOUD_INIT_FILE} $dir/cloud_init
467 else
468 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
469 exit 1
470 fi
471 fi
472
473 write_vnfd_tmpl $dir
474 else
475 write_nsd_tmpl $dir $vnfd
476 fi
477
478 if [ $ARCHIVE == true ]; then
479 # Create archive of the package
480 cd $dest_dir
481 if [ $VERBOSE == true ]; then
482 tar zcvf ${name}.tar.gz ${name}
483 echo "Created package ${name}.tar.gz in $dest_dir"
484 else
485 tar zcvf ${name}.tar.gz ${name} >/dev/null 2>&1
486 fi
487
488 if [ $? -ne 0 ]; then
489 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
490 exit 1
491 fi
492
493 echo "$dest_dir/${name}.tar.gz" >&2
494
495 if [ $RM == true ]; then
496 rm -rf ${name}
497 fi
498 fi
499}
500
501OPTS=`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 -- "$@"`
502
503if [ $? != 0 ] ; then
504 echo "ERROR: Failed parsing options ($?)." >&2
505 usage
506 exit 1
507fi
508
509echo "$OPTS"
510eval set -- "$OPTS >/dev/null 2>&1"
511
Philip Joseph15b76cb2016-07-08 01:42:08 +0530512while true; do
513 case "$1" in
514 -v | --verbose ) VERBOSE=true; shift ;;
515 -h | --help ) usage; exit 0; shift ;;
516 -n | --dry-run ) DRY_RUN=true; shift ;;
517 -t | --package-type ) check_type "$2"; shift; shift ;;
518 -d | --destination-dir ) DEST_DIR=$2; shift; shift;;
519 -c | --create-folder ) CREATE=true; shift;;
520 -N | --no-remove-files ) RM=''; shift;;
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000521 -a | --archive ) ARCHIVE=true; shift;;
522 --nsd ) CREATE_NSD=true; shift;;
523 --vendor ) VENDOR=$2; shift; shift;;
524 --interface-type ) INTF_TYPE=$2; shift; shift;;
525 --vcpu ) VCPU=$2; shift; shift;;
526 --memory ) MEMORY=$2; shift; shift;;
527 --storage ) STORAGE=$2; shift; shift;;
528 --image ) IMAGE=$2; shift; shift;;
529 --cloud-init ) CLOUD_INIT=$2; shift; shift;;
530 --cloud-init-file ) CLOUD_INIT_FILE=$2; shift; shift;;
531 --interfaces ) INTERFACES=$2; shift; shift;;
Philip Joseph15b76cb2016-07-08 01:42:08 +0530532 --debug ) DEBUG=true; shift;;
533 -- ) shift; break ;;
534 * ) break ;;
535 esac
536done
537
538if [ $DEBUG == true ]; then
539 echo "INFO: Debugging ON"
540 set -x
541fi
542
543if [ $VERBOSE == true ]; then
544 echo "INFO: Descriptor type: $TYPE"
545fi
546
547# Dry run is to validate existing descriptor folders
548if [ $DRY_RUN == true ] && [ $CREATE == true ]; then
549 echo "ERROR: Option dry-run with create-folders not supported!" >&2
550 exit 1
551fi
552
553if [ $# -gt 1 ]; then
554 BASE_DIR=$1
555 PKG=$(basename $2)
556else
557 BASE_DIR=$(dirname $1)
558 PKG=$(basename $1)
559fi
560
561if [ $VERBOSE == true ]; then
562 echo "INFO: Using base dir: $BASE_DIR"
563fi
564
565if [ $VERBOSE == true ]; then
566 echo "INFO: Using package: $PKG"
567fi
568
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000569if [[ -z "$PKG" ]]; then
570 echo "ERROR: Need to specify the package name" >&2
Philip Joseph15b76cb2016-07-08 01:42:08 +0530571 usage >&2
572 exit 1
573fi
574
Philip Joseph14060ca2016-09-14 18:30:57 +0000575if [[ ! -d $BASE_DIR ]]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000576 if [ $CREATE == true ]; then
577 mkdir -p $BASE_DIR
578 if [ $? -ne 0 ]; then
579 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
580 exit 1
581 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000582 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530583fi
Philip Joseph14060ca2016-09-14 18:30:57 +0000584cd $BASE_DIR
585if [ $? -ne 0 ]; then
586 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
587 exit 1
588fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530589
590# Get full base dir path
591BASE_DIR=`pwd`
592cd $cur_dir
593
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000594if [[ -z $DEST_DIR ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530595 DEST_DIR=$BASE_DIR # Default to base directory
596fi
597
598mkdir -p $DEST_DIR
599
600cd $DEST_DIR
601if [ $? -ne 0 ]; then
602 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
603 exit 1
604fi
605
606# Get the full destination dir path
607DEST_DIR=`pwd`
608cd $cur_dir
609
610dir=${BASE_DIR}/${PKG}
611
612function add_chksum() {
613 if [ $VERBOSE == true ]; then
614 echo "INFO: Add file $1 to $CHKSUM"
615 fi
616
617 md5sum $1 >> $CHKSUM
618}
619
620if [ $CREATE == false ]; then
621 if [ ! -d $dir ]; then
622 echo "INFO: Package folder $dir not found!" >&2
623 exit 1
624 fi
625
626 cd $dir
627 if [ $? -ne 0 ]; then
628 rc=$?
629 echo "ERROR: changing directory to $dir ($rc)" >&2
630 exit $rc
631 fi
632
633 # Remove checksum file, if present
634 rm -f $CHKSUM
635
636 # Check if the descriptor file is present
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000637 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530638 # Desc type not specified, look for the desc file and guess the type
639 # Required for backward compatibility
640 for ty in ${DESC_TYPES[@]}; do
641 re=$(get_expr false "$ty" "*_$ty" "*_${ty}_*")
642 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
643
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000644 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530645 # Check the vnfd|nsd folder
646 if [ ! -d $ty ]; then
647 continue
648 fi
649 re=$(get_expr false "*")
650 desc=$(find $ty/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000651 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530652 continue
653 elif [ ${#desc[@]} -gt 1 ]; then
654 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
655 exit 1
656 fi
657 # Descriptor sub directory
658 desc_sub_dir=$ty
659 fi
660
661 TYPE=$ty
662 if [ $TYPE == 'nsd' ]; then
663 folders=("${NSD_FOLDERS[@]}")
664 else
665 folders=("${VNFD_FOLDERS[@]}")
666 fi
667
668 if [ $VERBOSE == true ]; then
669 echo "INFO: Determined descriptor is of type $TYPE"
670 fi
671 break
672 done
673
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000674 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530675 echo "ERROR: Unable to determine the descriptor type!" >&2
676 exit 1
677 fi
678 else
679 if [ $TYPE == 'nsd' ]; then
680 folders=("${NSD_FOLDERS[@]}")
681 else
682 folders=("${VNFD_FOLDERS[@]}")
683 fi
684
685 # Check for descriptor of type provided on command line
686 re=$(get_expr false "$TYPE" "*_${TYPE}" "*_${TYPE}_*")
687 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
688
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000689 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530690 # Check if it is under vnfd/nsd subdirectory
691 # Backward compatibility support
692 re=$(get_expr false "*")
693 desc=$(find $TYPE/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000694 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530695 echo "ERROR: Did not find descriptor file of type $TYPE" \
696 " in $dir" >&2
697 exit 1
698 fi
699 desc_sub_dir=$ty
700 fi
701 fi
702
703 if [ ${#desc[@]} -gt 1 ]; then
704 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
705 exit 1
706 fi
707
708 descriptor=${desc[0]}
709
710 # Check if there are files not supported
711 files=$(find * -maxdepth 0 -type f ! -name $descriptor 2>/dev/null)
712
713 for f in ${files[@]}; do
714 if [ $(contains "${ALLOWED_FILES[@]}" $f) == "n" ]; then
715 echo "WARN: Unsupported file $f found"
716 fi
717 done
718
719 if [ $VERBOSE == true ]; then
720 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
721 fi
722
723 if [ $DRY_RUN == false ]; then
724 add_chksum ${descriptor}
725 fi
726
727 # Check the folders are supported ones
728 dirs=$( find * -maxdepth 0 -type d )
729
730 for d in ${dirs[@]}; do
731 if [ $(contains "${folders[@]}" $d) == "y" ]; then
732 if [ $DRY_RUN == false ]; then
733 find $d/* -type f 2>/dev/null|
734 while read file; do
735 add_chksum $file
736 done
737 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000738 elif [[ -z $desc_sub_dir ]] || [ $d != $desc_sub_dir ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530739 echo "WARN: $d is not part of standard folders " \
740 "for descriptor type $TYPE in $PKG"
741 fi
742 done
743
744 if [ $VERBOSE == true ]; then
745 echo "INFO: Creating archive for $PKG"
746 fi
747
748 cd $BASE_DIR
749 if [ $DRY_RUN == false ]; then
750 tar zcvf "$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
751 if [ $? -ne 0 ]; then
752 rc=$?
753 echo "ERROR: creating archive for $PKG ($rc)" >&2
754 exit $rc
755 fi
756 fi
757else
Philip Joseph15b76cb2016-07-08 01:42:08 +0530758 # Create, default to VNFD if no type is defined
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000759 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530760 TYPE=vnfd
Philip Joseph15b76cb2016-07-08 01:42:08 +0530761 if [ $VERBOSE == true ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000762 echo "WARNING: Defaulting to descriptor type $TYPE"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530763 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530764 fi
765
Philip Joseph15b76cb2016-07-08 01:42:08 +0530766 if [ $TYPE == 'vnfd' ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000767 if [[ -z $IMAGE ]]; then
768 echo "ERROR: Image file need to be specified for VNF"
769 exit 1
770 fi
771 generate_package vnfd $PKG $DEST_DIR
772 fi
773
774 if [ $TYPE == 'nsd' -o $CREATE_NSD == true ]; then
775 generate_package nsd $PKG $DEST_DIR
Philip Joseph15b76cb2016-07-08 01:42:08 +0530776 fi
777
778fi
779
780cd $cur_dir