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