[FN VM] Reboot VMs on jump, wait for all online
[fuel.git] / ci / deploy.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2034,SC2154,SC1090,SC1091
3 ##############################################################################
4 # Copyright (c) 2017 Ericsson AB, Mirantis Inc., Enea 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     local RC=$?
17     cleanup_mounts > /dev/null 2>&1
18     if [ ${RC} -eq 0 ]; then
19         notify_n "[OK] MCP: Openstack installation finished succesfully!" 2
20     else
21         notify_n "[ERROR] MCP: Openstack installation threw a fatal error!"
22     fi
23 }
24 #
25 # End of Exit handlers
26 ##############################################################################
27
28 ##############################################################################
29 # BEGIN of usage description
30 #
31 usage ()
32 {
33 cat << EOF
34 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
35 $(notify "$(basename "$0"): Deploy the Fuel@OPNFV MCP stack" 3)
36
37 $(notify "USAGE:" 2)
38   $(basename "$0") -l lab-name -p pod-name -s deploy-scenario \\
39     [-b Lab Config Base URI] \\
40     [-S storage-dir] [-L /path/to/log/file.tar.gz] \\
41     [-f[f]] [-F] [-e | -E[E]] [-d] [-D]
42
43 $(notify "OPTIONS:" 2)
44   -b  Base-uri for the stack-configuration structure
45   -d  Dry-run
46   -D  Debug logging
47   -e  Do not launch environment deployment
48   -E  Remove existing VCP VMs (use twice to redeploy baremetal nodes)
49   -f  Deploy on existing Salt master (use twice to also skip config sync)
50   -F  Do only create a Salt master
51   -h  Print this message and exit
52   -l  Lab-name
53   -p  Pod-name
54   -P  Skip installation of package dependencies
55   -s  Deploy-scenario short-name
56   -S  Storage dir for VM images
57   -L  Deployment log path and file name
58
59 $(notify_i "Description:" 2)
60 Deploys the Fuel@OPNFV stack on the indicated lab resource.
61
62 This script provides the Fuel@OPNFV deployment abstraction.
63 It depends on the OPNFV official configuration directory/file structure
64 and provides a fairly simple mechanism to execute a deployment.
65
66 $(notify_i "Input parameters to the build script are:" 2)
67 -b Base URI to the configuration directory (needs to be provided in URI style,
68    it can be a local resource: file:// or a remote resource http(s)://).
69    A POD Descriptor File (PDF) and its Installer Descriptor File (IDF)
70    companion should be available at:
71    <base-uri>/labs/<lab-name>/<pod-name>.yaml
72    <base-uri>/labs/<lab-name>/idf-<pod-name>.yaml
73    The default is using the git submodule tracking 'OPNFV Pharos' in
74    <./mcp/scripts/pharos>.
75    An example config is provided inside current repo in
76    <./mcp/config>, automatically linked as <./mcp/scripts/pharos/labs/local>.
77 -d Dry-run - Produce deploy config files, but do not execute deploy
78 -D Debug logging - Enable extra logging in sh deploy scripts (set -x)
79 -e Do not launch environment deployment
80 -E Remove existing VCP VMs. It will destroy and undefine all VCP VMs
81    currently defined on cluster KVM nodes. If specified twice (e.g. -E -E),
82    baremetal nodes (VCP too, implicitly) will be removed, then reprovisioned.
83    Only applicable for baremetal deploys.
84 -f Deploy on existing Salt master. It will skip infrastructure VM creation,
85    but it will still sync reclass configuration from current repo to Salt
86    Master node. If specified twice (e.g. -f -f), config sync will also be
87    skipped.
88 -F Do only create a Salt master
89 -h Print this message and exit
90 -L Deployment log path and name, eg. -L /home/jenkins/job.log.tar.gz
91 -l Lab name as defined in the configuration directory, e.g. lf
92 -p POD name as defined in the configuration directory, e.g. pod2
93 -P Skip installing dependency distro packages on current host
94    This flag should only be used if you have kept back older packages that
95    would be upgraded and that is undesirable on the current system.
96    Note that without the required packages, deploy will fail.
97 -s Deployment-scenario, this points to a short deployment scenario name, which
98    has to be defined in config directory (e.g. os-odl-nofeature-ha).
99 -S Storage dir for VM images, default is mcp/deploy/images
100
101 $(notify_i "[NOTE] sudo & virsh priviledges are needed for this script to run" 3)
102
103 Example:
104
105 $(notify_i "sudo $(basename "$0") \\
106   -b file:///home/jenkins/securedlab \\
107   -l lf -p pod2 \\
108   -s os-odl-nofeature-ha" 2)
109 EOF
110 }
111
112 #
113 # END of usage description
114 ##############################################################################
115
116 ##############################################################################
117 # BEGIN of variables to customize
118 #
119 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
120 REPO_ROOT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")
121 DEPLOY_DIR=$(cd "${REPO_ROOT_PATH}/mcp/scripts"; pwd)
122 STORAGE_DIR=$(cd "${REPO_ROOT_PATH}/mcp/deploy/images"; pwd)
123 DEPLOY_TYPE='baremetal'
124 BR_NAMES=('admin' 'mgmt' 'private' 'public')
125 OPNFV_BRIDGES=('pxebr' 'mgmt' 'internal' 'public')
126 URI_REGEXP='(file|https?|ftp)://.*'
127 BASE_CONFIG_URI="file://${REPO_ROOT_PATH}/mcp/scripts/pharos"
128
129 # Customize deploy workflow
130 DRY_RUN=${DRY_RUN:-0}
131 USE_EXISTING_PKGS=${USE_EXISTING_PKGS:-0}
132 USE_EXISTING_INFRA=${USE_EXISTING_INFRA:-0}
133 INFRA_CREATION_ONLY=${INFRA_CREATION_ONLY:-0}
134 NO_DEPLOY_ENVIRONMENT=${NO_DEPLOY_ENVIRONMENT:-0}
135 ERASE_ENV=${ERASE_ENV:-0}
136
137 source "${DEPLOY_DIR}/globals.sh"
138 source "${DEPLOY_DIR}/lib.sh"
139
140 #
141 # END of variables to customize
142 ##############################################################################
143
144 ##############################################################################
145 # BEGIN of main
146 #
147 set +x
148 while getopts "b:dDfEFl:L:p:Ps:S:he" OPTION
149 do
150     case $OPTION in
151         b)
152             BASE_CONFIG_URI=${OPTARG}
153             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
154                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI"
155                 usage
156                 exit 1
157             fi
158             ;;
159         d)
160             DRY_RUN=1
161             ;;
162         D)
163             CI_DEBUG=1
164             ;;
165         f)
166             ((USE_EXISTING_INFRA+=1))
167             ;;
168         F)
169             INFRA_CREATION_ONLY=1
170             ;;
171         e)
172             NO_DEPLOY_ENVIRONMENT=1
173             ;;
174         E)
175             ((ERASE_ENV+=1))
176             ;;
177         l)
178             TARGET_LAB=${OPTARG}
179             ;;
180         L)
181             DEPLOY_LOG="${OPTARG}"
182             ;;
183         p)
184             TARGET_POD=${OPTARG}
185             if [[ "${TARGET_POD}" =~ virtual ]]; then
186                 DEPLOY_TYPE='virtual'
187                 # All vPODs will use 'local-virtual1' PDF/IDF for now
188                 TARGET_LAB='local'
189                 TARGET_POD='virtual1'
190             fi
191             ;;
192         P)
193             USE_EXISTING_PKGS=1
194             ;;
195         s)
196             DEPLOY_SCENARIO=${OPTARG}
197             ;;
198         S)
199             if [[ ${OPTARG} ]]; then
200                 STORAGE_DIR="${OPTARG}"
201             fi
202             ;;
203         h)
204             usage
205             exit 0
206             ;;
207         *)
208             notify_e "[ERROR] Unsupported arg, see -h for help"
209             ;;
210     esac
211 done
212
213 if [[ "$(sudo whoami)" != 'root' ]]; then
214     notify_e "[ERROR] This script requires sudo rights"
215 fi
216
217 # Validate mandatory arguments are set
218 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
219    [ -z "${DEPLOY_SCENARIO}" ]; then
220     usage
221     notify_e "[ERROR] At least one of the mandatory args is missing!"
222 fi
223
224 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
225
226 # Enable the automatic exit trap
227 trap do_exit SIGINT SIGTERM EXIT
228
229 # Set no restrictive umask so that Jenkins can remove any residuals
230 umask 0000
231
232 pushd "${DEPLOY_DIR}" > /dev/null
233 # Prepare the deploy config files based on lab/pod information, deployment
234 # scenario, etc.
235
236 # Install required packages on jump server
237 if [ ${USE_EXISTING_PKGS} -eq 1 ]; then
238     notify "[NOTE] Skipping distro pkg installation" 2
239 else
240     notify "[NOTE] Installing required distro pkgs" 2
241     if [ -n "$(command -v apt-get)" ]; then
242       pkg_type='deb'; pkg_cmd='sudo apt-get install -y'
243     else
244       pkg_type='rpm'; pkg_cmd='sudo yum install -y --skip-broken'
245     fi
246     eval "$(parse_yaml "./requirements_${pkg_type}.yaml")"
247     for section in 'common' "${DEPLOY_TYPE}" "$(uname -m)"; do
248       section_var="requirements_pkg_${section}[*]"
249       pkg_list+=" ${!section_var}"
250     done
251     # shellcheck disable=SC2086
252     ${pkg_cmd} ${pkg_list}
253 fi
254
255 if ! virsh list >/dev/null 2>&1; then
256     notify_e "[ERROR] This script requires hypervisor access"
257 fi
258
259 # Collect jump server system information for deploy debugging
260 ./sysinfo_print.sh
261
262 # Clone git submodules and apply our patches
263 make -C "${REPO_ROOT_PATH}/mcp/patches" deepclean patches-import
264
265 # Expand scenario files, pod_config based on PDF
266 SCENARIO_DIR="$(readlink -f "../config/scenario")"
267 do_templates "${REPO_ROOT_PATH}" "${STORAGE_DIR}" "${TARGET_LAB}" \
268              "${TARGET_POD}" "${BASE_CONFIG_URI}" "${SCENARIO_DIR}"
269
270 # Check scenario file existence
271 if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
272     notify_e "[ERROR] Scenario definition file is missing!"
273 fi
274
275 # Check defaults file existence
276 if [ ! -f  "${SCENARIO_DIR}/defaults-$(uname -i).yaml" ]; then
277     notify_e "[ERROR] Scenario defaults file is missing!"
278 fi
279
280 # Expand jinja2 templates based on PDF data and env vars
281 do_templates "${REPO_ROOT_PATH}" "${STORAGE_DIR}" "${TARGET_LAB}" \
282              "${TARGET_POD}" "${BASE_CONFIG_URI}"
283
284 # Get required infra deployment data
285 set +x
286 eval "$(parse_yaml "${SCENARIO_DIR}/defaults-$(uname -i).yaml")"
287 eval "$(parse_yaml "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml")"
288 eval "$(parse_yaml "${LOCAL_PDF_RECLASS}")"
289 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
290
291 export CLUSTER_DOMAIN=${cluster_domain}
292
293 # Serialize vnode data as '<name0>,<ram0>,<vcpu0>|<name1>,<ram1>,<vcpu1>[...]'
294 for node in "${virtual_nodes[@]}"; do
295     virtual_custom_ram="virtual_${node}_ram"
296     virtual_custom_vcpus="virtual_${node}_vcpus"
297     virtual_nodes_data+="${node},"
298     virtual_nodes_data+="${!virtual_custom_ram:-$virtual_default_ram},"
299     virtual_nodes_data+="${!virtual_custom_vcpus:-$virtual_default_vcpus}|"
300 done
301 virtual_nodes_data=${virtual_nodes_data%|}
302
303 # Serialize repos, packages to (pre-)install/remove for:
304 # - foundation node VM base image (virtual: all VMs, baremetal: cfg01|mas01)
305 # - virtualized control plane VM base image (only when VCP is used)
306 base_image_flavors=common
307 if [[ "${cluster_states[*]}" =~ virtual_control ]]; then
308   base_image_flavors+=" control"
309 fi
310 for sc in ${base_image_flavors}; do
311   for va in apt_keys apt_repos pkg_install pkg_remove; do
312     key=virtual_${sc}_${va}
313     eval "${key}=\${${key}[@]// /|}"
314     eval "${key}=\${${key}// /,}"
315     virtual_repos_pkgs+="${!key}^"
316   done
317 done
318 virtual_repos_pkgs=${virtual_repos_pkgs%^}
319
320 # Determine 'admin', 'mgmt', 'private' and 'public' bridge names based on IDF
321 for ((i = 0; i < ${#BR_NAMES[@]}; i++)); do
322     br_jump=$(eval echo "\$parameters__param_opnfv_jump_bridge_${BR_NAMES[i]}")
323     if [ -n "${br_jump}" ] && [ "${br_jump}" != 'None' ]; then
324         OPNFV_BRIDGES[${i}]="${br_jump}"
325     fi
326 done
327 notify "[NOTE] Using bridges: ${OPNFV_BRIDGES[*]}" 2
328
329 # Infra setup
330 if [ ${DRY_RUN} -eq 1 ]; then
331     notify "[NOTE] Dry run, skipping all deployment tasks" 2
332     exit 0
333 elif [ ${USE_EXISTING_INFRA} -gt 0 ]; then
334     notify "[NOTE] Use existing infra" 2
335     check_connection
336 else
337     generate_ssh_key
338     prepare_vms "${base_image}" "${STORAGE_DIR}" "${virtual_repos_pkgs}" \
339       "${virtual_nodes[@]}"
340     create_networks "${OPNFV_BRIDGES[@]}"
341     do_sysctl_cfg
342     create_vms "${STORAGE_DIR}" "${virtual_nodes_data}" "${OPNFV_BRIDGES[@]}"
343     update_mcpcontrol_network
344     start_vms "${virtual_nodes[@]}"
345     check_connection
346 fi
347 if [ ${USE_EXISTING_INFRA} -lt 2 ]; then
348     wait_for 5 "./salt.sh ${LOCAL_PDF_RECLASS} ${virtual_nodes[*]}"
349 fi
350
351 # Openstack cluster setup
352 set +x
353 if [ ${INFRA_CREATION_ONLY} -eq 1 ] || [ ${NO_DEPLOY_ENVIRONMENT} -eq 1 ]; then
354     notify "[NOTE] Skip openstack cluster setup" 2
355 else
356     for state in "${cluster_states[@]}"; do
357         notify "[STATE] Applying state: ${state}" 2
358         # shellcheck disable=SC2086,2029
359         wait_for 5 "ssh ${SSH_OPTS} ${SSH_SALT} sudo \
360             CI_DEBUG=$CI_DEBUG ERASE_ENV=$ERASE_ENV \
361             /root/fuel/mcp/config/states/${state}"
362     done
363 fi
364
365 ./log.sh "${DEPLOY_LOG}"
366
367 popd > /dev/null
368
369 #
370 # END of main
371 ##############################################################################