Merge "[opendaylight] Update SQLAlchemy library"
[fuel.git] / ci / deploy.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2034,SC2154,SC1090,SC1091,SC2155
3 ##############################################################################
4 # Copyright (c) 2018 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 OPNFV Fuel 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] [-e | -E[E]] [-d] [-D] [-N] [-m]
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 or more to skip states)
50   -F  Same as -e, do not launch environment deployment (legacy option)
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 and other deploy artifacts
57   -L  Deployment log path and file name
58   -m  Use single socket CPU compute nodes (only affects virtual computes)
59   -N  Experimental: Do not virtualize control plane (novcp)
60
61 $(notify_i "Description:" 2)
62 Deploys the OPNFV Fuel stack on the indicated lab resource.
63
64 This script provides the OPNFV Fuel deployment abstraction.
65 It depends on the OPNFV official configuration directory/file structure
66 and provides a fairly simple mechanism to execute a deployment.
67
68 $(notify_i "Input parameters to the build script are:" 2)
69 -b Base URI to the configuration directory (needs to be provided in URI style,
70    it can be a local resource: file:// or a remote resource http(s)://).
71    A POD Descriptor File (PDF) and its Installer Descriptor File (IDF)
72    companion should be available at:
73    <base-uri>/labs/<lab-name>/<pod-name>.yaml
74    <base-uri>/labs/<lab-name>/idf-<pod-name>.yaml
75    The default is using the git submodule tracking 'OPNFV Pharos' in
76    <./mcp/scripts/pharos>.
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.
87    Each additional use skips one more state file. For example, -fff would
88    skip the first 3 state files (e.g. virtual_init, maas, baremetal_init).
89 -F Same as -e, do not launch environment deployment (legacy option)
90 -h Print this message and exit
91 -L Deployment log path and name, eg. -L /home/jenkins/job.log.tar.gz
92 -l Lab name as defined in the configuration directory, e.g. lf
93 -p POD name as defined in the configuration directory, e.g. pod2
94 -m Use single socket compute nodes. Instead of using default NUMA-enabled
95    topology for virtual compute nodes created via libvirt, configure a
96    single guest CPU socket.
97 -N Experimental: Instead of virtualizing the control plane (VCP), deploy
98    control plane directly on baremetal nodes
99 -P Skip installing dependency distro packages on current host
100    This flag should only be used if you have kept back older packages that
101    would be upgraded and that is undesirable on the current system.
102    Note that without the required packages, deploy will fail.
103 -s Deployment-scenario, this points to a short deployment scenario name, which
104    has to be defined in config directory (e.g. os-odl-nofeature-ha).
105 -S Storage dir for VM images, default is /var/lib/opnfv/tmpdir
106    It is recommended to store the deploy artifacts on a fast disk, outside of
107    the current git repository (so clean operations won't erase it).
108
109 $(notify_i "[NOTE] sudo & virsh priviledges are needed for this script to run" 3)
110
111 Example:
112
113 $(notify_i "sudo $(basename "$0") \\
114   -b file:///home/jenkins/securedlab \\
115   -l lf -p pod2 \\
116   -s os-odl-nofeature-ha \\
117   -S /home/jenkins/tmpdir" 2)
118 EOF
119 }
120
121 #
122 # END of usage description
123 ##############################################################################
124
125 ##############################################################################
126 # BEGIN of variables to customize
127 #
128 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
129 MCP_REPO_ROOT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")
130 DEPLOY_DIR=$(cd "${MCP_REPO_ROOT_PATH}/mcp/scripts"; pwd)
131 MCP_STORAGE_DIR='/var/lib/opnfv/tmpdir'
132 URI_REGEXP='(file|https?|ftp)://.*'
133 BASE_CONFIG_URI="file://${MCP_REPO_ROOT_PATH}/mcp/scripts/pharos"
134
135 # Customize deploy workflow
136 DRY_RUN=${DRY_RUN:-0}
137 USE_EXISTING_PKGS=${USE_EXISTING_PKGS:-0}
138 USE_EXISTING_INFRA=${USE_EXISTING_INFRA:-0}
139 NO_DEPLOY_ENVIRONMENT=${NO_DEPLOY_ENVIRONMENT:-0}
140 ERASE_ENV=${ERASE_ENV:-0}
141 MCP_VCP=${MCP_VCP:-1}
142 MCP_DOCKER_TAG=${MCP_DOCKER_TAG:-latest}
143 MCP_CMP_SS=${MCP_CMP_SS:-0}
144
145 source "${DEPLOY_DIR}/globals.sh"
146 source "${DEPLOY_DIR}/lib.sh"
147 source "${DEPLOY_DIR}/lib_template.sh"
148 source "${DEPLOY_DIR}/lib_jump_common.sh"
149 source "${DEPLOY_DIR}/lib_jump_deploy.sh"
150
151 #
152 # END of variables to customize
153 ##############################################################################
154
155 ##############################################################################
156 # BEGIN of main
157 #
158 set +x
159 while getopts "b:dDfEFl:L:Np:Ps:S:he" OPTION
160 do
161     case $OPTION in
162         b)
163             BASE_CONFIG_URI=${OPTARG}
164             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
165                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI"
166                 usage
167                 exit 1
168             fi
169             ;;
170         d)
171             DRY_RUN=1
172             ;;
173         D)
174             CI_DEBUG=1
175             ;;
176         f)
177             ((USE_EXISTING_INFRA+=1))
178             ;;
179         F|e)
180             NO_DEPLOY_ENVIRONMENT=1
181             ;;
182         E)
183             ((ERASE_ENV+=1))
184             ;;
185         l)
186             TARGET_LAB=${OPTARG}
187             ;;
188         L)
189             DEPLOY_LOG="${OPTARG}"
190             ;;
191         m)
192             MCP_CMP_SS=1
193             ;;
194         N)
195             MCP_VCP=0
196             ;;
197         p)
198             TARGET_POD=${OPTARG}
199             ;;
200         P)
201             USE_EXISTING_PKGS=1
202             ;;
203         s)
204             DEPLOY_SCENARIO=${OPTARG}
205             ;;
206         S)
207             if [[ ${OPTARG} ]]; then
208                 MCP_STORAGE_DIR="${OPTARG}"
209             fi
210             ;;
211         h)
212             usage
213             exit 0
214             ;;
215         *)
216             notify_e "[ERROR] Unsupported arg, see -h for help"
217             ;;
218     esac
219 done
220
221 if [[ "$(sudo whoami)" != 'root' ]]; then
222     notify_e "[ERROR] This script requires sudo rights"
223 fi
224
225 # Validate mandatory arguments are set
226 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
227    [ -z "${DEPLOY_SCENARIO}" ]; then
228     usage
229     notify_e "[ERROR] At least one of the mandatory args is missing!"
230 fi
231
232 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
233
234 # Enable the automatic exit trap
235 trap do_exit SIGINT SIGTERM EXIT
236
237 # Set no restrictive umask so that Jenkins can remove any residuals
238 umask 0000
239
240 pushd "${DEPLOY_DIR}" > /dev/null
241 # Prepare the deploy config files based on lab/pod information, deployment
242 # scenario, etc.
243
244 # Install required packages on jump server
245 sudo mkdir -p "${MCP_STORAGE_DIR}"
246 sudo chown -R "${USER}:${USER}" "${MCP_STORAGE_DIR}"
247 if [ ${USE_EXISTING_PKGS} -eq 1 ]; then
248     notify "[NOTE] Skipping distro pkg installation" 2
249 else
250     notify "[NOTE] Installing required distro pkgs" 2
251     jumpserver_pkg_install 'deploy'
252     docker_install "${MCP_STORAGE_DIR}"
253     virtinst_install "${MCP_STORAGE_DIR}"
254 fi
255
256 if ! ${VIRSH} list >/dev/null 2>&1; then
257     notify_e "[ERROR] This script requires hypervisor access"
258 fi
259
260 # Collect jump server system information for deploy debugging
261 ./sysinfo_print.sh
262
263 # Clone git submodules and apply our patches
264 make -C "${MCP_REPO_ROOT_PATH}/mcp/patches" deepclean patches-import
265
266 # Check scenario file existence
267 SCENARIO_DIR="$(readlink -f "../config/scenario")"
268 if [ ! -f "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml" ] && \
269    [ ! -f "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml.j2" ]; then
270     notify_e "[ERROR] Scenario definition file is missing!"
271 fi
272
273 # key might not exist yet ...
274 generate_ssh_key
275 export MAAS_SSH_KEY="$(cat "$(basename "${SSH_KEY}").pub")"
276
277 # Expand jinja2 templates based on PDF data and env vars
278 [[ "${DEPLOY_SCENARIO}" =~ -ha$ ]] || MCP_VCP=0
279 export MCP_REPO_ROOT_PATH MCP_VCP MCP_STORAGE_DIR MCP_DOCKER_TAG MCP_CMP_SS \
280        MCP_JUMP_ARCH=$(uname -i) MCP_DEPLOY_SCENARIO="${DEPLOY_SCENARIO}"
281 do_templates_scenario "${MCP_STORAGE_DIR}" "${TARGET_LAB}" "${TARGET_POD}" \
282                       "${BASE_CONFIG_URI}" "${SCENARIO_DIR}" \
283                       "${SCENARIO_DIR}/${DEPLOY_SCENARIO}.yaml"
284 do_templates_cluster  "${MCP_STORAGE_DIR}" "${TARGET_LAB}" "${TARGET_POD}" \
285                       "${MCP_REPO_ROOT_PATH}" \
286                       "${SCENARIO_DIR}/defaults.yaml"
287
288 # Determine additional data (e.g. jump bridge names) based on XDF
289 source "${DEPLOY_DIR}/xdf_data.sh"
290
291 # Jumpserver prerequisites check
292 notify "[NOTE] Using bridges: ${OPNFV_BRIDGES[*]}" 2
293 jumpserver_check_requirements "${virtual_nodes[*]}" "${OPNFV_BRIDGES[@]}"
294
295 # Infra setup
296 if [ ${DRY_RUN} -eq 1 ]; then
297     notify "[NOTE] Dry run, skipping all deployment tasks" 2
298     exit 0
299 elif [ ${USE_EXISTING_INFRA} -gt 0 ]; then
300     notify "[NOTE] Use existing infra: skip first ${USE_EXISTING_INFRA} states" 2
301     notify "[STATE] Skipping: ${cluster_states[*]::${USE_EXISTING_INFRA}}" 2
302 else
303     prepare_vms "${base_image}" "${MCP_STORAGE_DIR}" "${virtual_repos_pkgs}" \
304       "${virtual_nodes[@]}"
305     create_networks "${OPNFV_BRIDGES[@]}"
306     do_sysctl_cfg
307     do_udev_cfg
308     create_vms "${MCP_STORAGE_DIR}" "${virtual_nodes_data}" "${OPNFV_BRIDGES[@]}"
309     update_mcpcontrol_network
310     start_vms "${virtual_nodes[@]}"
311
312     # https://github.com/docker/libnetwork/issues/1743
313     # rm -f /var/lib/docker/network/files/local-kv.db
314     sudo systemctl restart docker
315     prepare_containers "${MCP_STORAGE_DIR}"
316 fi
317
318 start_containers "${MCP_STORAGE_DIR}"
319 check_connection
320
321 # Openstack cluster setup
322 set +x
323 if [ ${NO_DEPLOY_ENVIRONMENT} -eq 1 ]; then
324     notify "[NOTE] Skip openstack cluster setup" 2
325 else
326     for state in "${cluster_states[@]:${USE_EXISTING_INFRA}}"; do
327         notify "[STATE] Applying state: ${state}" 2
328         # shellcheck disable=SC2086,2029
329         wait_for 5 "ssh ${SSH_OPTS} ${SSH_SALT} sudo \
330             CI_DEBUG=$CI_DEBUG ERASE_ENV=$ERASE_ENV \
331             /root/fuel/mcp/config/states/${state}"
332         if [ "${state}" = 'maas' ]; then
333             # For hybrid PODs (virtual + baremetal nodes), the virtual nodes
334             # should be reset to force a DHCP request from MaaS DHCP
335             reset_vms "${virtual_nodes[@]}"
336         fi
337     done
338
339     ./log.sh "${DEPLOY_LOG}"
340 fi
341
342 popd > /dev/null
343
344 #
345 # END of main
346 ##############################################################################