Merge "JIRA:BGS-46 Updating the release-notes.rst with aligned language and more...
[genesis.git] / fuel / 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 functions=${topdir}/functions
15 tmpdir=$HOME/fueltmp
16 deployiso=${tmpdir}/deploy.iso
17 cloud_deploy=$(cd ${topdir}/cloud_deploy; pwd)
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 "Could 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=${topdir}/dea.yaml
58 else
59   deafile=$(cd `dirname $2`; echo `pwd`/`basename $2`)
60 fi
61 cp ${deafile} ${cloud_deploy}/
62
63 if [ ! -f $deafile ]; then
64   error-exit "Could not find DEA file $deafile"
65 fi
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 `ls libvirt/vms`
77 do
78   virsh destroy $node >/dev/null 2>&1
79 done
80
81
82 # Install the Fuel master
83 # (Convert to functions at later stage)
84 echo "Patching iso file"
85 ${functions}/patch-iso.sh $isofile $deployiso $tmpdir || error_exit "Failed to patch ISO"
86 # Swap isofiles from now on
87 isofile=$deployiso
88 . ${functions}/install_iso.sh
89
90 python ${cloud_deploy}/cloud_deploy.py
91
92 echo "Waiting for five minutes for deploy to stabilize"
93 sleep 5m
94
95 echo "Verifying node status after deployment"
96 # Any node with non-ready status?
97 ssh root@10.20.0.2 fuel node 2>/dev/null | tail -n +3 | cut -d "|" -f 2 | \
98   sed 's/ //g' | grep -v ready | wc -l | grep -q "^0$"
99 if [ $? -ne 0 ]; then
100   echo "Deploy failed to verify"
101   ssh root@10.20.0.2 fuel node 2>/dev/null
102   error_exit "Exiting with error status"
103 else
104   ssh root@10.20.0.2 fuel node 2>/dev/null
105   echo "Deployment verified"
106 fi
107