devzone 7.4.0 → 7.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/.devcontainer/devzone +13 -17
  2. package/.devcontainer/devzone-modules.sh +1 -1
  3. package/.devcontainer/devzone-repair.sh +2 -2
  4. package/.devcontainer/devzone-services.sh +7 -7
  5. package/.devcontainer/devzone-status.sh +5 -5
  6. package/.devcontainer/help.sh +3 -1
  7. package/.devcontainer/versions.env +3 -0
  8. package/.devcontainer/workspace-init.sh +8 -9
  9. package/.gitattributes +8 -0
  10. package/bin/SHA256SUMS +6 -6
  11. package/bin/devzone-darwin-amd64 +0 -0
  12. package/bin/devzone-darwin-arm64 +0 -0
  13. package/bin/devzone-linux-amd64 +0 -0
  14. package/bin/devzone-linux-arm64 +0 -0
  15. package/bin/devzone-windows-amd64.exe +0 -0
  16. package/bin/devzone-windows-arm64.exe +0 -0
  17. package/charts/devzone-mysql/values.yaml +1 -1
  18. package/charts/devzone-postgres/values.yaml +1 -1
  19. package/manage/devzone +2 -2
  20. package/manage/scripts/uninstall.sh +1 -1
  21. package/manage/scripts/workspace/argocd-workspace.sh +2 -2
  22. package/package.json +1 -1
  23. package/.devcontainer/catalog/app.sh +0 -202
  24. package/.devcontainer/catalog/catalog-watcher.sh +0 -127
  25. package/.devcontainer/catalog.sh +0 -208
  26. package/.devcontainer/chart-direct.sh +0 -49
  27. package/.devcontainer/cluster.sh +0 -388
  28. package/.devcontainer/clusters.sh +0 -86
  29. package/.devcontainer/devzone-cluster.sh +0 -209
  30. package/.devcontainer/isolation.sh +0 -65
  31. package/.devcontainer/kube-account.sh +0 -107
  32. package/.devcontainer/kube-prompt.sh +0 -42
  33. package/.devcontainer/memory.sh +0 -113
  34. package/.devcontainer/metrics-server.sh +0 -100
  35. package/.devcontainer/packages.sh +0 -228
  36. package/.devcontainer/password.sh +0 -148
  37. package/.devcontainer/ports-json.sh +0 -31
  38. package/.devcontainer/ports.sh +0 -132
