| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# read_serial attempts to read the serial output until the workload is finished |
| 4 |
# Use var=$(read_serial <VM_NAME> <ZONE>) to capture the output of this command into a variable. |
| 5 |
read_serial() { |
| 6 |
local base_cmd='gcloud compute instances get-serial-port-output $1 --zone $2 2>/workspace/next_start.txt' |
| 7 |
local serial_out=$(eval ${base_cmd}) |
| 8 |
local last='' |
| 9 |
|
| 10 |
# timeout after 10 min |
| 11 |
timeout="10 minute" |
| 12 |
endtime=$(date -ud "$timeout" +%s) |
| 13 |
|
| 14 |
echo "Reading serial console..." |
| 15 |
while [ -s /workspace/next_start.txt ]; do |
| 16 |
if [[ $(date -u +%s) -ge $endtime ]]; then |
| 17 |
echo "timed out reading serial console, or the workload is running more than ${timeout}" |
| 18 |
break |
| 19 |
fi |
| 20 |
|
| 21 |
# VM may already exit |
| 22 |
if grep -qi 'Could not fetch serial port output' /workspace/next_start.txt; then |
| 23 |
serial_out="$serial_out $1 VM stopped" |
| 24 |
break |
| 25 |
fi |
| 26 |
|
| 27 |
next=$(cat /workspace/next_start.txt | sed -n 2p | cut -d ' ' -f2) |
| 28 |
local next_cmd="${base_cmd} ${next}" |
| 29 |
|
| 30 |
# sleeping 5s for the next serial console read |
| 31 |
sleep 5 |
| 32 |
|
| 33 |
local tmp=$(eval ${next_cmd}) |
| 34 |
serial_out="$serial_out $tmp" |
| 35 |
|
| 36 |
# break the loop if the workload is finished |
| 37 |
if echo ${serial_out} | grep -qi "TEE container launcher exiting"; then |
| 38 |
break |
| 39 |
fi |
| 40 |
|
| 41 |
last=$next |
| 42 |
done |
| 43 |
|
| 44 |
echo $serial_out |
| 45 |
} |