-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdown.sh
More file actions
executable file
·78 lines (71 loc) · 4.34 KB
/
Copy pathdown.sh
File metadata and controls
executable file
·78 lines (71 loc) · 4.34 KB
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
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env bash
# Tear the rig down — DELETE the benchmark stack (+ its network stack, if present).
# Interactive + safe: pick the AWS account/region, SHOW which boxes will be destroyed,
# then require you to type the stack name to confirm. Nothing persists but the IaC in git.
#
# ./down.sh # interactive
# STACK=foo ./down.sh # target a different stack
# ASSUME_YES=1 ./down.sh # no prompts (CI)
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Reuse the operator's deploy.env (AWS_PROFILE / REGION / STACK) if present.
[ -f "$ROOT/deploy.env" ] && { set -a; . "$ROOT/deploy.env"; set +a; }
REGION="${REGION:-us-east-1}"; STACK="${STACK:-gw-bench}"; NET_STACK="${NET_STACK:-${STACK}-net}"
ASSUME_YES="${ASSUME_YES:-0}"
c(){ printf "\033[%sm%s\033[0m" "$1" "$2"; }
ok(){ echo "$(c 32 "✓") $*"; }; inf(){ echo "$(c 36 "•") $*"; }
warn(){ echo "$(c 33 "!") $*" >&2; }; die(){ echo "$(c 31 "✗") $*" >&2; exit 1; }
aws_(){ aws --region "$REGION" "$@"; }
# pick_one <title> <default> : "value|label" lines on stdin → numbered menu (stderr),
# chosen value (stdout). Enter = default. Honors ASSUME_YES (takes the default).
pick_one(){
local title="$1" def="${2:-}"; local -a vals=() labels=(); local v l
while IFS='|' read -r v l; do
[ -n "$v" ] || continue
case " ${vals[*]:-} " in *" $v "*) continue ;; esac
vals+=("$v"); labels+=("${l:-$v}")
done
[ "${#vals[@]}" -gt 0 ] || { echo "$def"; return 0; }
[ "$ASSUME_YES" = "1" ] && { echo "${def:-${vals[0]}}"; return 0; }
{ echo "$(c 1 "$title")"; local i mark
for i in "${!vals[@]}"; do mark=""; { [ -n "$def" ] && [ "${vals[$i]}" = "$def" ]; } && mark=" $(c 32 '(current)')"
printf " %2d) %s%s\n" "$((i+1))" "${labels[$i]}" "$mark"; done; } >&2
local ans; read -r -p "$(c 33 "?") choose 1-${#vals[@]}${def:+ (Enter=current)}: " ans </dev/tty || true
[ -n "$ans" ] || { echo "${def:-${vals[0]}}"; return 0; }
{ [[ "$ans" =~ ^[0-9]+$ ]] && [ "$ans" -ge 1 ] && [ "$ans" -le "${#vals[@]}" ]; } || die "invalid selection '$ans'"
echo "${vals[$((ans-1))]}"
}
del(){ # <stack>
aws_ cloudformation describe-stacks --stack-name "$1" >/dev/null 2>&1 || { inf "stack '$1' not present — skipped"; return 0; }
inf "deleting stack '$1'…"
aws_ cloudformation delete-stack --stack-name "$1"
aws_ cloudformation wait stack-delete-complete --stack-name "$1"
ok "deleted '$1'"
}
command -v aws >/dev/null || die "aws CLI not found."
# Account / region — pick the right one (don't tear down the wrong environment).
if [ -z "${AWS_PROFILE:-}" ]; then
profs="$(aws configure list-profiles 2>/dev/null || true)"
[ -n "$profs" ] && { AWS_PROFILE="$(printf '%s\n' "$profs" | sed 's/$/|/' | pick_one "AWS profile (which account to tear down):" "default")"; export AWS_PROFILE; }
fi
REGION="$(printf '%s\n' \
'us-east-1|us-east-1 (N. Virginia)' 'us-east-2|us-east-2 (Ohio)' 'us-west-2|us-west-2 (Oregon)' \
'eu-west-1|eu-west-1 (Ireland)' 'eu-central-1|eu-central-1 (Frankfurt)' 'ap-southeast-1|ap-southeast-1 (Singapore)' \
"${REGION}|${REGION} (current)" | pick_one "AWS region:" "$REGION")"
acct="$(aws_ sts get-caller-identity --query Account --output text 2>/dev/null)" || die "AWS credentials not working for profile '${AWS_PROFILE:-default}'."
aws_ cloudformation describe-stacks --stack-name "$STACK" >/dev/null 2>&1 \
|| die "stack '$STACK' not found in account $acct / $REGION — nothing to delete (right account/region/STACK?)."
# Show exactly what will be destroyed.
boxes="$(aws_ ec2 describe-instances \
--filters "Name=tag:aws:cloudformation:stack-name,Values=$STACK" "Name=instance-state-name,Values=running,stopped,stopping,pending" \
--query 'Reservations[].Instances[].[Tags[?Key==`Role`]|[0].Value,InstanceId,State.Name]' --output text 2>/dev/null || true)"
warn "about to DELETE stack '$STACK' (and '$NET_STACK' if present) in account $acct / region $REGION — these boxes go away:"
if [ -n "$boxes" ]; then printf '%s\n' "$boxes" | awk -F'\t' '{print " "$1" "$2" ["$3"]"}'
else echo " (no live instances found — the stack's resources are still deleted)"; fi
if [ "$ASSUME_YES" != "1" ]; then
read -r -p "$(c 33 "?") type the stack name '$STACK' to confirm DELETE: " a </dev/tty || true
[ "$a" = "$STACK" ] || die "aborted (got '$a', expected '$STACK')."
fi
del "$STACK"
del "$NET_STACK"
ok "teardown complete — account $acct / $REGION"