aboutsummaryrefslogtreecommitdiffstats
path: root/mcp/scripts/lib.sh
blob: 85360bf2ea8ff45d33e70d9413757fba2bdaa0d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash -e
# shellcheck disable=SC2155,SC2015
##############################################################################
# Copyright (c) 2018 Mirantis Inc., Enea AB and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
#
# Library of common shell functions used by build/deploy scripts, states etc.
#

function wait_for {
  # Execute in a subshell to prevent local variable override during recursion
  (
    local total_attempts=$1; shift
    local cmdstr=$*
    local sleep_time=10
    echo -e "\n[wait_for] Waiting for cmd to return success: ${cmdstr}"
    # shellcheck disable=SC2034
    for attempt in $(seq "${total_attempts}"); do
      echo "[wait_for] Attempt ${attempt}/${total_attempts%.*} for: ${cmdstr}"
      if [ "${total_attempts%.*}" = "${total_attempts}" ]; then
        eval "${cmdstr}" && echo "[wait_for] OK: ${cmdstr}" && return 0 || true
      else
        ! (eval "${cmdstr}" || echo 'No response') |& tee /dev/stderr | \
           grep -Eq '(Not connected|No response)' && \
           echo "[wait_for] OK: ${cmdstr}" && return 0 || true
      fi
      sleep "${sleep_time}"
    done
    echo "[wait_for] ERROR: Failed after max attempts: ${cmdstr}"
    return 1
  )
}

function cleanup_uefi {
  # Clean up Ubuntu boot entry if cfg01, kvm nodes online from previous deploy
  local cmd_str="ssh ${SSH_OPTS} ${SSH_SALT}"
  local grain_virtual=$(salt -C 'I@nova:compute and *01*' grains.get virtual --out txt | cut -d ' ' -f2)
  ping -c 1 -w 1 "${SALT_MASTER}" || return 0
  [ ! "$(hostname)" = 'cfg01' ] || cmd_str='eval'
  # NOTE: Targeting nodes by hostname is fragile and should be refactored to
  # also cover corner cases like noha scenarios on AArch64 baremetal's 'ctl01'
  ${cmd_str} "sudo salt -C 'kvm* or cmp*' cmd.run \
    \"which efibootmgr > /dev/null 2>&1 && \
    efibootmgr | grep -oP '(?<=Boot)[0-9]+(?=.*ubuntu)' | \
    xargs -I{} efibootmgr --delete-bootnum --bootnum {}; \
    rm -rf /boot/efi/*\"" || true

  if [ "${grain_virtual}" == physical ]; then
    ${cmd_str} "sudo salt -C 'kvm* or cmp*' cmd.run 'shutdown now'" || true
  fi
}

function get_nova_compute_pillar_data {
  local value=$(salt -C 'I@nova:compute and *01*' pillar.get _param:"${1}" --out yaml | cut -d ' ' -f2)
  if [ "${value}" != "''" ]; then
    echo "${value}"
  fi
}