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_readme
() {
160 Descriptor created by OSM descriptor package generated
166 function write_vnfd_tmpl
() {
168 desc_file
="${name}.yaml"
170 cat >$desc_file <<EOF
176 description: Generated by OSM pacakage generator
180 # Place the logo as png in icons directory and provide the name here
181 # logo: <update, optional>
183 # Management interface
187 # Atleast one VDU need to be specified
189 # Additional VDUs can be created by copying the
190 # VDU descriptor below
193 description: ${name}-VM
196 # Flavour of the VM to be instantiated for the VDU
200 storage-gb: ${STORAGE}
202 # Image including the full path
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
212 cloud-init-file: '${cif}'
214 elif [[ -n ${CLOUD_INIT} ]]; then
215 cat >>$desc_file <<EOF
217 cloud-init: '${CLOUD_INIT}'
221 # Add external interfaces
222 cat >>$desc_file <<EOF
224 # Specify the external interfaces
225 # There can be multiple interfaces defined
229 cat >>$desc_file <<EOF
236 external-connection-point-ref: eth0
239 # Add external interfaces
240 for i
in `seq 1 ${INTERFACES}`; do
243 cat >>$desc_file <<EOF
249 vpci: '0000:00:${pci}.0'
250 external-connection-point-ref: eth${eth}
254 # Add connection points
255 cat >>$desc_file <<EOF
260 for i
in `seq 0 ${INTERFACES}`; do
262 cat >>$desc_file <<EOF
268 cat >>$desc_file <<EOF
270 # Uncomment and update below to enable juju
271 # charm configuration for the VNF
274 # charm: <charm name>
278 # - name: <config parameter>
279 # data-type: [STRING|INTEGER]
280 # mandatory: [true|false]
281 # default-value: <value>
282 # - name: <action name>
284 # - name: <action parameter>
285 # data-type: [STRING|INTEGER]
286 # mandatory: [true|false]
287 # default-value: <value>
288 # initial-config-primitive:
291 # - name: <config name>
293 # - name: <action name>
295 # - name: <action parameter>
299 if [ $VERBOSE == true
]; then
300 echo "INFO: Created $desc_file"
304 function write_nsd_tmpl
() {
307 desc_file
="${name}.yaml"
309 cat >$desc_file <<EOF
315 description: Generated by OSM pacakage generator
319 # Place the logo as png in icons directory and provide the name here
320 # logo: <update, optional>
322 # Specify the VNFDs that are part of this NSD
324 # The member-vnf-index needs to be unique, starting from 1
325 # vnfd-id-ref is the id of the VNFD
326 # Multiple constituent VNFDs can be specified
327 - member-vnf-index: 1
332 cat >>$desc_file <<EOF
334 # Networks for the VNFs
338 cat >>$desc_file <<EOF
341 short-name: management
344 # vim-network-name: <update>
347 # segmentation_id: <update>
348 vnfd-connection-point-ref:
349 # Specify the constituent VNFs
350 # member-vnf-index-ref - entry from constituent vnf
351 # vnfd-id-ref - VNFD id
352 # vnfd-connection-point-ref - connection point name in the VNFD
353 - nsd:member-vnf-index-ref: 1
354 nsd:vnfd-id-ref: ${vnfd}
355 # NOTE: Validate the entry below
356 nsd:vnfd-connection-point-ref: eth0
360 for i
in `seq 1 ${INTERFACES}`; do
362 cat >>$desc_file <<EOF
363 - id: ${name}_vld${i}
364 name: ${name}_vld${i}
365 short-name: ${name}_vld${i}
367 # vim-network-name: <update>
370 # segmentation_id: <update>
371 vnfd-connection-point-ref:
372 # Specify the constituent VNFs
373 # member-vnf-index-ref - entry from constituent vnf
374 # vnfd-id-ref - VNFD id
375 # vnfd-connection-point-ref - connection point name in the VNFD
376 - nsd:member-vnf-index-ref: 1
377 nsd:vnfd-id-ref: ${vnfd}
378 # NOTE: Validate the entry below
379 nsd:vnfd-connection-point-ref: eth${eth}
383 if [ $VERBOSE == true
]; then
384 echo "INFO: Created $desc_file"
388 function write_nsd_config_tmpl
() {
390 cfg_file
="ns_config/$name.yaml"
396 if [ $VERBOSE == true
]; then
397 echo "INFO: Created $cfg_file"
403 # Check if the array contains a specific value
405 # http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
406 function contains
() {
409 for ((i
=1;i
< $#;i
++)); do
410 if [ "${!i}" == "${value}" ]; then
419 function check_type
() {
421 if [ $
(contains
"${DESC_TYPES[@]}" $type) == "y" ]; then
424 echo "ERROR: Unknown descriptor type $type!" >&2
430 # First argument is to specify if this is a negative match or not
431 # Rest are filename expressions without extension
437 for ((i
=2;i
<= $#;i
++)); do
438 for extn
in "${DESC_EXTN[@]}"; do
439 if [ $first == true
]; then
440 if [ $neg == true
]; then
447 if [ $neg == true
]; then
448 subexpr
=' -a ! -name'
454 regex
="$regex $subexpr ${!i}.$extn"
461 function generate_package
(){
464 vnfd
="${2}_vnfd" # Required for NSD
467 dir
="${dest_dir}/${name}"
469 # Create the folders for the descriptor
470 if [ $VERBOSE == true
]; then
471 echo "INFO: Creating folders for $PKG in $dest_dir"
474 # Remove any existing directory
476 rm -rf $dir >/dev
/null
2>&1
479 mkdir
-p $dir && cd $dir
480 if [ $?
-ne 0 ]; then
482 echo "ERROR: creating directory $dir ($rc)" >&2
486 if [ $type == 'nsd' ]; then
487 folders
=("${NSD_FOLDERS[@]}")
489 folders
=("${VNFD_FOLDERS[@]}")
492 for d
in ${folders[@]}; do
494 if [ $?
-ne 0 ]; then
496 echo "ERROR: creating directory $dir/$d ($rc)" >&2
499 if [ $VERBOSE == true
]; then
500 echo "Created folder $d in $dir"
504 if [ $VERBOSE == true
]; then
505 echo "INFO: Created folders for in $dir"
508 # Write a descriptor template file
509 if [ $type == 'vnfd' ]; then
511 # Copy cloud init file to correct folder
512 if [[ -n ${CLOUD_INIT_FILE} ]]; then
513 if [[ -e ${CLOUD_INIT_FILE} ]]; then
514 cp ${CLOUD_INIT_FILE} $dir/cloud_init
516 echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
523 write_nsd_tmpl
$dir $vnfd
528 if [ $ARCHIVE == true
]; then
529 # Create archive of the package
531 if [ $VERBOSE == true
]; then
532 tar zcvf
${name}.
tar.gz
${name}
533 echo "Created package ${name}.tar.gz in $dest_dir"
535 tar zcvf
${name}.
tar.gz
${name} >/dev
/null
2>&1
538 if [ $?
-ne 0 ]; then
539 echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
543 echo "$dest_dir/${name}.tar.gz" >&2
545 if [ $RM == true
]; then
551 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 -- "$@"`
553 if [ $?
!= 0 ] ; then
554 echo "ERROR: Failed parsing options ($?)." >&2
560 eval set -- "$OPTS >/dev/null 2>&1"
564 -v |
--verbose ) VERBOSE
=true
; shift ;;
565 -h |
--help ) usage
; exit 0; shift ;;
566 -n |
--dry-run ) DRY_RUN
=true
; shift ;;
567 -t |
--package-type ) check_type
"$2"; shift; shift ;;
568 -d |
--destination-dir ) DEST_DIR
=$2; shift; shift;;
569 -c |
--create-folder ) CREATE
=true
; shift;;
570 -N |
--no-remove-files ) RM
=''; shift;;
571 -a |
--archive ) ARCHIVE
=true
; shift;;
572 --nsd ) CREATE_NSD
=true
; shift;;
573 --vendor ) VENDOR
=$2; shift; shift;;
574 --interface-type ) INTF_TYPE
=$2; shift; shift;;
575 --vcpu ) VCPU
=$2; shift; shift;;
576 --memory ) MEMORY
=$2; shift; shift;;
577 --storage ) STORAGE
=$2; shift; shift;;
578 --image ) IMAGE
=$2; shift; shift;;
579 --cloud-init ) CLOUD_INIT
=$2; shift; shift;;
580 --cloud-init-file ) CLOUD_INIT_FILE
=$2; shift; shift;;
581 --interfaces ) INTERFACES
=$2; shift; shift;;
582 --debug ) DEBUG
=true
; shift;;
588 if [ $DEBUG == true
]; then
589 echo "INFO: Debugging ON"
593 if [ $VERBOSE == true
]; then
594 echo "INFO: Descriptor type: $TYPE"
597 # Dry run is to validate existing descriptor folders
598 if [ $DRY_RUN == true
] && [ $CREATE == true
]; then
599 echo "ERROR: Option dry-run with create-folders not supported!" >&2
603 if [ $# -gt 1 ]; then
607 BASE_DIR
=$
(dirname $1)
611 if [ $VERBOSE == true
]; then
612 echo "INFO: Using base dir: $BASE_DIR"
615 if [ $VERBOSE == true
]; then
616 echo "INFO: Using package: $PKG"
619 if [[ -z "$PKG" ]]; then
620 echo "ERROR: Need to specify the package name" >&2
625 if [[ ! -d $BASE_DIR ]]; then
626 if [ $CREATE == true
]; then
628 if [ $?
-ne 0 ]; then
629 echo "ERROR: Unable to create base directory $BASE_DIR" >&2
635 if [ $?
-ne 0 ]; then
636 echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
640 # Get full base dir path
644 if [[ -z $DEST_DIR ]]; then
645 DEST_DIR
=$BASE_DIR # Default to base directory
651 if [ $?
-ne 0 ]; then
652 echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
656 # Get the full destination dir path
660 dir
=${BASE_DIR}/${PKG}
662 function add_chksum
() {
663 if [ $VERBOSE == true
]; then
664 echo "INFO: Add file $1 to $CHKSUM"
670 if [ $CREATE == false
]; then
671 if [ ! -d $dir ]; then
672 echo "INFO: Package folder $dir not found!" >&2
677 if [ $?
-ne 0 ]; then
679 echo "ERROR: changing directory to $dir ($rc)" >&2
683 # Remove checksum file, if present
686 # Check if the descriptor file is present
687 if [[ -z $TYPE ]]; then
688 # Desc type not specified, look for the desc file and guess the type
689 # Required for backward compatibility
690 for ty
in ${DESC_TYPES[@]}; do
691 re
=$
(get_expr false
"$ty" "*_$ty" "*_${ty}_*")
692 desc
=$
(find * -maxdepth 0 -type f
$re 2>/dev
/null
)
694 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
695 # Check the vnfd|nsd folder
696 if [ ! -d $ty ]; then
699 re
=$
(get_expr false
"*")
700 desc
=$
(find $ty/* -maxdepth 0 -type f
$re 2>/dev
/null
)
701 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
703 elif [ ${#desc[@]} -gt 1 ]; then
704 echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
707 # Descriptor sub directory
712 if [ $TYPE == 'nsd' ]; then
713 folders
=("${NSD_FOLDERS[@]}")
715 folders
=("${VNFD_FOLDERS[@]}")
718 if [ $VERBOSE == true
]; then
719 echo "INFO: Determined descriptor is of type $TYPE"
724 if [[ -z $TYPE ]]; then
725 echo "ERROR: Unable to determine the descriptor type!" >&2
729 if [ $TYPE == 'nsd' ]; then
730 folders
=("${NSD_FOLDERS[@]}")
732 folders
=("${VNFD_FOLDERS[@]}")
735 # Check for descriptor of type provided on command line
736 re
=$
(get_expr false
"$TYPE" "*_${TYPE}" "*_${TYPE}_*")
737 desc
=$
(find * -maxdepth 0 -type f
$re 2>/dev
/null
)
739 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
740 # Check if it is under vnfd/nsd subdirectory
741 # Backward compatibility support
742 re
=$
(get_expr false
"*")
743 desc
=$
(find $TYPE/* -maxdepth 0 -type f
$re 2>/dev
/null
)
744 if [[ -z $desc ]] ||
[ ${#desc[@]} -eq 0 ]; then
745 echo "ERROR: Did not find descriptor file of type $TYPE" \
753 if [ ${#desc[@]} -gt 1 ]; then
754 echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
758 descriptor
=${desc[0]}
760 # Check if there are files not supported
761 files
=$
(find * -maxdepth 0 -type f
! -name $descriptor 2>/dev
/null
)
763 for f
in ${files[@]}; do
764 if [ $
(contains
"${ALLOWED_FILES[@]}" $f) == "n" ]; then
765 echo "WARN: Unsupported file $f found"
769 if [ $VERBOSE == true
]; then
770 echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
773 if [ $DRY_RUN == false
]; then
774 add_chksum
${descriptor}
777 # Check the folders are supported ones
778 dirs=$
( find * -maxdepth 0 -type d
)
780 for d
in ${dirs[@]}; do
781 if [ $
(contains
"${folders[@]}" $d) == "y" ]; then
782 if [ $DRY_RUN == false
]; then
783 find $d/* -type f
2>/dev
/null|
788 elif [[ -z $desc_sub_dir ]] ||
[ $d != $desc_sub_dir ]; then
789 echo "WARN: $d is not part of standard folders " \
790 "for descriptor type $TYPE in $PKG"
794 if [ $VERBOSE == true
]; then
795 echo "INFO: Creating archive for $PKG"
799 if [ $DRY_RUN == false
]; then
800 if [ $VERBOSE == true
]; then
801 tar zcvf
"$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
803 #tar zcvf ${name}.tar.gz ${name} >/dev/null 2>&1
804 tar zcvf
"$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM} > /dev
/null
2>&1
806 if [ $?
-ne 0 ]; then
808 echo "ERROR: creating archive for $PKG ($rc)" >&2
813 # Create, default to VNFD if no type is defined
814 if [[ -z $TYPE ]]; then
816 if [ $VERBOSE == true
]; then
817 echo "WARNING: Defaulting to descriptor type $TYPE"
821 if [ $TYPE == 'vnfd' ]; then
822 if [[ -z $IMAGE ]]; then
823 echo "ERROR: Image file need to be specified for VNF"
826 generate_package vnfd
$PKG $DEST_DIR
829 if [ $TYPE == 'nsd' -o $CREATE_NSD == true
]; then
830 generate_package nsd
$PKG $DEST_DIR