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