oai-ci-vm-tool 23.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 144 145 146 147 148
# function to set specific behavior depending on the TESTPLATFORM_OWNER variable
# which may be set by a jenkins server for exemple
function platform_set {
	if [ "$TESTPLATFORM_OWNER" != "" ]
	then
	  echo "Running on $TESTPLATFORM_OWNER platform"
149
	  if [ -x "/usr/local/bin/oai_${TESTPLATFORM_OWNER}_setenv.sh" ]
150
	  then
151
	    . /usr/local/bin/oai_${TESTPLATFORM_OWNER}_setenv.sh
152 153 154 155
	  fi
	fi
}

156
function setvar_usage {
157 158 159
	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\"" 
160 161
    HELP_VAR["OPTIONAL_APTCACHER"]="build and Run tests will fail if apt-cacher not installed and this variable not set to \"true\""
    HELP_VAR["TESTPLATFORM_OWNER"]="Allow pipeline customization via execution of an externel scripts residing on the jenkins server" 
162
    echo "--setvar_<varname> <value> where varname is one of:"
163
	for i in ${AUTHORIZED_VAR[@]}; do printf "%20s :     %s\n" "$i" "${HELP_VAR[$i]}" ;done
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
}

# 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"
182 183
}

184
function variant__v3__phy_sim {
185
    NB_PATTERN_FILES=12
186
    BUILD_OPTIONS="--phy_simulators"
187
    VM_MEMORY=8192
188
    RUN_OPTIONS="./run_exec_autotests.bash -g \"01510* 015111\" -q -np -b"
189 190 191 192 193 194
}

function variant__v4__cppcheck {
    VM_MEMORY=4096
    LOG_PATTERN=cppcheck.xml
    NB_PATTERN_FILES=1
yilmazt's avatar
yilmazt committed
195
    BUILD_OPTIONS="--enable=warning --force --xml --xml-version=2 -i openair1/PHY/CODING/nrLDPC_decoder/nrLDPC_decoder.c --suppressions-list=ci-scripts/cppcheck_suppressions.list -I common/utils -I openair3/NAS/COMMON/UTIL -j4"
196 197
}

198
function variant__v5__gnb_usrp {
yilmazt's avatar
yilmazt committed
199
    NB_PATTERN_FILES=9
200 201 202 203
    BUILD_OPTIONS="--gNB -w USRP"
}

function variant__v6__nr_ue_usrp {
yilmazt's avatar
yilmazt committed
204
    NB_PATTERN_FILES=9
205 206 207
    BUILD_OPTIONS="--nrUE -w USRP"
}

208 209 210 211 212 213
function variant__v7__enb_ethernet {
    VM_MEMORY=4096
    ARCHIVES_LOC=enb_eth
    NB_PATTERN_FILES=8
    BUILD_OPTIONS="--eNB"
}
214

215 216 217 218 219 220 221 222 223 224
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
225
    BUILD_OPTIONS="cmake . && make -j2"
226 227
    VARIANT_INFO="non-OSA"
}
228

229 230 231
function variant__v20__l1_sim {
    ARCHIVES_LOC=l1_sim
    RUN_OPTIONS="complex"
232
    EXPERIMENTAL="true"
233 234 235 236
}
    
function variant__v21__rf_sim {
    RUN_OPTIONS="complex"
237
    EXPERIMENTAL="true"
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
}

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
275
            EXPERIMENTAL=""
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
            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"
