oai-ci-vm-tool 22.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/bash
#/*
# * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
# * contributor license agreements.  See the NOTICE file distributed with
# * this work for additional information regarding copyright ownership.
# * The OpenAirInterface Software Alliance licenses this file to You under
# * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
# *
# * 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.
# *-------------------------------------------------------------------------------
# * For more information about the OpenAirInterface (OAI) Software Alliance:
# *      contact@openairinterface.org
# */

23

24 25 26 27 28
function top_usage {
    echo "OAI CI VM script"
    echo "   Original Author: Raphael Defosseux"
    echo "   Requirements:"
    echo "     -- uvtool uvtool-libvirt apt-cacher"
29
    echo "     -- $VM_OSREL image already synced"
30 31 32
    echo ""
    echo "Usage:"
    echo "------"
33
    echo "    oai-ci-vm-tool (-h|--help) { `for i in "${COMMANDS_NAME[@]}"; do echo -n $i,;done` ..." }
34 35 36 37
    echo ""
}

function variant_usage {
38 39 40 41 42 43 44 45 46 47 48 49 50 51

    for (( i=0 ; i<${#VARIANTS_LONG[@]} ; i++ )) 
    do        
    	unset BUILD_OPTIONS
    	unset VARIANT_INFO
    	check_set_variant "${VARIANTS_SHORT[$i]}"
        printf  "    --variant %-15s OR -%-3s" "${VARIANTS_LONG[$i]}" "${VARIANTS_SHORT[$i]}"
        if [ "$BUILD_OPTIONS" = "" ]
        then
        	echo -n "    ( test "
        else
        	echo -n "    ( build and test "
        fi
        echo "$VARIANT_INFO )"
52 53 54 55 56 57 58
        if [ "$1" = "full" ]
        then        
            printf  "               VM_NAME=%-15s          ARCHIVES_LOC=%-15s\n" "$VM_NAME" "$ARCHIVES_LOC"
            printf  "               VM_MEMORY=%-15s        VM_CPU=%-15s\n" "$VM_MEMORY" "$VM_CPU"       
            printf  "               NB_PATTERN_FILES=%-15s BUILD_OPTIONS=%-15s\n" "$NB_PATTERN_FILES" "\"$BUILD_OPTIONS\""
            printf  "               LOG_PATTERN=%-15s      EXPERIMENTAL=%-15s\n\n\n" "$LOG_PATTERN"  "$EXPERIMENTAL"
        fi
59 60 61
    done    
}

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
function command_options_usage {

    local -A HELP_MSG
    HELP_MSG["jn"]="    Specify the name of the Jenkins job."
    HELP_MSG["id"]="    Specify the build ID of the Jenkins job."
    HELP_MSG["ws"]="    Specify the workspace"
    HELP_MSG["var"]="    Specify the variant to build."
    HELP_MSG["k"]="    Keep the VM alive after the build."
    HELP_MSG["D"]="    Run as daemon"
    HELP_MSG["gu"]="    Specify the URL of the GIT Repository."
    HELP_MSG["pu"]="    trigger a push action from the Jenkins job to the specified url, \"--trigger pu\" can also be used"
    HELP_MSG["mr"]="    trigger a merge request action from the Jenkins job, \"--trigger mr\" can also be used"
    HELP_MSG["sb"]="    Specify the source branch of the merge request."
    HELP_MSG["sc"]="    Specify the source commit ID (SHA-1) of the merge request."
    HELP_MSG["tb"]="    Specify the target branch of the merge request (usually develop)."    
    HELP_MSG["tc"]="    Specify the target commit ID (SHA-1) of the merge request."
    HELP_MSG["br"]="    Specify the branch of the push event."
    HELP_MSG["co"]="    Specify the commit ID (SHA-1) of the push event."
    HELP_MSG["epc"]="    (two arguments) Specify the ip addresses of an external EPC, on respectively the core network and the UEs network"
    HELP_MSG["sim"]="   Specify the path to the sim file, used to emulate the oai UE sim card "
    for (( m=0 ; m<2 ; m++ ))
    do
      if [[ $m -eq 0  || ( $m > 0 && ${MANDATORY_OPTMASK[$m]} > 0 ) ]]
      then
        echo "Mandatory Options ($((m+1))):"
        echo "---------------------"    
        for (( i=0 ; i<${#SHORT_OPTIONS[@]} ; i++ )) 
        do  
    	  if [ $(( MANDATORY_OPTMASK[$m] & $(( 2**$i )) )) -ne 0 ]
    	  then
    	     if [ "${LONG_OPTIONS[$i]}" != "" ]
    	     then
    	       local ARGPATTERN
    	       if [[ !("${HELP_MSG[${SHORT_OPTIONS[$i]}]}" =~  "Specify") ]]
    	       then
    	       	   ARGPATTERN=""
    	       elif [[ "${HELP_MSG[${SHORT_OPTIONS[$i]}]}" =~  "(two arguments)" ]]
    	       then 
       	       	   ARGPATTERN="#### ####"
       	       else
       	       	   ARGPATTERN="####"
    	       fi
    	   	   printf "    %s %s OR "  "--${LONG_OPTIONS[$i]}" "$ARGPATTERN"
    	     fi
             printf "%s %s \n" "-${SHORT_OPTIONS[$i]}" "$ARGPATTERN"
             printf "  %s\n\n"  "${HELP_MSG[${SHORT_OPTIONS[$i]}]}"
          fi
        done
    fi
    done
    echo "Options:"
    echo "--------"

    for (( i=0 ; i<${#SHORT_OPTIONS[@]} ; i++ )) 
    do  
    	if [ $(( ALLOWED_OPTMASK & $(( 2**$i )) )) -ne 0 ]
    	then
    	   if [ "${LONG_OPTIONS[$i]}" != "" ]
    	   then
    	   	   printf "    %s #### OR "  "--${LONG_OPTIONS[$i]}"
    	   fi
           printf "%s #### \n" "-${SHORT_OPTIONS[$i]}"
           printf "  %s\n\n"  "${HELP_MSG[${SHORT_OPTIONS[$i]}]}"
        fi
    done 

    for (( i=0 ; i<${#SHORT_OPTIONS[@]} ; i++ )) 
    do  
    	if [ $(( ALLOWED_OPTMASK & $(( 2**$i )) )) -ne 0 ]
    	then
    	   if [ "$(type -t ${LONG_OPTIONS[$i]}_usage)" = 'function'  ]
    	   then
    	   	   ${LONG_OPTIONS[$i]}_usage
    	   fi
        fi
    done    
    echo "    --help OR -h"
    echo "    Print this help message."
    echo ""
}

143
function setvar_usage {
144 145 146
	declare -A HELP_VAR
	HELP_VAR["VM_OSREL"]="OS release to use in virtual machines"
	HELP_VAR["RUN_EXPERIMENTAL"]="Enforce execution of variants with EXPERIMENTAL variable set to \"true\"" 
147
    echo "--setvar_<varname> <value> where varname is one of:"
148
	for i in ${AUTHORIZED_VAR[@]}; do printf "%20s :     %s\n" "$i" "${HELP_VAR[$i]}" ;done
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
}

# functions variant__v<n>__<variant name> are used to define build or test variants
# The only thing to do to define a new variant is to add a function which name
# match this template. Note that "_" character in function <varriant name> part
# will be replaced by "-" character in  build_variant_arrays function, this is
# for compatibility reasons  

function variant__v1__enb_usrp {
    NB_PATTERN_FILES=9
    BUILD_OPTIONS="--eNB -w USRP --mu"
}
    
function variant__v2__basic_sim {
    NB_PATTERN_FILES=13
    BUILD_OPTIONS="--eNB --UE"
    VM_MEMORY=8192
    RUN_OPTIONS="complex"
167 168
}

169 170 171 172 173 174 175 176 177 178
function variant__v3__phy_sim {
    NB_PATTERN_FILES=3
    BUILD_OPTIONS="--phy_simulators"
    RUN_OPTIONS="./run_exec_autotests.bash -g \"01510*\" -q -np -b"
}

function variant__v4__cppcheck {
    VM_MEMORY=4096
    LOG_PATTERN=cppcheck.xml
    NB_PATTERN_FILES=1
179
    BUILD_OPTIONS="--enable=warning --force --xml --xml-version=2 --suppressions-list=ci-scripts/cppcheck_suppressions.list -I common/utils -I openair3/NAS/COMMON/UTIL -j4"
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
}

function variant__v7__enb_ethernet {
    VM_MEMORY=4096
    ARCHIVES_LOC=enb_eth
    NB_PATTERN_FILES=8
    BUILD_OPTIONS="--eNB"
}
function variant__v8__ue_ethernet {
    VM_MEMORY=4096
    ARCHIVES_LOC=ue_eth
    NB_PATTERN_FILES=12
    BUILD_OPTIONS="--UE"
}

function variant__v10__flexran_rtc {
    ARCHIVES_LOC=flexran
    NB_PATTERN_FILES=1
198
    BUILD_OPTIONS="cmake .. && make -j2"
199 200 201 202 203
    VARIANT_INFO="non-OSA"
}
function variant__v20__l1_sim {
    ARCHIVES_LOC=l1_sim
    RUN_OPTIONS="complex"
204
    EXPERIMENTAL="true"
205 206 207 208
}
    
function variant__v21__rf_sim {
    RUN_OPTIONS="complex"
209
    EXPERIMENTAL="true"
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
}

function variant__v22__l2_sim {
    RUN_OPTIONS="complex"
}

# Following function lists all variant__v<n>__<variant name> functions
# and set the VARIANTS_SHORT and VARIANTS_LONG arrays from
# the function names
function build_variant_arrays {
    VARIANTS=`declare -F`
    VARIANTS=${VARIANTS//"declare -f "}
    for i in $VARIANTS
    do
    	if [ `expr match "$i" 'variant__v'` -eq 10 ]
    	then
    	   IDX1=`expr match "$i" 'variant__v[0-9]\+__'`
    	   VARIANTS_SHORT+=(${i:9:$((IDX1-11))})
    	   LONGNAME=${i:$IDX1}
           VARIANTS_LONG+=(${LONGNAME//"_"/"-"})
    	fi
    done
}

# check that the passed argument is a valid variant, set
# default values for variant related variables and then execute
# the corresponding variant__v<n>__<variant name> function
function check_set_variant {
    for (( i=0 ; i<${#VARIANTS_SHORT[@]} ; i++ )) 
    do  
    	if [ "$1" = "${VARIANTS_SHORT[$i]}" ] || [ "$1" = "${VARIANTS_LONG[$i]}" ]
    	then
            VM_NAME=ci-${VARIANTS_LONG[$i]}
            LOG_PATTERN=.$ASN1VER.txt
            ARCHIVES_LOC=${VARIANTS_LONG[$i]//"-"/"_"}  
            VM_MEMORY=2048
            VM_CPU=4
247
            EXPERIMENTAL=""
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
            NBARGS=$[$NBARGS+$VARIANT_OPTID]   
            variant__${VARIANTS_SHORT[$i]}__${VARIANTS_LONG[$i]//"-"/"_"}
            return 0
        fi
    done   
    echo "$1" is not a valid variant
    variant_usage
    exit 1
    
}

function check_command_options {
	GIVEN_OPTIONS=$1
	declare -a MANDATORY_OPTIONS=("${!2}")
	if [ ${MANDATORY_OPTIONS[1]} -eq 0 ]
	then
		MANDATORY_OPTIONS[1]=${MANDATORY_OPTIONS[0]}
	fi
	OPTIONAL_OPTIONS=$3
	if [ $(($GIVEN_OPTIONS & ${MANDATORY_OPTIONS[0]} )) -ne ${MANDATORY_OPTIONS[0]} ] && [ $(($GIVEN_OPTIONS & ${MANDATORY_OPTIONS[1]} )) -ne ${MANDATORY_OPTIONS[1]} ]
    then
        echo "Syntax Error: missing option(s) for $4 command"
270
        ${4//-/_}_usage
271 272 273
        exit 1
    fi	
    
274
    if [ $(( $(($GIVEN_OPTIONS ^ ${MANDATORY_OPTIONS[0]})) | $OPTIONAL_OPTIONS )) -ne  $(( $OPTIONAL_OPTIONS )) ] && [ $(( $(($GIVEN_OPTIONS ^ ${MANDATORY_OPTIONS[1]})) | $OPTIONAL_OPTIONS )) -ne  $(( $OPTIONAL_OPTIONS )) ]
275 276
    then
        echo "Syntax Error: unknown option(s) for $4 command"
277
        ${4//-/_}_usage
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
        exit 1
    fi	 
}

function check_ipv4addr {
    local  ipaddr=$1
    local  stat=1

    if [[ $ipaddr =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]
    then
        OIFS=$IFS
        IFS='.'
        ipaddr=($ipaddr)
        IFS=$OIFS
        [[ ${ipaddr[0]} -le 255 && ${ipaddr[1]} -le 255 \
            && ${ipaddr[2]} -le 255 && ${ipaddr[3]} -le 255 ]]
        stat=$?
    fi
    if [ $stat -ne 0 ]
    then
    	echo $ipaddr " is not a valid ip V4 address"
    	exit 1
    fi
}

function check_setvar {
304
    for i in ${AUTHORIZED_VAR[@]}
305 306 307
    do
    	if [ "$1" = "$i" ]
    	then
308 309
    	   echo "Setting $1 to $2..."
    	   export "$1"="$2"
310 311 312 313 314 315 316 317
    	   return 0
    	fi
    done
    echo " $1 is not a variable you can set"
    setvar_usage
    exit 1
}	

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
MY_DIR=$(dirname $(readlink -f $0))
. $MY_DIR/createVM.sh
. $MY_DIR/buildOnVM.sh
. $MY_DIR/waitBuildOnVM.sh
. $MY_DIR/destroyAllRunningVM.sh
. $MY_DIR/runTestOnVM.sh
. $MY_DIR/reportBuildLocally.sh
. $MY_DIR/reportTestLocally.sh

if [ $# -lt 1 ]
then
    echo "Syntax Error: too few arguments"
    echo ""
    top_usage
    exit 1
fi

VM_TEMPLATE=ci-
336 337


338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
# look for default ASN1 release in cmake directives
# It will be the release used when building
ASN1VEROPT=( `grep "add_list2_option(RRC_ASN1_VERSION" cmake_targets/CMakeLists.txt` )
ASN1VER=${ASN1VEROPT[1]//\"}
unset ASN1VEROPT

# variable to identify and store the command (build, create ...)
# command index in the COMMAND and COMMANDS_NAME arrays 
HELP_CMD=0
CREATE_CMD=1
BUILD_CMD=2
WAIT_CMD=3
DESTROY_CMD=4
TEST_CMD=5
REPORT_BUILD_CMD=6
REPORT_TEST_CMD=7

COMMANDS_NAME=(help create build wait destroy test report-build report-test)

#COMMAND will be used to save the command entered as argument
declare -A COMMAND
for i in "${COMMANDS_NAME[@]}"
do 
	COMMAND[$i]=0
done

# build the variant arrays
declare -a VARIANTS_SHORT
declare -a VARIANTS_LONG
build_variant_arrays

#variables to process options, for each option we define a variable to store
# its value and a bit mask, used to check allowed options depending on 
# the command
372

373 374 375 376 377 378
# The two following arrays must be consistent: a given index must point to the long and short options
# of a given command
SHORT_OPTIONS=( "jn" "id" "ws" "k" "D" "gu" "pu" "mr" "var" "notused" "sb" "sc" "tb" "tc" "br" "co" "epc" "sim")
LONG_OPTIONS=( "job-name" "build-id" "workspace" "keep-vm-alive" "daemon" "git-url" "push" "merge-request" 
               "variant" "notused" "src-branch" "src-commit" "target-branch" "target-commit" "branch" "commit" 
               "external-epc" "sim-file")
379

380
JOB_NAME=XX
381
JOB_NAME_OPTINDEX=0
382
JOB_NAME_OPTID=1
383

384
BUILD_ID=XX
385
BUILD_ID_OPTINDEX=1
386
BUILD_ID_OPTID=2
387 388

JENKINS_WKSP_OPTINDEX=2
389
JENKINS_WKSP_OPTID=4
390 391

KEEP_VM_ALIVE_OPTINDEX=3
392
KEEP_VM_ALIVE_OPTID=8
393 394 395
KEEP_VM_ALIVE=0

DAEMON_OPTINDEX=4
396
DAEMON_OPTID=$((2**4))
397 398 399
DAEMON=0

GITURL_OPTINDEX=5
400
GITURL_OPTID=$((2**5))
401 402

PU_OPTINDEX=6
403
PU_OPTID=$((2**6))
404 405 406
PU_TRIG=0

MR_OPTINDEX=7
407
MR_OPTID=$((2**7))
408 409 410
MR_TRIG=0

VARIANT_OPTINDEX=8
411
VARIANT_OPTID=$((2**8))
412 413

SB_OPTINDEX=10
414
SB_OPTID=$((2**10))
415 416

SC_OPTINDEX=11
417
SC_OPTID=$((2**11))
418 419

TB_OPTINDEX=12
420
TB_OPTID=$((2**12))
421 422

TC_OPTINDEX=13
423
TC_OPTID=$((2**13))
424 425

BR_OPTINDEX=14
426
BR_OPTID=$((2**14))
427 428

CO_OPTINDEX=15
429
CO_OPTID=$((2**15))
430 431

EPC_IPADDR_OPTINDEX=16
432
EPC_IPADDR_OPTID=$((2**16))
433 434
EPC_IPADDR=""

435 436 437 438
SIM_OPTINDEX=17
SIM_OPTID=$((2**17))
SIMFILE=""

439 440
RUN_OPTIONS="none"

441

442
# list of variables that can be set via the --setvar option
443 444
AUTHORIZED_VAR=("VM_OSREL RUN_EXPERIMENTAL")

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

#variables to set which OS VM should use
#if [ -f "/etc/os-release" ]
#then
#    source /etc/os-release
#    VM_OS="$NAME"
#    VM_OSREL="$VERSION_CODENAME"
#    VM_ARCHI=`uname -m`	
#else
    VM_OS="ubuntu"
    VM_OSREL="xenial"
    VM_ARCHI="x86_64"		
#fi


# variant option, ie the test variant which will be run triggers the
# variables listed below, used to configure the VM and to define what this
# VM will do. The variant can be specified by the --variant option or by a 
# -V<xx> option.
VM_NAME=""
VM_MEMORY=0
VM_CPU=0
ARCHIVES_LOC=""
LOG_PATTERN=""
NB_PATTERN_FILES=0
BUILD_OPTIONS=""
RUN_OPTIONS=""



# NARGS is used to check that only valid options are used for the given command
# It is a bit mask, each bit set to 1 specifies the corresponding option 
# has been entered on the command line. MANDATORY_OPTMASK and ALLOWED_OPTMASK define
# respectively the mandatory and optional parameters of the entered command
479
NBARGS=0
480 481
MANDATORY_OPTMASK=(0 0)   # some command have two sets of mandatory options
ALLOWED_OPTMASK=0
482 483 484 485

while [[ $# -gt 0 ]]
do
key="$1"
486

487 488 489
if [ "$(type -t $funcname)" != 'function' ]; then
    funcname=do_it_normal
fi
490
case $key in
491 492 493 494 495 496 497
    --debug)
    set -v
    set -x
    shift
    ;;
    -h|--help|help)
    COMMAND["help"]=1
498 499 500
    # following word might specify the topic for the help request
    # but help can also be used without argument, so don't shift
    if [ "$2" != "" ] && [ "$(type -t ${2//-/_}_usage)" = 'function' ]
501
    then
502
           HELPCMD="${2//-/_}_usage full"
503
    fi
504 505 506
    shift
    ;;
    create)
507 508 509 510
    COMMAND[$key]=1
    # For create, mandatory options: jn, id. Optional: variant
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID ))
    ALLOWED_OPTMASK=$(($VARIANT_OPTID))
511 512 513 514 515
    # It is implied to keep the VM when creating it
    KEEP_VM_ALIVE=1
    shift
    ;;
    build)
516 517 518 519
    COMMAND[$key]=1
    # For build, mandatory options: jn, id, ws. Optional: variant, k, D
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID))
    ALLOWED_OPTMASK=$(($KEEP_VM_ALIVE_OPTID + $DAEMON_OPTID + $VARIANT_OPTID))
520 521 522
    shift
    ;;
    wait)
523 524 525 526
    # For test, mandatory options: jn, id, ws. Optional: variant, k     	
    COMMAND[$key]=1
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID)) 
    ALLOWED_OPTMASK=$(($KEEP_VM_ALIVE_OPTID + $VARIANT_OPTID))  
527 528
    shift
    ;;
529 530 531 532
    test) 	
    COMMAND[$key]=1
    # For test, mandatory options: jn, id, ws. Optional: variant, k, epc 
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID)) 
533
    ALLOWED_OPTMASK=$(($KEEP_VM_ALIVE_OPTID + $VARIANT_OPTID + $EPC_IPADDR_OPTID + $SIM_OPTID))
534 535 536
    shift
    ;;
    destroy)
537 538
    COMMAND[$key]=1
    # For destroy, mandatory options: jn, id. 
539
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID))   
540 541 542
    shift
    ;;
    report-build)
543 544 545 546
    COMMAND[$key]=1
    # For report-build, mandatory options: jn, id, ws, gu, pu, br,co.
    #                                  or: jn, id, ws, gu, mr, sb, sc, tb, tc.
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID + $GITURL_OPTID + $PU_OPTID + $BR_OPTID + $CO_OPTID))
547 548
    MANDATORY_OPTMASK[1]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID + $GITURL_OPTID + $MR_OPTID + $SB_OPTID + $SC_OPTID)) 
    MANDATORY_OPTMASK[1]=$(( ${MANDATORY_OPTMASK[1]} + $TB_OPTID + $TC_OPTID))     
549 550
    shift
    ;;
551 552 553 554 555
    report-test) 
    COMMAND[$key]=1
    # For report-test, mandatory options: jn, id, ws, gu, pu, br,co.
    #                                 or: jn, id, ws, gu, mr, sb, sc, tb, tc.
    MANDATORY_OPTMASK[0]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID))
556
    MANDATORY_OPTMASK[0]=$((${MANDATORY_OPTMASK[0]} + $GITURL_OPTID + $PU_OPTID + $BR_OPTID + $CO_OPTID))
557
    MANDATORY_OPTMASK[1]=$(($JOB_NAME_OPTID + $BUILD_ID_OPTID + $JENKINS_WKSP_OPTID))
558 559
    MANDATORY_OPTMASK[1]=$((${MANDATORY_OPTMASK[1]} + GITURL_OPTID + $MR_OPTID + $SB_OPTID + $SC_OPTID))  
    MANDATORY_OPTMASK[1]=$((${MANDATORY_OPTMASK[1]} + $TB_OPTID + $TC_OPTID))   
560 561 562 563
    shift
    ;;
    -jn|--job-name)
    JOB_NAME="$2"
564
    NBARGS=$[$NBARGS+$JOB_NAME_OPTID]
565 566 567 568 569
    shift
    shift
    ;;
    -id|--build-id)
    BUILD_ID="$2"
