summaryrefslogtreecommitdiff
path: root/leap-platform-test
blob: 8894c184d19e44c9d5dab64a0d9a86be39739893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
#!/bin/bash
#


usage()
{
    cat << EOF

usage: $0 [options] command [arguments...]

This script runs the leap platform deploy tests

OPTIONS

  -a|--all                 run command on all nodes
  -c|--config file         specify config file
  -h|--help                show help
  -l|--lock                write a lock file if tests fail, and refuse to deploy if it exists
  -V|--versions            show versions/git revision of leap_cli and leap_platoform in provider dir

COMMANDS
  bootstrap      <node(s)> bootstrap node(s):
                             - leap local start
                             - leap node int
                             - sets up hostname and runs apt-get dist-upgrade
                             - leap local save
  create_provider          creates a provider instance
  deploy         <node(s)> deploy node(s)
  init_deploy    <node(s)> initialize node, then do a deploy
  destroy_deploy <node(s)> destroy vms, init, and deploy
  reset_deploy   <node(s)> reset and deploy node(s)
  test           <node(s)> run leap test  
EOF
}

add_nodes() {
    suffix=$IP_SUFFIX_START
    for i in "$@"
    do
        node=${i%:*}
        services=${i#*:}
        let suffix++
        ip="${IP_PREFIX}.$suffix"
        case $services in
            openvpn)
                config="openvpn.gateway_address:${IP_PREFIX}.98 openvpn.second_gateway_address:${IP_PREFIX}.99"
                ;;
            *)
                config=
                ;;
        esac

        $LEAP_CMD node add --local "$node"  ip_address:"$ip"  $config services:"$services"
    done
}

destroy_vms() {
    for vm in "$@"
    do
        $LEAP_CMD local destroy $vm
    done
}

bootstrap_nodes() {

    check_for_running_instances || exit $?

    for vm in $@
    do
        $LEAP_CMD $OPTS local start "$vm"
        wait_for_node "$vm"
        $LEAP_CMD $OPTS node init "$vm"
        # set hostname + do dist-upgrade
        $LEAP_CMD $OPTS deploy "$vm" --tags site_apt::dist_upgrade,site_config::hosts,site_squid_deb_proxy::client

        # make sure machines are rebooted in order to be able to load kernel modules after a kernel update
        # https://leap.se/code/issues/6494
        cd ${PROVIDERDIR}/test
        vagrant reload $vm 

        $LEAP_CMD $OPTS local save "$vm"
    done
}

check_for_running_instances() {

    if [[ $( pidof -x "$( basename "$0" )" -o %PPID ) ]]
    then 
        echo "Other process(es) found running for $(basename "$0") - exiting."
        exit 1
    fi

}

create_provider() {
    
    check_for_running_instances || exit $?

    if [ -e "$PROVIDERDIR" ]
    then
        echo "$PROVIDERDIR" exists - exiting
        exit 1
    fi

    git clone -b "$PLATFORM_BRANCH" --recursive https://leap.se/git/leap_platform.git "$PLATFORMDIR"

    mkdir -p  "$PROVIDERDIR"
    cd  "$PROVIDERDIR"
    $LEAP_CMD $OPTS new --contacts "$CONTACTS" --domain "$DOMAIN" --name "$PROVIDER" --platform="$PLATFORMDIR" .


    # for now, we use the vagrant pubkey until https://leap.se/code/issues/2039 is solved
    $LEAP_CMD $OPTS add-user --self --ssh-pub-key="$SSHKEY"
    $LEAP_CMD $OPTS cert ca && $LEAP_CMD $OPTS cert csr

    # copy for faster testing
    #cp $ROOTDIR/dh.pem.test $PROVIDERDIR/files/ca/dh.pem
    $LEAP_CMD $OPTS cert dh
    add_nodes $NODES

    git init
    git add .
    git commit -m"finished create_provider"  
}

