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