summaryrefslogtreecommitdiffstats
path: root/fuel/prototypes/auto-deploy/deploy/deploy.sh
blob: f5aa634f96c372251302d35551f6c132605be6e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
##############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# stefan.k.berg@ericsson.com
# jonas.bjurel@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

# Setup locations
topdir=$(dirname $(readlink -f $BASH_SOURCE))
exampledir=$(cd $topdir/../examples; pwd)
functions=${topdir}/functions

# Define common functions
. ${functions}/common.sh

exit_handler() {
  # Remove safety catch
  kill -9 `ps -p $killpid -o pid --no-headers` \
        `ps --ppid $killpid -o pid --no-headers`\
  > /dev/null 2>&1
}

usage()
{
    cat <<EOF
Syntax: `basename $0` [-nf] <isofile> <deafile> <dhafile>
Arguments
  -nf   Do not install Fuel master
EOF
}


# maximum allowed deploy time (default three hours)
MAXDEPLOYTIME=${MAXDEPLOYTIME-3h}

####### MAIN ########

time0=`date +%s`

if [ "`whoami`" != "root" ]; then
  error_exit "You need be root to run this script"
fi

# Set initial veriables
nofuel=1


# Check for arguments
if [ "$1" == "-nf" ]; then
    nofuel=0
    shift
fi

if [ $# -ne 3 ]; then
    usage
    exit 1
fi

# Setup tmpdir - if TMPDIR env variable is set, use that one
# else create in $HOME/fueltmp
if [ -n "${TMPDIR}" ]; then
    if [ -d ${TMPDIR} ]; then
        tmpdir=${TMPDIR}/fueltmp
        echo "Using TMPDIR=${TMPDIR}, so tmpdir=${tmpdir}"
    else
        error_exit "No such directory for TMPDIR: ${TMPDIR}"
    fi
else
    tmpdir=${HOME}/fueltmp
    echo "Default: tmpdir=$tmpdir"
fi

# Umask must be changed so files created are readable by qemu
umask 0022

if [ -d $tmpdir ]; then
  rm -Rf $tmpdir || error_exit "Could not remove tmpdir $tmpdir"
fi
mkdir $tmpdir || error_exit "Could not create tmpdir $tmpdir"

isofile=$(cd `dirname $1`; echo `pwd`/`basename $1`)
deafile=$(cd `dirname $2`; echo `pwd`/`basename $2`)
dhafile=$(cd `dirname $3`; echo `pwd`/`basename $3`)

if [ ! -f $isofile ]; then
  error_exit "Could not find ISO file $isofile"
elif [ ! -f $deafile ]; then
  error_exit "Could not find DEA file $deafile"
elif [ ! -f $dhafile ]; then
  error_exit "Could not find DHA file $dhafile"
fi

# Connect adapter
adapter=`grep "^adapter: " $dhafile | sed 's/.*: //'`
if [ -z "$adapter" ]; then
    error_exit "No adapter in DHA file!"
elif [ ! -f $topdir/dha-adapters/${adapter}.sh ]; then
    error_exit "Could not find adapter for $adapter"
else
    . $topdir/dha-adapters/${adapter}.sh $dhafile
fi

# Connect DEA API
. ${topdir}/functions/dea-api.sh $deafile

# Enable safety catch
echo "Enabling auto-kill if deployment exceeds $MAXDEPLOYTIME"
(sleep $MAXDEPLOYTIME; echo "Auto-kill of deploy after a timeout of $MAXDEPLOYTIME"; kill $$) &
killpid=$!

# Enable exit handler
trap exit_handler exit

# Get Fuel node information
fuelIp=`dea getFuelIp` || error_exit "Could not get Fuel IP"
fuelNetmask=`dea getFuelNetmask` || error_exit "Could not get Fuel netmask"
fuelGateway=`dea getFuelGateway` || error_exit "Could not get Fuel Gateway"
fuelHostname=`dea getFuelHostname` || error_exit "Could not get Fuel hostname"
fuelDns=`dea getFuelDns` || error_exit "Could not get Fuel DNS"
fuelNodeId=`dha getFuelNodeId` || error_exit "Could not get fuel node id"
dha useFuelCustomInstall
fuelCustom=$?

# Stop all VMs
for id in `dha getAllNodeIds`
do
  if [ $nofuel -eq 0 -o $fuelCustom -eq 0 ]; then
      if  [ $fuelNodeId -ne $id ]; then
          echo "Powering off id $id"
          dha nodePowerOff $id
      fi
  else
      echo "Powering off id $id"
      dha nodePowerOff $id
  fi
done

# Install the Fuel master
if [ $nofuel -eq 1 ]; then
    echo "Patching iso file"

    deployiso="${tmpdir}/deploy-`basename $isofile`"
    ${functions}/patch-iso.sh $isofile $deployiso $tmpdir \
        $fuelIp $fuelNetmask $fuelGateway $fuelHostname $fuelDns \
        || error_exit "Failed to patch ISO"

    # Swap isofiles from now on
    isofile=$deployiso
    if dha useFuelCustomInstall; then
        echo "Custom Fuel install"
        dha fuelCustomInstall $isofile || error_exit "Failed to run Fuel custom install"
    else
        echo "Ordinary Fuel install"
        . ${functions}/install_iso.sh || error_exit "Failed to install Fuel"
    fi
else
    echo "Not installing Fuel master"
fi

. ${functions}/deploy_env.sh

echo "Waiting for one minute for deploy to stabilize"
sleep 1m

echo "Verifying node status after deployment"
# Any node with non-ready status?
ssh root@${fuelIp} fuel node 2>/dev/null | tail -n +3 | cut -d "|" -f 2 | \
  sed 's/ //g' | grep -v ready | wc -l | grep -q "^0$"
if [ $? -ne 0 ]; then
  echo -e "Deploy failed to verify\n"
  ssh root@${fuelIp} fuel node 2>/dev/null
  error_exit "Exiting with error status"
else
  echo -e "Deployment verified\n"
  ssh root@${fuelIp} fuel node 2>/dev/null
  echo -e "\nNow running sanity and smoke health checks"
  echo -e "\n\n"
  ssh root@${fuelIp} fuel health --env ${envId} --check sanity,smoke \
      --force
  if [ $? -eq 0 ]; then
      echo "Health checks passed!"
  else
      error_exit "One or several health checks failed!"
  fi

  time1=`date +%s`
  echo "Total deployment time: $[(time1-time0)/60] minutes"
  exit 0
fi