deploy() {

    # we need to deploy with verbose level 2, and filter out unwanted stuff
    # until puppet errors show up in verbose level 0 +1 (#1750)
    FILTER_CLI='= read|= loading|= no change| - executing| = executing| = applying| = ran git| = checking| = synching| = skipping file_path|   - cd .*; rsync -| - hiera| = created hiera/| = updated hiera/| = updated secrets.json| - cd /root/| - rolling backexecuting| - files/|\[bin,tests,puppet\] ->|] Hostname updated.| = Updating submodule puppet/modules|Warning: Permanently added.*to the list of known hosts.| = leap command v| = leap platform v| - \[.*\] ok| - \[.*\] STARTING APPLY| - \[.*\] APPLY COMPLETE'

    FILTER_PUPPET="] notice: |] No change to hostname|] Puppet apply complete \(changes made\).|] warning: Dynamic lookup|] warning: Scope\(Class|Skipping because of failed dependencies|warning: You cannot collect without storeconfigs being set|warning: default \`to_a' will be obsolete"

    if [ -n "$FILTER_COMMON" ]
    then 
        FILTER_ALL="($FILTER_CLI|$FILTER_PUPPET|$FILTER_COMMON)"
    else
        FILTER_ALL="($FILTER_CLI|$FILTER_PUPPET)"
    fi 


    for vm in "$@"
    do
        [ -e "$LOGDIR" ] || mkdir -p "$LOGDIR"
        date=$( date +"%F-%H%M%S" )
        LOG1="$LOGDIR/deploy-$vm.log"
        LOG2="$LOGDIR/deploy-$vm-$date.log"
        ERRLOG1="$LOGDIR/deploy-$vm-$date-error.log"
        ERRLOG2="$LOGDIR/deploy-error.log"
        echo "Deploying \"$vm\" on $( date )"|tee -a "$LOG1" "$LOG2"

        $LEAP_CMD $OPTS -v 2 deploy "$vm" 2>&1 | ts | tee -a "$LOG1" "$LOG2" | egrep -v "$FILTER_ALL" | tee -a "$ERRLOG1" "$ERRLOG2" > /dev/null
    done
} 

get_ip () {
    grep ip_address "$PROVIDERDIR/nodes/$1.json" |cut -f 2 -d:|sed 's/[ ",]//g'
}

ip_pingable () {
    ping -q -W10 -c1 "$1" >/dev/null 2>&1
    return $?
}

log_start() {
    echo
    echo "Starting $0 on $( date )"
}

deploy_failure_email() {
    versions=$( versions )
    if [ -s "$ERRLOG1" ]
    then
        touch "$deploy_failure"
        subj="WARNING - \"leap deploy\" of platform $PLATFORM_BRANCH branch on \"$vm\" had errors !"
        echo "$( date ): $msg" | tee -a "$LOG1" "$LOG2" "$ERRLOG2"
        echo
        msg="Output of error log below:\n\n$( cat "$ERRLOG1" ) \n\n"
        msg="${msg}-------------------------------------------------------------------\n\n"
        msg="${msg}error log: ${ERRLOG1}\n"
        msg="${msg}comlete log: ${LOG2}\n\n"

        msg="${msg}Tested on $( date ) on \"$vm\" with following versions/git commit IDs: \n\n$versions"

        cat "$ERRLOG1" 
        
        if [ "$MAIL_TO" != '' ]; then
            echo "Sending this mail to $MAIL_TO:"
            sendemail -f "$MAIL_FROM" -t "$MAIL_TO" -o tls=no -m "${msg}" -u "${subj}" 
        fi

    elif [ ! -e "$deploy_failure" -a -n "$MAIL_TO"  ]
    then
        echo "Deploy to $vm on $( date ) went fine."| tee -a "$LOG1" "$LOG2"
        rm "$ERRLOG1"
        subj="OK - \"leap deploy\" of platform $PLATFORM_BRANCH branch went fine."
        msg="Tested on $( date ) on these nodes: \"$nodes\"\nwith following versions/git commit IDs: \n\n$versions"

        echo "Sending deploy success mail to $MAIL_TO"
        sendemail -f "$MAIL_FROM" -t "$MAIL_TO" -o tls=no -m "${msg}" -u "${subj}"
    fi
}

test_failure_email() {
    test_failure=$?

    versions=$( versions )
    msg="Tested on $( date ) on these nodes: \"$nodes\"\nwith following versions/git commit IDs: \n\n$versions"
    if [ $test_failure -eq 0 ]
    then
        subj="OK - \"leap test\" of platform $PLATFORM_BRANCH branch is all green."
    else
        subj="WARNING - \"leap test\" of platform $PLATFORM_BRANCH branch failed !!"
    fi

    echo "$subj"

    echo "Sending test mail to $MAIL_TO"
    # unfortunatly, no tls atm, fixed in sendemail 1.56-3
    # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=679911
    sendemail -f "$MAIL_FROM" -t "$MAIL_TO" -o tls=no -m "${msg}" -u "${subj}" -a "$TEST_LOG2"
}

