This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharchitecture.sh
executable file
·67 lines (52 loc) · 1.65 KB
/
architecture.sh
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
63
64
65
66
67
#!/bin/bash
# terraform apply -var-file="../storage.json" -var 'Training=1' -state="states/state-1.tfstate"
#
set -e
set -o pipefail
if [[ $# -lt 2 ]]; then
echo "Wrong parameters" > /dev/stderr
echo "Usage: $0 <architecture> <number of instances>"
exit 1
fi
ARCH="$1"
COUNT="$2"
PWD=$(pwd)
CONF="${PWD}/architecture/${ARCH}.json"
ARCH_DIR="${PWD}/architecture/${ARCH}"
if [[ ! -d "${ARCH_DIR}" || ! -f "${CONF}" ]]; then
echo "Architecture: ${ARCH}" > /dev/stderr
echo "Missing architecture or configuration file" > /dev/stderr
exit 2
fi
if [[ ${COUNT} -lt -1 || ${COUNT} -gt 99 ]]; then
echo "Invalid count given: ${COUNT}" > /dev/stderr
exit 3
fi
cd "${ARCH_DIR}"
STATE_PATH="states"
mkdir -p "${STATE_PATH}"
ACTION="apply"
ACTION_NAME="Build"
if [[ ${COUNT} -eq -1 ]]; then
ACTION="destroy"
ACTION_NAME="Cleanup"
COUNT=$(find "${STATE_PATH}" -type f | grep "tfstate$" | sed -r 's/.*\/state-([[:digit:]]+)\.tfstate$/\1/' | sort --general-numeric-sort | uniq | tail -1)
if [[ ! -f "${STATE_PATH}/state-${COUNT}.tfstate" ]]; then
echo "Cannot correctly determine count. Calculated: ${COUNT}" > /dev/stderr
exit 5
fi
fi
echo "Init terraform"
terraform init -input=false > /dev/null
echo
echo "${ACTION_NAME} ${COUNT} architectures of type ${ARCH}"
for i in $(seq 0 "${COUNT}"); do
echo "${ACTION_NAME} architecture ${i}"
terraform ${ACTION} -var-file="${CONF}" -var "Training=${i}" -state="${STATE_PATH}/state-${i}.tfstate" -input=false -auto-approve
if [[ $2 -eq -1 ]] && [[ $? -eq 0 ]]; then
rm -f "${STATE_PATH}/state-${i}.tfstate"
fi
done
echo "Finished"
cd "${PWD}"
exit 0