Newer
Older
############################################################################
# Copyright 2016 RIFT.io Inc #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
#
# This shell script is used to create a descriptor package
# The main functions of this script include:
# - Generate checksums.txt file
# - Generate a tar.gz file
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# This script can be used to create the required folders for
# a descriptor package and a template descriptor
# Usage: generate_descriptor_pkg.sh <base-directory> <package-directory>
# Descriptor names should be
# - (nsd|vnfd).(yaml|yml|json|xml)
# - *_(nsd|vnfd).(yaml|yml|json|xml)
# - *_(nsd|vnfd)_*.(yaml|yml|json|xml)
# - (nsd|vnfd)/*.(yaml|yml|json|xml)
#
SCRIPTNAME=`basename $0`
# From https://osm.etsi.org/wikipub/index.php/Release_0_Data_Model_Details
# Supported folders for VNFD
# cloud_init - Rel 4.3, not yet part of OSM
VNFD_FOLDERS=(images scripts icons charms cloud_init)
# Supported folders for NSD
# OSM document specifies (ns|vnf)-config folder, while Rel 4.3
# is using (ns|vnf)_config.
NSD_FOLDERS=(scripts icons ns_config vnf_config)
# Other files allowed in the descriptor base directory
ALLOWED_FILES=(README)
DESC_TYPES=(vnfd nsd)
DESC_EXTN=(yml yaml json xml)
CHKSUM='checksums.txt'
VERBOSE=false
DRY_RUN=false
CREATE=false
RM="--remove-files"
DEBUG=false
function usage() {
cat <<EOF
Usage:
$SCRIPTNAME [-t <type>] [-N] [-c] [base-directory] <package-dir>
-h|--help : show this message
-t|--package-type <nsd|vnfd> : Descriptor package type
is NSD or VNFD. Script will try to
determine the type if not provided.
Default is vnfd for create-folders.
-d|--destination-dir <destination directory>: Directory to create the
archived file.
-N|--no-remove-files : Do not remove the package files after creating
archive
-c|--create-folder : Create folder with the structure for the
package type using the base-dir and package-dir
and a descriptor template
-v| --verbose : Generate logs
-n| --dry-run : Validate the package dir
base-dir : Directory where the archive file or folders are created,
if destination directory is not specified.
Default is current directory
package-dir : The descriptor name and directory
EOF
}
function write_vnfd_tmpl() {
name=$(basename $1)
desc_file="${name}_vnfd.yaml"
cat >$desc_file <<EOF
# Only one VNFD per descriptor package is supported
# Update the <update> tag with correct values
# Update or remove <update, optional> tags
vnfd:vnfd-catalog:
vnfd:
- id: ${name}
name: ${name}
short-name: ${name}
description: ${name}
# Place the logo as png in icons directory and provide the name here
logo: <update, optional>
# This is an optional section and can be removed
vnf-configuration:
config-attributes:
config-delay: '0'
config-priority: '1'
# Specify the config method
# Remove this section if the VNF does not use a config method
juju:
charm: <update>
# config-primitive specifies the config and actions for the VNF/charm
# This is an optional section
config-primitive:
# If there are no config to be set, remove this section
- name: config
# There can be multiple parameter sections
# The name and type should be same as VNF/charm specification
parameter:
- name: <update>
data-type: STRING
mandatory: <true|false>
default-value: <update>
# If there are no actions specified at VNF level, remove this section
# Multiple action sections can be specified
# The name should be the name of the action
- name: <update, optional>
# There can be multiple parameter sections
# The name and type should be same as charm specification
parameter:
- name: <update, optional>
data-type: STRING
mandatory: <true|false>
# Any config or action that need to be executed when VNF comes up
# This is an optional section
initial-config-primitive:
- name: config
# Any config to be set when VNF comes up
# For charm normally the VNF management IP need to be set
# in one of the config parameters
# Sepcifying the value a <rw_mgmt_ip> will cause the MANO
# to replace this with the management ip of VNF when it is
# instantiated
parameter:
- name: <update>
value: <rw_mgmt_ip>
seq: '1'
mgmt-interface:
# This should be id of one of the VDUs specified for this VNF
vdu-id: ${name}-VM
port: <update, optional>
connection-point:
# Specify the connection points from VDU
# Can specify multiple connection points here
- name: eth0
type: VPORT
- name: eth1
type: VPORT
# Atleast one VDU need to be specified
vdu:
- id: ${name}-VM
name: ${name}-VM
description: ${name}-VM
# Image including the full path
image: <update>
# Flavour of the VM to be instantiated for the VDU
vm-flavor:
memory-mb: '4096'
storage-gb: '10'
vcpu-count: '2'
external-interface:
# Specify the external interfaces
# There can be multiple interfaces defined
- name: eth0
virtual-interface:
bandwidth: '0'
type: VIRTIO
vpci: 0000:00:0a.0
vnfd-connection-point-ref: eth0
- name: eth1
virtual-interface:
bandwidth: '0'
type: OM-MGMT
vpci: 0000:00:0b.0
vnfd-connection-point-ref: eth1
# Mgmt vpci, match with the specification in the
# external interface for mgmt
mgmt-vpci: 0000:00:0a.0
# Specify EPA parameters
# This section is optional
guest-epa:
cpu-pinning-policy: DEDICATED
cpu-thread-pinning-policy: PREFER
mempage-size: LARGE
numa-node-policy:
mem-policy: STRICT
node:
- id: '0'
paired-threads:
num-paired-threads: '1'
node-cnt: '1'
EOF
if [ $VERBOSE == true ]; then
echo "INFO: Created $desc_file"
fi
}
function write_nsd_tmpl() {
name=$(basename $1)
desc_file="${name}_nsd.yaml"
cat >$desc_file <<EOF
# Only one NSD per descriptor package is supported
# Update the <update> tag with correct values
# Update or remove <update, optional> tags
nsd:nsd-catalog:
nsd:
- id: ${name}
name: ${name}
short-name: ${name}
description: ${name}
# Place the logo as png in icons directory and provide the name here
logo: <update, optional>
# Specify the VNFDs that are part of this NSD
constituent-vnfd:
# The member-vnf-index needs to be unique, starting from 1
# vnfd-id-ref is the id of the VNFD
# Multiple constituent VNFDs can be specified
- member-vnf-index: <update>
vnfd-id-ref: <update>
vld:
# Management network for the VNFs
- id: management
name: management
provider-network:
overlay-type: VLAN
physical-network: <update>
type: ELAN
vnfd-connection-point-ref:
# Specify all the constituent VNFs
# member-vnf-index-ref - entry from constituent vnf
# vnfd-id-ref - VNFD id
# vnfd-connection-point-ref - connection point name in the VNFD
- member-vnf-index-ref: '1'
vnfd-connection-point-ref: <update>
vnfd-id-ref: <update>
- member-vnf-index-ref: '2'
vnfd-connection-point-ref: <update>
vnfd-id-ref: <update>
# Addtional netowrk can be specified for data, etc
- id: <update>
name: <update>
type: ELAN
provider-network:
overlay-type: VLAN
physical-network: <update>
vnfd-connection-point-ref:
- member-vnf-index-ref: <update>
vnfd-connection-point-ref: <update>
vnfd-id-ref: <update>
# NS level configuration primitive
# This section is optional
config-primitive:
- name: <update>
# List of parameters, optional
parameter:
- name: <update>
data-type: string
default-value: <update>
mandatory: <true|false>
# List of parameters grouped by name
# optional section
parameter-group:
- mandatory: <true|false>
name: <update>
parameter:
- name: <update>
data-type: integer
default-value: <update>
hidden: <true|false>
mandatory: <true|false>
# Script based NS level configuration
user-defined-script: <update>
EOF
if [ $VERBOSE == true ]; then
echo "INFO: Created $desc_file"
fi
}
function write_nsd_config_tmpl() {
name=$(basename $1)
cfg_file="ns_config/$name.yaml"
cat >$cfg_file <<EOF
EOF
if [ $VERBOSE == true ]; then
echo "INFO: Created $cfg_file"
fi
}
OPTS=`getopt -o vhnt:d:cN --long verbose,dry-run,help,package-type:,destination-dir,create-folder,no-remove-files,debug -n $SCRIPTNAME -- "$@"`
if [ $? != 0 ] ; then
echo "ERROR: Failed parsing options ($?)." >&2
usage
exit 1
fi
echo "$OPTS"
eval set -- "$OPTS"
cur_dir=`pwd`
# Check if the array contains a specific value
# Taken from
# http://stackoverflow.com/questions/3685970/check-if-an-array-contains-a-value
function contains() {
local n=$#
local value=${!n}
for ((i=1;i < $#;i++)); do
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
echo "n"
return 1
}
function check_type() {
type=$1
if [ $(contains "${DESC_TYPES[@]}" $type) == "y" ]; then
TYPE=$type
else
echo "ERROR: Unknown descriptor type $type!" >&2
exit 1
fi
}
function get_expr(){
# First argument is to specify if this is a negative match or match
# Rest are filename expressions without extension
#
local regex=" "
local n=$1
local neg="${1}"
for ((i=2;i <= $#;i++)); do
if [ $i -eq 2 ]; then
if [ $neg == true ]; then
subexpr='! -name'
else
subexpr='-name'
fi
else
if [ $neg == true ]; then
subexpr=' -a ! -name'
else
subexpr=' -o -name'
fi
fi
for extn in ${DESC_EXTN[$@]}; do
regex="$regex $subexpr ${!i}.$extn"
done
done
if [ $VERBOSE == true ]; then
echo "INFO: Generate expression: $expr"
fi
echo "$expr"
}
function get_expr(){
# First argument is to specify if this is a negative match or not
# Rest are filename expressions without extension
local regex=" "
local n=$#
local neg="${1}"
local first="true"
for ((i=2;i <= $#;i++)); do
for extn in "${DESC_EXTN[@]}"; do
if [ $first == true ]; then
if [ $neg == true ]; then
subexpr='! -name'
else
subexpr='-name'
fi
first=false
else
if [ $neg == true ]; then
subexpr=' -a ! -name'
else
subexpr=' -o -name'
fi
fi
regex="$regex $subexpr ${!i}.$extn"
done
done
echo "$regex"
}
while true; do
case "$1" in
-v | --verbose ) VERBOSE=true; shift ;;
-h | --help ) usage; exit 0; shift ;;
-n | --dry-run ) DRY_RUN=true; shift ;;
-t | --package-type ) check_type "$2"; shift; shift ;;
-d | --destination-dir ) DEST_DIR=$2; shift; shift;;
-c | --create-folder ) CREATE=true; shift;;
-N | --no-remove-files ) RM=''; shift;;
--debug ) DEBUG=true; shift;;
-- ) shift; break ;;
* ) break ;;
esac
done
if [ $DEBUG == true ]; then
echo "INFO: Debugging ON"
set -x
fi
if [ $VERBOSE == true ]; then
echo "INFO: Descriptor type: $TYPE"
fi
# Dry run is to validate existing descriptor folders
if [ $DRY_RUN == true ] && [ $CREATE == true ]; then
echo "ERROR: Option dry-run with create-folders not supported!" >&2
exit 1
fi
if [ $# -gt 1 ]; then
BASE_DIR=$1
PKG=$(basename $2)
else
BASE_DIR=$(dirname $1)
PKG=$(basename $1)
fi
if [ $VERBOSE == true ]; then
echo "INFO: Using base dir: $BASE_DIR"
fi
if [ $VERBOSE == true ]; then
echo "INFO: Using package: $PKG"
fi
if [ -z "$PKG" ]; then
echo "ERROR: Need to specify the package-dir" >&2
usage >&2
exit 1
fi
cd $BASE_DIR
if [ $? -ne 0 ]; then
echo "ERROR: Unable to change to base directory $BASE_DIR!" >&2
exit 1
fi
# Get full base dir path
BASE_DIR=`pwd`
cd $cur_dir
if [ -z $DEST_DIR ]; then
DEST_DIR=$BASE_DIR # Default to base directory
fi
mkdir -p $DEST_DIR
cd $DEST_DIR
if [ $? -ne 0 ]; then
echo "ERROR: Not able to access destination directory $DEST_DIR!" >&2
exit 1
fi
# Get the full destination dir path
DEST_DIR=`pwd`
cd $cur_dir
dir=${BASE_DIR}/${PKG}
function add_chksum() {
if [ $VERBOSE == true ]; then
echo "INFO: Add file $1 to $CHKSUM"
fi
md5sum $1 >> $CHKSUM
}
if [ $CREATE == false ]; then
if [ ! -d $dir ]; then
echo "INFO: Package folder $dir not found!" >&2
exit 1
fi
cd $dir
if [ $? -ne 0 ]; then
rc=$?
echo "ERROR: changing directory to $dir ($rc)" >&2
exit $rc
fi
# Remove checksum file, if present
rm -f $CHKSUM
# Check if the descriptor file is present
if [ -z $TYPE ]; then
# Desc type not specified, look for the desc file and guess the type
# Required for backward compatibility
for ty in ${DESC_TYPES[@]}; do
re=$(get_expr false "$ty" "*_$ty" "*_${ty}_*")
desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
if [ -z $desc ] || [ ${#desc[@]} -eq 0 ]; then
# Check the vnfd|nsd folder
if [ ! -d $ty ]; then
continue
fi
re=$(get_expr false "*")
desc=$(find $ty/* -maxdepth 0 -type f $re 2>/dev/null)
if [ -z $desc ] || [ ${#desc[@]} -eq 0 ]; then
continue
elif [ ${#desc[@]} -gt 1 ]; then
echo "ERROR: Found multiple descriptor files: ${desc[@]}" >&2
exit 1
fi
# Descriptor sub directory
desc_sub_dir=$ty
fi
TYPE=$ty
if [ $TYPE == 'nsd' ]; then
folders=("${NSD_FOLDERS[@]}")
else
folders=("${VNFD_FOLDERS[@]}")
fi
if [ $VERBOSE == true ]; then
echo "INFO: Determined descriptor is of type $TYPE"
fi
break
done
if [ -z $TYPE ]; then
echo "ERROR: Unable to determine the descriptor type!" >&2
exit 1
fi
else
if [ $TYPE == 'nsd' ]; then
folders=("${NSD_FOLDERS[@]}")
else
folders=("${VNFD_FOLDERS[@]}")
fi
# Check for descriptor of type provided on command line
re=$(get_expr false "$TYPE" "*_${TYPE}" "*_${TYPE}_*")
desc=$(find * -maxdepth 0 -type f $re 2>/dev/null)
if [ -z $desc ] || [ ${#desc[@]} -eq 0 ]; then
# Check if it is under vnfd/nsd subdirectory
# Backward compatibility support
re=$(get_expr false "*")
desc=$(find $TYPE/* -maxdepth 0 -type f $re 2>/dev/null)
if [ -z $desc ] || [ ${#desc[@]} -eq 0 ]; then
echo "ERROR: Did not find descriptor file of type $TYPE" \
" in $dir" >&2
exit 1
fi
desc_sub_dir=$ty
fi
fi
if [ ${#desc[@]} -gt 1 ]; then
echo "ERROR: Found multiple files of type $TYPE in $dir: $desc" >&2
exit 1
fi
descriptor=${desc[0]}
# Check if there are files not supported
files=$(find * -maxdepth 0 -type f ! -name $descriptor 2>/dev/null)
for f in ${files[@]}; do
if [ $(contains "${ALLOWED_FILES[@]}" $f) == "n" ]; then
echo "WARN: Unsupported file $f found"
fi
done
if [ $VERBOSE == true ]; then
echo "INFO: Found descriptor package: ${desc_sub_dir} ${descriptor}"
fi
if [ $DRY_RUN == false ]; then
add_chksum ${descriptor}
fi
# Check the folders are supported ones
dirs=$( find * -maxdepth 0 -type d )
for d in ${dirs[@]}; do
if [ $(contains "${folders[@]}" $d) == "y" ]; then
if [ $DRY_RUN == false ]; then
find $d/* -type f 2>/dev/null|
while read file; do
add_chksum $file
done
fi
elif [ -z $desc_sub_dir ] || [ $d != $desc_sub_dir ]; then
echo "WARN: $d is not part of standard folders " \
"for descriptor type $TYPE in $PKG"
fi
done
if [ $VERBOSE == true ]; then
echo "INFO: Creating archive for $PKG"
fi
cd $BASE_DIR
if [ $DRY_RUN == false ]; then
tar zcvf "$DEST_DIR/$PKG.tar.gz" "${PKG}" ${RM}
if [ $? -ne 0 ]; then
rc=$?
echo "ERROR: creating archive for $PKG ($rc)" >&2
exit $rc
fi
fi
else
# Create the folders for the descriptor
if [ $VERBOSE == true ]; then
echo "INFO: Creating folders for $PKG in $dir"
fi
# Create, default to VNFD if no type is defined
if [ -z $TYPE ]; then
TYPE=vnfd
echo "WARNING: Defaulting to descriptor type $TYPE"
fi
mkdir -p $dir && cd $dir
if [ $? -ne 0 ]; then
rc=$?
echo "ERROR: creating directory $dir ($rc)" >&2
exit $rc
fi
if [ $TYPE == 'nsd' ]; then
folders=("${NSD_FOLDERS[@]}")
else
folders=("${VNFD_FOLDERS[@]}")
fi
for d in ${folders[@]}; do
mkdir -p $dir/$d
if [ $? -ne 0 ]; then
rc=$?
echo "ERROR: creating directory $dir/$d ($rc)" >&2
exit $rc
fi
if [ $VERBOSE == true ]; then
echo "Created folder $d in $dir"
fi
done
if [ $VERBOSE == true ]; then
echo "INFO: Created folders for in $dir"
fi
# Write a descriptor template file
if [ $TYPE == 'vnfd' ]; then
write_vnfd_tmpl $dir
else
write_nsd_tmpl $dir
# write_nsd_config_tmpl $dir
fi
fi
cd $cur_dir