[vnode parsing] Move core logic to j2
[fuel.git] / ci / deploy.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2034,SC2154,SC1090,SC1091,SC2155
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 URI_REGEXP='(file|https?|ftp)://.*'
124 BASE_CONFIG_URI="file://${REPO_ROOT_PATH}/mcp/scripts/pharos"
125
126 # Customize deploy workflow
127 DRY_RUN=${DRY_RUN:-0}
128 USE_EXISTING_PKGS=${USE_EXISTING_PKGS:-0}
129 USE_EXISTING_INFRA=${USE_EXISTING_INFRA:-0}
130 INFRA_CREATION_ONLY=${INFRA_CREATION_ONLY:-0}
131 NO_DEPLOY_ENVIRONMENT=${NO_DEPLOY_ENVIRONMENT:-0}
132 ERASE_ENV=${ERASE_ENV:-0}
133
134 source "${DEPLOY_DIR}/globals.sh"
135 source "${DEPLOY_DIR}/lib.sh"
136 source "${DEPLOY_DIR}/lib_template.sh"
137
138 #
139 # END of variables to customize
140 ##############################################################################
141
142 ##############################################################################
143 # BEGIN of main
144 #
145 set +x
146 while getopts "b:dDfEFl:L:p:Ps:S:he" OPTION
147 do
148     case $OPTION in
149         b)
150             BASE_CONFIG_URI=${OPTARG}
151             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
152                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI"
153                 usage
154                 exit 1
155             fi
156             ;;
157         d)
158             DRY_RUN=1
159             ;;
160         D)
161             CI_DEBUG=1
162             ;;
163         f)
164             ((USE_EXISTING_INFRA+=1))
165             ;;
166         F)
167             INFRA_CREATION_ONLY=1
168             ;;
169         e)
170             NO_DEPLOY_ENVIRONMENT=1
171             ;;
172         E)
173             ((ERASE_ENV+=1))
174             ;;
175         l)
176             TARGET_LAB=${OPTARG}
177             ;;
178         L)
179             DEPLOY_LOG="${OPTARG}"
180             ;;
181         p)
182             TARGET_POD=${OPTARG}
183             if [[ "${TARGET_POD}" =~ virtual ]]; then
184                 # All vPODs will use 'local-virtual1' PDF/IDF for now
185                 TARGET_LAB='local'
186                 TARGET_POD='virtual1'
187             fi
188             ;;
189         P)
190             USE_EXISTING_PKGS=1
191             ;;
192         s)
193             DEPLOY_SCENARIO=${OPTARG}
194             ;;
195         S)
196             if [[ ${OPTARG} ]]; then
197                 STORAGE_DIR="${OPTARG}"
198             fi
199             ;;
200         h)
201             usage
202             exit 0
203             ;;
204         *)
205             notify_e "[ERROR] Unsupported arg, see -h for help"
206             ;;
207     esac
208 done
209
210 if [[ "$(sudo whoami)" != 'root' ]]; then
211     notify_e "[ERROR] This script requires sudo rights"
212 fi
213
214 # Validate mandatory arguments are set
215 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
216    [ -z "${DEPLOY_SCENARIO}" ]; then
217     usage
218     notify_e "[ERROR] At least one of the mandatory args is missing!"
219 fi
220
221 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
222
223 # Enable the automatic exit trap
224 trap do_exit SIGINT SIGTERM EXIT
225
226 # Set no restrictive umask so that Jenkins can remove any residuals
227 umask 0000
228
229 pushd "${DEPLOY_DIR}" > /dev/null
230 # Prepare the deploy config files based on lab/pod information, deployment
231 # scenario, etc.
232
233 # Install required packages on jump server
234 if [ ${USE_EXISTING_PKGS} -eq 1 ]; then
235     notify "[NOTE] Skipping distro pkg installation" 2
236 else
237     notify "[NOTE] Installing required distro pkgs" 2
238     jumpserver_pkg_install
239 fi
240
241 if ! virsh list >/dev/null 2>&1; then
242     notify_e "[ERROR] This script requires hypervisor access"
243 fi
244
245 # Collect jump server system information for deploy debugging
246 ./sysinfo_print.sh
247
248 # Clone git submodules and apply our patches
249 make -C "${REPO_ROOT_PATH}/mcp/patches" deepclean patches-import
250
251 # Check scenario file existence
252 SCENARIO_DIR="$(readlink -f "../config/scenario")"
253 if [ ! -f "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml" ] && \
254    [ ! -f "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml.j2" ]; then
255     notify_e "[ERROR] Scenario definition file is missing!"
256 fi
257
258 # key might not exist yet ...
259 generate_ssh_key
260 export MAAS_SSH_KEY="$(cat "$(basename "${SSH_KEY}").pub")"
261
262 # Expand jinja2 templates based on PDF data and env vars
263 export MCP_JUMP_ARCH=$(uname -i)
264 do_templates_scenario "${STORAGE_DIR}" "${TARGET_LAB}" "${TARGET_POD}" \
265                       "${BASE_CONFIG_URI}" "${SCENARIO_DIR}"
266 do_templates_cluster  "${STORAGE_DIR}" "${TARGET_LAB}" "${TARGET_POD}" \
267                       "${REPO_ROOT_PATH}" \
268                       "${SCENARIO_DIR}/defaults.yaml" \
269                       "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml"
270
271 # Determine additional data (e.g. jump bridge names) based on XDF
272 source "${DEPLOY_DIR}/xdf_data.sh"
273
274 # Jumpserver prerequisites check
275 notify "[NOTE] Using bridges: ${OPNFV_BRIDGES[*]}" 2
276 jumpserver_check_requirements "${virtual_nodes[*]}" "${OPNFV_BRIDGES[@]}"
277
278 # Infra setup
279 if [ ${DRY_RUN} -eq 1 ]; then
280     notify "[NOTE] Dry run, skipping all deployment tasks" 2
281     exit 0
282 elif [ ${USE_EXISTING_INFRA} -gt 0 ]; then
283     notify "[NOTE] Use existing infra" 2
284     check_connection
285 else
286     prepare_vms "${base_image}" "${STORAGE_DIR}" "${virtual_repos_pkgs}" \
287       "${virtual_nodes[@]}"
288     create_networks "${OPNFV_BRIDGES[@]}"
289     do_sysctl_cfg
290     create_vms "${STORAGE_DIR}" "${virtual_nodes_data}" "${OPNFV_BRIDGES[@]}"
291     update_mcpcontrol_network
292     start_vms "${virtual_nodes[@]}"
293     check_connection
294 fi
295 if [ ${USE_EXISTING_INFRA} -lt 2 ]; then
296     wait_for 5 "./salt.sh ${STORAGE_DIR}/pod_config.yml ${virtual_nodes[*]}"
297 fi
298
299 # Openstack cluster setup
300 set +x
301 if [ ${INFRA_CREATION_ONLY} -eq 1 ] || [ ${NO_DEPLOY_ENVIRONMENT} -eq 1 ]; then
302     notify "[NOTE] Skip openstack cluster setup" 2
303 else
304     for state in "${cluster_states[@]}"; do
305         notify "[STATE] Applying state: ${state}" 2
306         # shellcheck disable=SC2086,2029
307         wait_for 5 "ssh ${SSH_OPTS} ${SSH_SALT} sudo \
308             CI_DEBUG=$CI_DEBUG ERASE_ENV=$ERASE_ENV \
309             /root/fuel/mcp/config/states/${state}"
310     done
311 fi
312
313 ./log.sh "${DEPLOY_LOG}"
314
315 popd > /dev/null
316
317 #
318 # END of main
319 ##############################################################################