3 ############################################################################
4 # Copyright 2016 RIFT.io Inc #
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 #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
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 ############################################################################
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
24 # This script can be used to create the required folders for
25 # a descriptor package and a template descriptor
27 # Usage: generate_descriptor_pkg.sh <base-directory> <package-directory>
29 # 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)
36 SCRIPTNAME
=`basename $0`
38 # 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
41 VNFD_FOLDERS
=(images scripts icons charms cloud_init
)
43 # Supported folders for NSD
44 # OSM document specifies (ns|vnf)-config folder, while Rel 4.3
45 # is using (ns|vnf)_config.
46 NSD_FOLDERS
=(scripts icons ns_config vnf_config
)
48 # Other files allowed in the descriptor base directory
49 ALLOWED_FILES
=(README
)
52 DESC_EXTN
=(yml yaml json xml
)
53 CHKSUM
='checksums.txt'
68 CLOUD_INIT
='#cloud-config '
74 $SCRIPTNAME [-t <type>] [-N] [-c] [base-directory] <package-name>
76 -h|--help : show this message
78 -t|--package-type <nsd|vnfd> : Descriptor package type
79 is NSD or VNFD. Script will try to
80 determine the type if not provided.
81 Default is vnfd for create-folders.
83 -d|--destination-dir <destination directory>: Directory to create the
85 Default is base-directory
87 -N|--no-remove-files : Do not remove the package files after creating
90 Options specifc for create descriptor:
92 -c|--create-folder : Create folder with the structure for the
93 package type using the base-dir and package-dir
94 and a descriptor template
96 -a|--archive: Create package for the descriptor
98 --nsd : Generate NSD descriptor package also.
100 --vendor : Vendor name for descriptor. Default OSM
102 --interface-type : Interface type [VIRTIO|SR-IOV|PCI-PASSTHROUGH|E1000|OM-MGMT]
107 --vcpu : Virtual CPU count. Default 2
109 --memory : Memory for VM in MB. Default 4096MB
111 --storage : Storage size for VM in GB. Default 10GB
115 --image : Location URI of the image
117 --cloud-init-file : Cloud init file
119 --cloud-init : Cloud init script. Will be ignored if
120 cloud-init-file is specified
122 --interfaces : Number of external interfaces. Default 1.
124 End of create descriptor specific options
126 -v| --verbose : Generate progress details
128 -n| --dry-run : Validate the package dir
130 base-dir : Directory where the archive file or folders are created,
131 if destination directory is not specified.
132 Default is current directory
134 package-name : The descriptor name (full path if base-dir not specified)
139 function get_cp_type
() {
141 VIRTIO
) CP_TYPE
='VPORT';;
142 SR-IOV
) CP_TYPE
='VPORT';;
143 PCI-PASSTHROUGH
) CP_TYPE
='VPORT';;
144 OM-MGMT
) CP_TYPE
='VPORT';;
145 E1000
) CP_TYPE
='VPORT';;
146 * ) echo "ERROR: Unknown interface type ${INTF_TYPE}"; exit 1;;
150 # Get pci number starting from 0x0a
152 printf '%02x' $
((10 + $1)) |
tr '[:upper:]' '[:lower:]'
155 function write_vnfd_tmpl
() {
157 desc_file
="${name}.yaml"
159 cat >$desc_file <<EOF
165 description: Generated by OSM pacakage generator
169 # Place the logo as png in icons directory and provide the name here
170 # logo: <update, optional>
172 # Management interface
176 # Atleast one VDU need to be specified
180 description: ${name}-VM
183 # Flavour of the VM to be instantiated for the VDU
187 storage-gb: ${STORAGE}
189 # Image including the full path
194 # Add the cloud init file or script
195 if [[ -n ${CLOUD_INIT_FILE} ]]; then
196 cif
=$
(basename ${CLOUD_INIT_FILE})
197 cat >>$desc_file <<EOF
199 cloud-init-file: '${cif}'
202 cat >>$desc_file <<EOF
204 cloud-init: '${CLOUD_INIT}'
208 # Add external interfaces
209 cat >>$desc_file <<EOF
211 # Specify the external interfaces
212 # There can be multiple interfaces defined
215 # Add external interfaces
216 for i
in `seq 1 ${INTERFACES}`; do
219 cat >>$desc_file <<EOF
224 vpci: 0000:00:${pci}.0
225 vnfd-connection-point-ref: eth${eth}
229 # Add connection points
230 cat >>$desc_file <<EOF
235 for i
in `seq 1 ${INTERFACES}`; do
237 cat >>$desc_file <<EOF
243 if [ $VERBOSE == true
]; then
244 echo "INFO: Created $desc_file"
248 function write_nsd_tmpl
() {
251 desc_file
="${name}.yaml"
253 cat >$desc_file <<EOF
259 description: Generated by OSM pacakage generator
263 # Place the logo as png in icons directory and provide the name here
264 # logo: <update, optional>
266 # Specify the VNFDs that are part of this NSD
268 # The member-vnf-index needs to be unique, starting from 1
269 # vnfd-id-ref is the id of the VNFD
270 # Multiple constituent VNFDs can be specified
271 - member-vnf-index: 1
276 cat >>$desc_file <<EOF
278 # Networks for the VNFs
281 for i
in `seq 1 ${INTERFACES}`; do
283 cat >>$desc_file <<EOF
284 - id: ${name}_vld${i}
285 name: ${name}_vld${i}
289 # physical-network: <update>
290 # segmentation_id: <update>
291 vnfd-connection-point-ref:
292 # Specify the constituent VNFs
293 # member-vnf-index-ref - entry from constituent vnf
294 # vnfd-id-ref - VNFD id
295 # vnfd-connection-point-ref - connection point name in the VNFD
296 - nsd:member-vnf-index-ref: 1
297 nsd:vnfd-id-ref: ${vnfd}
298 # NOTE: Validate the entry below
299 nsd:vnfd-connection-point-ref: eth${eth}
303 if [ $VERBOSE == true
]; then
304 echo "INFO: Created $desc_file"
308 function write_nsd_config_tmpl
() {
310 cfg_file
="ns_config/$name.yaml"
316 if [ $VERBOSE == true
]; then
317 echo "INFO: Created $cfg_file"
323 # Check if the array contains a specific value
325 # http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
326 function contains
() {
329 for ((i
=1;i
< $#;i
++)); do
330 if [ "${!i}" == "${value}" ]; then
339 function check_type
() {
341 if [ $
(contains
"${DESC_TYPES[@]}" $type) == "y" ]; then
344 echo "ERROR: Unknown descriptor type $type!" >&2
350 # First argument is to specify if this is a negative match or not
351 # Rest are filename expressions without extension
357 for ((i
=2;i
<= $#;i
++)); do
358 for extn
in "${DESC_EXTN[@]}"; do
359 if [ $first == true
]; then
360 if [ $neg == true
]; then
367 if [ $neg == true
]; then
368 subexpr
=' -a ! -name'
374 regex
="$regex $subexpr ${!i}.$extn"
381 function generate_package
(){
384 vnfd
="${2}_vnfd" # Required for NSD
387 dir
="${dest_dir}/${name}"
389 # Create the folders for the descriptor
390 if [ $VERBOSE == true
]; then
391 echo "INFO: Creating folders for $PKG in $dest_dir"
394 # Remove any existing directory
396 rm -rf $dir >/dev
/null
2>&1
399 mkdir
-p $dir && cd $dir
400 if [ $?
-ne 0 ]; then
402 echo "ERROR: creating directory $dir ($rc)" >&2
406 if [ $type == 'nsd' ]; then
407 folders
=("${NSD_FOLDERS[@]}")
409 folders
=("${VNFD_FOLDERS[@]}")
412 for d
in ${folders[@]}; do
414 if [ $?
-ne 0 ]; then
416 echo "ERROR: creating directory $dir/$d ($rc)" >&2
419 if [ $VERBOSE == true
]; then
420 echo "Created folder $d in $dir"
424 if [ $VERBOSE == true
]; then
425 echo "INFO: Created folders for in $dir"
428 # Write a descriptor template file
429 if [ $type == 'vnfd' ]; then
431 # Copy cloud init file to correct folder
432 if [[ -n ${CLOUD_INIT_FILE} ]]; then
433 if [[ -e ${CLOUD_INIT_FILE} ]]; then
434 cp ${CLOUD_INIT_FILE} $dir/cloud_init
436 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
443 write_nsd_tmpl
$dir $vnfd
446 if [ $ARCHIVE == true
]; then
447 # Create archive of the package
449 if [ $VERBOSE == true
]; then
450 tar zcvf
${name}.
tar.gz
${name}
451 echo "Created package ${name}.tar.gz in $dest_dir"
453 tar zcvf
${name}.
tar.gz
${name} >/dev
/null
2>&1
456 if [ $?
-ne 0 ]; then
457 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
461 echo "$dest_dir/${name}.tar.gz" >&2
463 if [ $RM == true
]; then
469 OPTS
=`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 -- "$@"`
471 if [ $?
!= 0 ] ; then
472 echo "ERROR: Failed parsing options ($?)." >&2
478 eval set -- "$OPTS >/dev/null 2>&1"
482 -v |
--verbose ) VERBOSE
=true
; shift ;;
483 -h |
--help ) usage
; exit 0; shift ;;
484 -n |
--dry-run ) DRY_RUN
=true
; shift ;;
485 -t |
--package-type ) check_type
"$2"; shift; shift ;;
486 -d |
--destination-dir ) DEST_DIR
=$2; shift; shift;;
487 -c |
--create-folder ) CREATE
=true
; shift;;
488 -N |
--no-remove-files ) RM
=''; shift;;
489 -a |
--archive ) ARCHIVE
=true
; shift;;
490 --nsd ) CREATE_NSD
=true
; shift;;
491 --vendor ) VENDOR
=$2; shift; shift;;
492 --interface-type ) INTF_TYPE
=$2; shift; shift;;
493 --vcpu ) VCPU
=$2; shift; shift;;
494 --memory ) MEMORY
=$2; shift; shift;;
495 --storage ) STORAGE
=$2; shift; shift;;
496 --image ) IMAGE
=$2; shift; shift;;
497 --cloud-init ) CLOUD_INIT
=$2; shift; shift;;
498 --cloud-init-file ) CLOUD_INIT_FILE
=$2; shift; shift;;
499 --interfaces ) INTERFACES
=$2; shift; shift;;
500 --debug ) DEBUG
=true
; shift;;
506 if [ $DEBUG == true
]; then
507 echo "INFO: Debugging ON"
511 if [ $VERBOSE == true
]; then
512 echo "INFO: Descriptor type: $TYPE"
515 # Dry run is to validate existing descriptor folders
516 if [ $DRY_RUN == true
] && [ $CREATE == true
]; then
517 echo "ERROR: Option dry-run with create-folders not supported!" >&2
521 if [ $# -gt 1 ]; then
525 BASE_DIR
=$
(dirname $1)
529 if [ $VERBOSE == true
]; then
530 echo "INFO: Using base dir: $BASE_DIR"
533 if [ $VERBOSE == true
]; then
534 echo "INFO: Using package: $PKG"
537 if [[ -z "$PKG" ]]; then
538 echo "ERROR: Need to specify the package name" >&2
543 if [[ ! -d $BASE_DIR ]]; then
544 if [ $CREATE == true
]; then
546 if [ $?
-ne 0 ]; then
547 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
553 if [ $?
-ne 0 ]; then
554 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
558 # Get full base dir path
562 if [[ -z $DEST_DIR ]]; then
563 DEST_DIR
=$BASE_DIR # Default to base directory
569 if [ $?
-ne 0 ]; then
570 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
574 # Get the full destination dir path
578 dir
=${BASE_DIR}/${PKG}
580 function add_chksum
() {
581 if [ $VERBOSE == true
]; then
582 echo "INFO: Add file $1 to $CHKSUM"
588 if [ $CREATE == false
]; then
589 if [ ! -d $dir ]; then
590 echo "INFO: Package folder $dir not found!" >&2
595 if [ $?
-ne 0 ]; then
597 echo "ERROR: changing directory to $dir ($rc)" >&2
601 # Remove checksum file, if present
604 # Check if the descriptor file is present
605 if [[ -z $TYPE ]]; then
606 # Desc type not specified, look for the desc file and guess the type
607 # Required for backward compatibility
608 for ty
in ${DESC_TYPES[@]}; do
609 re
=$
(get_expr false
"$ty" "*_$ty" "*_${ty}_*")
610 desc
=$
(find * -maxdepth 0 -type f
$re 2>/dev
/null
)
612 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
613 # Check the vnfd|nsd folder
614 if [ ! -d $ty ]; then
617 re
=$
(get_expr false
"*")
618 desc
=$
(find $ty/* -maxdepth 0 -type f
$re 2>/dev
/null
)
619 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
621 elif [ ${#desc[@]} -gt 1 ]; then
622 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
625 # Descriptor sub directory
630 if [ $TYPE == 'nsd' ]; then
631 folders
=("${NSD_FOLDERS[@]}")
633 folders
=("${VNFD_FOLDERS[@]}")
636 if [ $VERBOSE == true
]; then
637 echo "INFO: Determined descriptor is of type $TYPE"
642 if [[ -z $TYPE ]]; then
643 echo "ERROR: Unable to determine the descriptor type!" >&2
647 if [ $TYPE == 'nsd' ]; then
648 folders
=("${NSD_FOLDERS[@]}")
650 folders
=("${VNFD_FOLDERS[@]}")
653 # Check for descriptor of type provided on command line
654 re
=$
(get_expr false
"$TYPE" "*_${TYPE}" "*_${TYPE}_*")
655 desc
=$
(find * -maxdepth 0 -type f
$re 2>/dev
/null
)
657 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
658 # Check if it is under vnfd/nsd subdirectory
659 # Backward compatibility support
660 re
=$
(get_expr false
"*")
661 desc
=$
(find $TYPE/* -maxdepth 0 -type f
$re 2>/dev
/null
)
662 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
663 echo "ERROR: Did not find descriptor file of type $TYPE" \
671 if [ ${#desc[@]} -gt 1 ]; then
672 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
676 descriptor
=${desc[0]}
678 # Check if there are files not supported
679 files
=$
(find * -maxdepth 0 -type f
! -name $descriptor 2>/dev
/null
)
681 for f
in ${files[@]}; do
682 if [ $
(contains
"${ALLOWED_FILES[@]}" $f) == "n" ]; then
683 echo "WARN: Unsupported file $f found"
687 if [ $VERBOSE == true
]; then
688 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
691 if [ $DRY_RUN == false
]; then
692 add_chksum
${descriptor}
695 # Check the folders are supported ones
696 dirs=$
( find * -maxdepth 0 -type d
)
698 for d
in ${dirs[@]}; do
699 if [ $
(contains
"${folders[@]}" $d) == "y" ]; then
700 if [ $DRY_RUN == false
]; then
701 find $d/* -type f
2>/dev
/null|
706 elif [[ -z $desc_sub_dir ]] ||
[ $d != $desc_sub_dir ]; then
707 echo "WARN: $d is not part of standard folders " \
708 "for descriptor type $TYPE in $PKG"
712 if [ $VERBOSE == true
]; then
713 echo "INFO: Creating archive for $PKG"
717 if [ $DRY_RUN == false
]; then
718 tar zcvf
"$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
719 if [ $?
-ne 0 ]; then
721 echo "ERROR: creating archive for $PKG ($rc)" >&2
726 # Create, default to VNFD if no type is defined
727 if [[ -z $TYPE ]]; then
729 if [ $VERBOSE == true
]; then
730 echo "WARNING: Defaulting to descriptor type $TYPE"
734 if [ $TYPE == 'vnfd' ]; then
735 if [[ -z $IMAGE ]]; then
736 echo "ERROR: Image file need to be specified for VNF"
739 generate_package vnfd
$PKG $DEST_DIR
742 if [ $TYPE == 'nsd' -o $CREATE_NSD == true
]; then
743 generate_package nsd
$PKG $DEST_DIR