blob: a09fb6afd6cfe2dcbe0321813d5d9068158dce46 [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:
Varun Prasadfde1ca52016-11-28 04:17:49 -0500232 type: VIRTIO
Philip Joseph89c5ea02016-09-28 00:35:59 +0530233 bandwidth: '0'
Pablo Montes Morenoe5ddc812016-12-14 16:57:15 +0100234 vpci: '0000:00:0a.0'
Philip Joseph89c5ea02016-09-28 00:35:59 +0530235 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'
Pablo Montes Morenoe5ddc812016-12-14 16:57:15 +0100247 vpci: '0000:00:${pci}.0'
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000248 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
Varun Prasadfde1ca52016-11-28 04:17:49 -0500341 mgmt-network: 'true'
Philip Joseph89c5ea02016-09-28 00:35:59 +0530342 # vim-network-name: <update>
343 # provider-network:
344 # overlay-type: VLAN
345 # segmentation_id: <update>
346 vnfd-connection-point-ref:
347 # Specify the constituent VNFs
348 # member-vnf-index-ref - entry from constituent vnf
349 # vnfd-id-ref - VNFD id
350 # vnfd-connection-point-ref - connection point name in the VNFD
351 - nsd:member-vnf-index-ref: 1
352 nsd:vnfd-id-ref: ${vnfd}
353 # NOTE: Validate the entry below
354 nsd:vnfd-connection-point-ref: eth0
355EOF
356
357 # Add rest of VLDs
Philip Joseph8f0826d2016-09-28 21:40:39 +0530358 for i in `seq 1 ${INTERFACES}`; do
359 eth=$(($i))
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000360 cat >>$desc_file <<EOF
361 - id: ${name}_vld${i}
362 name: ${name}_vld${i}
Philip Joseph89c5ea02016-09-28 00:35:59 +0530363 short-name: ${name}_vld${i}
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000364 type: ELAN
Philip Joseph89c5ea02016-09-28 00:35:59 +0530365 # vim-network-name: <update>
Philip Joseph14060ca2016-09-14 18:30:57 +0000366 # provider-network:
367 # overlay-type: VLAN
Philip Joseph14060ca2016-09-14 18:30:57 +0000368 # segmentation_id: <update>
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000369 vnfd-connection-point-ref:
370 # Specify the constituent VNFs
371 # member-vnf-index-ref - entry from constituent vnf
372 # vnfd-id-ref - VNFD id
373 # vnfd-connection-point-ref - connection point name in the VNFD
374 - nsd:member-vnf-index-ref: 1
375 nsd:vnfd-id-ref: ${vnfd}
Philip Joseph14060ca2016-09-14 18:30:57 +0000376 # NOTE: Validate the entry below
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000377 nsd:vnfd-connection-point-ref: eth${eth}
378EOF
379 done
380
Philip Joseph15b76cb2016-07-08 01:42:08 +0530381 if [ $VERBOSE == true ]; then
382 echo "INFO: Created $desc_file"
383 fi
384}
385
386function write_nsd_config_tmpl() {
387 name=$(basename $1)
388 cfg_file="ns_config/$name.yaml"
389
390 cat >$cfg_file <<EOF
391
392EOF
393
394 if [ $VERBOSE == true ]; then
395 echo "INFO: Created $cfg_file"
396 fi
397}
398
Philip Joseph15b76cb2016-07-08 01:42:08 +0530399cur_dir=`pwd`
400
401# Check if the array contains a specific value
402# Taken from
403# http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
404function contains() {
405 local n=$#
406 local value=${!n}
407 for ((i=1;i < $#;i++)); do
408 if [ "${!i}" == "${value}" ]; then
409 echo "y"
410 return 0
411 fi
Austin Cormier3aeed122016-05-23 19:12:00 -0400412 done
Philip Joseph15b76cb2016-07-08 01:42:08 +0530413 echo "n"
414 return 1
415}
416
417function check_type() {
418 type=$1
419 if [ $(contains "${DESC_TYPES[@]}" $type) == "y" ]; then
420 TYPE=$type
421 else
422 echo "ERROR: Unknown descriptor type $type!" >&2
423 exit 1
424 fi
425}
426
427function get_expr(){
Philip Joseph15b76cb2016-07-08 01:42:08 +0530428 # First argument is to specify if this is a negative match or not
429 # Rest are filename expressions without extension
430
431 local regex=" "
432 local n=$#
433 local neg="${1}"
434 local first="true"
435 for ((i=2;i <= $#;i++)); do
436 for extn in "${DESC_EXTN[@]}"; do
437 if [ $first == true ]; then
438 if [ $neg == true ]; then
439 subexpr='! -name'
440 else
441 subexpr='-name'
442 fi
443 first=false
444 else
445 if [ $neg == true ]; then
446 subexpr=' -a ! -name'
447 else
448 subexpr=' -o -name'
449 fi
450 fi
451
452 regex="$regex $subexpr ${!i}.$extn"
453 done
454 done
455
456 echo "$regex"
457}
458
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000459function generate_package(){
460 type=$1
461 name="${2}_${type}"
462 vnfd="${2}_vnfd" # Required for NSD
463 dest_dir=$3
464
465 dir="${dest_dir}/${name}"
466
467 # Create the folders for the descriptor
468 if [ $VERBOSE == true ]; then
469 echo "INFO: Creating folders for $PKG in $dest_dir"
470 fi
471
472 # Remove any existing directory
473 if [ -d $dir ]; then
474 rm -rf $dir >/dev/null 2>&1
475 fi
476
477 mkdir -p $dir && cd $dir
478 if [ $? -ne 0 ]; then
479 rc=$?
480 echo "ERROR: creating directory $dir ($rc)" >&2
481 exit $rc
482 fi
483
484 if [ $type == 'nsd' ]; then
485 folders=("${NSD_FOLDERS[@]}")
486 else
487 folders=("${VNFD_FOLDERS[@]}")
488 fi
489
490 for d in ${folders[@]}; do
491 mkdir -p $dir/$d
492 if [ $? -ne 0 ]; then
493 rc=$?
494 echo "ERROR: creating directory $dir/$d ($rc)" >&2
495 exit $rc
496 fi
497 if [ $VERBOSE == true ]; then
498 echo "Created folder $d in $dir"
499 fi
500 done
501
502 if [ $VERBOSE == true ]; then
503 echo "INFO: Created folders for in $dir"
504 fi
505
506 # Write a descriptor template file
507 if [ $type == 'vnfd' ]; then
508
509 # Copy cloud init file to correct folder
510 if [[ -n ${CLOUD_INIT_FILE} ]]; then
511 if [[ -e ${CLOUD_INIT_FILE} ]]; then
512 cp ${CLOUD_INIT_FILE} $dir/cloud_init
513 else
514 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
515 exit 1
516 fi
517 fi
518
519 write_vnfd_tmpl $dir
520 else
521 write_nsd_tmpl $dir $vnfd
522 fi
523
Philip Joseph92b42602016-09-30 20:28:42 +0530524 write_readme $dir
525
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000526 if [ $ARCHIVE == true ]; then
527 # Create archive of the package
528 cd $dest_dir
529 if [ $VERBOSE == true ]; then
530 tar zcvf ${name}.tar.gz ${name}
531 echo "Created package ${name}.tar.gz in $dest_dir"
532 else
533 tar zcvf ${name}.tar.gz ${name} >/dev/null 2>&1
534 fi
535
536 if [ $? -ne 0 ]; then
537 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
538 exit 1
539 fi
540
541 echo "$dest_dir/${name}.tar.gz" >&2
542
543 if [ $RM == true ]; then
544 rm -rf ${name}
545 fi
546 fi
547}
548
549OPTS=`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 -- "$@"`
550
551if [ $? != 0 ] ; then
552 echo "ERROR: Failed parsing options ($?)." >&2
553 usage
554 exit 1
555fi
556
557echo "$OPTS"
558eval set -- "$OPTS >/dev/null 2>&1"
559
Philip Joseph15b76cb2016-07-08 01:42:08 +0530560while true; do
561 case "$1" in
562 -v | --verbose ) VERBOSE=true; shift ;;
563 -h | --help ) usage; exit 0; shift ;;
564 -n | --dry-run ) DRY_RUN=true; shift ;;
565 -t | --package-type ) check_type "$2"; shift; shift ;;
566 -d | --destination-dir ) DEST_DIR=$2; shift; shift;;
567 -c | --create-folder ) CREATE=true; shift;;
568 -N | --no-remove-files ) RM=''; shift;;
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000569 -a | --archive ) ARCHIVE=true; shift;;
570 --nsd ) CREATE_NSD=true; shift;;
571 --vendor ) VENDOR=$2; shift; shift;;
572 --interface-type ) INTF_TYPE=$2; shift; shift;;
573 --vcpu ) VCPU=$2; shift; shift;;
574 --memory ) MEMORY=$2; shift; shift;;
575 --storage ) STORAGE=$2; shift; shift;;
576 --image ) IMAGE=$2; shift; shift;;
577 --cloud-init ) CLOUD_INIT=$2; shift; shift;;
578 --cloud-init-file ) CLOUD_INIT_FILE=$2; shift; shift;;
579 --interfaces ) INTERFACES=$2; shift; shift;;
Philip Joseph15b76cb2016-07-08 01:42:08 +0530580 --debug ) DEBUG=true; shift;;
581 -- ) shift; break ;;
582 * ) break ;;
583 esac
584done
585
586if [ $DEBUG == true ]; then
587 echo "INFO: Debugging ON"
588 set -x
589fi
590
591if [ $VERBOSE == true ]; then
592 echo "INFO: Descriptor type: $TYPE"
593fi
594
595# Dry run is to validate existing descriptor folders
596if [ $DRY_RUN == true ] && [ $CREATE == true ]; then
597 echo "ERROR: Option dry-run with create-folders not supported!" >&2
598 exit 1
599fi
600
601if [ $# -gt 1 ]; then
602 BASE_DIR=$1
603 PKG=$(basename $2)
604else
605 BASE_DIR=$(dirname $1)
606 PKG=$(basename $1)
607fi
608
609if [ $VERBOSE == true ]; then
610 echo "INFO: Using base dir: $BASE_DIR"
611fi
612
613if [ $VERBOSE == true ]; then
614 echo "INFO: Using package: $PKG"
615fi
616
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000617if [[ -z "$PKG" ]]; then
618 echo "ERROR: Need to specify the package name" >&2
Philip Joseph15b76cb2016-07-08 01:42:08 +0530619 usage >&2
620 exit 1
621fi
622
Philip Joseph14060ca2016-09-14 18:30:57 +0000623if [[ ! -d $BASE_DIR ]]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000624 if [ $CREATE == true ]; then
625 mkdir -p $BASE_DIR
626 if [ $? -ne 0 ]; then
627 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
628 exit 1
629 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000630 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530631fi
Philip Joseph14060ca2016-09-14 18:30:57 +0000632cd $BASE_DIR
633if [ $? -ne 0 ]; then
634 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
635 exit 1
636fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530637
638# Get full base dir path
639BASE_DIR=`pwd`
640cd $cur_dir
641
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000642if [[ -z $DEST_DIR ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530643 DEST_DIR=$BASE_DIR # Default to base directory
644fi
645
646mkdir -p $DEST_DIR
647
648cd $DEST_DIR
649if [ $? -ne 0 ]; then
650 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
651 exit 1
652fi
653
654# Get the full destination dir path
655DEST_DIR=`pwd`
656cd $cur_dir
657
658dir=${BASE_DIR}/${PKG}
659
660function add_chksum() {
661 if [ $VERBOSE == true ]; then
662 echo "INFO: Add file $1 to $CHKSUM"
663 fi
664
665 md5sum $1 >> $CHKSUM
666}
667
668if [ $CREATE == false ]; then
669 if [ ! -d $dir ]; then
670 echo "INFO: Package folder $dir not found!" >&2
671 exit 1
672 fi
673
674 cd $dir
675 if [ $? -ne 0 ]; then
676 rc=$?
677 echo "ERROR: changing directory to $dir ($rc)" >&2
678 exit $rc
679 fi
680
681 # Remove checksum file, if present
682 rm -f $CHKSUM
683
684 # Check if the descriptor file is present
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000685 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530686 # Desc type not specified, look for the desc file and guess the type
687 # Required for backward compatibility
688 for ty in ${DESC_TYPES[@]}; do
689 re=$(get_expr false "$ty" "*_$ty" "*_${ty}_*")
690 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
691
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000692 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530693 # Check the vnfd|nsd folder
694 if [ ! -d $ty ]; then
695 continue
696 fi
697 re=$(get_expr false "*")
698 desc=$(find $ty/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000699 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530700 continue
701 elif [ ${#desc[@]} -gt 1 ]; then
702 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
703 exit 1
704 fi
705 # Descriptor sub directory
706 desc_sub_dir=$ty
707 fi
708
709 TYPE=$ty
710 if [ $TYPE == 'nsd' ]; then
711 folders=("${NSD_FOLDERS[@]}")
712 else
713 folders=("${VNFD_FOLDERS[@]}")
714 fi
715
716 if [ $VERBOSE == true ]; then
717 echo "INFO: Determined descriptor is of type $TYPE"
718 fi
719 break
720 done
721
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000722 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530723 echo "ERROR: Unable to determine the descriptor type!" >&2
724 exit 1
725 fi
726 else
727 if [ $TYPE == 'nsd' ]; then
728 folders=("${NSD_FOLDERS[@]}")
729 else
730 folders=("${VNFD_FOLDERS[@]}")
731 fi
732
733 # Check for descriptor of type provided on command line
734 re=$(get_expr false "$TYPE" "*_${TYPE}" "*_${TYPE}_*")
735 desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
736
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000737 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530738 # Check if it is under vnfd/nsd subdirectory
739 # Backward compatibility support
740 re=$(get_expr false "*")
741 desc=$(find $TYPE/* -maxdepth 0 -type f $re 2>/dev/null)
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000742 if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530743 echo "ERROR: Did not find descriptor file of type $TYPE" \
744 " in $dir" >&2
745 exit 1
746 fi
747 desc_sub_dir=$ty
748 fi
749 fi
750
751 if [ ${#desc[@]} -gt 1 ]; then
752 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
753 exit 1
754 fi
755
756 descriptor=${desc[0]}
757
758 # Check if there are files not supported
759 files=$(find * -maxdepth 0 -type f ! -name $descriptor 2>/dev/null)
760
761 for f in ${files[@]}; do
762 if [ $(contains "${ALLOWED_FILES[@]}" $f) == "n" ]; then
763 echo "WARN: Unsupported file $f found"
764 fi
765 done
766
767 if [ $VERBOSE == true ]; then
768 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
769 fi
770
771 if [ $DRY_RUN == false ]; then
772 add_chksum ${descriptor}
773 fi
774
775 # Check the folders are supported ones
776 dirs=$( find * -maxdepth 0 -type d )
777
778 for d in ${dirs[@]}; do
779 if [ $(contains "${folders[@]}" $d) == "y" ]; then
780 if [ $DRY_RUN == false ]; then
781 find $d/* -type f 2>/dev/null|
782 while read file; do
783 add_chksum $file
784 done
785 fi
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000786 elif [[ -z $desc_sub_dir ]] || [ $d != $desc_sub_dir ]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530787 echo "WARN: $d is not part of standard folders " \
788 "for descriptor type $TYPE in $PKG"
789 fi
790 done
791
792 if [ $VERBOSE == true ]; then
793 echo "INFO: Creating archive for $PKG"
794 fi
795
796 cd $BASE_DIR
797 if [ $DRY_RUN == false ]; then
798 tar zcvf "$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
799 if [ $? -ne 0 ]; then
800 rc=$?
801 echo "ERROR: creating archive for $PKG ($rc)" >&2
802 exit $rc
803 fi
804 fi
805else
Philip Joseph15b76cb2016-07-08 01:42:08 +0530806 # Create, default to VNFD if no type is defined
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000807 if [[ -z $TYPE ]]; then
Philip Joseph15b76cb2016-07-08 01:42:08 +0530808 TYPE=vnfd
Philip Joseph15b76cb2016-07-08 01:42:08 +0530809 if [ $VERBOSE == true ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000810 echo "WARNING: Defaulting to descriptor type $TYPE"
Philip Joseph15b76cb2016-07-08 01:42:08 +0530811 fi
Philip Joseph15b76cb2016-07-08 01:42:08 +0530812 fi
813
Philip Joseph15b76cb2016-07-08 01:42:08 +0530814 if [ $TYPE == 'vnfd' ]; then
Philip Joseph1c36f4a2016-09-14 07:05:33 +0000815 if [[ -z $IMAGE ]]; then
816 echo "ERROR: Image file need to be specified for VNF"
817 exit 1
818 fi
819 generate_package vnfd $PKG $DEST_DIR
820 fi
821
822 if [ $TYPE == 'nsd' -o $CREATE_NSD == true ]; then
823 generate_package nsd $PKG $DEST_DIR
Philip Joseph15b76cb2016-07-08 01:42:08 +0530824 fi
825
826fi
827
828cd $cur_dir