570
    NBARGS=$[$NBARGS+$BUILD_ID_OPTID]
571 572 573 574 575
    shift
    shift
    ;;
    -ws|--workspace)
    JENKINS_WKSP="$2"
576
    NBARGS=$[$NBARGS+$JENKINS_WKSP_OPTID]
577 578 579 580 581
    shift
    shift
    ;;
    -k|--keep-vm-alive)
    KEEP_VM_ALIVE=1
582
    NBARGS=$[$NBARGS+$KEEP_VM_ALIVE_OPTID]
583 584 585 586
    shift
    ;;
    -D|--daemon)
    DAEMON=1
587
    NBARGS=$[$NBARGS+$DAEMON_OPTID]
588 589 590 591
    shift
    ;;
    -gu|--git-url)
    GIT_URL="$2"
592
    NBARGS=$[$NBARGS+$GITURL_OPTID]
593 594 595 596 597 598 599 600
    shift
    shift
    ;;
    --trigger)
    TRIG="$2"
    case $TRIG in
        merge-request)
        MR_TRIG=1
601
        NBARGS=$[$NBARGS+$MR_OPTID]       
602 603 604
        ;;
        push)
        PU_TRIG=1
605
        NBARGS=$[$NBARGS+$PU_OPTID]        
606 607 608 609 610 611 612 613 614 615 616 617
        ;;
        *)
        echo ""
        echo "Syntax Error: Invalid Trigger option -> $TRIG"
        echo ""
        trigger_usage
        exit
        ;;
    esac
    shift
    shift
    ;;
