Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 3 | set -xe |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 4 | |
Leonardo Sandoval | eb94e91 | 2021-01-29 12:23:59 -0600 | [diff] [blame] | 5 | # Run the given command passed through parameters, if fails, try |
| 6 | # at most more N-times with a pause of M-seconds until success. |
| 7 | resilient_cmd() { |
| 8 | local cmd="$*" |
| 9 | local max_wait=10 |
| 10 | local sleep_body=2 |
| 11 | local iter=0 |
| 12 | |
| 13 | echo "Waiting for $cmd to complete" |
| 14 | while true; do |
| 15 | if ${cmd}; then |
| 16 | echo "$cmd job finished" |
| 17 | break |
| 18 | fi |
| 19 | |
| 20 | sleep ${sleep_body} |
| 21 | |
| 22 | iter=$(( iter + 1 )) |
| 23 | if [ ${iter} -ge ${max_wait} ]; then |
| 24 | return 1 |
| 25 | fi |
| 26 | done |
| 27 | return 0 |
| 28 | } |
| 29 | |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 30 | ls -l ${WORKSPACE} |
| 31 | |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 32 | if [ -n "${QA_SERVER_VERSION}" ]; then |
| 33 | if [ -n "${GERRIT_CHANGE_NUMBER}" ] && [ -n "${GERRIT_PATCHSET_NUMBER}" ]; then |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 34 | curl \ |
| 35 | --retry 4 \ |
| 36 | -X POST \ |
| 37 | --header "Auth-Token: ${QA_REPORTS_TOKEN}" \ |
| 38 | -d patch_source=${GERRIT_HOST} \ |
| 39 | -d patch_id=${GERRIT_CHANGE_NUMBER},${GERRIT_PATCHSET_NUMBER} \ |
| 40 | ${QA_SERVER}/api/createbuild/${QA_SERVER_TEAM}/${QA_SERVER_PROJECT}/${QA_SERVER_VERSION} |
| 41 | fi |
| 42 | |
| 43 | TESTJOB_ID=$(curl \ |
| 44 | --retry 4 \ |
| 45 | -X POST \ |
| 46 | --header "Auth-Token: ${QA_REPORTS_TOKEN}" \ |
| 47 | --form backend=${LAVA_SERVER} \ |
| 48 | --form definition=@artefacts/debug/job.yaml \ |
| 49 | ${QA_SERVER}/api/submitjob/${QA_SERVER_TEAM}/${QA_SERVER_PROJECT}/${QA_SERVER_VERSION}/${DEVICE_TYPE}) |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 50 | |
| 51 | if [ -n "${TESTJOB_ID}" ]; then |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 52 | echo "TEST JOB URL: ${QA_SERVER}/testjob/${TESTJOB_ID} TEST JOB ID: ${TESTJOB_ID}" |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 53 | |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 54 | |
Leonardo Sandoval | 73d301a | 2021-02-12 13:42:55 -0600 | [diff] [blame] | 55 | # The below loop with a sleep is intentional: LAVA could be under heavy load so previous job creation can |
| 56 | # take 'some' time to get the right numeric LAVA JOB ID |
| 57 | renumber='^[0-9]+$' |
| 58 | LAVAJOB_ID="null" |
| 59 | iter=0 |
| 60 | max_tries=10 |
| 61 | while ! [[ $LAVAJOB_ID =~ $renumber ]]; do |
| 62 | if [ $iter -eq $max_tries ] ; then |
| 63 | LAVAJOB_ID='' |
| 64 | break |
| 65 | fi |
| 66 | sleep 2 |
| 67 | LAVAJOB_ID=$(curl --retry 4 ${QA_SERVER}/api/testjobs/${TESTJOB_ID}/?fields=job_id) |
| 68 | |
| 69 | # Get the job_id value (whatever it is) |
| 70 | LAVAJOB_ID=$(echo ${LAVAJOB_ID} | jq '.job_id') |
| 71 | LAVAJOB_ID="${LAVAJOB_ID//\"/}" |
| 72 | |
| 73 | iter=$(( iter + 1 )) |
| 74 | done |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 75 | |
| 76 | # check that rest query at least get non-empty value |
| 77 | if [ -n "${LAVAJOB_ID}" ]; then |
| 78 | |
Leonardo Sandoval | 73d301a | 2021-02-12 13:42:55 -0600 | [diff] [blame] | 79 | echo "LAVA URL: https://${LAVA_SERVER}/scheduler/job/${LAVAJOB_ID} LAVA JOB ID: ${LAVAJOB_ID}" |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 80 | |
Leonardo Sandoval | 73d301a | 2021-02-12 13:42:55 -0600 | [diff] [blame] | 81 | resilient_cmd lavacli identities add --username ${LAVA_USER} --token ${LAVA_TOKEN} --uri "https://${LAVA_SERVER}/RPC2" default |
Leonardo Sandoval | a8078d6 | 2021-02-11 16:29:25 -0600 | [diff] [blame] | 82 | |
Leonardo Sandoval | 6ed6209 | 2021-03-09 09:32:05 -0600 | [diff] [blame] | 83 | # timeout at 3600 secs (1 hour) |
| 84 | timeout_seconds=3600 |
| 85 | wait_cmd="timeout $timeout_seconds lavacli jobs wait ${LAVAJOB_ID}" |
| 86 | |
| 87 | # if timeout on waiting for LAVA to complete, create an 'artificial' lava.log indicating |
| 88 | # job ID and timeout seconds |
| 89 | if ! $wait_cmd ; then |
| 90 | echo "Stopped monitoring LAVA JOB ${LAVAJOB_ID} after ${timeout_seconds} seconds, likely stuck or timeout too short?" > "${WORKSPACE}/lava.log" |
Leonardo Sandoval | 73d301a | 2021-02-12 13:42:55 -0600 | [diff] [blame] | 91 | echo "LAVA JOB RESULT: 1" |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 92 | else |
Leonardo Sandoval | 6ed6209 | 2021-03-09 09:32:05 -0600 | [diff] [blame] | 93 | resilient_cmd lavacli jobs logs ${LAVAJOB_ID} > "${WORKSPACE}/lava.log" |
| 94 | |
| 95 | # Fetch and store LAVA job result (1 failure, 0 success) |
Leonardo Sandoval | 5bb3ad6 | 2021-03-09 15:59:19 -0600 | [diff] [blame^] | 96 | resilient_cmd lavacli jobs show ${LAVAJOB_ID} | tee "${WORKSPACE}/lava.show" |
| 97 | if grep 'state.*: Finished' "${WORKSPACE}/lava.show"; then |
| 98 | if grep 'Health.*: Complete' "${WORKSPACE}/lava.show"; then |
| 99 | echo "LAVA JOB RESULT: 0" |
| 100 | else |
| 101 | echo "LAVA JOB RESULT: 1" |
| 102 | fi |
Leonardo Sandoval | 6ed6209 | 2021-03-09 09:32:05 -0600 | [diff] [blame] | 103 | else |
Leonardo Sandoval | 5bb3ad6 | 2021-03-09 15:59:19 -0600 | [diff] [blame^] | 104 | echo "LAVA JOB RESULT: 1" |
Leonardo Sandoval | 6ed6209 | 2021-03-09 09:32:05 -0600 | [diff] [blame] | 105 | fi |
Leonardo Sandoval | 9f15980 | 2021-01-20 16:47:25 -0600 | [diff] [blame] | 106 | fi |
| 107 | else |
| 108 | echo "LAVA Job ID could not be obtained" |
| 109 | fi |
Milosz Wasilewski | 1043858 | 2020-12-03 11:36:21 +0000 | [diff] [blame] | 110 | fi |
| 111 | fi |