init_deploy() {
    # init_deploy tests that a re-init of a node, then a deploy works this
    # accepts that the limited tagged deploy that happens in bootstrap_nodes()
    # is ok. The following steps happen:
    #
    #. make sure the platform and leap cli are up-to-date
    #. reset the nodes to their saved state (saved is after bootstrap_nodes has
    #  been run)
    #. run bootstrap_nodes (this will: start vm, run init, run a limited tag
    #  deploy, then reset the node)
    #. deploy the nodes
    #. run tests
    #. send email

    check_for_running_instances || exit $?

    local nodes="$*"
    update_platform
    update_leap_cli

    cd "$PROVIDERDIR"

    log_start

    echo "Starting init_deploy for nodes $@ as background tasks on $( date )"

    for i in $nodes
    do
        $LEAP_CMD $OPTS local reset "$i"
        wait_for_node "$i"
        bootstrap_nodes "$i"
        deploy "$i" &
    done

    # needed in a detached screen session, otherwise it would terminate before deploy jobs
    # have finished
    echo "Waiting until last deploy process has finished..."
    wait

    # send mail if deployment fails
    deploy_failure_email

    # run tests
    run_tests

    # send mail if tests fail
    test_failure_email
}

reset_deploy() {
    # reset_deploy tests that a deploy works after the node has been reset to
    # the bootstrap_nodes() state it does not re-run bootstrap_nodes(), so a
    # 'leap node init' is not run again, this is usually sufficient. The
    # following steps happen:
    #
    #. make sure the platform and leap cli are up-to-date
    #. reset the nodes to their saved state (saved is after bootstrap_nodes has
    #  been run)
    #. deploy the nodes
    #. run tests
    #. send email

    check_for_running_instances || exit $?

    local nodes="$*"
    update_platform
    update_leap_cli

    cd "$PROVIDERDIR"

    log_start

    echo "Starting reset_deploy for nodes $@ as background tasks on $( date )"

    for i in $nodes
    do
        $LEAP_CMD $OPTS local reset "$i"
        wait_for_node "$i"
        deploy "$i" &
    done

    # needed in a detached screen session, otherwise it would terminate before deploy jobs
    # have finished
    echo "Waiting until last deploy process has finished..."
    wait

    # send mail if deployment fails
    deploy_failure_email

    # run tests
    run_tests

    # send mail if tests fail
    test_failure_email
}

destroy_deploy() {
    # destroy_deploy tests a full-cycle, it destroys the VMs, re-creates them
    # from scratch and then bootstraps them. The following steps happen:
    #. make sure the platform and leap cli are up-to-date
    #. destroy the vms
    #. run bootstrap_nodes (this will: start vm, run init, run a limited tag
    #  deploy, then reset the node)
    #. deploy the nodes
    #. run tests
    #. send email

    check_for_running_instances || exit $?

    local nodes="$*"
    update_platform
    update_leap_cli

    cd "$PROVIDERDIR"

    log_start

    echo "Starting destroy_deploy for nodes $@ as background tasks on $( date )"

    destroy_vms "$@"

    for i in $nodes
    do
        bootstrap_nodes "$i"
        deploy "$i" &
    done

    # needed in a detached screen session, otherwise it would terminate before deploy jobs
    # have finished
    echo "Waiting until last deploy process has finished..."
    wait

    # send mail if deployment fails
    deploy_failure_email

    # run tests
    run_tests

    # send mail if tests fail
    test_failure_email

}

ssh_up () {
    nc -w 4 "$1" 22 > /dev/null
    return $?
}