618
    -mr|--merge-request)
619
    MR_TRIG=1
620
    NBARGS=$[$NBARGS+$MR_OPTID]
621 622
    shift
    ;;
623
    -pu|--push)
624
    PU_TRIG=1
625
    NBARGS=$[$NBARGS+$PU_OPTID]
626 627 628 629
    shift
    ;;
    -sb|--src-branch)
    SOURCE_BRANCH="$2"
630
    NBARGS=$[$NBARGS+$SB_OPTID]
631 632 633 634 635
    shift
    shift
    ;;
    -sc|--src-commit)
    SOURCE_COMMIT_ID="$2"
636
    NBARGS=$[$NBARGS+$SC_OPTID]
637 638 639 640 641
    shift
    shift
    ;;
    -tb|--target-branch)
    TARGET_BRANCH="$2"
642
    NBARGS=$[$NBARGS+$TB_OPTID]
643 644 645 646 647
    shift
    shift
    ;;
    -tc|--target-commit)
    TARGET_COMMIT_ID="$2"
648
    NBARGS=$[$NBARGS+$TC_OPTID]
649 650 651 652 653
    shift
    shift
    ;;
    -br|--branch)
    SOURCE_BRANCH="$2"
654
    NBARGS=$[$NBARGS+$BR_OPTID]
