Merge "deploy.sh: Disable net.bridge.bridge-nf-call"
[fuel.git] / ci / deploy.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2034,SC2154,SC1090,SC1091
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!\n\n" 2
20     else
21         notify "\n[ERROR] MCP: Openstack installation threw a fatal error!\n\n"
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 "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 "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    An example config is provided inside current repo in
74    <./mcp/config>.
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. 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 "[NOTE] sudo & virsh priviledges are needed for this script to run" 3)
102
103 Example:
104
105 $(notify "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 colored notification wrapper
118 #
119 notify() {
120     tput setaf "${2:-1}" || true
121     echo -en "${1:-"[WARN] Unsupported opt arg: $3\\n"}"
122     tput sgr0
123 }
124 #
125 # END of colored notification wrapper
126 ##############################################################################
127
128 ##############################################################################
129 # BEGIN of variables to customize
130 #
131 CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
132 REPO_ROOT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")
133 DEPLOY_DIR=$(cd "${REPO_ROOT_PATH}/mcp/scripts"; pwd)
134 STORAGE_DIR=$(cd "${REPO_ROOT_PATH}/mcp/deploy/images"; pwd)
135 RECLASS_CLUSTER_DIR=$(cd "${REPO_ROOT_PATH}/mcp/reclass/classes/cluster"; pwd)
136 DEPLOY_TYPE='baremetal'
137 BR_NAMES=('admin' 'mgmt' 'private' 'public')
138 OPNFV_BRIDGES=('pxebr' 'mgmt' 'internal' 'public')
139 URI_REGEXP='(file|https?|ftp)://.*'
140 BASE_CONFIG_URI="file://${REPO_ROOT_PATH}/mcp/scripts/pharos"
141
142 # Customize deploy workflow
143 DRY_RUN=${DRY_RUN:-0}
144 USE_EXISTING_PKGS=${USE_EXISTING_PKGS:-0}
145 USE_EXISTING_INFRA=${USE_EXISTING_INFRA:-0}
146 INFRA_CREATION_ONLY=${INFRA_CREATION_ONLY:-0}
147 NO_DEPLOY_ENVIRONMENT=${NO_DEPLOY_ENVIRONMENT:-0}
148 ERASE_ENV=${ERASE_ENV:-0}
149
150 source "${DEPLOY_DIR}/globals.sh"
151 source "${DEPLOY_DIR}/lib.sh"
152
153 #
154 # END of variables to customize
155 ##############################################################################
156
157 ##############################################################################
158 # BEGIN of main
159 #
160 set +x
161 while getopts "b:dDfEFl:L:p:Ps:S:he" OPTION
162 do
163     case $OPTION in
164         b)
165             BASE_CONFIG_URI=${OPTARG}
166             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
167                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI\n"
168                 usage
169                 exit 1
170             fi
171             ;;
172         d)
173             DRY_RUN=1
174             ;;
175         D)
176             CI_DEBUG=1
177             ;;
178         f)
179             ((USE_EXISTING_INFRA+=1))
180             ;;
181         F)
182             INFRA_CREATION_ONLY=1
183             ;;
184         e)
185             NO_DEPLOY_ENVIRONMENT=1
186             ;;
187         E)
188             ((ERASE_ENV+=1))
189             ;;
190         l)
191             TARGET_LAB=${OPTARG}
192             ;;
193         L)
194             DEPLOY_LOG="${OPTARG}"
195             ;;
196         p)
197             TARGET_POD=${OPTARG}
198             if [[ "${TARGET_POD}" =~ "virtual" ]]; then
199                 DEPLOY_TYPE='virtual'
200             fi
201             ;;
202         P)
203             USE_EXISTING_PKGS=1
204             ;;
205         s)
206             DEPLOY_SCENARIO=${OPTARG}
207             ;;
208         S)
209             if [[ ${OPTARG} ]]; then
210                 STORAGE_DIR="${OPTARG}"
211             fi
212             ;;
213         h)
214             usage
215             exit 0
216             ;;
217         *)
218             notify "[ERROR] Arguments not according to new argument style\n"
219             exit 1
220             ;;
221     esac
222 done
223
224 if [[ "$(sudo whoami)" != 'root' ]]; then
225     notify "[ERROR] This script requires sudo rights\n" 1>&2
226     exit 1
227 fi
228
229 # Validate mandatory arguments are set
230 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
231    [ -z "${DEPLOY_SCENARIO}" ]; then
232     notify "[ERROR] At least one of the mandatory args is missing!\n" 1>&2
233     usage
234     exit 1
235 fi
236
237 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
238
239 # Enable the automatic exit trap
240 trap do_exit SIGINT SIGTERM EXIT
241
242 # Set no restrictive umask so that Jenkins can remove any residuals
243 umask 0000
244
245 pushd "${DEPLOY_DIR}" > /dev/null
246 # Prepare the deploy config files based on lab/pod information, deployment
247 # scenario, etc.
248
249 # Install required packages on jump server
250 if [ ${USE_EXISTING_PKGS} -eq 1 ]; then
251     notify "[NOTE] Skipping distro pkg installation\n" 2 1>&2
252 else
253     notify "[NOTE] Installing required distro pkgs\n" 2 1>&2
254     if [ -n "$(command -v apt-get)" ]; then
255       pkg_type='deb'; pkg_cmd='sudo apt-get install -y'
256     else
257       pkg_type='rpm'; pkg_cmd='sudo yum install -y --skip-broken'
258     fi
259     eval "$(parse_yaml "./requirements_${pkg_type}.yaml")"
260     for section in 'common' "${DEPLOY_TYPE}" "$(uname -m)"; do
261       section_var="requirements_pkg_${section}[*]"
262       pkg_list+=" ${!section_var}"
263     done
264     # shellcheck disable=SC2086
265     ${pkg_cmd} ${pkg_list}
266 fi
267
268 if ! virsh list >/dev/null 2>&1; then
269     notify "[ERROR] This script requires hypervisor access\n" 1>&2
270     exit 1
271 fi
272
273 # Collect jump server system information for deploy debugging
274 ./sysinfo_print.sh
275
276 # Clone git submodules and apply our patches
277 make -C "${REPO_ROOT_PATH}/mcp/patches" deepclean patches-import
278
279 # Convert Pharos-compatible POD Descriptor File (PDF) to reclass model input
280 PHAROS_GEN_CONFIG_SCRIPT="./pharos/config/utils/generate_config.py"
281 PHAROS_INSTALLER_ADAPTER="./pharos/config/installers/fuel/pod_config.yml.j2"
282 BASE_CONFIG_PDF="${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}.yaml"
283 BASE_CONFIG_IDF="${BASE_CONFIG_URI}/labs/${TARGET_LAB}/idf-${TARGET_POD}.yaml"
284 LOCAL_PDF="${STORAGE_DIR}/$(basename "${BASE_CONFIG_PDF}")"
285 LOCAL_IDF="${STORAGE_DIR}/$(basename "${BASE_CONFIG_IDF}")"
286 LOCAL_PDF_RECLASS="${STORAGE_DIR}/pod_config.yml"
287 if ! curl --create-dirs -o "${LOCAL_PDF}" "${BASE_CONFIG_PDF}"; then
288     if [ "${DEPLOY_TYPE}" = 'baremetal' ]; then
289         notify "[ERROR] Could not retrieve PDF (Pod Descriptor File)!\n" 1>&2
290         exit 1
291     else
292         notify "[WARN] Could not retrieve PDF (Pod Descriptor File)!\n" 3
293     fi
294 elif ! curl -o "${LOCAL_IDF}" "${BASE_CONFIG_IDF}"; then
295     notify "[WARN] POD has no IDF (Installer Descriptor File)!\n" 3
296 elif ! "${PHAROS_GEN_CONFIG_SCRIPT}" -y "${LOCAL_PDF}" \
297     -j "${PHAROS_INSTALLER_ADAPTER}" > "${LOCAL_PDF_RECLASS}"; then
298     notify "[ERROR] Could not convert PDF to reclass model input!\n" 1>&2
299     exit 1
300 fi
301
302 # Check scenario file existence
303 SCENARIO_DIR="../config/scenario"
304 if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
305     notify "[ERROR] Scenario definition file is missing!\n" 1>&2
306     exit 1
307 fi
308
309 # Check defaults file existence
310 if [ ! -f  "${SCENARIO_DIR}/defaults-$(uname -i).yaml" ]; then
311     notify "[ERROR] Scenario defaults file is missing!\n" 1>&2
312     exit 1
313 fi
314
315 # Get required infra deployment data
316 set +x
317 eval "$(parse_yaml "${SCENARIO_DIR}/defaults-$(uname -i).yaml")"
318 eval "$(parse_yaml "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml")"
319 eval "$(parse_yaml "${LOCAL_PDF_RECLASS}")"
320 [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
321
322 export CLUSTER_DOMAIN=${cluster_domain}
323
324 # Serialize vnode data as '<name0>,<ram0>,<vcpu0>|<name1>,<ram1>,<vcpu1>[...]'
325 for node in "${virtual_nodes[@]}"; do
326     virtual_custom_ram="virtual_${node}_ram"
327     virtual_custom_vcpus="virtual_${node}_vcpus"
328     virtual_nodes_data+="${node},"
329     virtual_nodes_data+="${!virtual_custom_ram:-$virtual_default_ram},"
330     virtual_nodes_data+="${!virtual_custom_vcpus:-$virtual_default_vcpus}|"
331 done
332 virtual_nodes_data=${virtual_nodes_data%|}
333
334 # Serialize repos, packages to (pre-)install/remove for:
335 # - foundation node VM base image (virtual: all VMs, baremetal: cfg01|mas01)
336 # - virtualized control plane VM base image (only when VCP is used)
337 base_image_flavors=common
338 if [[ "${cluster_states[*]}" =~ virtual_control ]]; then
339   base_image_flavors+=" control"
340 fi
341 for sc in ${base_image_flavors}; do
342   for va in apt_keys apt_repos pkg_install pkg_remove; do
343     key=virtual_${sc}_${va}
344     eval "${key}=\${${key}[@]// /|}"
345     eval "${key}=\${${key}// /,}"
346     virtual_repos_pkgs+="${!key}^"
347   done
348 done
349 virtual_repos_pkgs=${virtual_repos_pkgs%^}
350
351 # Expand reclass and virsh network templates
352 for tp in "${RECLASS_CLUSTER_DIR}/all-mcp-arch-common/opnfv/"*.template \
353     net_*.template; do
354         eval "cat <<-EOF
355                 $(<"${tp}")
356                 EOF" 2> /dev/null > "${tp%.template}"
357 done
358
359 # Convert Pharos-compatible PDF to reclass network definitions
360 if [ "${DEPLOY_TYPE}" = 'baremetal' ]; then
361     find "${RECLASS_CLUSTER_DIR}" -name '*.j2' | while read -r tp
362     do
363         if ! "${PHAROS_GEN_CONFIG_SCRIPT}" -y "${LOCAL_PDF}" \
364           -j "${tp}" > "${tp%.j2}"; then
365              notify "[ERROR] Could not convert PDF to reclass network defs!\n"
366              exit 1
367         fi
368     done
369 fi
370
371 # Determine 'admin', 'mgmt', 'private' and 'public' bridge names based on IDF
372 for ((i = 0; i < ${#BR_NAMES[@]}; i++)); do
373     br_jump=$(eval echo "\$parameters__param_opnfv_jump_bridge_${BR_NAMES[i]}")
374     if [ -n "${br_jump}" ] && [ "${br_jump}" != 'None' ]; then
375         OPNFV_BRIDGES[${i}]="${br_jump}"
376     fi
377 done
378 notify "[NOTE] Using bridges: ${OPNFV_BRIDGES[*]}\n" 2
379
380 # Infra setup
381 if [ ${DRY_RUN} -eq 1 ]; then
382     notify "[NOTE] Dry run, skipping all deployment tasks\n" 2 1>&2
383     exit 0
384 elif [ ${USE_EXISTING_INFRA} -gt 0 ]; then
385     notify "[NOTE] Use existing infra\n" 2 1>&2
386     check_connection
387 else
388     generate_ssh_key
389     prepare_vms "${base_image}" "${STORAGE_DIR}" "${virtual_repos_pkgs}" \
390       "${virtual_nodes[@]}"
391     create_networks "${OPNFV_BRIDGES[@]}"
392     do_sysctl_cfg
393     create_vms "${STORAGE_DIR}" "${virtual_nodes_data}" "${OPNFV_BRIDGES[@]}"
394     update_mcpcontrol_network
395     start_vms "${virtual_nodes[@]}"
396     check_connection
397 fi
398 if [ ${USE_EXISTING_INFRA} -lt 2 ]; then
399     wait_for 5 "./salt.sh ${LOCAL_PDF_RECLASS}"
400 fi
401
402 # Openstack cluster setup
403 set +x
404 if [ ${INFRA_CREATION_ONLY} -eq 1 ] || [ ${NO_DEPLOY_ENVIRONMENT} -eq 1 ]; then
405     notify "[NOTE] Skip openstack cluster setup\n" 2
406 else
407     for state in "${cluster_states[@]}"; do
408         notify "[STATE] Applying state: ${state}\n" 2
409         # shellcheck disable=SC2086,2029
410         wait_for 5 "ssh ${SSH_OPTS} ${SSH_SALT} sudo \
411             CI_DEBUG=$CI_DEBUG ERASE_ENV=$ERASE_ENV \
412             /root/fuel/mcp/config/states/${state}"
413     done
414 fi
415
416 ./log.sh "${DEPLOY_LOG}"
417
418 popd > /dev/null
419
420 #
421 # END of main
422 ##############################################################################