00f710464a6aff23979f280c41420e26d85ec1f5
[fuel.git] / ci / deploy.sh
1 #!/bin/bash
2 set -ex
3 ##############################################################################
4 # Copyright (c) 2017 Ericsson AB, Mirantis Inc. 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
33        [-B PXE Bridge [-B Mgmt Bridge [-B Internal Bridge [-B Public Bridge]]]]
34        [-f] [-F] [-H] -l lab-name -p pod-name -s deploy-scenario
35        [-S image-dir] [-T timeout] -i iso
36        -s deployment-scenario [-S optional Deploy-scenario path URI]
37        [-R optional local relen repo (containing deployment Scenarios]
38
39 OPTIONS:
40   -b  Base-uri for the stack-configuration structure
41   -B  Bridge(s): 1st usage = PXE, 2nd = Mgmt, 3rd = Internal, 4th = Public
42   -d  Dry-run
43   -f  Deploy on existing Fuel master
44   -e  Do not launch environment deployment
45   -F  Do only create a Fuel master
46   -h  Print this message and exit
47   -H  No health check
48   -l  Lab-name
49   -L  Deployment log path and file name
50   -p  Pod-name
51   -s  Deploy-scenario short-name/base-file-name
52   -S  Storage dir for VM images
53   -T  Timeout, in minutes, for the deploy.
54   -i  iso url
55
56 Description:
57 Deploys the Fuel@OPNFV stack on the indicated lab resource
58
59 This script provides the Fuel@OPNFV deployment abstraction
60 It depends on the OPNFV official configuration directory/file structure
61 and provides a fairly simple mechanism to execute a deployment.
62 Input parameters to the build script is:
63 -b Base URI to the configuration directory (needs to be provided in a URI
64    style, it can be a local resource: file:// or a remote resource http(s)://)
65 -B Bridges to be used by deploy script. It can be specified several times,
66    or as a comma separated list of bridges, or both: -B br1 -B br2,br3
67    First occurence sets PXE Brige, next Mgmt, then Internal and Public.
68    For an empty value, the deploy script will use virsh to create the default
69    expected network (e.g. -B pxe,,,public will use existing "pxe" and "public"
70    bridges, respectively create "mgmt" and "internal").
71    The default is pxebr.
72 -d Dry-run - Produces deploy config files (config/dea.yaml and
73    config/dha.yaml), but does not execute deploy
74 -f Deploy on existing Fuel master
75 -e Do not launch environment deployment
76 -F Do only create a Fuel master
77 -h Print this message and exit
78 -H Do not run fuel built in health-check after successfull deployment
79 -l Lab name as defined in the configuration directory, e.g. lf
80 -L Deployment log path and name, eg. -L /home/jenkins/logs/job888.log.tar.gz
81 -p POD name as defined in the configuration directory, e.g. pod-1
82 -s Deployment-scenario, this points to a deployment/test scenario file as
83    defined in the configuration directory:
84    e.g fuel-ocl-heat-ceilometer_scenario_0.0.1.yaml
85    or a deployment short-name as defined by scenario.yaml in the deployment
86    scenario path.
87 -S Storage dir for VM images, default is fuel/deploy/images
88 -T Timeout, in minutes, for the deploy. It defaults to using the DEPLOY_TIMEOUT
89    environment variable when defined, or to the default in deploy.py otherwise
90 -i .iso image to be deployed (needs to be provided in a URI
91    style, it can be a local resource: file:// or a remote resource http(s)://)
92
93 NOTE: Root priviledges are needed for this script to run
94
95
96 Examples:
97 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
98 EOF
99 }
100
101 #
102 # END of usage description
103 ############################################################################
104
105 ############################################################################
106 # BEGIN of deployment clean-up
107 #
108 clean() {
109     echo "Cleaning up deploy tmp directories"
110     rm -rf ${SCRIPT_PATH}/ISO
111 }
112 #
113 # END of deployment clean-up
114 ############################################################################
115
116 ############################################################################
117 # BEGIN of shorthand variables for internal use
118 #
119 SCRIPT_PATH=$(readlink -f $(dirname ${BASH_SOURCE[0]}))
120 DEPLOY_DIR=$(cd ${SCRIPT_PATH}/../mcp/scripts; pwd)
121 OPNFV_BRIDGES=('pxe' 'mgmt' 'internal' 'public')
122 NO_HEALTH_CHECK=''
123 USE_EXISTING_FUEL=''
124 FUEL_CREATION_ONLY=''
125 NO_DEPLOY_ENVIRONMENT=''
126 STORAGE_DIR=''
127 DRY_RUN=0
128 if ! [ -z $DEPLOY_TIMEOUT ]; then
129     DEPLOY_TIMEOUT="-dt $DEPLOY_TIMEOUT"
130 else
131     DEPLOY_TIMEOUT=""
132 fi
133 #
134 # END of variables to customize
135 ############################################################################
136
137 ############################################################################
138 # BEGIN of main
139 #
140 OPNFV_BRIDGE_IDX=0
141 while getopts "b:B:dfFHl:L:p:s:S:T:i:he" OPTION
142 do
143     case $OPTION in
144         b)
145             BASE_CONFIG_URI=${OPTARG}
146             if [[ ! $BASE_CONFIG_URI == file://* ]] && \
147                [[ ! $BASE_CONFIG_URI == http://* ]] && \
148                [[ ! $BASE_CONFIG_URI == https://* ]] && \
149                [[ ! $BASE_CONFIG_URI == ftp://* ]]; then
150                 echo "-b $BASE_CONFIG_URI - Not given in URI style"
151                 usage
152                 exit 1
153             fi
154             ;;
155         B)
156             OIFS=${IFS}
157             IFS=','
158             OPT_BRIDGES=($OPTARG)
159             for bridge in "${OPT_BRIDGES[@]}"; do
160                 if [ -n "${bridge}" ]; then
161                     OPNFV_BRIDGES[${OPNFV_BRIDGE_IDX}]="${bridge}"
162                 fi
163                 OPNFV_BRIDGE_IDX=$[OPNFV_BRIDGE_IDX + 1]
164             done
165             IFS=${OIFS}
166             ;;
167         d)
168             DRY_RUN=1
169             ;;
170         f)
171             USE_EXISTING_FUEL='-nf'
172             ;;
173         F)
174             FUEL_CREATION_ONLY='-fo'
175             ;;
176         e)
177             NO_DEPLOY_ENVIRONMENT='-nde'
178             ;;
179         H)
180             NO_HEALTH_CHECK='-nh'
181             ;;
182         l)
183             TARGET_LAB=${OPTARG}
184             ;;
185         L)
186             DEPLOY_LOG="-log ${OPTARG}"
187             ;;
188         p)
189             TARGET_POD=${OPTARG}
190             ;;
191         s)
192             DEPLOY_SCENARIO=${OPTARG}
193             ;;
194         S)
195             if [[ ${OPTARG} ]]; then
196                 STORAGE_DIR="-s ${OPTARG}"
197             fi
198             ;;
199         T)
200             DEPLOY_TIMEOUT="-dt ${OPTARG}"
201             ;;
202         i)
203             ISO=${OPTARG}
204             if [[ ! $ISO == file://* ]] && \
205                [[ ! $ISO == http://* ]] && \
206                [[ ! $ISO == https://* ]] && \
207                [[ ! $ISO == ftp://* ]]; then
208                 echo "-i $ISO - Not given in URI style"
209                 usage
210                 exit 1
211             fi
212             ;;
213         h)
214             usage
215             exit 0
216             ;;
217         *)
218             echo "${OPTION} is not a valid argument"
219             echo "Arguments not according to new argument style"
220             echo "Trying old-style compatibility mode"
221             pushd ${DEPLOY_DIR} > /dev/null
222             python deploy.py "$@"
223             popd > /dev/null
224             exit 0
225             ;;
226     esac
227 done
228
229 if [[ $EUID -ne 0 ]]; then
230     echo "This script must be run as root" 1>&2
231     exit 1
232 fi
233
234 # Enable the automatic exit trap
235 trap do_exit SIGINT SIGTERM EXIT
236
237 # Set no restrictive umask so that Jenkins can removeeee any residuals
238 umask 0000
239
240 clean
241
242 pushd ${DEPLOY_DIR} > /dev/null
243 # Prepare the deploy config files based on lab/pod information, deployment
244 # scenario, etc.
245
246 # Install required packages
247 [ -n "$(command -v apt-get)" ] && apt-get install -y mkisofs curl virtinst cpu-checker qemu-kvm
248 [ -n "$(command -v yum)" ] && yum install -y genisoimage curl virt-install qemu-kvm
249
250 # Check scenario file existence
251 if [[ ! -f  ../config/${DEPLOY_SCENARIO}.yaml ]]; then
252     echo "[WARN] ${DEPLOY_SCENARIO}.yaml not found, setting simplest scenario"
253     DEPLOY_SCENARIO='os-nosdn-nofeature-noha'
254 fi
255
256 # Get required infra deployment data
257 source lib.sh
258 eval $(parse_yaml ../config/defaults.yaml)
259 eval $(parse_yaml ../config/${DEPLOY_SCENARIO}.yaml)
260
261 declare -A virtual_nodes_ram virtual_nodes_vcpus
262 for node in "${virtual_nodes[@]}"; do
263     virtual_custom_ram="virtual_${node}_ram"
264     virtual_custom_vcpus="virtual_${node}_vcpus"
265     virtual_nodes_ram[$node]=${!virtual_custom_ram:-$virtual_default_ram}
266     virtual_nodes_vcpus[$node]=${!virtual_custom_vcpus:-$virtual_default_vcpus}
267 done
268
269 export CLUSTER_DOMAIN=$cluster_domain
270 export SSH_KEY=${SSH_KEY:-mcp.rsa}
271 export SALT_MASTER=${SALT_MASTER_IP:-192.168.10.100}
272 export SSH_OPTS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${SSH_KEY}"
273
274 # Infra setup
275 generate_ssh_key
276 prepare_vms virtual_nodes $base_image
277 create_networks OPNFV_BRIDGES
278 create_vms virtual_nodes virtual_nodes_ram virtual_nodes_vcpus OPNFV_BRIDGES
279 update_pxe_network OPNFV_BRIDGES
280 start_vms virtual_nodes
281 check_connection
282
283 ./salt.sh
284
285 # Openstack cluster setup
286 for state in "${cluster_states[@]}"; do
287     echo "STATE: $state"
288     ssh ${SSH_OPTS} ubuntu@${SALT_MASTER} sudo /root/fuel/mcp/config/states/$state
289 done
290
291 ## Disable Fuel deployment engine
292 #
293 # 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"
294 #
295 # 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
296 #
297 # if [ $DRY_RUN -eq 0 ]; then
298 #     # Download iso if it doesn't already exists locally
299 #     if [[ $ISO == file://* ]]; then
300 #         ISO=${ISO#file://}
301 #     else
302 #         mkdir -p ${SCRIPT_PATH}/ISO
303 #         curl -o ${SCRIPT_PATH}/ISO/image.iso $ISO
304 #         ISO=${SCRIPT_PATH}/ISO/image.iso
305 #     fi
306 #     # Start deployment
307 #     echo "python deploy.py $DEPLOY_LOG $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 $DEPLOY_TIMEOUT"
308 #     python deploy.py $DEPLOY_LOG $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 $DEPLOY_TIMEOUT
309 # fi
310 popd > /dev/null
311
312 #
313 # END of main
314 ############################################################################