-#!/bin/bash
-function wait_for() {
- local cmdstr=$@
- local total_attempts=360
- local sleep_time=10
- local attempt=1
- echo "[NOTE] Waiting for cmd to return success: ${cmdstr}"
- while ((attempt <= total_attempts)); do
- eval "${cmdstr}" && break || true
- echo -n '.'; sleep "${sleep_time}"
- ((attempt+=1))
+#!/bin/bash -e
+# shellcheck disable=SC1090,SC2155
+##############################################################################
+# Copyright (c) 2017 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
+##############################################################################
+
+CI_DEBUG=${CI_DEBUG:-0}; [[ "${CI_DEBUG}" =~ (false|0) ]] || set -x
+ERASE_ENV=${ERASE_ENV:-0}
+
+source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/globals.sh"
+source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
+
+# Wait for MaaS commissioning/deploy to finish, retry on failure
+function maas_fixup() {
+ local statuscmd="salt 'mas01*' --out yaml state.apply maas.machines.status"
+ local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
+ grep -cE '^\s{2}\w+:$')
+
+ # wait_for has 10sec timeout * 96 = 16 min > 15min for Failed state
+ wait_for 96 "${statuscmd} | tee /dev/stderr | " \
+ "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
+ local statusout=$(eval "${statuscmd}")
+
+ local fcnodes=$(echo "${statusout}" | \
+ grep -Pzo 'status: Failed commissioning\n\s+system_id: \K.+\n')
+ for node_system_id in ${fcnodes}; do
+ salt -C 'mas01*' state.apply maas.machines.delete \
+ pillar="{'system_id': '${node_system_id}'}"
+ sleep 10