forked from NVIDIA/infra-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-devspace-on-host.sh
More file actions
executable file
·613 lines (556 loc) · 17 KB
/
Copy pathreset-devspace-on-host.sh
File metadata and controls
executable file
·613 lines (556 loc) · 17 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/env bash
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Remove the state created by setup-devspace-on-host.sh.
#
# This script is intentionally dry-run by default. It preserves the VM-provided
# Docker packages and the Docker data directory itself, while removing the
# local kind cluster, Docker build data, checkout, tools, caches, shell setup,
# and host configuration written by the setup script. /dockerroot is preferred
# when present; otherwise the regular Docker data path is used.
set -euo pipefail
DEV_USER=""
REPO_DIR=""
CLUSTER_NAME="nico-dev"
DOCKER_ROOT="/dockerroot"
DOCKER_ROOT_EXPLICIT=0
CONFIRM_HOST=""
APPLY=0
FORCE_DIRTY_CHECKOUT=0
KEEP_CHECKOUT=0
KEEP_HOST_CONFIG=0
DOCKER_DATA_ROOT_IS_SETUP=0
CONTAINERD_ROOT_IS_SETUP=0
SCRIPT_START_SECONDS="${SECONDS}"
format_duration() {
local total_seconds="$1"
printf '%02d:%02d:%02d' \
"$((total_seconds / 3600))" \
"$(((total_seconds % 3600) / 60))" \
"$((total_seconds % 60))"
}
log() {
printf '[reset-devspace-on-host] %s\n' "$*"
}
die() {
printf '[reset-devspace-on-host] error: %s\n' "$*" >&2
exit 1
}
report_duration() {
local status=$?
trap - EXIT
local outcome="completed"
if [[ "${status}" -ne 0 ]]; then
outcome="failed with status ${status}"
fi
log "Run ${outcome}; total duration: $(
format_duration "$((SECONDS - SCRIPT_START_SECONDS))"
)"
exit "${status}"
}
usage() {
cat <<'EOF'
Usage:
reset-devspace-on-host.sh [options]
The default is a dry run. Destructive execution requires both --apply and
--confirm-host matching the VM's hostname.
Options:
--user USER Developer account to reset. Defaults to the user that
invoked sudo.
--repo-dir PATH Checkout to delete. Auto-detects ~/infra-controller or
the legacy ~/ncx-infra-controller-core path.
--cluster-name NAME kind cluster to delete. Default: nico-dev.
--docker-root PATH Docker data path. Defaults to /dockerroot when present,
otherwise Docker's active root or /var/lib/docker.
--confirm-host NAME Required with --apply; must match hostname or hostname -f.
--apply Perform the reset. Without this flag, print the plan.
--force-dirty-checkout
Allow deletion when the checkout has uncommitted files.
--keep-checkout Preserve the repository checkout.
--keep-host-config Preserve Docker storage/TLS config and docker membership.
-h, --help Show this help.
Copy this script outside the checkout and first inspect the plan:
bash /tmp/reset-devspace-on-host.sh \
--user devuser \
--repo-dir /home/devuser/infra-controller
Then apply it:
bash /tmp/reset-devspace-on-host.sh \
--apply \
--confirm-host host.example.com \
--user devuser \
--repo-dir /home/devuser/infra-controller \
--force-dirty-checkout
EOF
}
while (($#)); do
case "$1" in
--user)
(($# >= 2)) || die "--user requires a value"
DEV_USER="$2"
shift 2
;;
--repo-dir)
(($# >= 2)) || die "--repo-dir requires a value"
REPO_DIR="$2"
shift 2
;;
--cluster-name)
(($# >= 2)) || die "--cluster-name requires a value"
CLUSTER_NAME="$2"
shift 2
;;
--docker-root)
(($# >= 2)) || die "--docker-root requires a value"
DOCKER_ROOT="$2"
DOCKER_ROOT_EXPLICIT=1
shift 2
;;
--confirm-host)
(($# >= 2)) || die "--confirm-host requires a value"
CONFIRM_HOST="$2"
shift 2
;;
--apply)
APPLY=1
shift
;;
--force-dirty-checkout)
FORCE_DIRTY_CHECKOUT=1
shift
;;
--keep-checkout)
KEEP_CHECKOUT=1
shift
;;
--keep-host-config)
KEEP_HOST_CONFIG=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
done
if [[ "${EUID}" -ne 0 && "${APPLY}" == "1" ]]; then
if [[ -z "${DEV_USER}" ]]; then
DEV_USER="${USER}"
fi
sudo_args=(
bash "$0"
--user "${DEV_USER}"
--cluster-name "${CLUSTER_NAME}"
)
if [[ "${DOCKER_ROOT_EXPLICIT}" == "1" ]]; then
sudo_args+=(--docker-root "${DOCKER_ROOT}")
fi
if [[ -n "${REPO_DIR}" ]]; then
sudo_args+=(--repo-dir "${REPO_DIR}")
fi
if [[ -n "${CONFIRM_HOST}" ]]; then
sudo_args+=(--confirm-host "${CONFIRM_HOST}")
fi
if [[ "${APPLY}" == "1" ]]; then
sudo_args+=(--apply)
fi
if [[ "${FORCE_DIRTY_CHECKOUT}" == "1" ]]; then
sudo_args+=(--force-dirty-checkout)
fi
if [[ "${KEEP_CHECKOUT}" == "1" ]]; then
sudo_args+=(--keep-checkout)
fi
if [[ "${KEEP_HOST_CONFIG}" == "1" ]]; then
sudo_args+=(--keep-host-config)
fi
exec sudo "${sudo_args[@]}"
fi
trap report_duration EXIT
if [[ -z "${DEV_USER}" ]]; then
DEV_USER="${SUDO_USER:-${USER:-}}"
fi
[[ -n "${DEV_USER}" ]] || die "use --user when running directly as root"
id "${DEV_USER}" >/dev/null 2>&1 || die "user does not exist: ${DEV_USER}"
USER_HOME="$(getent passwd "${DEV_USER}" | cut -d: -f6)"
USER_GROUP="$(id -gn "${DEV_USER}")"
[[ -n "${USER_HOME}" && "${USER_HOME}" != "/" ]] || \
die "invalid home directory for ${DEV_USER}"
run_as_user() {
local user_env=(
HOME="${USER_HOME}"
USER="${DEV_USER}"
PATH="${USER_HOME}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
GODEBUG="tlsmlkem=0"
)
if [[ "${EUID}" -eq 0 ]]; then
runuser -u "${DEV_USER}" -- env "${user_env[@]}" "$@"
elif [[ "$(id -un)" == "${DEV_USER}" ]]; then
env "${user_env[@]}" "$@"
else
die "dry run as another user requires sudo"
fi
}
resolve_docker_root() {
if [[ "${DOCKER_ROOT_EXPLICIT}" == "1" || -d "${DOCKER_ROOT}" ]]; then
return
fi
local active_root
active_root="$(docker info --format '{{.DockerRootDir}}' 2>/dev/null || true)"
if [[ -n "${active_root}" ]]; then
DOCKER_ROOT="${active_root}"
else
DOCKER_ROOT="/var/lib/docker"
fi
log "/dockerroot is absent; using regular Docker data path ${DOCKER_ROOT}"
}
detect_checkout() {
if [[ "${KEEP_CHECKOUT}" == "1" || -n "${REPO_DIR}" ]]; then
return
fi
local current="${USER_HOME}/infra-controller"
local legacy="${USER_HOME}/ncx-infra-controller-core"
if [[ -e "${current}" && -e "${legacy}" ]]; then
die "both checkout paths exist; select one with --repo-dir"
elif [[ -e "${current}" ]]; then
REPO_DIR="${current}"
elif [[ -e "${legacy}" ]]; then
REPO_DIR="${legacy}"
else
REPO_DIR="${current}"
fi
}
validate_checkout() {
if [[ "${KEEP_CHECKOUT}" == "1" || ! -e "${REPO_DIR}" ]]; then
return
fi
[[ "${REPO_DIR}" == "${USER_HOME}/"* ]] || \
die "checkout must be a child of ${USER_HOME}: ${REPO_DIR}"
[[ "${REPO_DIR}" != "${USER_HOME}" ]] || die "refusing to delete the user home"
[[ -d "${REPO_DIR}/.git" && -f "${REPO_DIR}/devspace.yaml" ]] || \
die "not an infra-controller checkout: ${REPO_DIR}"
local origin
origin="$(run_as_user git -C "${REPO_DIR}" remote get-url origin 2>/dev/null || true)"
case "${origin}" in
*infra-controller|*infra-controller.git) ;;
*) die "unexpected checkout origin: ${origin:-unset}" ;;
esac
if [[ -n "$(run_as_user git -C "${REPO_DIR}" status --porcelain)" && \
"${FORCE_DIRTY_CHECKOUT}" != "1" ]]; then
if [[ "${APPLY}" == "1" ]]; then
die "checkout is dirty; inspect it or pass --force-dirty-checkout"
fi
log "Warning: checkout is dirty; --apply will require --force-dirty-checkout"
fi
}
validate_docker_root() {
[[ "${DOCKER_ROOT}" == /* ]] || die "--docker-root must be absolute"
case "${DOCKER_ROOT}" in
/|/home|/var|/usr|/etc) die "unsafe Docker root: ${DOCKER_ROOT}" ;;
esac
[[ ! -e "${DOCKER_ROOT}" || -d "${DOCKER_ROOT}" ]] || \
die "Docker root is not a directory: ${DOCKER_ROOT}"
}
validate_host_config() {
if [[ "${KEEP_HOST_CONFIG}" == "1" ]]; then
return
fi
if [[ -e /etc/docker/daemon.json ]]; then
jq -e 'type == "object"' /etc/docker/daemon.json >/dev/null || \
die "/etc/docker/daemon.json is not a JSON object"
local configured_docker_root
configured_docker_root="$(
jq -r '."data-root" // empty' /etc/docker/daemon.json
)"
if [[ -n "${configured_docker_root}" && \
"${configured_docker_root}" != "${DOCKER_ROOT}" ]]; then
die "unexpected Docker data root: ${configured_docker_root}"
fi
if [[ "${configured_docker_root}" == "${DOCKER_ROOT}" ]]; then
DOCKER_DATA_ROOT_IS_SETUP=1
fi
fi
if [[ -e /etc/containerd/config.toml ]]; then
local configured_root
configured_root="$(sed -nE \
's|^root[[:space:]]*=[[:space:]]*"([^"]+)".*$|\1|p' \
/etc/containerd/config.toml)"
if [[ "${configured_root}" == "${DOCKER_ROOT}/containerd" ]]; then
CONTAINERD_ROOT_IS_SETUP=1
elif [[ -z "${configured_root}" || \
"${configured_root}" == "/var/lib/containerd" ]]; then
CONTAINERD_ROOT_IS_SETUP=0
else
die "unexpected containerd root: ${configured_root:-unset}"
fi
fi
local dropin
for dropin in \
/etc/systemd/system/docker.service.d/10-tls-compat.conf \
/etc/systemd/system/containerd.service.d/10-tls-compat.conf; do
if [[ -e "${dropin}" ]] && \
! cmp -s "${dropin}" <(
printf '%s\n' '[Service]' 'Environment="GODEBUG=tlsmlkem=0"'
); then
die "systemd drop-in contains unexpected content: ${dropin}"
fi
done
}
validate_host_confirmation() {
if [[ "${APPLY}" != "1" ]]; then
return
fi
[[ -n "${CONFIRM_HOST}" ]] || die "--apply requires --confirm-host"
local short_name fqdn
short_name="$(hostname)"
fqdn="$(hostname -f 2>/dev/null || true)"
if [[ "${CONFIRM_HOST}" != "${short_name}" && \
"${CONFIRM_HOST}" != "${fqdn}" ]]; then
die "hostname confirmation '${CONFIRM_HOST}' does not match ${short_name}/${fqdn}"
fi
}
print_plan() {
cat <<EOF
[reset-devspace-on-host] Reset plan for $(hostname -f 2>/dev/null || hostname):
user: ${DEV_USER}
cluster: kind-${CLUSTER_NAME}
Docker data: ${DOCKER_ROOT} (ALL images, containers, and volumes are
deleted, including any unrelated to NICo; path is preserved)
checkout: $(
if [[ "${KEEP_CHECKOUT}" == "1" ]]; then
printf 'preserved'
else
printf '%s' "${REPO_DIR}"
fi
)
host config: $(
if [[ "${KEEP_HOST_CONFIG}" == "1" ]]; then
printf 'preserved'
else
printf 'remove setup-owned overrides and docker membership'
fi
)
user tools/state: ~/.local DevSpace tools, kube/Helm/DevSpace/Docker caches,
REST integration temp files, and shell environment block
Docker packages: preserved
Docker data path: preserved after all Docker data is deleted
EOF
}
delete_kind_cluster() {
if [[ -x "${USER_HOME}/.local/bin/kind" ]] && \
run_as_user kind get clusters 2>/dev/null | grep -Fxq "${CLUSTER_NAME}"; then
log "Deleting kind cluster ${CLUSTER_NAME}"
run_as_user kind delete cluster --name "${CLUSTER_NAME}"
else
log "kind cluster ${CLUSTER_NAME} is already absent"
fi
}
clear_docker_data() {
log "Stopping Docker and containerd"
systemctl stop docker.service docker.socket containerd.service \
>/dev/null 2>&1 || true
log "Removing ALL Docker images, containers, and volumes from ${DOCKER_ROOT}"
rm -rf -- \
"${DOCKER_ROOT}/buildkit" \
"${DOCKER_ROOT}/containerd" \
"${DOCKER_ROOT}/containers" \
"${DOCKER_ROOT}/engine-id" \
"${DOCKER_ROOT}/image" \
"${DOCKER_ROOT}/network" \
"${DOCKER_ROOT}/overlay2" \
"${DOCKER_ROOT}/plugins" \
"${DOCKER_ROOT}/runtimes" \
"${DOCKER_ROOT}/swarm" \
"${DOCKER_ROOT}/tmp" \
"${DOCKER_ROOT}/trust" \
"${DOCKER_ROOT}/volumes"
}
restore_host_config() {
if [[ "${KEEP_HOST_CONFIG}" == "1" ]]; then
log "Restarting Docker with preserved host configuration"
systemctl start containerd.service docker.service
wait_for_docker
return
fi
log "Restoring Docker and containerd host configuration"
if [[ "${DOCKER_DATA_ROOT_IS_SETUP}" == "1" ]]; then
local daemon_tmp
daemon_tmp="$(mktemp)"
jq 'del(.["data-root"])' /etc/docker/daemon.json >"${daemon_tmp}"
if jq -e 'length == 0' "${daemon_tmp}" >/dev/null; then
rm -f -- /etc/docker/daemon.json
else
install -m 0644 "${daemon_tmp}" /etc/docker/daemon.json
fi
rm -f "${daemon_tmp}"
fi
rm -f -- \
/etc/systemd/system/docker.service.d/10-tls-compat.conf \
/etc/systemd/system/containerd.service.d/10-tls-compat.conf
if [[ "${CONTAINERD_ROOT_IS_SETUP}" == "1" && \
-e /etc/containerd/config.toml ]]; then
local config_tmp
config_tmp="$(mktemp)"
sed -E \
's|^root[[:space:]]*=.*$|root = "/var/lib/containerd"|' \
/etc/containerd/config.toml >"${config_tmp}"
install -m 0644 "${config_tmp}" /etc/containerd/config.toml
rm -f "${config_tmp}"
fi
if id -nG "${DEV_USER}" | tr ' ' '\n' | grep -Fxq docker; then
gpasswd -d "${DEV_USER}" docker >/dev/null
fi
systemctl daemon-reload
systemctl enable --now containerd.service docker.service
wait_for_docker
}
remove_shell_block() {
local rc_file rc_tmp has_end mode
local marker="# NICo DevSpace VM bootstrap"
local end_marker="# end NICo DevSpace VM bootstrap"
for rc_file in \
"${USER_HOME}/.cshrc" \
"${USER_HOME}/.bash_profile" \
"${USER_HOME}/.bash_login" \
"${USER_HOME}/.profile"; do
[[ -e "${rc_file}" ]] || continue
rc_tmp="$(mktemp)"
mode="$(stat -c '%a' "${rc_file}")"
has_end="$(awk -v marker="${marker}" -v end_marker="${end_marker}" '
$0 == marker { saw_marker = 1; next }
saw_marker && $0 == end_marker { print 1; exit }
' "${rc_file}")"
has_end="${has_end:-0}"
awk -v marker="${marker}" -v end_marker="${end_marker}" \
-v has_end="${has_end}" '
function flush_blanks() {
printf "%s", blanks
blanks = ""
}
in_block {
if ((has_end && $0 == end_marker) || (!has_end && ++legacy_lines == 2)) {
in_block = 0
}
next
}
$0 == marker {
blanks = ""
in_block = 1
legacy_lines = 0
next
}
$0 == "" {
blanks = blanks $0 ORS
next
}
{
flush_blanks()
print
}
END {
if (!in_block) {
flush_blanks()
}
}
' "${rc_file}" >"${rc_tmp}"
if [[ -n "$(tr -d '[:space:]' <"${rc_tmp}")" ]]; then
install -o "${DEV_USER}" -g "${USER_GROUP}" -m "${mode}" \
"${rc_tmp}" "${rc_file}"
else
rm -f -- "${rc_file}"
fi
rm -f "${rc_tmp}"
done
}
wait_for_docker() {
local _attempt
for _attempt in {1..30}; do
if docker info >/dev/null 2>&1; then
return
fi
sleep 1
done
die "Docker did not become ready"
}
remove_user_state() {
log "Removing setup-owned tools and user state"
if [[ -d "${USER_HOME}/.kube" ]]; then
local remaining_contexts
if remaining_contexts="$(
run_as_user kubectl config get-contexts -o name 2>/dev/null
)"; then
if [[ -z "${remaining_contexts}" ]]; then
rm -rf -- "${USER_HOME}/.kube"
else
log "Preserving ~/.kube because unrelated contexts remain"
fi
else
log "Preserving ~/.kube because Kubernetes contexts could not be enumerated"
fi
fi
rm -f -- \
"${USER_HOME}/.local/bin/devspace" \
"${USER_HOME}/.local/bin/helm" \
"${USER_HOME}/.local/bin/kind" \
"${USER_HOME}/.local/bin/kubectl"
rm -rf -- \
"${USER_HOME}/.cache/helm" \
"${USER_HOME}/.config/helm" \
"${USER_HOME}/.devspace" \
"${USER_HOME}/.docker/buildx" \
"${USER_HOME}/.docker/.token_seed" \
"${USER_HOME}/.docker/.token_seed.lock" \
"${USER_HOME}/Developer/_agent-tmp/devspace-rest"
remove_shell_block
rmdir "${USER_HOME}/.local/bin" "${USER_HOME}/.local" \
"${USER_HOME}/.docker" \
"${USER_HOME}/Developer/_agent-tmp" "${USER_HOME}/Developer" \
2>/dev/null || true
}
remove_checkout() {
if [[ "${KEEP_CHECKOUT}" == "1" || ! -e "${REPO_DIR}" ]]; then
return
fi
log "Deleting checkout ${REPO_DIR}"
rm -rf -- "${REPO_DIR}"
}
verify_reset() {
log "Verifying reset"
if docker ps -a --format '{{.Names}}' | grep -Fxq "${CLUSTER_NAME}-control-plane"; then
die "kind control-plane container still exists"
fi
if [[ "${KEEP_CHECKOUT}" != "1" && -e "${REPO_DIR}" ]]; then
die "checkout still exists: ${REPO_DIR}"
fi
if [[ -e "${USER_HOME}/.local/bin/devspace" ]]; then
die "DevSpace binary still exists"
fi
log "Reset complete; Docker packages and ${DOCKER_ROOT} were preserved"
if [[ -d "${DOCKER_ROOT}" ]]; then
df -h / "${DOCKER_ROOT}"
else
df -h /
fi
}
main() {
resolve_docker_root
detect_checkout
validate_checkout
validate_docker_root
validate_host_config
validate_host_confirmation
print_plan
if [[ "${APPLY}" != "1" ]]; then
log "Dry run only; pass --apply and --confirm-host to execute"
return
fi
delete_kind_cluster
clear_docker_data
restore_host_config
remove_user_state
remove_checkout
verify_reset
}
main "$@"