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'
73 $SCRIPTNAME [-t <type>] [-N] [-c] [base-directory] <package-name>
75 -h|--help : show this message
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.
82 -d|--destination-dir <destination directory>: Directory to create the
84 Default is base-directory
86 -N|--no-remove-files : Do not remove the package files after creating
89 Options specifc for create descriptor:
91 -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
95 -a|--archive: Create package for the descriptor
97 --nsd : Generate NSD descriptor package also.
99 --vendor : Vendor name for descriptor. Default OSM
101 --interface-type : Interface type [VIRTIO|SR-IOV|PCI-PASSTHROUGH|E1000]
106 --vcpu : Virtual CPU count. Default 2
108 --memory : Memory for VM in MB. Default 4096MB
110 --storage : Storage size for VM in GB. Default 10GB
114 --image : Location URI of the image
116 --cloud-init-file : Cloud init file
118 --cloud-init : Cloud init script. Will be ignored if
119 cloud-init-file is specified
121 --interfaces : Number of external interfaces in additon to OM-MGMT. Default 1.
123 End of create descriptor specific options
125 -v| --verbose : Generate progress details
127 -n| --dry-run : Validate the package dir
129 base-dir : Directory where the archive file or folders are created,
130 if destination directory is not specified.
131 Default is current directory
133 package-name : The descriptor name (full path if base-dir not specified)
138 function get_cp_type
() {
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;;
149 # Get pci number starting from 0x0a
151 printf '%02x' $
((10 + $1)) |
tr '[:upper:]' '[:lower:]'
154 function write_vnfd_tmpl
() {
156 desc_file
="${name}.yaml"
158 cat >$desc_file <<EOF
164 description: Generated by OSM pacakage generator
168 # Place the logo as png in icons directory and provide the name here
169 # logo: <update, optional>
171 # Management interface
175 # Atleast one VDU need to be specified
179 description: ${name}-VM
182 # Flavour of the VM to be instantiated for the VDU
186 storage-gb: ${STORAGE}
188 # Image including the full path
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
198 cloud-init-file: '${cif}'
200 elif [[ -n ${CLOUD_INIT} ]]; then
201 cat >>$desc_file <<EOF
203 cloud-init: '${CLOUD_INIT}'
207 # Add external interfaces
208 cat >>$desc_file <<EOF
210 # Specify the external interfaces
211 # There can be multiple interfaces defined
215 cat >>$desc_file <<EOF
221 vnfd-connection-point-ref: eth0
224 # Add external interfaces
225 for i
in `seq 1 ${INTERFACES}`; do
228 cat >>$desc_file <<EOF
233 vpci: 0000:00:${pci}.0
234 vnfd-connection-point-ref: eth${eth}
238 # Add connection points
239 cat >>$desc_file <<EOF
244 for i
in `seq 0 ${INTERFACES}`; do
246 cat >>$desc_file <<EOF
252 if [ $VERBOSE == true
]; then
253 echo "INFO: Created $desc_file"
257 function write_nsd_tmpl
() {
260 desc_file
="${name}.yaml"
262 cat >$desc_file <<EOF
268 description: Generated by OSM pacakage generator
272 # Place the logo as png in icons directory and provide the name here
273 # logo: <update, optional>
275 # Specify the VNFDs that are part of this NSD
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
280 - member-vnf-index: 1
285 cat >>$desc_file <<EOF
287 # Networks for the VNFs
291 cat >>$desc_file <<EOF
294 short-name: management
296 # vim-network-name: <update>
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
312 for i
in `seq 1 ${INTERFACES}`; do
314 cat >>$desc_file <<EOF
315 - id: ${name}_vld${i}
316 name: ${name}_vld${i}
317 short-name: ${name}_vld${i}
319 # vim-network-name: <update>
322 # segmentation_id: <update>
323 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}
330 # NOTE: Validate the entry below
331 nsd:vnfd-connection-point-ref: eth${eth}
335 if [ $VERBOSE == true
]; then
336 echo "INFO: Created $desc_file"
340 function write_nsd_config_tmpl
() {
342 cfg_file
="ns_config/$name.yaml"
348 if [ $VERBOSE == true
]; then
349 echo "INFO: Created $cfg_file"
355 # Check if the array contains a specific value
357 # http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
358 function contains
() {
361 for ((i
=1;i
< $#;i
++)); do
362 if [ "${!i}" == "${value}" ]; then
371 function check_type
() {
373 if [ $
(contains
"${DESC_TYPES[@]}" $type) == "y" ]; then
376 echo "ERROR: Unknown descriptor type $type!" >&2
382 # First argument is to specify if this is a negative match or not
383 # Rest are filename expressions without extension
389 for ((i
=2;i
<= $#;i
++)); do
390 for extn
in "${DESC_EXTN[@]}"; do
391 if [ $first == true
]; then
392 if [ $neg == true
]; then
399 if [ $neg == true
]; then
400 subexpr
=' -a ! -name'
406 regex
="$regex $subexpr ${!i}.$extn"
413 function generate_package
(){
416 vnfd
="${2}_vnfd" # Required for NSD
419 dir
="${dest_dir}/${name}"
421 # Create the folders for the descriptor
422 if [ $VERBOSE == true
]; then
423 echo "INFO: Creating folders for $PKG in $dest_dir"
426 # Remove any existing directory
428 rm -rf $dir >/dev
/null
2>&1
431 mkdir
-p $dir && cd $dir
432 if [ $?
-ne 0 ]; then
434 echo "ERROR: creating directory $dir ($rc)" >&2
438 if [ $type == 'nsd' ]; then
439 folders
=("${NSD_FOLDERS[@]}")
441 folders
=("${VNFD_FOLDERS[@]}")
444 for d
in ${folders[@]}; do
446 if [ $?
-ne 0 ]; then
448 echo "ERROR: creating directory $dir/$d ($rc)" >&2
451 if [ $VERBOSE == true
]; then
452 echo "Created folder $d in $dir"
456 if [ $VERBOSE == true
]; then
457 echo "INFO: Created folders for in $dir"
460 # Write a descriptor template file
461 if [ $type == 'vnfd' ]; then
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
468 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
475 write_nsd_tmpl
$dir $vnfd
478 if [ $ARCHIVE == true
]; then
479 # Create archive of the package
481 if [ $VERBOSE == true
]; then
482 tar zcvf
${name}.
tar.gz
${name}
483 echo "Created package ${name}.tar.gz in $dest_dir"
485 tar zcvf
${name}.
tar.gz
${name} >/dev
/null
2>&1
488 if [ $?
-ne 0 ]; then
489 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
493 echo "$dest_dir/${name}.tar.gz" >&2
495 if [ $RM == true
]; then
501 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 -- "$@"`
503 if [ $?
!= 0 ] ; then
504 echo "ERROR: Failed parsing options ($?)." >&2
510 eval set -- "$OPTS >/dev/null 2>&1"
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;;
521 -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;;
532 --debug ) DEBUG
=true
; shift;;
538 if [ $DEBUG == true
]; then
539 echo "INFO: Debugging ON"
543 if [ $VERBOSE == true
]; then
544 echo "INFO: Descriptor type: $TYPE"
547 # Dry run is to validate existing descriptor folders
548 if [ $DRY_RUN == true
] && [ $CREATE == true
]; then
549 echo "ERROR: Option dry-run with create-folders not supported!" >&2
553 if [ $# -gt 1 ]; then
557 BASE_DIR
=$
(dirname $1)
561 if [ $VERBOSE == true
]; then
562 echo "INFO: Using base dir: $BASE_DIR"
565 if [ $VERBOSE == true
]; then
566 echo "INFO: Using package: $PKG"
569 if [[ -z "$PKG" ]]; then
570 echo "ERROR: Need to specify the package name" >&2
575 if [[ ! -d $BASE_DIR ]]; then
576 if [ $CREATE == true
]; then
578 if [ $?
-ne 0 ]; then
579 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
585 if [ $?
-ne 0 ]; then
586 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
590 # Get full base dir path
594 if [[ -z $DEST_DIR ]]; then
595 DEST_DIR
=$BASE_DIR # Default to base directory
601 if [ $?
-ne 0 ]; then
602 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
606 # Get the full destination dir path
610 dir
=${BASE_DIR}/${PKG}
612 function add_chksum
() {
613 if [ $VERBOSE == true
]; then
614 echo "INFO: Add file $1 to $CHKSUM"
620 if [ $CREATE == false
]; then
621 if [ ! -d $dir ]; then
622 echo "INFO: Package folder $dir not found!" >&2
627 if [ $?
-ne 0 ]; then
629 echo "ERROR: changing directory to $dir ($rc)" >&2
633 # Remove checksum file, if present
636 # Check if the descriptor file is present
637 if [[ -z $TYPE ]]; then
638 # 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
)
644 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
645 # Check the vnfd|nsd folder
646 if [ ! -d $ty ]; then
649 re
=$
(get_expr false
"*")
650 desc
=$
(find $ty/* -maxdepth 0 -type f
$re 2>/dev
/null
)
651 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
653 elif [ ${#desc[@]} -gt 1 ]; then
654 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
657 # Descriptor sub directory
662 if [ $TYPE == 'nsd' ]; then
663 folders
=("${NSD_FOLDERS[@]}")
665 folders
=("${VNFD_FOLDERS[@]}")
668 if [ $VERBOSE == true
]; then
669 echo "INFO: Determined descriptor is of type $TYPE"
674 if [[ -z $TYPE ]]; then
675 echo "ERROR: Unable to determine the descriptor type!" >&2
679 if [ $TYPE == 'nsd' ]; then
680 folders
=("${NSD_FOLDERS[@]}")
682 folders
=("${VNFD_FOLDERS[@]}")
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
)
689 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
690 # 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
)
694 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
695 echo "ERROR: Did not find descriptor file of type $TYPE" \
703 if [ ${#desc[@]} -gt 1 ]; then
704 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
708 descriptor
=${desc[0]}
710 # Check if there are files not supported
711 files
=$
(find * -maxdepth 0 -type f
! -name $descriptor 2>/dev
/null
)
713 for f
in ${files[@]}; do
714 if [ $
(contains
"${ALLOWED_FILES[@]}" $f) == "n" ]; then
715 echo "WARN: Unsupported file $f found"
719 if [ $VERBOSE == true
]; then
720 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
723 if [ $DRY_RUN == false
]; then
724 add_chksum
${descriptor}
727 # Check the folders are supported ones
728 dirs=$
( find * -maxdepth 0 -type d
)
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|
738 elif [[ -z $desc_sub_dir ]] ||
[ $d != $desc_sub_dir ]; then
739 echo "WARN: $d is not part of standard folders " \
740 "for descriptor type $TYPE in $PKG"
744 if [ $VERBOSE == true
]; then
745 echo "INFO: Creating archive for $PKG"
749 if [ $DRY_RUN == false
]; then
750 tar zcvf
"$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
751 if [ $?
-ne 0 ]; then
753 echo "ERROR: creating archive for $PKG ($rc)" >&2
758 # Create, default to VNFD if no type is defined
759 if [[ -z $TYPE ]]; then
761 if [ $VERBOSE == true
]; then
762 echo "WARNING: Defaulting to descriptor type $TYPE"
766 if [ $TYPE == 'vnfd' ]; then
767 if [[ -z $IMAGE ]]; then
768 echo "ERROR: Image file need to be specified for VNF"
771 generate_package vnfd
$PKG $DEST_DIR
774 if [ $TYPE == 'nsd' -o $CREATE_NSD == true
]; then
775 generate_package nsd
$PKG $DEST_DIR