-#!/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
+##############################################################################
+# 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
+##############################################################################
+
+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"
+
+bm_nodes=$(salt --out yaml 'mas01*' pillar.get maas:region:machines | \
+ awk '/^\s+\w+[[:digit:]]+:$/ {gsub(/:$/, "*"); printf "%s ", $1}')
+
+# Optionally destroy MaaS machines from a previous run
+if [ "${ERASE_ENV}" -gt 1 ]; then
+ cleanup_uefi
+ for node_hostname in ${bm_nodes//\*/}; do
+ salt -C 'mas01*' maasng.delete_machine "${node_hostname}" || true