-#!/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
+##############################################################################
+# 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}
+
+# shellcheck disable=SC1090
+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"
+ # shellcheck disable=SC2155
+ local ncount=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
+ grep -cE '^\s{2}\w+:$')
+ wait_for 180 "${statuscmd} | tee /dev/stderr | " \
+ "grep -Eq '((Deployed|Ready): ${ncount}|status: (Failed|Allocated))'"
+ # shellcheck disable=SC2155
+ local statusout=$(eval "${statuscmd}")
+
+ # shellcheck disable=SC2155
+ 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 30