Apex: fix iso verify
[releng.git] / jjb / apex / apex-iso-verify.sh
1 #!/bin/bash
2 set -o errexit
3 set -o nounset
4 set -o pipefail
5
6 # log info to console
7 echo "Starting the Apex iso verify."
8 echo "--------------------------------------------------------"
9 echo
10
11 source $WORKSPACE/../$BUILD_DIRECTORY/../opnfv.properties
12
13 if ! rpm -q virt-install > /dev/null; then
14   sudo yum -y install virt-install
15 fi
16
17 # define a clean function
18 rm_apex_iso_verify () {
19 if sudo virsh list --all | grep apex-iso-verify | grep running; then
20     sudo virsh destroy apex-iso-verify
21 fi
22 if sudo virsh list --all | grep apex-iso-verify; then
23     sudo virsh undefine apex-iso-verify
24 fi
25 }
26
27 # Make sure a pre-existing iso-verify isn't there
28 rm_apex_iso_verify
29
30 # run an install from the iso
31 # This streams a serial console to tcp port 3737 on localhost
32 sudo virt-install -n apex-iso-verify -r 4096 --vcpus 4 --os-variant=rhel7 \
33  --accelerate -v --noautoconsole --nographics \
34  --disk path=/var/lib/libvirt/images/apex-iso-verify.qcow2,size=30,format=qcow2 \
35  -l $BUILD_DIRECTORY/release/OPNFV-CentOS-7-x86_64-$OPNFV_ARTIFACT_VERSION.iso \
36  --extra-args 'console=ttyS0 console=ttyS0,115200n8 serial inst.ks=file:/iso-verify.ks inst.stage2=hd:LABEL=OPNFV\x20CentOS\x207\x20x86_64:/' \
37  --initrd-inject $BUILD_DIRECTORY/../ci/iso-verify.ks \
38  --serial tcp,host=:3737,protocol=raw
39
40 # Attach to tcpport 3737 and echo the output to stdout
41 # watch for a 5 min time out, a power off message or a tcp disconnect
42 python << EOP
43 #!/usr/bin/env python
44
45 import sys
46 import socket
47 from time import sleep
48 from time import time
49
50
51 TCP_IP = '127.0.0.1'
52 TCP_PORT = 3737
53 BUFFER_SIZE = 1024
54
55 try:
56     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
57     s.connect((TCP_IP, TCP_PORT))
58 except Exception, e:
59     print "Failed to connect to the iso-verofy vm's serial console"
60     print "this probably means that the VM failed to start"
61     raise e
62
63 activity = time()
64 data = s.recv(BUFFER_SIZE)
65 last_data = data
66 while time() - activity < 300:
67     try:
68         if data != last_data:
69             activity = time()
70         last_data = data
71         data = s.recv(BUFFER_SIZE)
72         sys.stdout.write(data)
73         if 'Powering off' in data:
74             break
75         sleep(.5)
76     except socket.error, e:
77         # for now assuming that the connection was closed
78         # which is good, means the vm finished installing
79         # printing the error output just in case we need to debug
80         print "VM console connection lost: %s" % msg
81         break
82 s.close()
83
84 if time() - activity > 300:
85     print "failing due to console inactivity"
86     exit(1)
87 else:
88     print "Success!"
89 EOP
90
91 # save the python return code for after cleanup
92 python_rc=$?
93
94 # clean up
95 rm_apex_iso_verify
96
97 # Exit with the RC of the Python job
98 exit $python_rc
99
100 echo
101 echo "--------------------------------------------------------"
102 echo "Done!"