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