655 656 657 658 659
    shift
    shift
    ;;
    -co|--commit)
    SOURCE_COMMIT_ID="$2"
660
    NBARGS=$[$NBARGS+$CO_OPTID]
661 662 663
    shift
    shift
    ;;
664
    -epc|--external-epc)
665 666 667 668 669
    check_ipv4addr "$2"
    check_ipv4addr "$3"
    EPC_IPADDR="$2"
    EPC_TUN_IPADDR="$3"
    NBARGS=$[$NBARGS+$EPC_IPADDR_OPTID]
670 671 672 673
    shift
    shift
    shift
    ;;
674 675 676 677 678 679
    -sim|--sim-file)
    SIMFILE="$2"
    NBARGS=$[$NBARGS+$SIM_OPTID]
    shift
    shift
    ;;   
680 681
    --setvar_*)
    check_setvar "${key:9}" "$2"
682
    shift
683 684
    shift
    ;;
685 686
    -v[0-9] | -v[0-9][0-9])
    check_set_variant ${key//"-"} 
687 688
    shift
    ;;
689
    -var|--variant)
690 691
    variant="$2" 
    check_set_variant ${2}
692 693 694 695 696 697 698 699 700 701 702
    shift
    shift
    ;;
    *)
    echo "Syntax Error: unknown option: $key"
    echo ""
    top_usage
    exit 1
