Merge "ci/deploy.sh: Move global vars to separate file"
[fuel.git] / ci / deploy.sh
1 #!/bin/bash
2 # shellcheck disable=SC2034,SC2154,SC1091
3 set -ex
4 ##############################################################################
5 # Copyright (c) 2017 Ericsson AB, Mirantis Inc., Enea AB and others.
6 # jonas.bjurel@ericsson.com
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Apache License, Version 2.0
9 # which accompanies this distribution, and is available at
10 # http://www.apache.org/licenses/LICENSE-2.0
11 ##############################################################################
12
13 ##############################################################################
14 # BEGIN of Exit handlers
15 #
16 do_exit () {
17     clean
18     echo "Exiting ..."
19 }
20 #
21 # End of Exit handlers
22 ##############################################################################
23
24 ##############################################################################
25 # BEGIN of usage description
26 #
27 usage ()
28 {
29 cat << EOF
30 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
31 $(notify "$(basename "$0"): Deploy the Fuel@OPNFV MCP stack" 3)
32
33 $(notify "USAGE:" 2)
34   $(basename "$0") -b base-uri -l lab-name -p pod-name -s deploy-scenario \\
35     [-B PXE Bridge [-B Mgmt Bridge [-B Internal Bridge [-B Public Bridge]]]] \\
36     [-S storage-dir] [-L /path/to/log/file.tar.gz] [-f] [-F] [-e] [-d]
37
38 $(notify "OPTIONS:" 2)
39   -b  Base-uri for the stack-configuration structure
40   -B  Bridge(s): 1st usage = PXE, 2nd = Mgmt, 3rd = Internal, 4th = Public
41   -d  Dry-run
42   -e  Do not launch environment deployment
43   -f  Deploy on existing Salt master
44   -F  Do only create a Salt master
45   -h  Print this message and exit
46   -l  Lab-name
47   -p  Pod-name
48   -s  Deploy-scenario short-name
49   -S  Storage dir for VM images
50   -L  Deployment log path and file name
51
52 $(notify "DISABLED OPTIONS (not yet supported with MCP):" 3)
53   -i  (disabled) iso url
54   -T  (disabled) Timeout, in minutes, for the deploy.
55
56 $(notify "Description:" 2)
57 Deploys the Fuel@OPNFV stack on the indicated lab resource.
58
59 This script provides the Fuel@OPNFV deployment abstraction.
60 It depends on the OPNFV official configuration directory/file structure
61 and provides a fairly simple mechanism to execute a deployment.
62
63 $(notify "Input parameters to the build script are:" 2)
64 -b Base URI to the configuration directory (needs to be provided in URI style,
65    it can be a local resource: file:// or a remote resource http(s)://).
66    A POD Descriptor File (PDF) should be available at:
67    <base-uri>/labs/<lab-name>/<pod-name>.yaml
68    The default is './mcp/config'.
69 -B Bridges to be used by deploy script. It can be specified several times,
70    or as a comma separated list of bridges, or both: -B br1 -B br2,br3
71    First occurence sets PXE Brige, next Mgmt, then Internal and Public.
72    For an empty value, the deploy script will use virsh to create the default
73    expected network (e.g. -B pxe,,,public will use existing "pxe" and "public"
74    bridges, respectively create "mgmt" and "internal").
75    Note that a virtual network "mcpcontrol" is always created. For virtual
76    deploys, "mcpcontrol" is also used for PXE, leaving the PXE bridge unused.
77    For baremetal deploys, PXE bridge is used for baremetal node provisioning,
78    while "mcpcontrol" is used to provision the infrastructure VMs only.
79    The default is 'pxebr'.
80 -d Dry-run - Produce deploy config files, but do not execute deploy
81 -e Do not launch environment deployment
82 -f Deploy on existing Salt master
83 -F Do only create a Salt master
84 -h Print this message and exit
85 -L Deployment log path and name, eg. -L /home/jenkins/job.log.tar.gz
86 -l Lab name as defined in the configuration directory, e.g. lf
87 -p POD name as defined in the configuration directory, e.g. pod2
88 -s Deployment-scenario, this points to a short deployment scenario name, which
89    has to be defined in config directory (e.g. os-odl-nofeature-ha).
90 -S Storage dir for VM images, default is mcp/deploy/images
91
92 $(notify "Disabled input parameters (not yet supported with MCP):" 3)
93 -T (disabled) Timeout, in minutes, for the deploy.
94    It defaults to using the DEPLOY_TIMEOUT environment variable when defined.
95 -i (disabled) .iso image to be deployed (needs to be provided in a URI
96    style, it can be a local resource: file:// or a remote resource http(s)://)
97
98 $(notify "[NOTE] sudo & virsh priviledges are needed for this script to run" 3)
99
100 Example:
101
102 $(notify "sudo $(basename "$0") \\
103   -b file:///home/jenkins/securedlab \\
104   -l lf -p pod2 \\
105   -s os-odl-nofeature-ha" 2)
106 EOF
107 }
108
109 #
110 # END of usage description
111 ##############################################################################
112
113 ##############################################################################
114 # BEGIN of colored notification wrapper
115 #
116 notify() {
117     tput setaf "${2:-1}" || true
118     echo -en "${1:-"[WARN] Unsupported opt arg: $3\\n"}"
119     tput sgr0
120 }
121 #
122 # END of colored notification wrapper
123 ##############################################################################
124
125 ##############################################################################
126 # BEGIN of deployment clean-up
127 #
128 clean() {
129     echo "Cleaning up deploy tmp directories"
130 }
131 #
132 # END of deployment clean-up
133 ##############################################################################
134
135 ##############################################################################
136 # BEGIN of variables to customize
137 #
138 REPO_ROOT_PATH=$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")
139 DEPLOY_DIR=$(cd "${REPO_ROOT_PATH}/mcp/scripts"; pwd)
140 STORAGE_DIR=$(cd "${REPO_ROOT_PATH}/mcp/deploy/images"; pwd)
141 RECLASS_CLUSTER_DIR=$(cd "${REPO_ROOT_PATH}/mcp/reclass/classes/cluster"; pwd)
142 DEPLOY_TYPE='baremetal'
143 OPNFV_BRIDGES=('pxebr' 'mgmt' 'internal' 'public')
144 URI_REGEXP='(file|https?|ftp)://.*'
145 BASE_CONFIG_URI="file://${REPO_ROOT_PATH}/mcp/config"
146
147 # Customize deploy workflow
148 DRY_RUN=${DRY_RUN:-0}
149 USE_EXISTING_INFRA=${USE_EXISTING_INFRA:-0}
150 INFRA_CREATION_ONLY=${INFRA_CREATION_ONLY:-0}
151 NO_DEPLOY_ENVIRONMENT=${NO_DEPLOY_ENVIRONMENT:-0}
152
153 source "${DEPLOY_DIR}/globals.sh"
154
155 # Variables below are disabled for now, to be re-introduced or removed later
156 set +x
157 if ! [ -z "${DEPLOY_TIMEOUT}" ]; then
158     DEPLOY_TIMEOUT="-dt ${DEPLOY_TIMEOUT}"
159 else
160     DEPLOY_TIMEOUT=""
161 fi
162 set -x
163 #
164 # END of variables to customize
165 ##############################################################################
166
167 ##############################################################################
168 # BEGIN of main
169 #
170 set +x
171 OPNFV_BRIDGE_IDX=0
172 while getopts "b:B:dfFl:L:p:s:S:T:i:he" OPTION
173 do
174     case $OPTION in
175         b)
176             BASE_CONFIG_URI=${OPTARG}
177             if [[ ! $BASE_CONFIG_URI =~ ${URI_REGEXP} ]]; then
178                 notify "[ERROR] -b $BASE_CONFIG_URI - invalid URI\n"
179                 usage
180                 exit 1
181             fi
182             ;;
183         B)
184             OIFS=${IFS}
185             IFS=','
186             OPT_BRIDGES=($OPTARG)
187             for bridge in "${OPT_BRIDGES[@]}"; do
188                 if [ -n "${bridge}" ]; then
189                     OPNFV_BRIDGES[${OPNFV_BRIDGE_IDX}]="${bridge}"
190                 fi
191                 OPNFV_BRIDGE_IDX=$((OPNFV_BRIDGE_IDX + 1))
192             done
193             IFS=${OIFS}
194             ;;
195         d)
196             DRY_RUN=1
197             ;;
198         f)
199             USE_EXISTING_INFRA=1
200             ;;
201         F)
202             INFRA_CREATION_ONLY=1
203             ;;
204         e)
205             NO_DEPLOY_ENVIRONMENT=1
206             ;;
207         l)
208             TARGET_LAB=${OPTARG}
209             ;;
210         L)
211             DEPLOY_LOG="${OPTARG}"
212             ;;
213         p)
214             TARGET_POD=${OPTARG}
215             if [[ "${TARGET_POD}" =~ "virtual" ]]; then
216                 DEPLOY_TYPE='virtual'
217             fi
218             ;;
219         s)
220             DEPLOY_SCENARIO=${OPTARG}
221             ;;
222         S)
223             if [[ ${OPTARG} ]]; then
224                 STORAGE_DIR="${OPTARG}"
225             fi
226             ;;
227         T)
228             notify '' 3 "${OPTION}"; continue
229             DEPLOY_TIMEOUT="-dt ${OPTARG}"
230             ;;
231         i)
232             notify '' 3 "${OPTION}"; continue
233             ISO=${OPTARG}
234             if [[ ! $ISO =~ ${URI_REGEXP} ]]; then
235                 notify "[ERROR] -i $ISO - invalid URI\n"
236                 usage
237                 exit 1
238             fi
239             ;;
240         h)
241             usage
242             exit 0
243             ;;
244         *)
245             notify "[ERROR] Arguments not according to new argument style\n"
246             exit 1
247             ;;
248     esac
249 done
250
251 if [[ "$(sudo whoami)" != 'root' ]]; then
252     notify "This script requires sudo rights\n" 1>&2
253     exit 1
254 fi
255
256 if ! virsh list >/dev/null 2>&1; then
257     notify "This script requires hypervisor access\n" 1>&2
258     exit 1
259 fi
260
261 # Validate mandatory arguments are set
262 if [ -z "${TARGET_LAB}" ] || [ -z "${TARGET_POD}" ] || \
263    [ -z "${DEPLOY_SCENARIO}" ]; then
264     notify "[ERROR] At least one of the mandatory args is missing!\n" 1>&2
265     usage
266     exit 1
267 fi
268
269 set -x
270
271 # Enable the automatic exit trap
272 trap do_exit SIGINT SIGTERM EXIT
273
274 # Set no restrictive umask so that Jenkins can remove any residuals
275 umask 0000
276
277 clean
278
279 pushd "${DEPLOY_DIR}" > /dev/null
280 # Prepare the deploy config files based on lab/pod information, deployment
281 # scenario, etc.
282
283 # Install required packages
284 [ -n "$(command -v apt-get)" ] && sudo apt-get install -y \
285   git make rsync mkisofs curl virtinst cpu-checker qemu-kvm
286 [ -n "$(command -v yum)" ] && sudo yum install -y --skip-broken \
287   git make rsync genisoimage curl virt-install qemu-kvm
288
289 if [ "$(uname -i)" = "aarch64" ]; then
290   [ -n "$(command -v apt-get)" ] && sudo apt-get install -y vgabios && \
291   sudo ln -sf /usr/share/vgabios/vgabios.bin /usr/share/qemu/vgabios-stdvga.bin
292   [ -n "$(command -v yum)" ] && sudo yum install -y --skip-broken vgabios
293 fi
294
295 # Clone git submodules and apply our patches
296 make -C "${REPO_ROOT_PATH}/mcp/patches" deepclean patches-import
297
298 # Convert Pharos-compatible POD Descriptor File (PDF) to reclass model input
299 PHAROS_GEN_CONFIG_SCRIPT="./pharos/config/utils/generate_config.py"
300 PHAROS_INSTALLER_ADAPTER="./pharos/config/installers/fuel/pod_config.yml.j2"
301 BASE_CONFIG_PDF="${BASE_CONFIG_URI}/labs/${TARGET_LAB}/${TARGET_POD}.yaml"
302 BASE_CONFIG_IDF="${BASE_CONFIG_URI}/labs/${TARGET_LAB}/idf-${TARGET_POD}.yaml"
303 LOCAL_PDF="${STORAGE_DIR}/$(basename "${BASE_CONFIG_PDF}")"
304 LOCAL_IDF="${STORAGE_DIR}/$(basename "${BASE_CONFIG_IDF}")"
305 LOCAL_PDF_RECLASS="${STORAGE_DIR}/pod_config.yml"
306 if ! curl --create-dirs -o "${LOCAL_PDF}" "${BASE_CONFIG_PDF}"; then
307     if [ "${DEPLOY_TYPE}" = 'baremetal' ]; then
308         notify "[ERROR] Could not retrieve PDF (Pod Descriptor File)!\n" 1>&2
309         exit 1
310     else
311         notify "[WARN] Could not retrieve PDF (Pod Descriptor File)!\n" 3
312     fi
313 elif ! curl -o "${LOCAL_IDF}" "${BASE_CONFIG_IDF}"; then
314     notify "[WARN] POD has no IDF (Installer Descriptor File)!\n" 3
315 elif ! "${PHAROS_GEN_CONFIG_SCRIPT}" -y "${LOCAL_PDF}" \
316     -j "${PHAROS_INSTALLER_ADAPTER}" > "${LOCAL_PDF_RECLASS}"; then
317     notify "[ERROR] Could not convert PDF to reclass model input!\n" 1>&2
318     exit 1
319 fi
320
321 # Check scenario file existence
322 SCENARIO_DIR="../config/scenario"
323 if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
324     notify "[WARN] ${DEPLOY_SCENARIO}.yaml not found! \
325             Setting simplest scenario (os-nosdn-nofeature-noha)\n" 3
326     DEPLOY_SCENARIO='os-nosdn-nofeature-noha'
327     if [ ! -f  "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml" ]; then
328         notify "[ERROR] Scenario definition file is missing!\n" 1>&2
329         exit 1
330     fi
331 fi
332
333 # Check defaults file existence
334 if [ ! -f  "${SCENARIO_DIR}/defaults-$(uname -i).yaml" ]; then
335     notify "[ERROR] Scenario defaults file is missing!\n" 1>&2
336     exit 1
337 fi
338
339 # Get required infra deployment data
340 source lib.sh
341 eval "$(parse_yaml "${SCENARIO_DIR}/defaults-$(uname -i).yaml")"
342 eval "$(parse_yaml "${SCENARIO_DIR}/${DEPLOY_TYPE}/${DEPLOY_SCENARIO}.yaml")"
343 eval "$(parse_yaml "${LOCAL_PDF_RECLASS}")"
344
345 export CLUSTER_DOMAIN=${cluster_domain}
346
347 declare -A virtual_nodes_ram virtual_nodes_vcpus
348 for node in "${virtual_nodes[@]}"; do
349     virtual_custom_ram="virtual_${node}_ram"
350     virtual_custom_vcpus="virtual_${node}_vcpus"
351     virtual_nodes_ram[$node]=${!virtual_custom_ram:-$virtual_default_ram}
352     virtual_nodes_vcpus[$node]=${!virtual_custom_vcpus:-$virtual_default_vcpus}
353 done
354
355 # Expand reclass and virsh network templates
356 for tp in "${RECLASS_CLUSTER_DIR}/all-mcp-ocata-common/opnfv/"*.template \
357     net_*.template; do
358         eval "cat <<-EOF
359                 $(<"${tp}")
360                 EOF" 2> /dev/null > "${tp%.template}"
361 done
362
363 # Map PDF networks 'admin', 'mgmt', 'private' and 'public' to bridge names
364 BR_NAMES=('admin' 'mgmt' 'private' 'public')
365 BR_NETS=( \
366     "${parameters__param_opnfv_maas_pxe_address}" \
367     "${parameters__param_opnfv_infra_config_address}" \
368     "${parameters__param_opnfv_openstack_compute_node01_tenant_address}" \
369     "${parameters__param_opnfv_openstack_compute_node01_external_address}" \
370 )
371 for ((i = 0; i < ${#BR_NETS[@]}; i++)); do
372     br_jump=$(eval echo "\$parameters__param_opnfv_jump_bridge_${BR_NAMES[i]}")
373     if [ -n "${br_jump}" ] && [ "${br_jump}" != 'None' ] && \
374        [ -d "/sys/class/net/${br_jump}/bridge" ]; then
375             notify "[OK] Bridge found for '${BR_NAMES[i]}': ${br_jump}\n" 2
376             OPNFV_BRIDGES[${i}]="${br_jump}"
377     elif [ -n "${BR_NETS[i]}" ]; then
378         bridge=$(ip addr | awk "/${BR_NETS[i]%.*}./ {print \$NF; exit}")
379         if [ -n "${bridge}" ] && [ -d "/sys/class/net/${bridge}/bridge" ]; then
380             notify "[OK] Bridge found for net ${BR_NETS[i]%.*}.0: ${bridge}\n" 2
381             OPNFV_BRIDGES[${i}]="${bridge}"
382         fi
383     fi
384 done
385 notify "[NOTE] Using bridges: ${OPNFV_BRIDGES[*]}\n" 2
386
387 # Infra setup
388 if [ ${DRY_RUN} -eq 1 ]; then
389     notify "Dry run, skipping all deployment tasks\n" 2 1>&2
390     exit 0
391 elif [ ${USE_EXISTING_INFRA} -eq 1 ]; then
392     notify "Use existing infra\n" 2 1>&2
393     check_connection
394 else
395     generate_ssh_key
396     prepare_vms virtual_nodes "${base_image}" "${STORAGE_DIR}"
397     create_networks OPNFV_BRIDGES
398     create_vms virtual_nodes virtual_nodes_ram virtual_nodes_vcpus \
399       OPNFV_BRIDGES "${STORAGE_DIR}"
400     update_mcpcontrol_network
401     start_vms virtual_nodes
402     check_connection
403     ./salt.sh "${LOCAL_PDF_RECLASS}"
404 fi
405
406 if [ ${INFRA_CREATION_ONLY} -eq 1 ] || [ ${NO_DEPLOY_ENVIRONMENT} -eq 1 ]; then
407     notify "Skip openstack cluster setup\n" 2
408 else
409     # Openstack cluster setup
410     for state in "${cluster_states[@]}"; do
411         notify "STATE: ${state}\n" 2
412         # shellcheck disable=SC2086,2029
413         ssh ${SSH_OPTS} "ubuntu@${SALT_MASTER}" \
414             sudo "/root/fuel/mcp/config/states/${state} || true"
415     done
416 fi
417
418 ./log.sh "${DEPLOY_LOG}"
419
420 popd > /dev/null
421
422 #
423 # END of main
424 ##############################################################################