-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtalos-wipe.sh
More file actions
executable file
·452 lines (392 loc) · 14.1 KB
/
talos-wipe.sh
File metadata and controls
executable file
·452 lines (392 loc) · 14.1 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/bash
#
# Talos Cluster Wipe Script
# Wipes all drives, shuts down nodes, and verifies power off via TuringPi BMC
#
# Usage: ./talos-wipe.sh [OPTIONS]
#
# Options:
# -t, --talosconfig PATH Path to talosconfig file (default: ./talosconfig)
# -n, --nodes IPs Comma-separated list of node IPs
# -b, --bmc IP TuringPi BMC IP address (or TURINGPI_ENDPOINT env)
# -u, --user USER BMC username (default: root, or TURINGPI_USERNAME env)
# -p, --password PASS BMC password (default: turing, or TURINGPI_PASSWORD env)
# -d, --disks DEVICES Comma-separated user disks to wipe (default: /dev/nvme0n1)
# --no-nvme Skip NVMe wipe
# --clean-terraform Also clean terraform state files
# --force-power-off Force power off via BMC if graceful shutdown fails
# --yes, -y Skip interactive confirmation (for automation)
# --log FILE Log output to file
# --dry-run Show commands without executing
# -h, --help Show this help message
#
# Environment Variables:
# TURINGPI_ENDPOINT - BMC endpoint (alternative to -b flag)
# TURINGPI_USERNAME - BMC username (alternative to -u flag)
# TURINGPI_PASSWORD - BMC password (alternative to -p flag)
#
# Credential Files (checked if env vars not set):
# ~/.secrets/turingpi-bmc-user - BMC username
# ~/.secrets/turingpi-bmc-password - BMC password
set -euo pipefail
# Default values
TALOSCONFIG="./talosconfig"
NODES=""
BMC_IP="${TURINGPI_ENDPOINT:-}"
BMC_USER="${TURINGPI_USERNAME:-}"
BMC_PASSWORD="${TURINGPI_PASSWORD:-}"
USER_DISKS="/dev/nvme0n1"
WIPE_NVME=true
CLEAN_TERRAFORM=false
FORCE_POWER_OFF=false
SKIP_CONFIRM=false
LOG_FILE=""
DRY_RUN=false
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_output() {
local msg="$1"
echo -e "$msg"
if [[ -n "$LOG_FILE" ]]; then
echo -e "$msg" | sed 's/\x1b\[[0-9;]*m//g' >> "$LOG_FILE"
fi
}
log_info() { log_output "${GREEN}[INFO]${NC} $1"; }
log_warn() { log_output "${YELLOW}[WARN]${NC} $1"; }
log_error() { log_output "${RED}[ERROR]${NC} $1"; }
log_step() { log_output "${BLUE}[STEP]${NC} $1"; }
show_help() {
head -32 "$0" | tail -29
exit 0
}
# Load credentials from files if not set via env
load_credentials() {
local secrets_dir="$HOME/.secrets"
# BMC IP - strip protocol if present
if [[ -n "$BMC_IP" ]]; then
BMC_IP=$(echo "$BMC_IP" | sed 's|https\?://||' | sed 's|/.*||')
fi
# Try turning-pi-cluster-bmc format first (contains ip, username, password)
if [[ -f "$secrets_dir/turning-pi-cluster-bmc" ]]; then
if [[ -z "$BMC_USER" ]]; then
BMC_USER=$(grep "^username:" "$secrets_dir/turning-pi-cluster-bmc" | cut -d' ' -f2) || true
fi
if [[ -z "$BMC_PASSWORD" ]]; then
BMC_PASSWORD=$(grep "^password:" "$secrets_dir/turning-pi-cluster-bmc" | cut -d' ' -f2) || true
fi
if [[ -z "$BMC_IP" ]]; then
BMC_IP=$(grep "^ip:" "$secrets_dir/turning-pi-cluster-bmc" | cut -d' ' -f2) || true
fi
fi
# Try individual files
if [[ -z "$BMC_USER" && -f "$secrets_dir/turingpi-bmc-user" ]]; then
BMC_USER=$(tr -d '\n' < "$secrets_dir/turingpi-bmc-user")
fi
if [[ -z "$BMC_PASSWORD" && -f "$secrets_dir/turingpi-bmc-password" ]]; then
BMC_PASSWORD=$(tr -d '\n' < "$secrets_dir/turingpi-bmc-password")
fi
# Apply defaults
: "${BMC_USER:=root}"
: "${BMC_PASSWORD:=turing}"
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-t|--talosconfig) TALOSCONFIG="$2"; shift 2 ;;
-n|--nodes) NODES="$2"; shift 2 ;;
-b|--bmc) BMC_IP="$2"; shift 2 ;;
-u|--user) BMC_USER="$2"; shift 2 ;;
-p|--password) BMC_PASSWORD="$2"; shift 2 ;;
-d|--disks) USER_DISKS="$2"; shift 2 ;;
--no-nvme) WIPE_NVME=false; shift ;;
--clean-terraform) CLEAN_TERRAFORM=true; shift ;;
--force-power-off) FORCE_POWER_OFF=true; shift ;;
--yes|-y) SKIP_CONFIRM=true; shift ;;
--log) LOG_FILE="$2"; shift 2 ;;
--dry-run) DRY_RUN=true; shift ;;
-h|--help) show_help ;;
*) log_error "Unknown option: $1"; show_help ;;
esac
done
# Load credentials from env/files
load_credentials
# Validate required arguments
if [[ -z "$NODES" ]]; then
log_error "Node IPs required. Use -n or --nodes"
exit 1
fi
if [[ -z "$BMC_IP" ]]; then
log_error "BMC IP required. Use -b, --bmc, or set TURINGPI_ENDPOINT"
exit 1
fi
if [[ ! -f "$TALOSCONFIG" ]]; then
log_warn "Talosconfig not found: $TALOSCONFIG (will skip talosctl commands)"
fi
# Convert comma-separated nodes to array
IFS=',' read -ra NODE_ARRAY <<< "$NODES"
# Initialize log file
if [[ -n "$LOG_FILE" ]]; then
echo "=== Talos Wipe Log - $(date) ===" > "$LOG_FILE"
fi
run_cmd() {
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] $*"
else
"$@"
fi
}
# Get node slot from IP (last octet mapping)
get_slot_from_ip() {
local ip=$1
local last_octet
last_octet=$(echo "$ip" | cut -d'.' -f4)
case $last_octet in
73) echo 1 ;;
74) echo 2 ;;
75) echo 3 ;;
76) echo 4 ;;
*) echo $((last_octet - 72)) ;;
esac
}
# Check BMC power status for a slot
check_power_status() {
local slot=$1
local response
response=$(curl -sk -u "${BMC_USER}:${BMC_PASSWORD}" \
"https://${BMC_IP}/api/bmc?opt=get&type=power" 2>/dev/null || echo "{}")
local power_state
power_state=$(echo "$response" | grep -o "\"node${slot}\":[0-1]" | cut -d':' -f2 || echo "unknown")
if [[ "$power_state" == "0" ]]; then
echo "off"
elif [[ "$power_state" == "1" ]]; then
echo "on"
else
echo "unknown"
fi
}
# Force power off a node via BMC
force_power_off() {
local slot=$1
log_warn " Force powering off slot $slot via BMC..."
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] curl -sk -u ${BMC_USER}:*** 'https://${BMC_IP}/api/bmc?opt=set&type=power&node${slot}=0'"
return 0
fi
local response
response=$(curl -sk -u "${BMC_USER}:${BMC_PASSWORD}" \
"https://${BMC_IP}/api/bmc?opt=set&type=power&node${slot}=0" 2>/dev/null || echo "error")
if [[ "$response" == *"error"* || "$response" == "" ]]; then
log_error " Failed to force power off slot $slot"
return 1
fi
return 0
}
# Wait for node to power off with retries
wait_for_power_off() {
local slot=$1
local max_attempts=${2:-30}
local attempt=0
while [[ $attempt -lt $max_attempts ]]; do
local status
status=$(check_power_status "$slot")
if [[ "$status" == "off" ]]; then
return 0
fi
((attempt++))
sleep 2
done
return 1
}
# Clean terraform state files
clean_terraform_state() {
local terraform_dir="${1:-.}"
log_step "Cleaning Terraform state files in $terraform_dir..."
local files_removed=0
for pattern in "terraform.tfstate" "terraform.tfstate.backup" ".terraform.lock.hcl"; do
if [[ -f "$terraform_dir/$pattern" ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] rm $terraform_dir/$pattern"
else
rm -f "$terraform_dir/$pattern"
fi
((files_removed++))
fi
done
if [[ -d "$terraform_dir/.terraform" ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] rm -rf $terraform_dir/.terraform"
else
rm -rf "$terraform_dir/.terraform"
fi
((files_removed++))
fi
if [[ $files_removed -gt 0 ]]; then
log_info " Removed $files_removed terraform state file(s)/directories"
else
log_info " No terraform state files found"
fi
}
echo "=============================================="
echo " Talos Cluster Wipe Workflow"
echo "=============================================="
echo ""
echo -e "${RED}╔════════════════════════════════════════════╗${NC}"
echo -e "${RED}║ ⚠️ WARNING: DATA DESTRUCTION ⚠️ ║${NC}"
echo -e "${RED}╚════════════════════════════════════════════╝${NC}"
echo ""
echo "Nodes to wipe: ${NODE_ARRAY[*]}"
echo "BMC: $BMC_IP"
echo "Talosconfig: $TALOSCONFIG"
echo ""
echo "Data to be PERMANENTLY DESTROYED:"
echo " • Talos system partitions (STATE, EPHEMERAL)"
[[ "$WIPE_NVME" == "true" ]] && echo -e " ${RED}• NVMe drives: $USER_DISKS${NC}"
echo ""
echo -e "${YELLOW}Note: eMMC is system disk and cannot be wiped via talosctl reset${NC}"
echo ""
[[ "$CLEAN_TERRAFORM" == "true" ]] && echo "Terraform cleanup: enabled"
[[ "$FORCE_POWER_OFF" == "true" ]] && echo "Force power off: enabled"
[[ -n "$LOG_FILE" ]] && echo "Logging to: $LOG_FILE"
[[ "$DRY_RUN" == "true" ]] && echo -e "${YELLOW}DRY RUN MODE - No changes will be made${NC}"
echo ""
# Confirm before proceeding (unless --yes flag or dry-run)
if [[ "$DRY_RUN" != "true" && "$SKIP_CONFIRM" != "true" ]]; then
echo -e "${RED}This will PERMANENTLY DESTROY ALL DATA on these nodes!${NC}"
echo ""
read -rp "Type 'DESTROY' to confirm: " confirm
if [[ "$confirm" != "DESTROY" ]]; then
log_warn "Aborted by user"
exit 0
fi
fi
STEP=1
echo ""
# Step: Wipe Talos system partitions
log_step "Step $STEP: Wiping Talos system partitions (STATE, EPHEMERAL)..."
STEP=$((STEP + 1))
if [[ -f "$TALOSCONFIG" ]]; then
# Build wipe command
WIPE_CMD="talosctl --talosconfig $TALOSCONFIG reset --nodes $NODES --graceful=false"
WIPE_CMD+=" --system-labels-to-wipe STATE --system-labels-to-wipe EPHEMERAL"
# Add NVMe disks if enabled
# Note: eMMC is system disk and cannot be wiped via talosctl reset
if [[ "$WIPE_NVME" == "true" && -n "$USER_DISKS" ]]; then
IFS=',' read -ra DISK_ARRAY <<< "$USER_DISKS"
for disk in "${DISK_ARRAY[@]}"; do
WIPE_CMD+=" --user-disks-to-wipe $disk"
done
fi
# Don't reboot - we want to shutdown
WIPE_CMD+=" --reboot=false"
run_cmd "$WIPE_CMD" || {
log_warn "Reset command returned non-zero (node may already be in maintenance mode)"
}
else
log_warn "Skipping talosctl reset - talosconfig not found"
fi
echo ""
log_step "Step $STEP: Shutting down nodes..."
STEP=$((STEP + 1))
for node in "${NODE_ARRAY[@]}"; do
log_info " Shutting down $node..."
if [[ -f "$TALOSCONFIG" ]]; then
run_cmd talosctl --talosconfig "$TALOSCONFIG" shutdown --nodes "$node" --force 2>/dev/null || {
log_warn " Shutdown command failed for $node (may already be down)"
}
else
log_warn " Cannot shutdown $node - no talosconfig"
fi
done
echo ""
log_step "Step $STEP: Verifying power off via BMC..."
STEP=$((STEP + 1))
sleep 5 # Give nodes time to shutdown
nodes_still_on=()
for node in "${NODE_ARRAY[@]}"; do
slot=$(get_slot_from_ip "$node")
log_info " Checking node $node (slot $slot)..."
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] Would check power status for slot $slot"
continue
fi
if wait_for_power_off "$slot" 15; then
log_info " ✓ Node $node (slot $slot) is OFF"
else
status=$(check_power_status "$slot")
log_warn " ✗ Node $node (slot $slot) status: $status"
nodes_still_on+=("$node:$slot")
fi
done
# Force power off remaining nodes if enabled
if [[ ${#nodes_still_on[@]} -gt 0 && "$FORCE_POWER_OFF" == "true" ]]; then
echo ""
log_step "Step $STEP: Force powering off remaining nodes..."
STEP=$((STEP + 1))
for node_slot in "${nodes_still_on[@]}"; do
node=$(echo "$node_slot" | cut -d':' -f1)
slot=$(echo "$node_slot" | cut -d':' -f2)
if force_power_off "$slot"; then
sleep 3
if [[ $(check_power_status "$slot") == "off" ]]; then
log_info " ✓ Node $node (slot $slot) force powered OFF"
# Remove from nodes_still_on
nodes_still_on=("${nodes_still_on[@]/$node_slot/}")
else
log_error " ✗ Node $node (slot $slot) still ON after force power off"
fi
fi
done
fi
# Clean terraform state if requested
if [[ "$CLEAN_TERRAFORM" == "true" ]]; then
echo ""
log_step "Step $STEP: Cleaning Terraform state..."
STEP=$((STEP + 1))
# Look for terraform files in common locations
terraform_dir=$(dirname "$TALOSCONFIG")
if [[ -f "$terraform_dir/main.tf" || -f "$terraform_dir/terraform.tfstate" ]]; then
clean_terraform_state "$terraform_dir"
elif [[ -f "./main.tf" || -f "./terraform.tfstate" ]]; then
clean_terraform_state "."
else
log_warn "No terraform configuration found in current directory or talosconfig directory"
fi
fi
# Also remove generated files
if [[ "$CLEAN_TERRAFORM" == "true" ]]; then
for file in "kubeconfig" "talosconfig" "controlplane.yaml" "worker.yaml"; do
if [[ -f "./$file" ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
log_output "[DRY-RUN] rm ./$file"
else
rm -f "./$file"
log_info " Removed ./$file"
fi
fi
done
fi
echo ""
echo "=============================================="
# Filter out empty elements from nodes_still_on
mapfile -t nodes_still_on < <(printf '%s\n' "${nodes_still_on[@]}" | grep -v '^$')
if [[ ${#nodes_still_on[@]} -eq 0 || "$DRY_RUN" == "true" ]]; then
log_info "Wipe workflow complete!"
echo ""
echo "Next steps:"
echo " 1. Flash new OS image via BMC"
echo " 2. Power on nodes"
echo " 3. Deploy new cluster"
else
log_warn "Some nodes may still be powered on: ${nodes_still_on[*]}"
echo ""
echo "To manually power off via BMC:"
echo " curl -sk -u $BMC_USER:*** \\"
echo " 'https://$BMC_IP/api/bmc?opt=set&type=power&node1=0&node2=0&node3=0&node4=0'"
fi
echo "=============================================="
[[ -n "$LOG_FILE" ]] && log_info "Log saved to: $LOG_FILE"