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