From dc5adfcfe2f4c2534ad512f8d3b811d86a473089 Mon Sep 17 00:00:00 2001 From: Martin Kulhavy Date: Tue, 15 Aug 2017 18:18:48 +0300 Subject: [PATCH] Simplify argument parsing and add usage info The code for initial parsing of the script arguments was simplified and added better checks of the arguments. For clearer information, usage information can be printed out. Pod special cases were removed (can be directly specified using custom). Change-Id: I42ca0ed703d5062f6deae74aa931513c235e2898 Signed-off-by: Martin Kulhavy --- ci/03-maasdeploy.sh | 78 +++++++++++++++++++++++++++++++++-------------------- ci/tools.sh | 19 ++++++++++++- 2 files changed, 67 insertions(+), 30 deletions(-) diff --git a/ci/03-maasdeploy.sh b/ci/03-maasdeploy.sh index 5ed39341..2b99ff6c 100755 --- a/ci/03-maasdeploy.sh +++ b/ci/03-maasdeploy.sh @@ -42,9 +42,21 @@ sudo apt-get install bridge-utils openssh-server bzr git virtinst qemu-kvm libvi sudo -H pip install --upgrade pip -#first parameter should be custom and second should be either -# absolute location of file (including file name) or url of the -# file to download. + +usage() { + # no xtrace output + { set +x; } 2> /dev/null + + echo "OPNFV JOID deployer of the MAAS (Metal as a Service) infrastructure." + echo "Usage: $0 custom " + echo " $0 virtual" + exit ${1-0} +} + +if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] +then + usage; +fi # @@ -52,43 +64,51 @@ sudo -H pip install --upgrade pip # # Get labconfig and generate deployconfig.yaml -echo_info "Parsing lab configuration file" + case "$labname" in - intelpod[569]|orangepod[12]|cengnpod[12] ) - array=(${labname//pod/ }) - cp ../labconfig/${array[0]}/pod${array[1]}/labconfig.yaml . - python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml - ;; - 'attvirpod1' ) - cp ../labconfig/att/virpod1/labconfig.yaml . - python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml - ;; 'custom') + # Deployment with a custom labconfig file labfile=$2 - if [ -e $labfile ]; then - cp $labfile ./labconfig.yaml || true - else - wget $labconfigfile -t 3 -T 10 -O ./labconfig.yaml || true + if [ -z "$labfile" ]; then + if [ ! -e ./labconfig.yaml ]; then + # no labconfig file was specified and no ci/labconfig.yaml is present + echo_error "Labconfig file must be specified when using custom" + usage 1 + else + # no labconfig file was specified and but a (backup) ci/labconfig.yaml found + echo_warning "Labconfig was not specified, using ./labconfig.yaml instead" + # no action needed, ./labconfig.yaml already present + fi + elif [ ! -e "$labfile" ]; then + # labconfig file was specified but does not exist on disk + echo_warning "Labconfig not found locally, trying download" + + wget $labfile -t 3 -T 10 -O ./labconfig.yaml || true count=`wc -l labconfig.yaml | cut -d " " -f 1` if [ $count -lt 10 ]; then - rm -rf labconfig.yaml + echo_error "Unable to download labconfig" + exit 1 fi - fi - if [ ! -e ./labconfig.yaml ]; then - virtinstall=1 - labname="default" - cp ../labconfig/default/labconfig.yaml ./ - cp ../labconfig/default/deployconfig.yaml ./ else - python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml - labname=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'` + echo_info "Using $labfile to setup deployment" + cp $labfile ./labconfig.yaml fi + + python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml + labname=`grep "maas_name" deployconfig.yaml | cut -d ':' -f 2 | sed -e 's/ //'` ;; - * ) - virtinstall=1 - labname="default" + 'virtual'|'') + # Virtual deployment using a default labconfig file + echo_info "Using default labconfig for virtual install" cp ../labconfig/default/labconfig.yaml ./ python genDeploymentConfig.py -l labconfig.yaml > deployconfig.yaml + labname="default" + virtinstall=1 + ;; + * ) + # Unknown argument + echo_error "Unknown script argument: $labname" + usage 1 ;; esac diff --git a/ci/tools.sh b/ci/tools.sh index ecb2e806..45db67e1 100644 --- a/ci/tools.sh +++ b/ci/tools.sh @@ -28,7 +28,7 @@ function echo_info { ( } ####################################### -# Echo error +# Echo error (to stderr) # Arguments: # Same as for echo # Returns: @@ -43,3 +43,20 @@ function echo_error { ( >&2 echo "${@:1:($#-1)}" -e "$red_bold${@: -1}$color_off"; ) } + +####################################### +# Echo warning (to stderr) +# Arguments: +# Same as for echo +# Returns: +# None +####################################### +function echo_warning { ( + # don't clutter the script output with the xtrace of the echo command + { set +x; } 2> /dev/null + + red_italic='\033[3;91m' + color_off='\033[0m' + >&2 echo "${@:1:($#-1)}" -e "$red_italic${@: -1}$color_off"; + ) +} -- 2.16.6