+
+ if [ $VERBOSE == true ]; then
+ echo "INFO: Created folders for in $dir"
+ fi
+
+ # Write a descriptor template file
+ if [ $type == 'vnfd' ]; then
+
+ # Copy cloud init file to correct folder
+ if [[ -n ${CLOUD_INIT_FILE} ]]; then
+ if [[ -e ${CLOUD_INIT_FILE} ]]; then
+ cp ${CLOUD_INIT_FILE} $dir/cloud_init
+ else
+ echo "ERROR: Unable to find cloud-init-file ${CLOUD_INIT_FILE}"
+ exit 1
+ fi
+ fi
+
+ write_vnfd_tmpl $dir
+ else
+ write_nsd_tmpl $dir $vnfd
+ fi
+
+ write_readme $dir
+
+ if [ $ARCHIVE == true ]; then
+ # Create archive of the package
+ cd $dest_dir
+ if [ $VERBOSE == true ]; then
+ tar zcvf ${name}.tar.gz ${name}
+ echo "Created package ${name}.tar.gz in $dest_dir"
+ else
+ tar zcvf ${name}.tar.gz ${name} >/dev/null 2>&1
+ fi
+
+ if [ $? -ne 0 ]; then
+ echo "ERROR: Creating archive for ${name} in $dest_dir" >&2
+ exit 1
+ fi
+
+ echo "$dest_dir/${name}.tar.gz" >&2
+
+ if [ $RM == true ]; then
+ rm -rf ${name}
+ fi
+ fi
+}
+
+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 -- "$@"`
+
+if [ $? != 0 ] ; then
+ echo "ERROR: Failed parsing options ($?)." >&2
+ usage
+ exit 1
+fi
+
+echo "$OPTS"
+eval set -- "$OPTS >/dev/null 2>&1"
+
+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;;
+ -a | --archive ) ARCHIVE=true; shift;;
+ --nsd ) CREATE_NSD=true; shift;;
+ --vendor ) VENDOR=$2; shift; shift;;
+ --interface-type ) INTF_TYPE=$2; shift; shift;;
+ --vcpu ) VCPU=$2; shift; shift;;
+ --memory ) MEMORY=$2; shift; shift;;
+ --storage ) STORAGE=$2; shift; shift;;
+ --image ) IMAGE=$2; shift; shift;;
+ --cloud-init ) CLOUD_INIT=$2; shift; shift;;
+ --cloud-init-file ) CLOUD_INIT_FILE=$2; shift; shift;;
+ --interfaces ) INTERFACES=$2; shift; 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 name" >&2
+ usage >&2
+ exit 1
+fi
+
+if [[ ! -d $BASE_DIR ]]; then
+ if [ $CREATE == true ]; then
+ mkdir -p $BASE_DIR
+ if [ $? -ne 0 ]; then
+ echo "ERROR: Unable to create base directory $BASE_DIR" >&2
+ exit 1
+ fi
+ fi
+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, default to VNFD if no type is defined
+ if [[ -z $TYPE ]]; then
+ TYPE=vnfd
+ if [ $VERBOSE == true ]; then
+ echo "WARNING: Defaulting to descriptor type $TYPE"
+ fi
+ fi
+
+ if [ $TYPE == 'vnfd' ]; then
+ if [[ -z $IMAGE ]]; then
+ echo "ERROR: Image file need to be specified for VNF"
+ exit 1
+ fi
+ generate_package vnfd $PKG $DEST_DIR
+ fi
+
+ if [ $TYPE == 'nsd' -o $CREATE_NSD == true ]; then
+ generate_package nsd $PKG $DEST_DIR
+ fi
+
+fi
+
+cd $cur_dir