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