ba7f7cd69069d6d3da91e76cf4ebd88585806393
[genesis.git] / fuel / prototypes / libvirt / deploy / deploy.sh
1 #!/bin/bash
2 ##############################################################################
3 # Copyright (c) 2015 Ericsson AB and others.
4 # stefan.k.berg@ericsson.com
5 # jonas.bjurel@ericsson.com
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Setup locations
13 topdir=$(cd `dirname $0`; pwd)
14 exampledir=$(cd $topdir/../examples; pwd)
15 functions=${topdir}/functions
16 tmpdir=$HOME/fueltmp
17 deployiso=${tmpdir}/deploy.iso
18
19 # Define common functions
20 . ${functions}/common.sh
21
22 exit_handler() {
23   # Remove safety catch
24   kill -9 `ps -p $killpid -o pid --no-headers` \
25         `ps --ppid $killpid -o pid --no-headers`\
26   > /dev/null 2>&1
27 }
28
29 # Set maximum allowed deploy time (default three hours)
30 MAXDEPLOYTIME=${MAXDEPLOYTIME-3h}
31
32 ####### MAIN ########
33
34 if [ "`whoami`" != "root" ]; then
35   error_exit "You need be root to run this script"
36 fi
37
38 if [ $# -eq 0 -o $# -gt 2 ]; then
39   error_exit "Argument error"
40 fi
41
42 # Setup tmpdir
43 if [ -d $tmpdir ]; then
44   rm -Rf $tmpdir || error_exit "Coul not remove tmpdir $tmpdir"
45 fi
46
47 mkdir $tmpdir || error_exit "Could not create tmpdir $tmpdir"  
48
49 if [ ! -f $1 ]; then
50   error_exit "Could not find ISO file $1"
51 else
52   isofile=$(cd `dirname $1`; echo `pwd`/`basename $1`)
53 fi
54
55 # If no DEA specified, use the example one
56 if [ $# -eq 1 ]; then
57   deafile=${exampledir}/libvirt_dea.yaml
58 else
59   deafile=$(cd `dirname $2`; echo `pwd`/`basename $2`)
60 fi
61
62 if [ ! -f $deafile ]; then
63   error-exit "Could not find DEA file $deafile"
64 fi
65
66
67 # Enable safety catch
68 echo "Enabling auto-kill if deployment exceeds $MAXDEPLOYTIME"
69 (sleep $MAXDEPLOYTIME; echo "Auto-kill of deploy after a timeout of $MAXDEPLOYTIME"; kill $$) &
70 killpid=$!
71
72 # Enable exit handler
73 trap exit_handler exit
74
75 # Stop all VMs
76 for node in controller1 controller2 controller3 compute4 compute5 fuel-master
77 do
78   virsh destroy $node >/dev/null 2>&1
79 done
80
81 # Install the Fuel master
82 # (Convert to functions at later stage)
83 echo "Patching iso file"
84 ${functions}/patch-iso.sh $isofile $deployiso $tmpdir || error_exit "Failed to patch ISO"
85 # Swap isofiles from now on
86 isofile=$deployiso
87 . ${functions}/install_iso.sh
88 . ${functions}/deploy_env.sh
89
90 echo "Waiting for five minutes for deploy to stabilize"
91 sleep 5m
92
93 echo "Verifying node status after deployment"
94 # Any node with non-ready status?
95 ssh root@10.20.0.2 fuel node 2>/dev/null | tail -n +3 | cut -d "|" -f 2 | \
96   sed 's/ //g' | grep -v ready | wc -l | grep -q "^0$"
97 if [ $? -ne 0 ]; then
98   echo "Deploy failed to verify"
99   ssh root@10.20.0.2 fuel node 2>/dev/null
100   error_exit "Exiting with error status"
101 else
102   ssh root@10.20.0.2 fuel node 2>/dev/null
103   echo "Deployment verified"
104 fi
105