| velandy | 9d1a08d | 2016-06-27 16:48:41 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 2 | |
| 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 Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 19 | # |
| 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 Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 24 | # This script can be used to create the required folders for |
| 25 | # a descriptor package and a template descriptor |
| Austin Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 26 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 27 | # Usage: generate_descriptor_pkg.sh <base-directory> <package-directory> |
| Austin Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 28 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 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) |
| 34 | # |
| Austin Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 35 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 36 | SCRIPTNAME=`basename $0` |
| Austin Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 37 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 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) |
| 42 | |
| 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) |
| 47 | |
| 48 | # Other files allowed in the descriptor base directory |
| 49 | ALLOWED_FILES=(README) |
| 50 | |
| 51 | DESC_TYPES=(vnfd nsd) |
| 52 | DESC_EXTN=(yml yaml json xml) |
| 53 | CHKSUM='checksums.txt' |
| 54 | |
| 55 | VERBOSE=false |
| 56 | DRY_RUN=false |
| 57 | CREATE=false |
| 58 | RM="--remove-files" |
| 59 | DEBUG=false |
| 60 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 61 | ARCHIVE=false |
| 62 | CREATE_NSD=false |
| 63 | VENDOR='OSM' |
| 64 | INTF_TYPE='VIRTIO' |
| 65 | VCPU=2 |
| 66 | MEMORY=4096 |
| 67 | STORAGE=10 |
| Philip Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 68 | INTERFACES=1 |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 69 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 70 | function usage() { |
| 71 | cat <<EOF |
| 72 | Usage: |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 73 | $SCRIPTNAME [-t <type>] [-N] [-c] [base-directory] <package-name> |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 74 | |
| 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 84 | Default is base-directory |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 85 | |
| 86 | -N|--no-remove-files : Do not remove the package files after creating |
| 87 | archive |
| 88 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 89 | Options specifc for create descriptor: |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 90 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 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 |
| 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 Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 101 | --interface-type : Interface type [VIRTIO|SR-IOV|PCI-PASSTHROUGH|E1000] |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 102 | 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 Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 121 | --interfaces : Number of external interfaces in additon to OM-MGMT. Default 1. |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 122 | |
| 123 | End of create descriptor specific options |
| 124 | |
| 125 | -v| --verbose : Generate progress details |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 126 | |
| 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 133 | package-name : The descriptor name (full path if base-dir not specified) |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 134 | EOF |
| 135 | } |
| 136 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 137 | CP_TYPE='VPORT' |
| 138 | function 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 |
| 150 | get_pci() { |
| 151 | printf '%02x' $((10 + $1)) | tr '[:upper:]' '[:lower:]' |
| 152 | } |
| 153 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 154 | function write_vnfd_tmpl() { |
| 155 | name=$(basename $1) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 156 | desc_file="${name}.yaml" |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 157 | |
| 158 | cat >$desc_file <<EOF |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 159 | vnfd:vnfd-catalog: |
| 160 | vnfd: |
| 161 | - id: ${name} |
| 162 | name: ${name} |
| 163 | short-name: ${name} |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 164 | description: Generated by OSM pacakage generator |
| 165 | vendor: ${VENDOR} |
| 166 | version: '1.0' |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 167 | |
| 168 | # Place the logo as png in icons directory and provide the name here |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 169 | # logo: <update, optional> |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 170 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 171 | # Management interface |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 172 | mgmt-interface: |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 173 | vdu-id: ${name}-VM |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 174 | |
| 175 | # Atleast one VDU need to be specified |
| 176 | vdu: |
| 177 | - id: ${name}-VM |
| 178 | name: ${name}-VM |
| 179 | description: ${name}-VM |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 180 | count: 1 |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 181 | |
| 182 | # Flavour of the VM to be instantiated for the VDU |
| 183 | vm-flavor: |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 184 | vcpu-count: ${VCPU} |
| 185 | memory-mb: ${MEMORY} |
| 186 | storage-gb: ${STORAGE} |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 187 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 188 | # Image including the full path |
| 189 | image: '${IMAGE}' |
| 190 | |
| 191 | EOF |
| 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}' |
| 199 | EOF |
| Philip Joseph | c737a09 | 2016-09-27 12:34:32 +0530 | [diff] [blame] | 200 | elif [[ -n ${CLOUD_INIT} ]]; then |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 201 | cat >>$desc_file <<EOF |
| 202 | # Cloud init to use |
| 203 | cloud-init: '${CLOUD_INIT}' |
| 204 | EOF |
| 205 | fi |
| 206 | |
| 207 | # Add external interfaces |
| 208 | cat >>$desc_file <<EOF |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 209 | external-interface: |
| 210 | # Specify the external interfaces |
| 211 | # There can be multiple interfaces defined |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 212 | EOF |
| 213 | |
| Philip Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 214 | # 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 |
| 222 | EOF |
| 223 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 224 | # Add external interfaces |
| Philip Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 225 | for i in `seq 1 ${INTERFACES}`; do |
| 226 | eth=$(($i)) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 227 | 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} |
| 235 | EOF |
| 236 | done |
| 237 | |
| 238 | # Add connection points |
| 239 | cat >>$desc_file <<EOF |
| 240 | |
| 241 | connection-point: |
| 242 | EOF |
| 243 | |
| Philip Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 244 | for i in `seq 0 ${INTERFACES}`; do |
| 245 | eth=$(($i)) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 246 | cat >>$desc_file <<EOF |
| 247 | - name: eth${eth} |
| 248 | type: ${CP_TYPE} |
| 249 | EOF |
| 250 | done |
| 251 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 252 | if [ $VERBOSE == true ]; then |
| 253 | echo "INFO: Created $desc_file" |
| 254 | fi |
| 255 | } |
| 256 | |
| 257 | function write_nsd_tmpl() { |
| 258 | name=$(basename $1) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 259 | vnfd=$2 |
| 260 | desc_file="${name}.yaml" |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 261 | |
| 262 | cat >$desc_file <<EOF |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 263 | nsd:nsd-catalog: |
| 264 | nsd: |
| 265 | - id: ${name} |
| 266 | name: ${name} |
| 267 | short-name: ${name} |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 268 | description: Generated by OSM pacakage generator |
| 269 | vendor: ${VENDOR} |
| 270 | version: '1.0' |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 271 | |
| 272 | # Place the logo as png in icons directory and provide the name here |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 273 | # logo: <update, optional> |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 274 | |
| 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 280 | - member-vnf-index: 1 |
| 281 | vnfd-id-ref: ${vnfd} |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 282 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 283 | EOF |
| 284 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 285 | cat >>$desc_file <<EOF |
| 286 | vld: |
| 287 | # Networks for the VNFs |
| 288 | EOF |
| 289 | |
| Philip Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 290 | # Add management VLD |
| 291 | cat >>$desc_file <<EOF |
| Philip Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 292 | - id: ${name}_vld0 |
| Philip Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 293 | 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 |
| 309 | EOF |
| 310 | |
| 311 | # Add rest of VLDs |
| Philip Joseph | 8f0826d | 2016-09-28 21:40:39 +0530 | [diff] [blame] | 312 | for i in `seq 1 ${INTERFACES}`; do |
| 313 | eth=$(($i)) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 314 | cat >>$desc_file <<EOF |
| 315 | - id: ${name}_vld${i} |
| 316 | name: ${name}_vld${i} |
| Philip Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 317 | short-name: ${name}_vld${i} |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 318 | type: ELAN |
| Philip Joseph | 89c5ea0 | 2016-09-28 00:35:59 +0530 | [diff] [blame] | 319 | # vim-network-name: <update> |
| Philip Joseph | 14060ca | 2016-09-14 18:30:57 +0000 | [diff] [blame] | 320 | # provider-network: |
| 321 | # overlay-type: VLAN |
| Philip Joseph | 14060ca | 2016-09-14 18:30:57 +0000 | [diff] [blame] | 322 | # segmentation_id: <update> |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 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} |
| Philip Joseph | 14060ca | 2016-09-14 18:30:57 +0000 | [diff] [blame] | 330 | # NOTE: Validate the entry below |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 331 | nsd:vnfd-connection-point-ref: eth${eth} |
| 332 | EOF |
| 333 | done |
| 334 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 335 | if [ $VERBOSE == true ]; then |
| 336 | echo "INFO: Created $desc_file" |
| 337 | fi |
| 338 | } |
| 339 | |
| 340 | function write_nsd_config_tmpl() { |
| 341 | name=$(basename $1) |
| 342 | cfg_file="ns_config/$name.yaml" |
| 343 | |
| 344 | cat >$cfg_file <<EOF |
| 345 | |
| 346 | EOF |
| 347 | |
| 348 | if [ $VERBOSE == true ]; then |
| 349 | echo "INFO: Created $cfg_file" |
| 350 | fi |
| 351 | } |
| 352 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 353 | cur_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 |
| 358 | function 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 Cormier | 3aeed12 | 2016-05-23 19:12:00 -0400 | [diff] [blame] | 366 | done |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 367 | echo "n" |
| 368 | return 1 |
| 369 | } |
| 370 | |
| 371 | function 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 | |
| 381 | function get_expr(){ |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 382 | # 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 413 | function 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 | |
| 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 -- "$@"` |
| 502 | |
| 503 | if [ $? != 0 ] ; then |
| 504 | echo "ERROR: Failed parsing options ($?)." >&2 |
| 505 | usage |
| 506 | exit 1 |
| 507 | fi |
| 508 | |
| 509 | echo "$OPTS" |
| 510 | eval set -- "$OPTS >/dev/null 2>&1" |
| 511 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 512 | while 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 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;; |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 532 | --debug ) DEBUG=true; shift;; |
| 533 | -- ) shift; break ;; |
| 534 | * ) break ;; |
| 535 | esac |
| 536 | done |
| 537 | |
| 538 | if [ $DEBUG == true ]; then |
| 539 | echo "INFO: Debugging ON" |
| 540 | set -x |
| 541 | fi |
| 542 | |
| 543 | if [ $VERBOSE == true ]; then |
| 544 | echo "INFO: Descriptor type: $TYPE" |
| 545 | fi |
| 546 | |
| 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 |
| 550 | exit 1 |
| 551 | fi |
| 552 | |
| 553 | if [ $# -gt 1 ]; then |
| 554 | BASE_DIR=$1 |
| 555 | PKG=$(basename $2) |
| 556 | else |
| 557 | BASE_DIR=$(dirname $1) |
| 558 | PKG=$(basename $1) |
| 559 | fi |
| 560 | |
| 561 | if [ $VERBOSE == true ]; then |
| 562 | echo "INFO: Using base dir: $BASE_DIR" |
| 563 | fi |
| 564 | |
| 565 | if [ $VERBOSE == true ]; then |
| 566 | echo "INFO: Using package: $PKG" |
| 567 | fi |
| 568 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 569 | if [[ -z "$PKG" ]]; then |
| 570 | echo "ERROR: Need to specify the package name" >&2 |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 571 | usage >&2 |
| 572 | exit 1 |
| 573 | fi |
| 574 | |
| Philip Joseph | 14060ca | 2016-09-14 18:30:57 +0000 | [diff] [blame] | 575 | if [[ ! -d $BASE_DIR ]]; then |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 576 | 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 582 | fi |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 583 | fi |
| Philip Joseph | 14060ca | 2016-09-14 18:30:57 +0000 | [diff] [blame] | 584 | cd $BASE_DIR |
| 585 | if [ $? -ne 0 ]; then |
| 586 | echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2 |
| 587 | exit 1 |
| 588 | fi |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 589 | |
| 590 | # Get full base dir path |
| 591 | BASE_DIR=`pwd` |
| 592 | cd $cur_dir |
| 593 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 594 | if [[ -z $DEST_DIR ]]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 595 | DEST_DIR=$BASE_DIR # Default to base directory |
| 596 | fi |
| 597 | |
| 598 | mkdir -p $DEST_DIR |
| 599 | |
| 600 | cd $DEST_DIR |
| 601 | if [ $? -ne 0 ]; then |
| 602 | echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2 |
| 603 | exit 1 |
| 604 | fi |
| 605 | |
| 606 | # Get the full destination dir path |
| 607 | DEST_DIR=`pwd` |
| 608 | cd $cur_dir |
| 609 | |
| 610 | dir=${BASE_DIR}/${PKG} |
| 611 | |
| 612 | function add_chksum() { |
| 613 | if [ $VERBOSE == true ]; then |
| 614 | echo "INFO: Add file $1 to $CHKSUM" |
| 615 | fi |
| 616 | |
| 617 | md5sum $1 >> $CHKSUM |
| 618 | } |
| 619 | |
| 620 | if [ $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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 637 | if [[ -z $TYPE ]]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 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) |
| 643 | |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 644 | if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 645 | # 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 651 | if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 652 | 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 674 | if [[ -z $TYPE ]]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 675 | 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 689 | if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 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) |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 694 | if [[ -z $desc ]] || [ ${#desc[@]} -eq 0 ]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 695 | 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 Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 738 | elif [[ -z $desc_sub_dir ]] || [ $d != $desc_sub_dir ]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 739 | 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 |
| 757 | else |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 758 | # Create, default to VNFD if no type is defined |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 759 | if [[ -z $TYPE ]]; then |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 760 | TYPE=vnfd |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 761 | if [ $VERBOSE == true ]; then |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 762 | echo "WARNING: Defaulting to descriptor type $TYPE" |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 763 | fi |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 764 | fi |
| 765 | |
| Philip Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 766 | if [ $TYPE == 'vnfd' ]; then |
| Philip Joseph | 1c36f4a | 2016-09-14 07:05:33 +0000 | [diff] [blame] | 767 | 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 Joseph | 15b76cb | 2016-07-08 01:42:08 +0530 | [diff] [blame] | 776 | fi |
| 777 | |
| 778 | fi |
| 779 | |
| 780 | cd $cur_dir |