298
        ${4//-/_}_usage
299 300 301
        exit 1
    fi	
    
302
    if [ $(( $(($GIVEN_OPTIONS ^ ${MANDATORY_OPTIONS[0]})) | $OPTIONAL_OPTIONS )) -ne  $(( $OPTIONAL_OPTIONS )) ] && [ $(( $(($GIVEN_OPTIONS ^ ${MANDATORY_OPTIONS[1]})) | $OPTIONAL_OPTIONS )) -ne  $(( $OPTIONAL_OPTIONS )) ]
303 304
    then
        echo "Syntax Error: unknown option(s) for $4 command"
305
        ${4//-/_}_usage
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
        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 {
332
    for i in ${AUTHORIZED_VAR[@]}
333 334 335
    do
    	if [ "$1" = "$i" ]
    	then
336 337
    	   echo "Setting $1 to $2..."
    	   export "$1"="$2"
338 339 340 341 342 343 344 345
    	   return 0
    	fi
    done
    echo " $1 is not a variable you can set"
    setvar_usage
    exit 1
}	

346
platform_set
347 348 349 350 351 352 353 354 355
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

356

357 358 359 360 361 362 363 364 365
if [ $# -lt 1 ]
then
    echo "Syntax Error: too few arguments"
    echo ""
    top_usage
    exit 1
fi

VM_TEMPLATE=ci-
366 367


368
# look for ASN1 release in cmake directives
369
# It will be the release used when building
370 371 372
ASN1VEROPT=( `grep "set (RRC_ASN1_VERSION" cmake_targets/CMakeLists.txt` )
ASN1VER=${ASN1VEROPT[2]//\"}
ASN1VER=${ASN1VER//)}
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
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
403

404 405 406 407 408 409
# 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")
410

411
JOB_NAME=XX
412
JOB_NAME_OPTINDEX=0
413
JOB_NAME_OPTID=1
414

415
BUILD_ID=XX
416
BUILD_ID_OPTINDEX=1
417
BUILD_ID_OPTID=2
418 419

JENKINS_WKSP_OPTINDEX=2
420
JENKINS_WKSP_OPTID=4
421 422

KEEP_VM_ALIVE_OPTINDEX=3
423
KEEP_VM_ALIVE_OPTID=8
424 425 426
KEEP_VM_ALIVE=0

DAEMON_OPTINDEX=4
427
DAEMON_OPTID=$((2**4))
428 429 430
DAEMON=0

GITURL_OPTINDEX=5
431
GITURL_OPTID=$((2**5))
432 433

PU_OPTINDEX=6
434
PU_OPTID=$((2**6))
435 436 437
PU_TRIG=0

MR_OPTINDEX=7
438
MR_OPTID=$((2**7))
439 440 441
MR_TRIG=0

VARIANT_OPTINDEX=8
442
VARIANT_OPTID=$((2**8))
443 444

SB_OPTINDEX=10
445
SB_OPTID=$((2**10))
446 447

SC_OPTINDEX=11
448
SC_OPTID=$((2**11))
449 450

TB_OPTINDEX=12
451
TB_OPTID=$((2**12))
452 453

TC_OPTINDEX=13
454
TC_OPTID=$((2**13))
455 456

BR_OPTINDEX=14
457
BR_OPTID=$((2**14))
458 459

CO_OPTINDEX=15
460
CO_OPTID=$((2**15))
461 462

EPC_IPADDR_OPTINDEX=16
463
EPC_IPADDR_OPTID=$((2**16))
464 465
EPC_IPADDR=""

466 467 468 469
SIM_OPTINDEX=17
SIM_OPTID=$((2**17))
SIMFILE=""

470 471
RUN_OPTIONS="none"

472

473
# list of variables that can be set via the --setvar option
474
AUTHORIZED_VAR=("VM_OSREL RUN_EXPERIMENTAL OPTIONAL_APTCACHER TESTPLATFORM_OWNER")
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

#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
510
NBARGS=0
511 512
MANDATORY_OPTMASK=(0 0)   # some command have two sets of mandatory options
ALLOWED_OPTMASK=0
513 514 515 516

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

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

734 735 736 737 738 739 740 741 742
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
743

744
if [ ${COMMAND["help"]} -eq 1 ]
745
then
746 747
    if [ ${#COMMAND_KEY[@]} -ne 1 ] && [ "$HELPCMD" == "" ]
    then 
748
       top_usage
749 750 751
    elif  [ "$HELPCMD" != "" ]
    then
       $HELPCMD
752
    else
753
       ${COMMAND_KEY[0]//-/_}_usage
754 755
    fi 
    exit 0
756
else
757
    if [ ${#COMMAND_KEY[@]} -gt 1 ]
758
    then
759
        echo "Syntax Error, too many commands:  ${COMMAND_KEY[@]}" 
760 761 762
        top_usage
        exit 1
    fi
763 764

    if [ -z "${COMMAND_KEY[0]}" ]
765
    then
766 767
        echo "Syntax Error: no command, specify one of: ${COMMANDS_NAME[@]}"
        top_usage
768 769
        exit 1
    fi
770 771

    check_command_options $NBARGS MANDATORY_OPTMASK[@] $ALLOWED_OPTMASK ${COMMAND_KEY[0]}
772 773
fi

774
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_BUILD_CMD]}]} -ne 1 ]  && [ ${COMMAND[${COMMANDS_NAME[$REPORT_TEST_CMD]}]} -ne 1 ]
775
then
776 777 778 779 780 781 782 783
    # 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
784 785 786 787 788 789 790 791
fi

if [ "$JOB_NAME" == "XX" ] || [ "$BUILD_ID" == "XX" ]
then
    VM_TEMPLATE=ci-
else
    VM_TEMPLATE=${JOB_NAME}-b${BUILD_ID}-
fi
792 793 794 795 796
# set default variant
if [ "$VM_NAME" = "" ]
then
	check_set_variant "v1"
fi
797 798 799 800 801
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
802
if [ ${COMMAND[${COMMANDS_NAME[$CREATE_CMD]}]} -eq 1 ]
803
then
804
    if [[ "$BUILD_OPTIONS" = "" ]]
805
    then
806
        echo "$VM_NAME is not a build variant"
807 808 809 810
        exit 0
    else
        create_vm
    fi
811
fi
812
if [ ${COMMAND[${COMMANDS_NAME[$BUILD_CMD]}]} -eq 1 ]
813
then
814
    if [[  "$BUILD_OPTIONS" = "" ]]
815
    then
816
        echo " $VM_NAME is not a build variant"
817 818 819 820
        exit 0
    else
        build_on_vm
    fi
821 822 823 824 825
    if [ $DAEMON -eq 0 ] && [ $STATUS -eq 0 ]
    then
        check_on_vm_build
    fi
fi
826
if [ ${COMMAND[${COMMANDS_NAME[$WAIT_CMD]}]} -eq 1 ]
827
then
828
    if [[  "$BUILD_OPTIONS" = "" ]]
829
    then
830
        echo "$VM_NAME is not a build variant"
831 832 833 834
        exit 0
    else
        wait_on_vm_build
    fi
835 836 837 838 839
    if [ $STATUS -eq 0 ]
    then
        check_on_vm_build
    fi
fi
840
if [ ${COMMAND[${COMMANDS_NAME[$TEST_CMD]}]} -eq 1 ]
841
then
842 843 844
    # 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" ]
845
    then
846
        echo "Currently $VM_NAME Testing is not implemented / enabled"
847 848
        echo "remove EXPERIMENTAL variable definition from this variant"
        echo " or use option --setvar_RUN_EXPERIMENTAL=true to enforce testing it"
849 850 851
        echo "STATUS seems OK"
        exit $STATUS
    fi
852

853 854 855
    ARCHIVES_LOC=${ARCHIVES_LOC}/test
    run_test_on_vm
fi
856
if [ ${COMMAND[${COMMANDS_NAME[$DESTROY_CMD]}]} -eq 1 ]
857 858 859
then
    destroy_vm
fi
860
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_BUILD_CMD]}]} -eq 1 ]
861 862 863
then
    report_build
fi
864
if [ ${COMMAND[${COMMANDS_NAME[$REPORT_TEST_CMD]}]} -eq 1 ]
865 866 867 868 869 870 871 872 873 874 875 876
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