-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpin.sh
executable file
·155 lines (135 loc) · 3.78 KB
/
unpin.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Globals
readonly SCRIPT_NAME="${0##*/}"
readonly INVENTORY_FILE="inventory"
# Error codes
readonly E_ARGS=1
readonly E_INVENTORY=2
readonly E_EXTRACTION=3
readonly E_CATEGORIZATION=4
readonly E_UNPIN=5
# Log levels
readonly LOG_ERROR="ERROR"
readonly LOG_WARN="WARN"
readonly LOG_INFO="INFO"
readonly LOG_DEBUG="DEBUG"
log() {
local level="$1"
shift
echo "[$(date +'%Y-%m-%d %H:%M:%S')] [${level}] $*" >&2
}
die() {
log "${LOG_ERROR}" "$@"
exit "${E_ARGS}"
}
safe_source() {
[[ -r "$1" ]] || die "Cannot read file: $1"
# shellcheck source=/dev/null
source "$1"
}
validate_inventory() {
[[ -r "${INVENTORY_FILE}" ]] || die "Cannot read inventory file: ${INVENTORY_FILE}"
grep -qE '^\[(polkadot|cumulus)\]$' "${INVENTORY_FILE}" || die "Invalid inventory file format"
}
extract_hosts() {
local group="$1"
local result
result=$(awk -v group="[$group]" '
$0 == group {f=1; next}
/^\[/ {f=0}
f && NF && !/^[[:space:]]*#/ {gsub(/[[:space:]]/, ""); print}
' "${INVENTORY_FILE}")
[[ -n "${result}" ]] || die "No hosts found for group: ${group}"
echo "${result}"
}
categorize_hosts() {
local -n hosts=$1
local -n primary=$2
local -n secondary=$3
for host in "${hosts[@]}"; do
if [[ ! "${host}" =~ ^[a-zA-Z0-9_-]+$ ]]; then
log "${LOG_WARN}" "Invalid hostname: ${host}"
continue
fi
local last_digit
last_digit=$(echo "${host}" | grep -oE '[0-9]$' || echo "")
if [[ -n "${last_digit}" ]]; then
if ((last_digit % 2 == 1)); then
primary+=("${host}")
else
secondary+=("${host}")
fi
else
log "${LOG_WARN}" "No digit found in hostname: ${host}"
secondary+=("${host}")
fi
done
}
unpin_host() {
local host="$1"
log "${LOG_INFO}" "Unpinning ${host}..."
if ! inv unpin "${host}"; then
log "${LOG_ERROR}" "Failed to unpin ${host}"
return 1
fi
}
unpin_hosts_concurrently() {
local -n hosts=$1
local pids=()
for host in "${hosts[@]}"; do
unpin_host "${host}" &
pids+=($!)
done
for pid in "${pids[@]}"; do
if ! wait "${pid}"; then
log "${LOG_ERROR}" "A host unpin operation failed"
return 1
fi
done
}
main() {
if [[ "$#" -ne 1 ]]; then
die "Usage: ${SCRIPT_NAME} <list_number>
1: polkadot primary (odd last digit)
2: polkadot secondary (even last digit)
3: cumulus primary (odd last digit)
4: cumulus secondary (even last digit)"
fi
local list_number="$1"
validate_inventory
local polkadot_hosts cumulus_hosts
readarray -t polkadot_hosts < <(extract_hosts "polkadot")
readarray -t cumulus_hosts < <(extract_hosts "cumulus")
local polkadot_primary=() polkadot_secondary=() cumulus_primary=() cumulus_secondary=()
categorize_hosts polkadot_hosts polkadot_primary polkadot_secondary
categorize_hosts cumulus_hosts cumulus_primary cumulus_secondary
case "${list_number}" in
1)
log "${LOG_INFO}" "Unpinning Polkadot primary hosts (odd last digit):"
printf '%s\n' "${polkadot_primary[@]}"
unpin_hosts_concurrently polkadot_primary || die "Failed to unpin some Polkadot primary hosts"
;;
2)
log "${LOG_INFO}" "Unpinning Polkadot secondary hosts (even last digit):"
printf '%s\n' "${polkadot_secondary[@]}"
unpin_hosts_concurrently polkadot_secondary || die "Failed to unpin some Polkadot secondary hosts"
;;
3)
log "${LOG_INFO}" "Unpinning Cumulus primary hosts (odd last digit):"
printf '%s\n' "${cumulus_primary[@]}"
unpin_hosts_concurrently cumulus_primary || die "Failed to unpin some Cumulus primary hosts"
;;
4)
log "${LOG_INFO}" "Unpinning Cumulus secondary hosts (even last digit):"
printf '%s\n' "${cumulus_secondary[@]}"
unpin_hosts_concurrently cumulus_secondary || die "Failed to unpin some Cumulus secondary hosts"
;;
*)
die "Invalid list number: ${list_number}"
;;
esac
log "${LOG_INFO}" "Unpin operation completed successfully"
}
main "$@"