esac
done

703 704 705 706 707 708 709 710 711
i=0
for (( COUNT=1 ; COUNT<${#COMMAND[@]} ; COUNT++ )) 
do    
   if [ ${COMMAND[${COMMANDS_NAME[$COUNT]}]} -eq 1 ]
   then
   	   COMMAND_KEY[$i]=${COMMANDS_NAME[$COUNT]} 
      (( i++ ))	   
   fi
done
712

713
if [ ${COMMAND["help"]} -eq 1 ]
714
then
715 716
    if [ ${#COMMAND_KEY[@]} -ne 1 ] && [ "$HELPCMD" == "" ]
    then 
717
       top_usage
718 719 720
    elif  [ "$HELPCMD" != "" ]
    then
       $HELPCMD
721
    else
722
       ${COMMAND_KEY[0]//-/_}_usage
723 724
    fi 
    exit 0
725
else
726
    if [ ${#COMMAND_KEY[@]} -gt 1 ]
727
    then
728
        echo "Syntax Error, too many commands:  ${COMMAND_KEY[@]}" 
729 730 731
        top_usage
        exit 1
    fi
732 733

    if [ -z "${COMMAND_KEY[0]}" ]
734
    then
735 736
        echo "Syntax Error: no command, specify one of: ${COMMANDS_NAME[@]}"
        top_usage
737 738
        exit 1
    fi
739 740

    check_command_options $NBARGS MANDATORY_OPTMASK[@] $ALLOWED_OPTMASK ${COMMAND_KEY[0]}
741 742
fi

743
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_BUILD_CMD]}]} -ne 1 ]  && [ ${COMMAND[${COMMANDS_NAME[$REPORT_TEST_CMD]}]} -ne 1 ]
744
then
Raphael Defosseux's avatar
Raphael Defosseux committed
745 746 747 748 749 750 751 752
    # Checking uvt-kvm is installed
    UVT_KVM_PATH=`which uvt-kvm | grep -c uvt-kvm`
    if [ $UVT_KVM_PATH -eq 0 ]
    then
        echo "Error: uvt-kvm is not installed"
        top_usage
        exit 1
    fi
753 754 755 756 757 758 759 760
fi

if [ "$JOB_NAME" == "XX" ] || [ "$BUILD_ID" == "XX" ]
then
    VM_TEMPLATE=ci-
else
    VM_TEMPLATE=${JOB_NAME}-b${BUILD_ID}-
fi
761 762 763 764 765
# set default variant
if [ "$VM_NAME" = "" ]
then
	check_set_variant "v1"
fi
766 767 768 769 770
VM_NAME=`echo $VM_NAME | sed -e "s#ci-#$VM_TEMPLATE#"`
VM_CMDS=${VM_NAME}_cmds.txt
ARCHIVES_LOC=${JENKINS_WKSP}/archives/${ARCHIVES_LOC}

STATUS=0
771
if [ ${COMMAND[${COMMANDS_NAME[$CREATE_CMD]}]} -eq 1 ]
772
then
773
    if [[ "$BUILD_OPTIONS" = "" ]]
Raphael Defosseux's avatar
Raphael Defosseux committed
774
    then
775
        echo "$VM_NAME is not a build variant"
Raphael Defosseux's avatar
Raphael Defosseux committed
776 777 778 779
        exit 0
    else
        create_vm
    fi
780
fi
781
if [ ${COMMAND[${COMMANDS_NAME[$BUILD_CMD]}]} -eq 1 ]
782
then
783
    if [[  "$BUILD_OPTIONS" = "" ]]
Raphael Defosseux's avatar
Raphael Defosseux committed
784
    then
785
        echo " $VM_NAME is not a build variant"
Raphael Defosseux's avatar
Raphael Defosseux committed
786 787 788 789
        exit 0
    else
        build_on_vm
    fi
790 791 792 793 794
    if [ $DAEMON -eq 0 ] && [ $STATUS -eq 0 ]
    then
        check_on_vm_build
    fi
fi
795
if [ ${COMMAND[${COMMANDS_NAME[$WAIT_CMD]}]} -eq 1 ]
796
then
797
    if [[  "$BUILD_OPTIONS" = "" ]]
Raphael Defosseux's avatar
Raphael Defosseux committed
798
    then
799
        echo "$VM_NAME is not a build variant"
Raphael Defosseux's avatar
Raphael Defosseux committed
800 801 802 803
        exit 0
    else
        wait_on_vm_build
    fi
804 805 806 807 808
    if [ $STATUS -eq 0 ]
    then
        check_on_vm_build
    fi
fi
809
if [ ${COMMAND[${COMMANDS_NAME[$TEST_CMD]}]} -eq 1 ]
810
then
811 812 813
    # variant with EXPERIMENTAL variable set to true won't run and will be
    # considered OK, except if --setvar_RUN_EXPERIMENTAL option has been set to true
    if [ "$EXPERIMENTAL" = "true" ] && [ "$RUN_EXPERIMENTAL" != "true" ]
814
    then
815
        echo "Currently $VM_NAME Testing is not implemented / enabled"
816 817
        echo "remove EXPERIMENTAL variable definition from this variant"
        echo " or use option --setvar_RUN_EXPERIMENTAL=true to enforce testing it"
818 819 820
        echo "STATUS seems OK"
        exit $STATUS
    fi
821

822 823 824
    ARCHIVES_LOC=${ARCHIVES_LOC}/test
    run_test_on_vm
fi
825
if [ ${COMMAND[${COMMANDS_NAME[$DESTROY_CMD]}]} -eq 1 ]
826 827 828
then
    destroy_vm
fi
829
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_BUILD_CMD]}]} -eq 1 ]
830 831 832
then
    report_build
fi
833
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_TEST_CMD]}]} -eq 1 ]
834 835 836 837 838 839 840 841 842 843 844 845
then
    report_test
fi

# Did command pass or fail?
if [ $STATUS -eq 0 ]
then
    echo "STATUS seems OK"
else
    echo "STATUS failed?"
fi
exit $STATUS