run_tests () {

    date=$( date +"%F-%H%M%S" )
    TEST_FILTER='net.ssh.authentication.agent.*could not connect to ssh-agent'


    echo -e "\nRunning leap test on $date" | tee -a "$TEST_LOG1" "$TEST_LOG2" 
    $LEAP_CMD $OPTS test --continue 2>&1 | ts | egrep -v "$TEST_FILTER" | tee -a "$TEST_LOG1" "$TEST_LOG2" 
    test_failure=${PIPESTATUS[0]}
    
    if [ "$test_failure" -ne 0 ]
    then
        echo 'WARNING - "leap test" failed !' | tee -a "$TEST_LOG1" "$TEST_LOG2"
    else
        echo 'OK - "leap test" is all green !' | tee -a "$TEST_LOG1" "$TEST_LOG2"
    fi
    return "$test_failure"

}

update_leap_cli () {
    cd "$LEAP_SRC"
    git pull
    sudo bundle
}


update_platform () {
    cd "$PLATFORMDIR"
    git pull
}

versions () {

    cd "$PROVIDERDIR"
    [ -d .git ] && provider_head=$( git rev-parse HEAD )
    if [ -n "$provider_head" ]
    then
        provider_head='not under version control'
    fi  

    echo "Provider ($PROVIDERDIR): $provider_head"
    echo

    $LEAP_CMD -v 2 list | grep ' = leap command v'
    echo
    echo
    
    #echo "leap_platform:"
    $LEAP_CMD -v 2 list | grep ' = leap platform v'
    echo
    echo
}

wait_for_node() {
    vm=$1
    ip=$( get_ip "$vm" )
    online=0
    echo "Waiting for ssh on VM $vm (IP: $ip) to come up..."
    while [ $online -eq 0 ]
    do
        ssh_up "$ip" && online=1
        sleep 1
    done
}


# http://docs.vagrantup.com/v2/providers/default.html
export VAGRANT_DEFAULT_PROVIDER="libvirt"



config=""
all=false
print_versions=false
# default in lib/leap_cli/leapfile.rb
IP_PREFIX='10.5.5'

deploy_failure="/tmp/deploy_failure"
[ -e $deploy_failure ] && rm $deploy_failure

if ! options=$(getopt -o avVc:h -l all,verbose,versions,config:,help -- "$@")
then 
    # something went wrong, getopt will put out an error message for us
    usage
    exit 1
fi  

eval set -- "$options"

while [ $# -gt 0 ]
do
    case $1 in
        -h|--help)     usage; exit 1;;
        -c|--config)   config=$2; shift ;;
        -a|--all)      all=true;;
        -V|--versions)  print_versions=true;; 
        (--) shift; break;;
        (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
        (*) break;;
    esac
    shift
done


cmd=$1
shift
nodelist="$*"

#echo "config:   $config"
#echo "cmd:      $cmd"
#echo "nodelist: $nodelist"
#echo "all:      $all"

if [ -z "$config" ]
then
    usage
    echo "Please provide a config file"
    exit 1
fi

# provider specific config
source "$config"
# common config for all providers
source /etc/leap/platform-test-common.cfg

date=$( date +"%F-%H%M%S" )

TEST_LOG1="$LOGDIR/test.log"
TEST_LOG2="$LOGDIR/test-$date.log"

[ -e "$PROVIDERDIR" ] && cd  "$PROVIDERDIR"

if $print_versions
then
    versions=$( versions )
    echo "$versions"
    echo
    exit 0
fi

if [ -z "$LEAP_CMD" -o -z "$LEAP_SRC" ] 
then
    echo "please provide a path to the leap_cli binary and the source in the config file, using the LEAP_CMD and LEAP_SRC var."
    exit 1
fi

if $all ; then
    # use NODES variable from the config file
    nodes=$NODES

    # strip services from nodes_services
    # i.e. nodes_services='redevcouchdb1:couchdb redevcouchdb2:couchdb'
    # -> nodes='redevcouchdb1 redevcouchdb2'
    nodes=$( echo "$NODES" | sed 's/:[[:alnum:],]*//g' )
else
    # use nodelist provided via cmdline parameters
    nodes=$nodelist
fi


case $cmd in 
    add_nodes)        add_nodes "$nodes";;
    bootstrap)        bootstrap_nodes "$nodes";;
    create_provider)  create_provider;;
    deploy)           deploy "$nodes";;
    destroy_deploy)   destroy_deploy "$nodes";;
    init_deploy)      init_deploy "$nodes";;
    reset_deploy)     reset_deploy "$nodes";;
    test)             run_tests "$nodes";;
    (*)               usage; echo "Please specify a command."; exit 1;;
esac