Updates docs for SR1 with final revision
[genesis.git] / fuel / prototypes / auto-deploy / 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=$(dirname $(readlink -f $BASH_SOURCE))
14 exampledir=$(cd $topdir/../examples; pwd)
15 functions=${topdir}/functions
16
17 # Define common functions
18 . ${functions}/common.sh
19
20 exit_handler() {
21   # Remove safety catch
22   kill -9 `ps -p $killpid -o pid --no-headers` \
23         `ps --ppid $killpid -o pid --no-headers`\
24   > /dev/null 2>&1
25 }
26
27 usage()
28 {
29     cat <<EOF
30 Syntax: `basename $0` [-nf] <isofile> <deafile> <dhafile>
31 Arguments
32   -nf   Do not install Fuel master
33 EOF
34 }
35
36
37 # maximum allowed deploy time (default three hours)
38 MAXDEPLOYTIME=${MAXDEPLOYTIME-3h}
39
40 ####### MAIN ########
41
42 time0=`date +%s`
43
44 if [ "`whoami`" != "root" ]; then
45   error_exit "You need be root to run this script"
46 fi
47
48 # Set initial veriables
49 nofuel=1
50
51
52 # Check for arguments
53 if [ "$1" == "-nf" ]; then
54     nofuel=0
55     shift
56 fi
57
58 if [ $# -ne 3 ]; then
59     usage
60     exit 1
61 fi
62
63 # Setup tmpdir - if TMPDIR env variable is set, use that one
64 # else create in $HOME/fueltmp
65 if [ -n "${TMPDIR}" ]; then
66     if [ -d ${TMPDIR} ]; then
67         tmpdir=${TMPDIR}/fueltmp
68         echo "Using TMPDIR=${TMPDIR}, so tmpdir=${tmpdir}"
69     else
70         error_exit "No such directory for TMPDIR: ${TMPDIR}"
71     fi
72 else
73     tmpdir=${HOME}/fueltmp
74     echo "Default: tmpdir=$tmpdir"
75 fi
76
77 # Umask must be changed so files created are readable by qemu
78 umask 0022
79
80 if [ -d $tmpdir ]; then
81   rm -Rf $tmpdir || error_exit "Could not remove tmpdir $tmpdir"
82 fi
83 mkdir $tmpdir || error_exit "Could not create tmpdir $tmpdir"
84
85 isofile=$(cd `dirname $1`; echo `pwd`/`basename $1`)
86 deafile=$(cd `dirname $2`; echo `pwd`/`basename $2`)
87 dhafile=$(cd `dirname $3`; echo `pwd`/`basename $3`)
88
89 if [ ! -f $isofile ]; then
90   error_exit "Could not find ISO file $isofile"
91 elif [ ! -f $deafile ]; then
92   error_exit "Could not find DEA file $deafile"
93 elif [ ! -f $dhafile ]; then
94   error_exit "Could not find DHA file $dhafile"
95 fi
96
97 # Connect adapter
98 adapter=`grep "^adapter: " $dhafile | sed 's/.*: //'`
99 if [ -z "$adapter" ]; then
100     error_exit "No adapter in DHA file!"
101 elif [ ! -f $topdir/dha-adapters/${adapter}.sh ]; then
102     error_exit "Could not find adapter for $adapter"
103 else
104     . $topdir/dha-adapters/${adapter}.sh $dhafile
105 fi
106
107 # Connect DEA API
108 . ${topdir}/functions/dea-api.sh $deafile
109
110 # Enable safety catch
111 echo "Enabling auto-kill if deployment exceeds $MAXDEPLOYTIME"
112 (sleep $MAXDEPLOYTIME; echo "Auto-kill of deploy after a timeout of $MAXDEPLOYTIME"; kill $$) &
113 killpid=$!
114
115 # Enable exit handler
116 trap exit_handler exit
117
118 # Get Fuel node information
119 fuelIp=`dea getFuelIp` || error_exit "Could not get Fuel IP"
120 fuelNetmask=`dea getFuelNetmask` || error_exit "Could not get Fuel netmask"
121 fuelGateway=`dea getFuelGateway` || error_exit "Could not get Fuel Gateway"
122 fuelHostname=`dea getFuelHostname` || error_exit "Could not get Fuel hostname"
123 fuelDns=`dea getFuelDns` || error_exit "Could not get Fuel DNS"
124 fuelNodeId=`dha getFuelNodeId` || error_exit "Could not get fuel node id"
125 dha useFuelCustomInstall
126 fuelCustom=$?
127
128 # Stop all VMs
129 for id in `dha getAllNodeIds`
130 do
131   if [ $nofuel -eq 0 -o $fuelCustom -eq 0 ]; then
132       if  [ $fuelNodeId -ne $id ]; then
133           echo "Powering off id $id"
134           dha nodePowerOff $id
135       fi
136   else
137       echo "Powering off id $id"
138       dha nodePowerOff $id
139   fi
140 done
141
142 # Install the Fuel master
143 if [ $nofuel -eq 1 ]; then
144     echo "Patching iso file"
145
146     deployiso="${tmpdir}/deploy-`basename $isofile`"
147     ${functions}/patch-iso.sh $isofile $deployiso $tmpdir \
148         $fuelIp $fuelNetmask $fuelGateway $fuelHostname $fuelDns \
149         || error_exit "Failed to patch ISO"
150
151     # Swap isofiles from now on
152     isofile=$deployiso
153     if dha useFuelCustomInstall; then
154         echo "Custom Fuel install"
155         dha fuelCustomInstall $isofile || error_exit "Failed to run Fuel custom install"
156     else
157         echo "Ordinary Fuel install"
158         . ${functions}/install_iso.sh || error_exit "Failed to install Fuel"
159     fi
160 else
161     echo "Not installing Fuel master"
162 fi
163
164 . ${functions}/deploy_env.sh
165
166 echo "Waiting for one minute for deploy to stabilize"
167 sleep 1m
168
169 echo "Verifying node status after deployment"
170 # Any node with non-ready status?
171 ssh root@${fuelIp} fuel node 2>/dev/null | tail -n +3 | cut -d "|" -f 2 | \
172   sed 's/ //g' | grep -v ready | wc -l | grep -q "^0$"
173 if [ $? -ne 0 ]; then
174   echo -e "Deploy failed to verify\n"
175   ssh root@${fuelIp} fuel node 2>/dev/null
176   error_exit "Exiting with error status"
177 else
178   echo -e "Deployment verified\n"
179   ssh root@${fuelIp} fuel node 2>/dev/null
180   echo -e "\nNow running sanity and smoke health checks"
181   echo -e "\n\n"
182   ssh root@${fuelIp} fuel health --env ${envId} --check sanity,smoke \
183       --force
184   if [ $? -eq 0 ]; then
185       echo "Health checks passed!"
186   else
187       error_exit "One or several health checks failed!"
188   fi
189
190   time1=`date +%s`
191   echo "Total deployment time: $[(time1-time0)/60] minutes"
192   exit 0
193 fi