@@ -1,388 +0,0 @@
1
- #!/usr/bin/env bash
2
- # devzone cluster (#280): the cluster where the modules that provide a service
3
- # are deployed (postgres; gitlab with #279). A single choice per instance, kept
4
- # in the state (line « cluster=<kubectl context> »): the services of an instance
5
- # live in the same place and move together (devzone cluster CTX).
6
- # Sourced library: the caller defines RACINE and ETAT.
7
- # cluster_saved chosen context, nothing if there is none
8
- # cluster_save CTX keeps the choice in the state
9
- # cluster_is_local CTX 0 if local cluster (config, no network)
10
- # cluster_candidates local, then dedicated kind, then remote
11
- # cluster_choose [CTX] [--confirm CTX]
12
- # CTX validated (remote: confirmation),
13
- # otherwise kept choice, otherwise menu
14
- # (terminal only), otherwise local default
15
- # cluster_prepare CTX creates the dedicated kind cluster if missing
16
- # cluster_release CTX deletes the dedicated kind cluster once empty
17
- # relay_start NAME CTX NS TARGET PORTS… port-forward kept alive
18
- # relay_active NAME / relay_stop NAME
19
- # account_* (kube-account.sh #430) limited account and admin context
20
-
21
- # Kind and host clusters, as remoteEnv of devcontainer.json: a docker exec or a
22
- # script run outside the container terminal does not have it, and the relay
23
- # must see the host cluster
24
- : "${KUBECONFIG:=$HOME/.kube/config:$HOME/.kube/hote.config}"
25
- export KUBECONFIG
26
-
27
- # Module state: defined by the caller; default value for the tests
28
- : "${ETAT:=${DEVZONE_STATE:-$HOME/.devzone.modules}}"
29
-
30
- # Safe state write (#417): lock, temporary file in the state folder, backup copy
31
- # .prev — shared by devzone, ports
32
-
33
- # Instance name: empty or « devzone » = default instance (no suffix)
34
- : "${DEVZONE_INSTANCE:=}"
35
- INSTANCE_SUFFIX=""
36
- if [ -n "$DEVZONE_INSTANCE" ] && [ "$DEVZONE_INSTANCE" != "devzone" ]; then
37
- INSTANCE_SUFFIX="-${DEVZONE_INSTANCE}"
38
- fi
39
-
40
- # Dedicated kind cluster, created on the host Docker; name suffixed per instance
41
- CLUSTER_DEDICATED=kind-devzone${INSTANCE_SUFFIX}
42
- # Label set on every namespace created by devzone : a dedicated kind without a
43
- # labelled namespace has no use left and can be deleted
44
- CLUSTER_LABEL=app.kubernetes.io/managed-by=devzone
45
-
46
- # Limited Kubernetes account (#430): account_setup, account_allow,
47
- # account_remove, account_token, account_admin (admin context); the installers
48
- # use it, cluster_release too
49
- # shellcheck source=SCRIPTDIR/kube-account.sh
50
- . "$(dirname "${BASH_SOURCE[0]}")/kube-account.sh"
51
-
52
- cluster_saved() {
53
- grep -E '^cluster=' "$ETAT" 2>/dev/null | tail -1 | cut -d= -f2- || true
54
- }
55
-
56
- cluster_save() {
57
- DEVZONE_STATE="$ETAT" bash "${ICI:-$(dirname "${BASH_SOURCE[0]}")}/devzone" state write cluster "$1"
58
- }
59
-
60
- cluster_is_dedicated() {
61
- [ "$1" = "${CLUSTER_DEDICATED}" ]
62
- }
63
-
64
- # Local cluster: read from the KUBECONFIG configuration, without network call.
65
- # The dedicated kind is always local; a host cluster is local when its server
66
- # listens on a loopback address (127.0.0.1, localhost, ::1, host.docker.internal).
67
- cluster_is_local() {
68
- local ctx="$1" cluster server host
69
- cluster_is_dedicated "$ctx" && return 0
70
- cluster="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.cluster}" 2>/dev/null || true)"
71
- [ -n "$cluster" ] || return 1
72
- server="$(kubectl config view -o jsonpath="{.clusters[?(@.name==\"$cluster\")].cluster.server}" 2>/dev/null || true)"
73
- [ -n "$server" ] || return 1
74
- # Rule by name (old rule of metrics-server.sh)
75
- case "$ctx" in
76
- kind-* | docker-desktop | rancher-desktop | orbstack | minikube) return 0 ;;
77
- esac
78
- # Host extraction (IPv6 addresses in brackets handled)
79
- host="${server#*://}"
80
- host="${host%%/*}"
81
- if [[ "$host" == \[* ]]; then
82
- host="${host#[}"
83
- host="${host%%]*}"
84
- else
85
- host="${host%%:*}"
86
- fi
87
- case "$host" in
88
- 127.0.0.1 | localhost | ::1 | host.docker.internal) return 0 ;;
89
- *-control-plane) return 0 ;;
90
- esac
91
- return 1
92
- }
93
-
94
- # Sets the « devzone » context (cluster + user) on the given context (default:
95
- # the current one): after an installation on a new machine, --context devzone
96
- # targets the chosen services cluster from the start (#396)
97
- devzone_context_set() {
98
- local ctx cluster user
99
- ctx="${1:-$(kubectl config current-context 2>/dev/null || true)}"
100
- if [ -z "$ctx" ]; then
101
- return 1
102
- fi
103
- cluster="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.cluster}" 2>/dev/null || true)"
104
- user="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.user}" 2>/dev/null || true)"
105
- if [ -n "$cluster" ]; then
106
- kubectl config set-context devzone --cluster="$cluster" --user="$user" >/dev/null 2>&1
107
- else
108
- return 1
109
- fi
110
- }
111
-
112
- cluster_reachable() {
113
- kubectl --context "$1" --request-timeout=3s get --raw /readyz >/dev/null 2>&1
114
- }
115
-
116
- # Host contexts: local ones first, then the dedicated kind, finally the remote ones
117
- cluster_candidates() {
118
- local host="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" c
119
- if [ -f "$host" ]; then
120
- # 1. Local clusters of the host (server on a loopback address)
121
- for c in $(KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null); do
122
- cluster_is_dedicated "$c" && continue
123
- if cluster_is_local "$c" && cluster_reachable "$c"; then
124
- echo "$c"
125
- fi
126
- done
127
- fi
128
- # 2. Dedicated kind cluster
129
- echo "$CLUSTER_DEDICATED"
130
- if [ -f "$host" ]; then
131
- # 3. Remote clusters of the host
132
- for c in $(KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null); do
133
- cluster_is_dedicated "$c" && continue
134
- if ! cluster_is_local "$c" && cluster_reachable "$c"; then
135
- echo "$c"
136
- fi
137
- done
138
- fi
139
- return 0
140
- }
141
-
142
- # Menu on stderr (stdout carries the answer); Enter = first candidate
143
- cluster_menu() {
144
- local candidates=() c i=1 answer
145
- while IFS= read -r c; do candidates+=("$c"); done < <(cluster_candidates)
146
- {
147
- echo "devzone : on which cluster should the devzone services be installed?"
148
- for c in "${candidates[@]}"; do
149
- if cluster_is_dedicated "$c"; then
150
- echo " $i) $c dedicated kind cluster, in the container (default)"
151
- elif cluster_is_local "$c"; then
152
- echo " $i) $c local cluster"
153
- else
154
- echo " $i) $c remote cluster"
155
- fi
156
- i=$((i + 1))
157
- done
158
- [ -f "${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" ] ||
159
- echo " (host clusters not visible: devzone enable kubernetes)"
160
- } >&2
161
- printf 'Number [1]: ' >&2
162
- read -r answer || answer=""
163
- answer="${answer:-1}"
164
- case "$answer" in
165
- *[!0-9]* | 0) echo "devzone : invalid choice: $answer" >&2; return 2 ;;
166
- esac
167
- if [ "$answer" -gt "${#candidates[@]}" ]; then
168
- echo "devzone : invalid choice: $answer" >&2
169
- return 2
170
- fi
171
- echo "${candidates[$((answer - 1))]}"
172
- }
173
-
174
- # Interactive terminal; DEVZONE_INTERACTIVE=1 for the tests (stdin redirected)
175
- cluster_interactive() {
176
- [ "${DEVZONE_INTERACTIVE:-}" = 1 ] || [ -t 0 ]
177
- }
178
-
179
- cluster_choose() {
180
- local wanted="" confirm="" c answer arg
181
- while [ "$#" -gt 0 ]; do
182
- arg="$1"
183
- shift
184
- case "$arg" in
185
- --confirm)
186
- if [ "$#" -eq 0 ]; then
187
- echo "devzone : --confirm needs a context" >&2
188
- return 2
189
- fi
190
- confirm="$1"
191
- shift
192
- ;;
193
- --confirm=*) confirm="${arg#*=}" ;;
194
- -*)
195
- echo "devzone : unknown argument: $arg (cluster_choose [CTX] [--confirm CTX])" >&2
196
- return 2
197
- ;;
198
- *)
199
- if [ -n "$wanted" ]; then
200
- echo "devzone : unknown argument: $arg (cluster_choose [CTX] [--confirm CTX])" >&2
201
- return 2
202
- fi
203
- wanted="$arg"
204
- ;;
205
- esac
206
- done
207
- if [ -n "$wanted" ]; then
208
- while IFS= read -r c; do
209
- [ "$c" = "$wanted" ] && break
210
- done < <(cluster_candidates)
211
- if [ "$c" != "$wanted" ]; then
212
- echo "devzone : unknown or unreachable cluster: $wanted; candidates: $(cluster_candidates | tr '\n' ' ')" >&2
213
- return 2
214
- fi
215
- # Remote: confirmation required. --confirm counts only if it is exactly
216
- # the requested context; DEVZONE_CLUSTER_REMOTE is still accepted for
217
- # the tests and CI
218
- if ! cluster_is_local "$wanted"; then
219
- if [ -n "$confirm" ]; then
220
- if [ "$confirm" != "$wanted" ]; then
221
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
222
- return 2
223
- fi
224
- elif [ -n "${DEVZONE_CLUSTER_REMOTE:-}" ]; then
225
- if [ "${DEVZONE_CLUSTER_REMOTE}" != "$wanted" ]; then
226
- echo "devzone : remote cluster refused: $wanted (DEVZONE_CLUSTER_REMOTE must be exactly $wanted)" >&2
227
- return 2
228
- fi
229
- elif cluster_interactive; then
230
- printf 'remote cluster: %s ; type its name to confirm: ' "$wanted" >&2
231
- read -r answer || answer=""
232
- if [ "$answer" != "$wanted" ]; then
233
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
234
- return 2
235
- fi
236
- else
237
- echo "devzone : remote cluster refused: $wanted (--confirm $wanted)" >&2
238
- return 2
239
- fi
240
- fi
241
- echo "$wanted"
242
- return 0
243
- fi
244
- c="$(cluster_saved)"
245
- if [ -n "$c" ]; then
246
- echo "$c"
247
- elif cluster_interactive; then
248
- cluster_menu
249
- else
250
- # Default: local only (never remote by default)
251
- local host_choice=""
252
- if [ -f "${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" ]; then
253
- host_choice="$(KUBECONFIG="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}" kubectl config current-context 2>/dev/null || true)"
254
- fi
255
- if [ -n "$host_choice" ] && cluster_is_local "$host_choice" && cluster_reachable "$host_choice"; then
256
- echo "$host_choice"
257
- else
258
- echo "$CLUSTER_DEDICATED"
259
- fi
260
- fi
261
- }
262
-
263
- # Runs kind without switching kubectl's current context (#366): the previous
264
- # one is restored, or removed if there was none. The kind exit code is returned
265
- # even under set -e: the restore happens first.
266
- kind_keep_context() {
267
- local previous code=0
268
- previous="$(kubectl config current-context 2>/dev/null || true)"
269
- kind "$@" || code=$?
270
- if [ -n "$previous" ]; then
271
- kubectl config use-context "$previous" >/dev/null 2>&1 || true
272
- else
273
- KUBECONFIG="$HOME/.kube/config" kubectl config unset current-context >/dev/null 2>&1 || true
274
- fi
275
- return "$code"
276
- }
277
-
278
- # Creates a kind cluster without switching kubectl's current context.
279
- kind_create_keep_context() {
280
- kind_keep_context create cluster "$@"
281
- }
282
-
283
- cluster_prepare() {
284
- local ctx="$1"
285
- if [ "$ctx" = "$CLUSTER_DEDICATED" ]; then
286
- if ! kind get clusters 2>/dev/null | grep -qx "devzone${INSTANCE_SUFFIX}"; then
287
- echo "▶ Creating the kind devzone cluster (about 1 min)…"
288
- # Busy host Docker (its Kubernetes, other clusters): the node API can
289
- # restart during initialization and kind gives up within seconds. A
290
- # second attempt, on a fresh cluster.
291
- if ! kind_create_keep_context --name "devzone${INSTANCE_SUFFIX}" --wait 120s; then
292
- echo "▶ Retrying the kind cluster creation…"
293
- kind delete cluster --name "devzone${INSTANCE_SUFFIX}" >/dev/null 2>&1 || true
294
- kind_create_keep_context --name "devzone${INSTANCE_SUFFIX}" --wait 120s || return 1
295
- fi
296
- fi
297
- elif ! cluster_reachable "$ctx"; then
298
- echo "devzone : cluster $ctx unreachable (kubernetes module enabled? cluster started?)" >&2
299
- return 1
300
- fi
301
- # If it has just been created, the services cluster must carry the devzone
302
- # context at once, otherwise the installation targets the old cluster (#396)
303
- devzone_context_set "$ctx" || echo "devzone : devzone context not set on $ctx" >&2
304
- }
305
-
306
- # Dedicated kind with no devzone namespace left: deleted (it only uses memory).
307
- # Never a host cluster.
308
- cluster_release() {
309
- local ctx="$1" left
310
- cluster_is_dedicated "$ctx" || return 0
311
- kind get clusters 2>/dev/null | grep -qx "devzone${INSTANCE_SUFFIX}" || return 0
312
- # Reading the namespaces labelled by the administrator (#430): the namespace
313
- # list is refused to the limited account
314
- left="$(account_admin "$ctx" get namespaces -l "$CLUSTER_LABEL" -o name 2>/dev/null || true)"
315
- if [ -z "$left" ]; then
316
- kind delete cluster --name "devzone${INSTANCE_SUFFIX}" >/dev/null 2>&1 || true
317
- echo "▶ kind devzone cluster removed (no service left)."
318
- fi
319
- }
320
-
321
- # ---- Relay: port of a cluster service on the container's 127.0.0.1 ----
322
- # kubectl port-forward works on every cluster whose API answers (dedicated kind
323
- # or host cluster), without exposing a port on the nodes. It stops when the pod
324
- # restarts: the loop restarts it. PID and log in workspace/.<name>/ (module
325
- # folder, ignored by git).
326
- relay_dir() {
327
- echo "$RACINE/workspace/.$1"
328
- }
329
-
330
- relay_active() {
331
- local pid
332
- pid="$(cat "$(relay_dir "$1")/relais.pid" 2>/dev/null || true)"
333
- [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null
334
- }
335
-
336
- relay_start() {
337
- local name="$1" ctx="$2" ns="$3" target="$4" dir
338
- shift 4
339
- dir="$(relay_dir "$name")"
340
- mkdir -p "$dir"
341
- relay_active "$name" && return 0
342
- # setsid: separate session, the relay survives the end of the script that
343
- # started it (postStartCommand, devzone workspace shell); absent on macOS (bash 3.2 tests)
344
- local detached=()
345
- command -v setsid >/dev/null 2>&1 && detached=(setsid)
346
- # port-forward targets one precise pod and sees its disappearance only at the
347
- # next connection, which fails. For a service, the loop therefore follows the
348
- # ready pod behind it (UID in its endpoints): waits until there is one, then
349
- # restarts port-forward as soon as it changes (pod restarted).
350
- # shellcheck disable=SC2016 # variables expanded by the relay's bash
351
- ${detached[@]+"${detached[@]}"} nohup bash -c '
352
- pod() {
353
- case "$2" in
354
- svc/*) kubectl --context "$0" -n "$1" get endpoints "${2#svc/}" -o jsonpath="{.subsets[*].addresses[*].targetRef.uid}" 2>/dev/null ;;
355
- *) echo fixe ;;
356
- esac
357
- }
358
- while :; do
359
- until [ -n "$(pod "$@")" ]; do sleep 2; done
360
- actuel="$(pod "$@")"
361
- kubectl --context "$0" -n "$1" port-forward --address 127.0.0.1 "$2" "${@:3}" &
362
- pf=$!
363
- while kill -0 "$pf" 2>/dev/null; do
364
- sleep 5
365
- [ "$(pod "$@")" = "$actuel" ] || { kill "$pf" 2>/dev/null; break; }
366
- done
367
- wait "$pf" 2>/dev/null
368
- sleep 1
369
- done' \
370
- "$ctx" "$ns" "$target" "$@" >"$dir/relais.log" 2>&1 &
371
- echo "$!" >"$dir/relais.pid"
372
- }
373
-
374
- relay_stop() {
375
- local file pid children
376
- file="$(relay_dir "$1")/relais.pid"
377
- pid="$(cat "$file" 2>/dev/null || true)"
378
- if [ -n "$pid" ]; then
379
- # Children (port-forward) listed before: once the loop is killed, they
380
- # are reattached to init and pgrep -P no longer sees them. The loop first,
381
- # otherwise it would restart the port-forward.
382
- children="$(pgrep -P "$pid" 2>/dev/null || true)"
383
- kill "$pid" 2>/dev/null || true
384
- # shellcheck disable=SC2086 # list of PIDs
385
- [ -z "$children" ] || kill $children 2>/dev/null || true
386
- fi
387
- rm -f "$file"
388
- }
@@ -1,86 +0,0 @@
1
- #!/usr/bin/env bash
2
- # View of the connected clusters (#337): visible contexts, type, state, namespace.
3
- ICI="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4
- if ! declare -F cluster_is_local >/dev/null 2>&1; then
5
- # shellcheck source=SCRIPTDIR/cluster.sh
6
- # shellcheck disable=SC1091
7
- . "$ICI/cluster.sh"
8
- fi
9
-
10
- # Shows the view of the connected clusters.
11
- clusters_show() {
12
- local contexts=() ctx ctx_work ctx_services tmp i ns type state mark marks
13
- ctx_work="$(kubectl config current-context 2>/dev/null || true)"
14
- ctx_services="$(cluster_saved 2>/dev/null || true)"
15
- while IFS= read -r ctx; do
16
- [ -n "$ctx" ] || continue
17
- contexts+=("$ctx")
18
- done < <(kubectl config get-contexts -o name 2>/dev/null || true)
19
-
20
- if [ "${#contexts[@]}" -eq 0 ]; then
21
- echo "no cluster: kind create cluster, or devzone enable kubernetes"
22
- return 0
23
- fi
24
-
25
- printf '%s %-14s %-8s %-12s %s\n' " " "CONTEXT" "TYPE" "STATE" "NAMESPACE"
26
- tmp="$(mktemp -d)"
27
- for i in "${!contexts[@]}"; do
28
- ctx="${contexts[$i]}"
29
- (
30
- if kubectl --context "$ctx" --request-timeout=3s get --raw /readyz >/dev/null 2>&1; then
31
- echo reachable
32
- else
33
- echo unreachable
34
- fi
35
- ) >"$tmp/$i" &
36
- done
37
- wait
38
- for i in "${!contexts[@]}"; do
39
- ctx="${contexts[$i]}"
40
- state="$(cat "$tmp/$i" 2>/dev/null || echo unreachable)"
41
- if cluster_is_local "$ctx" >/dev/null 2>&1; then
42
- type=local
43
- else
44
- type=remote
45
- fi
46
- ns="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.namespace}" 2>/dev/null || true)"
47
- [ -n "$ns" ] || ns=default
48
- mark=""
49
- marks=0
50
- if [ "$ctx" = "$ctx_work" ]; then
51
- mark+="★"
52
- marks=$((marks + 1))
53
- fi
54
- if [ "$ctx" = "$ctx_services" ]; then
55
- mark+="●"
56
- marks=$((marks + 1))
57
- fi
58
- while [ "$marks" -lt 2 ]; do
59
- mark+=" "
60
- marks=$((marks + 1))
61
- done
62
- printf '%s %-14s %-8s %-12s %s\n' "$mark" "$ctx" "$type" "$state" "$ns"
63
- done
64
- rm -rf "$tmp"
65
- echo
66
- echo "★ working cluster (kubectl, k9s, helm) · ● devzone services cluster (devzone cluster)"
67
- # Display of the NetworkPolicy isolation state for the services cluster
68
- if [ -n "$ctx_services" ] && cluster_is_local "$ctx_services"; then
69
- if kubectl --context "$ctx_services" get networkpolicy devzone-meme-namespace -n devzone-postgres >/dev/null 2>&1; then
70
- printf ' ✔ isolated (NetworkPolicy) → %s\n' "$ctx_services"
71
- else
72
- printf ' ⚠ isolated (NetworkPolicy missing) → %s\n' "$ctx_services"
73
- fi
74
- printf ' devzone → %s\n' "$ctx_services"
75
- fi
76
- return 0
77
- }
78
-
79
- # Aliases of the devzone clusters command.
80
- clusters() { clusters_show "$@"; }
81
- # Alias for the tests and direct calls.
82
- devzone_clusters() { clusters_show "$@"; }
83
-
84
- if [ "${BASH_SOURCE[0]}" = "$0" ]; then
85
- clusters_show "$@"
86
- fi
@@ -1,209 +0,0 @@
1
- #!/usr/bin/env bash
2
- # shellcheck shell=bash
3
- # devzone, working cluster: change, use.
4
- # Sourced by .devcontainer/devzone (split of the 1 600 lines file, #722):
5
- # functions only, shared variables are defined in devzone.
6
-
7
- # All the databases disappear: confirmation in the terminal (without a terminal,
8
- # the command is explicit); a yes or --yes sets DELETE_DATA, read by
9
- # apply_mysql to run delete-data (#441); only --yes reaches this function.
10
- confirm_removal() {
11
- local app="$1" answer arg
12
- DELETE_DATA=""
13
- [ "$(value_of "$app")" = on ] || return 0
14
- for arg in "${@:2}"; do
15
- case "$arg" in --yes) DELETE_DATA=1 ;; esac
16
- done
17
- [ -z "$DELETE_DATA" ] || return 0
18
- cluster_interactive || return 0
19
- # mysql: the question is about the namespace (volume and databases stay);
20
- # postgres: everything goes, the refusal cancels; gitlab: the Rails secret
21
- # stays, its deletion only happens on a full uninstall on purpose
22
- if [ "$app" = mysql ]; then
23
- printf 'Disabling mysql removes the server; the volume and databases stay. Also delete the devzone-mysql namespace and its volumes? [y/N] ' >&2
24
- elif [ "$app" = gitlab ]; then
25
- printf 'Disabling gitlab keeps the Rails secret (browser sessions stay valid at reinstall). Also delete the Rails secret? [y/N] ' >&2
26
- else
27
- printf 'Disabling %s deletes all its databases. Continue? [y/N] ' "$app" >&2
28
- fi
29
- read -r answer || answer=""
30
- case "$answer" in
31
- y | Y | yes) DELETE_DATA=1; return 0 ;;
32
- esac
33
- # refusal: mysql and gitlab still disable (data kept), postgres cancels
34
- case "$app" in
35
- mysql | gitlab) return 0 ;;
36
- *) echo "Aborted: nothing was deleted."; return 1 ;;
37
- esac
38
- }
39
-
40
- # devzone cluster [CTX] [--yes]: shows the services cluster, or moves the
41
- # services to it (uninstalled from the old one, installed on the new one: data
42
- # not migrated, hence the confirmation)
43
- change_cluster() {
44
- local wanted="" yes="" confirm_ctx="" expect="" arg current new answer ctx_old ctx_new NS
45
- NS=devzone-postgres
46
- for arg in "$@"; do
47
- if [ -n "$expect" ]; then
48
- confirm_ctx="$arg"
49
- expect=""
50
- continue
51
- fi
52
- case "$arg" in
53
- --yes) yes=1 ;;
54
- --confirm) expect=1 ;;
55
- -*) echo "devzone : unknown argument: $arg (devzone cluster [CTX] [--yes] [--confirm CTX])" >&2; return 2 ;;
56
- *) wanted="$arg" ;;
57
- esac
58
- done
59
- if [ -n "$expect" ]; then
60
- echo "devzone : --confirm expects a context" >&2
61
- return 2
62
- fi
63
- current="$(cluster_saved)"
64
- if [ -z "$wanted" ]; then
65
- if [ -z "$current" ]; then
66
- echo "devzone : no cluster chosen (set when a module is enabled: devzone enable postgres)"
67
- elif cluster_is_dedicated "$current"; then
68
- echo "devzone : cluster $current (dedicated kind, in the container)"
69
- else
70
- echo "devzone : cluster $current (host cluster)"
71
- fi
72
- echo " possible: $(cluster_candidates | tr '\n' ' ')"
73
- return 0
74
- fi
75
- if [ -n "$confirm_ctx" ]; then
76
- new="$(cluster_choose "$wanted" --confirm "$confirm_ctx")" || return 2
77
- else
78
- new="$(cluster_choose "$wanted")" || return 2
79
- fi
80
- if [ "$new" = "$current" ]; then
81
- echo "devzone : already on $new, nothing to do."
82
- return 0
83
- fi
84
- ctx_old="$current"
85
- ctx_new="$new"
86
- if [ "$(value_of postgres)" = on ]; then
87
- if [ -z "$yes" ]; then
88
- if ! cluster_interactive; then
89
- echo "devzone : the postgres databases will be deleted from $ctx_old (not migrated): devzone cluster $ctx_new --yes" >&2
90
- return 1
91
- fi
92
- printf 'postgres will be reinstalled on %s, its databases deleted from %s (not migrated). Continue? [y/N] ' "$ctx_new" "$ctx_old" >&2
93
- read -r answer || answer=""
94
- case "$answer" in
95
- y | Y | yes) ;;
96
- *) echo "Aborted: nothing changed."; return 0 ;;
97
- esac
98
- fi
99
- # Removes PostgreSQL from the old cluster (explicit context);
100
- # namespace and RoleBinding by the administrator (#430)
101
- export KUBECONFIG="$HOME/.kube/config:$HOME/.kube/hote.config"
102
- account_remove "$ctx_old" "$NS" >/dev/null 2>&1 || true
103
- account_admin "$ctx_old" delete namespace "$NS" --wait=true >/dev/null 2>&1 || true
104
- cluster_release "$ctx_old"
105
- # The new cluster becomes the devzone context
106
- cluster_save "$ctx_new"
107
- # Sets the devzone context on the new cluster (cluster + user:
108
- # important command, failure shown and rendered #396)
109
- if ! devzone_context_set "$ctx_new"; then
110
- echo "devzone : cannot set the devzone context on $ctx_new" >&2
111
- return 1
112
- fi
113
- # Installs PostgreSQL on the new cluster (via --context devzone which now points to it)
114
- devzone postgres install
115
- else
116
- cluster_save "$ctx_new"
117
- # Sets the devzone context on the new cluster (cluster + user:
118
- # important command, failure shown and rendered #396)
119
- if ! devzone_context_set "$ctx_new"; then
120
- echo "devzone : cannot set the devzone context on $ctx_new" >&2
121
- return 1
122
- fi
123
- fi
124
- echo "devzone : services cluster: $ctx_new"
125
- }
126
-
127
- # Switches the user's working cluster (#341). Never touches the devzone services
128
- # cluster: no cluster_save, no cluster= line in the state.
129
- use_cluster() {
130
- local ctx="" confirm_ctx="" namespace="" answer arg current
131
- while [ "$#" -gt 0 ]; do
132
- arg="$1"
133
- shift
134
- case "$arg" in
135
- -n | --namespace)
136
- if [ "$#" -eq 0 ]; then
137
- echo "devzone : -n expects a namespace" >&2
138
- return 2
139
- fi
140
- namespace="$1"
141
- shift
142
- ;;
143
- --namespace=*) namespace="${arg#*=}" ;;
144
- --confirm)
145
- if [ "$#" -eq 0 ]; then
146
- echo "devzone : --confirm expects a context" >&2
147
- return 2
148
- fi
149
- confirm_ctx="$1"
150
- shift
151
- ;;
152
- --confirm=*) confirm_ctx="${arg#*=}" ;;
153
- -*)
154
- echo "devzone : unknown argument: $arg (devzone use CTX [-n NS] [--confirm CTX])" >&2
155
- return 2
156
- ;;
157
- *)
158
- if [ -n "$ctx" ]; then
159
- echo "devzone : unknown argument: $arg (devzone use CTX [-n NS] [--confirm CTX])" >&2
160
- return 2
161
- fi
162
- ctx="$arg"
163
- ;;
164
- esac
165
- done
166
- if [ -z "$ctx" ]; then
167
- if [ -z "$namespace" ]; then
168
- echo "devzone : missing context: devzone use CTX" >&2
169
- return 2
170
- fi
171
- current="$(kubectl config current-context 2>/dev/null || true)"
172
- if [ -z "$current" ]; then
173
- echo "devzone : no current context" >&2
174
- return 2
175
- fi
176
- ctx="$current"
177
- fi
178
- if ! kubectl config get-contexts -o name 2>/dev/null | grep -qxF -- "$ctx"; then
179
- echo "devzone : unknown context: $ctx (see kubectl config get-contexts)" >&2
180
- return 2
181
- fi
182
- current="$(kubectl config current-context 2>/dev/null || true)"
183
- if [ "$ctx" != "$current" ]; then
184
- if ! cluster_is_local "$ctx"; then
185
- if [ -n "$confirm_ctx" ]; then
186
- if [ "$confirm_ctx" != "$ctx" ]; then
187
- echo "devzone : remote cluster refused: $ctx (--confirm $ctx)" >&2
188
- return 2
189
- fi
190
- elif cluster_interactive; then
191
- printf 'remote cluster: %s; type its name to confirm: ' "$ctx" >&2
192
- read -r answer || answer=""
193
- if [ "$answer" != "$ctx" ]; then
194
- echo "devzone : remote cluster refused: $ctx (--confirm $ctx)" >&2
195
- return 2
196
- fi
197
- else
198
- echo "devzone : remote cluster refused: $ctx (--confirm $ctx)" >&2
199
- return 2
200
- fi
201
- fi
202
- kubectl config use-context "$ctx" >/dev/null
203
- fi
204
- if [ -n "$namespace" ]; then
205
- kubectl config set-context "$ctx" --namespace "$namespace" >/dev/null
206
- fi
207
- echo "working cluster: $ctx"
208
- [ -z "$namespace" ] || echo "namespace: $namespace"
209
- }