docker build, deploy: Switch tooling to python3
[fuel.git] / mcp / scripts / lib.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2155,SC2015
3 ##############################################################################
4 # Copyright (c) 2018 Mirantis Inc., Enea AB and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 #
11 # Library of common shell functions used by build/deploy scripts, states etc.
12 #
13
14 function wait_for {
15   # Execute in a subshell to prevent local variable override during recursion
16   (
17     local total_attempts=$1; shift
18     local cmdstr=$*
19     local sleep_time=10
20     echo -e "\n[wait_for] Waiting for cmd to return success: ${cmdstr}"
21     # shellcheck disable=SC2034
22     for attempt in $(seq "${total_attempts}"); do
23       echo "[wait_for] Attempt ${attempt}/${total_attempts%.*} for: ${cmdstr}"
24       if [ "${total_attempts%.*}" = "${total_attempts}" ]; then
25         eval "${cmdstr}" && echo "[wait_for] OK: ${cmdstr}" && return 0 || true
26       else
27         ! (eval "${cmdstr}" || echo 'No response') |& tee /dev/stderr | \
28            grep -Eq '(Not connected|No response)' && \
29            echo "[wait_for] OK: ${cmdstr}" && return 0 || true
30       fi
31       sleep "${sleep_time}"
32     done
33     echo "[wait_for] ERROR: Failed after max attempts: ${cmdstr}"
34     return 1
35   )
36 }
37
38 function cleanup_uefi {
39   # Clean up Ubuntu boot entry if cfg01, baremetal nodes online from previous deploy
40   local cmd_str="ssh ${SSH_OPTS} ${SSH_SALT}"
41   ping -c 1 -w 1 "${SALT_MASTER}" || return 0
42   [ ! "$(hostname)" = 'cfg01' ] || cmd_str='eval'
43   ${cmd_str} "sudo salt -C 'G@virtual:physical and not cfg01*' cmd.run \
44     \"which efibootmgr > /dev/null 2>&1 && \
45     efibootmgr | grep -oP '(?<=Boot)[0-9]+(?=.*ubuntu)' | \
46     xargs -I{} efibootmgr --delete-bootnum --bootnum {}; \
47     rm -rf /boot/efi/*\"" || true
48
49   ${cmd_str} "sudo salt -C 'G@virtual:physical and not cfg01*' cmd.run 'shutdown now'" || true
50 }
51
52 function get_nova_compute_pillar_data {
53   local value=$(salt -C 'I@nova:compute and *01*' pillar.get _param:"${1}" --out yaml | cut -d ' ' -f2)
54   if [ "${value}" != "''" ]; then
55     echo "${value}"
56   fi
57 }