-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_deploy.sh
More file actions
executable file
·81 lines (68 loc) · 2.06 KB
/
pre_deploy.sh
File metadata and controls
executable file
·81 lines (68 loc) · 2.06 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
79
80
81
#!/bin/bash
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# Drain agent nodes that are about to be destroyed.
# All arguments are forwarded to terraform plan to generate a temporary
# plan file used only for inspection.
set -euo pipefail
KUBECONFIG="elsa.yaml"
PLANFILE=".tfplan.$$"
cleanup() { rm -f "$PLANFILE"; }
trap cleanup EXIT
terraform plan "$@" -out="$PLANFILE" >/dev/null
plan_json=$(terraform show -json "$PLANFILE")
# Build a compact summary of every resource change.
summary=$(echo "$plan_json" \
| jq -r '
.resource_changes[]
| select(.change.actions != ["no-op"])
| "\(.change.actions | join(",")) \(.address)"
')
# Extract hostnames of agent nodes being deleted.
hostnames=$(echo "$plan_json" \
| jq -r '
.resource_changes[]
| select(.type == "vultr_instance")
| select(.change.actions | index("delete"))
| .change.before.hostname
| select(startswith("elsa-agent-"))
')
if [ -z "$summary" ]; then
echo "No pending changes."
exit 0
fi
echo "Pending changes:"
echo "$summary" | while IFS= read -r line; do
echo " $line"
done
if [ -n "$hostnames" ] && [ -f "$KUBECONFIG" ]; then
echo ""
echo "Agent nodes to drain before apply:"
for node in $hostnames; do
echo " $node"
done
fi
echo ""
read -rp "Proceed? [y/N] " answer
if [[ ! "$answer" =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 1
fi
if [ -n "$hostnames" ] && [ -f "$KUBECONFIG" ]; then
# Drain all nodes in parallel to avoid rescheduling pods onto nodes
# that are about to be drained themselves. Node objects are deleted
# later by the new agent instances on boot (via agent-init.sh).
pids=()
for node in $hostnames; do
(
echo "Draining $node..."
kubectl --kubeconfig "$KUBECONFIG" drain "$node" \
--ignore-daemonsets --delete-emptydir-data --force --timeout=120s || true
) &
pids+=($!)
done
for pid in "${pids[@]}"; do
wait "$pid"
done
fi