Merge "small fix for deploy.py invocation"
[fuel.git] / ci / deploy.sh
1 #!/bin/bash
2 set -e
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
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 ############################################################################
13 # BEGIN of Exit handlers
14 #
15 do_exit () {
16     clean
17     echo "Exiting ..."
18 }
19 #
20 # End of Exit handlers
21 ############################################################################
22
23 ############################################################################
24 # BEGIN of usage description
25 #
26 usage ()
27 {
28 cat << EOF
29 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
30 `basename $0`: Deploys the Fuel@OPNFV stack
31
32 usage: `basename $0` -b base-uri [-B PXE Bridge] [-f] [-F] [-H] -l lab-name -p pod-name -s deploy-scenario [-S image-dir] -i iso
33        -s deployment-scenario [-S optional Deploy-scenario path URI]
34        [-R optional local relen repo (containing deployment Scenarios]
35
36 OPTIONS:
37   -b  Base-uri for the stack-configuration structure
38   -B  PXE Bridge for booting of Fuel master
39   -d  Dry-run
40   -f  Deploy on existing Fuel master
41   -e  Do not launch environment deployment
42   -F  Do only create a Fuel master
43   -H  No health check
44   -l  Lab-name
45   -p  Pod-name
46   -s  Deploy-scenario short-name/base-file-name
47   -S  Storage dir for VM images
48   -i  iso url
49
50 Description:
51 Deploys the Fuel@OPNFV stack on the indicated lab resource
52
53 This script provides the Fuel@OPNFV deployment abstraction
54 It depends on the OPNFV official configuration directory/file structure
55 and provides a fairly simple mechanism to execute a deployment.
56 Input parameters to the build script is:
57 -b Base URI to the configuration directory (needs to be provided in a URI
58    style, it can be a local resource: file:// or a remote resource http(s)://)
59 -B PXE Bridge for booting of Fuel master, default is pxebr
60 -d Dry-run - Produces deploy config files (config/dea.yaml and
61    config/dha.yaml), but does not execute deploy
62 -f Deploy on existing Fuel master
63 -e Do not launch environment deployment
64 -F Do only create a Fuel master
65 -H Do not run fuel built in health-check after successfull deployment
66 -l Lab name as defined in the configuration directory, e.g. lf
67 -p POD name as defined in the configuration directory, e.g. pod-1
68 -s Deployment-scenario, this points to a deployment/test scenario file as
69    defined in the configuration directory:
70    e.g fuel-ocl-heat-ceilometer_scenario_0.0.1.yaml
71    or a deployment short-name as defined by scenario.yaml in the deployment
72    scenario path.
73 -S Storage dir for VM images, default is fuel/deploy/images
74 -i .iso image to be deployed (needs to be provided in a URI
75    style, it can be a local resource: file:// or a remote resource http(s)://)
76
77 NOTE: Root priviledges are needed for this script to run
78
79
80 Examples:
81 sudo `basename $0` -b file:///home/jenkins/lab-config -l lf -p pod1 -s ha_odl-l3_heat_ceilometer -i file:///home/jenkins/myiso.iso
82 EOF
83 }
84
85 #
86 # END of usage description
87 ############################################################################
88
89 ############################################################################
90 # BEGIN of deployment clean-up
91 #
92 clean() {
93     echo "Cleaning up deploy tmp directories"
94     rm -rf ${SCRIPT_PATH}/ISO
95 }
96 #
97 # END of deployment clean-up
98 ############################################################################
99
100 ############################################################################
101 # BEGIN of shorthand variables for internal use
102 #
103 SCRIPT_PATH=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
104 DEPLOY_DIR=$(cd ${SCRIPT_PATH}/../deploy; pwd)
105 PXE_BRIDGE=''
106 NO_HEALTH_CHECK=''
107 USE_EXISTING_FUEL=''
108 FUEL_CREATION_ONLY=''
109 NO_DEPLOY_ENVIRONMENT=''
110 STORAGE_DIR=''
111 DRY_RUN=0
112 #
113 # END of variables to customize
114 ############################################################################
115
116 ############################################################################
117 # BEGIN of main
118 #
119 while getopts "b:B:dfFHl:p:s:S:i:h:e" OPTION
120 do
121     case $OPTION in
122         b)
123             BASE_CONFIG_URI=${OPTARG}
124             if [[ ! $BASE_CONFIG_URI == file://* ]] && \
125                [[ ! $BASE_CONFIG_URI == http://* ]] && \
126                [[ ! $BASE_CONFIG_URI == https://* ]] && \
127                [[ ! $BASE_CONFIG_URI == ftp://* ]]; then
128                 echo "-b $BASE_CONFIG_URI - Not given in URI style"
129                 usage
130                 exit 1
131             fi
132             ;;
133         B)
134             if [[ ${OPTARG} ]]; then
135                 PXE_BRIDGE="-b ${OPTARG}"
136             fi
137             ;;
138         d)
139             DRY_RUN=1
140             ;;
141         f)
142             USE_EXISTING_FUEL='-nf'
143             ;;
144         F)
145             FUEL_CREATION_ONLY='-fo'
146             ;;
147         e)
148             NO_DEPLOY_ENVIRONMENT='-nde'
149             ;;
150         H)
151             NO_HEALTH_CHECK='-nh'
152             ;;
153         l)
154             TARGET_LAB=${OPTARG}
155             ;;
156         p)
157             TARGET_POD=${OPTARG}
158             ;;
159         s)
160             DEPLOY_SCENARIO=${OPTARG}
161             ;;
162         S)
163             if [[ ${OPTARG} ]]; then
164                 STORAGE_DIR="-s ${OPTARG}"
165             fi
166             ;;
167         i)
168             ISO=${OPTARG}
169             if [[ ! $ISO == file://* ]] && \
170                [[ ! $ISO == http://* ]] && \
171                [[ ! $ISO == https://* ]] && \
172                [[ ! $ISO == ftp://* ]]; then
173                 echo "-i $ISO - Not given in URI style"
174                 usage
175                 exit 1
176             fi
177             ;;
178         h)
179             usage
180             exit 0
181             ;;
182         *)
183             echo "${OPTION} is not a valid argument"
184             echo "Arguments not according to new argument style"
185             echo "Trying old-style compatibility mode"
186             pushd ${DEPLOY_DIR} > /dev/null
187             python deploy.py "$@"
188             popd > /dev/null
189             exit 0
190             ;;
191     esac
192 done
193
194 if [[ $EUID -ne 0 ]]; then
195     echo "This script must be run as root" 1>&2
196     exit 1
197 fi
198
199 if [ -z $BASE_CONFIG_URI ] || [ -z $TARGET_LAB ] || \
200    [ -z $TARGET_POD ] || [ -z $DEPLOY_SCENARIO ] || \
201    [ -z $ISO ]; then
202     echo "Arguments not according to new argument style"
203     echo "Trying old-style compatibility mode"
204     pushd ${DEPLOY_DIR} > /dev/null
205     python deploy.py "$@"
206     popd > /dev/null
207     exit 0
208 fi
209
210 # Enable the automatic exit trap
211 trap do_exit SIGINT SIGTERM EXIT
212
213 # Set no restrictive umask so that Jenkins can removeeee any residuals
214 umask 0000
215
216 clean
217
218 pushd ${DEPLOY_DIR} > /dev/null
219 # Prepare the deploy config files based on lab/pod information, deployment
220 # scenario, etc.
221
222 echo "python deploy-config.py -dha ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dha.yaml -deab file://${DEPLOY_DIR}/config/dea_base.yaml -deao ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dea-pod-override.yaml -scenario-base-uri file://${DEPLOY_DIR}/scenario -scenario ${DEPLOY_SCENARIO} -plugins file://${DEPLOY_DIR}/config/plugins -output ${SCRIPT_PATH}/config"
223
224 python deploy-config.py -dha ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dha.yaml -deab file://${DEPLOY_DIR}/config/dea_base.yaml -deao ${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}/fuel/config/dea-pod-override.yaml -scenario-base-uri file://${DEPLOY_DIR}/scenario -scenario ${DEPLOY_SCENARIO} -plugins file://${DEPLOY_DIR}/config/plugins -output ${SCRIPT_PATH}/config
225
226 if [ $DRY_RUN -eq 0 ]; then
227     # Download iso if it doesn't already exists locally
228     if [[ $ISO == file://* ]]; then
229         ISO=${ISO#file://}
230     else
231         mkdir -p ${SCRIPT_PATH}/ISO
232         curl -o ${SCRIPT_PATH}/ISO/image.iso $ISO
233         ISO=${SCRIPT_PATH}/ISO/image.iso
234     fi
235     # Start deployment
236     echo "python deploy.py $STORAGE_DIR $PXE_BRIDGE $USE_EXISTING_FUEL $FUEL_CREATION_ONLY $NO_HEALTH_CHECK $NO_DEPLOY_ENVIRONMENT -dea ${SCRIPT_PATH}/config/dea.yaml -dha ${SCRIPT_PATH}/config/dha.yaml -iso $ISO"
237     python deploy.py $STORAGE_DIR $PXE_BRIDGE $USE_EXISTING_FUEL $FUEL_CREATION_ONLY $NO_HEALTH_CHECK $NO_DEPLOY_ENVIRONMENT -dea ${SCRIPT_PATH}/config/dea.yaml -dha ${SCRIPT_PATH}/config/dha.yaml -iso $ISO
238 fi
239 popd > /dev/null
240
241 #
242 # END of main
243 ############################################################################