devzone 7.4.1 → 7.5.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 (39) hide show
  1. package/.devcontainer/devzone +18 -60
  2. package/.devcontainer/devzone-modules.sh +8 -3
  3. package/.devcontainer/devzone-services.sh +20 -20
  4. package/.devcontainer/devzone-status.sh +14 -11
  5. package/.devcontainer/services-admin.sh +1 -1
  6. package/.devcontainer/versions.env +5 -0
  7. package/README.md +2 -2
  8. package/bin/SHA256SUMS +6 -6
  9. package/bin/devzone-darwin-amd64 +0 -0
  10. package/bin/devzone-darwin-arm64 +0 -0
  11. package/bin/devzone-linux-amd64 +0 -0
  12. package/bin/devzone-linux-arm64 +0 -0
  13. package/bin/devzone-windows-amd64.exe +0 -0
  14. package/bin/devzone-windows-arm64.exe +0 -0
  15. package/manage/devzone +8 -7
  16. package/manage/scripts/create-mode.sh +3 -3
  17. package/manage/scripts/uninstall.sh +3 -4
  18. package/manage/scripts/workspace/argocd-services.sh +1 -1
  19. package/manage/workspace.sh +11 -25
  20. package/package.json +1 -1
  21. package/.devcontainer/ai/host-check.sh +0 -173
  22. package/.devcontainer/ai/host-models.sh +0 -200
  23. package/.devcontainer/devzone-ai-guard.sh +0 -98
  24. package/.devcontainer/devzone-ai.sh +0 -201
  25. package/.devcontainer/devzone-repair.sh +0 -85
  26. package/.devcontainer/devzone-skill.sh +0 -97
  27. package/.devcontainer/devzone-version.sh +0 -78
  28. package/.devcontainer/help.sh +0 -44
  29. package/manage/scripts/create-cluster.sh +0 -107
  30. package/manage/scripts/devzone-update.sh +0 -237
  31. package/manage/scripts/docker.sh +0 -43
  32. package/manage/scripts/workspace/ports-apps.sh +0 -156
  33. package/manage/scripts/workspace/ports-workspace.sh +0 -235
  34. package/manage/scripts/workspace/services-workspace.sh +0 -193
  35. package/manage/scripts/workspace/workspace-clone.sh +0 -12
  36. package/manage/scripts/workspace/workspace-copies.sh +0 -44
  37. package/manage/scripts/workspace/workspace-egress.sh +0 -184
  38. package/manage/scripts/workspace/workspace-update.sh +0 -106
  39. package/manage/scripts/workspace/workspace-wait.sh +0 -56
@@ -1,237 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Update of a devzone (devzone update [--yes] [--no-rebuild]): replaces
3
- # the files of the package, backs up the replaced or removed ones, removes the
4
- # old layout, then offers the rebuild. Sourced by manage/devzone, after
5
- # devzone-manifest.sh, which provides the manifest helpers.
6
- # shellcheck disable=SC2154 # PAQUET: set by manage/devzone
7
-
8
- # Starts the port watcher again when the legacy names migration stopped it: it
9
- # leaves the file restart in the watcher folder, which a start or a stop by the
10
- # user removes, so a watcher the user stopped is never started here. Called once
11
- # the new files are in place. DEVZONE_PORTS_RESTART replaces the command (tests).
12
- restart_ports_watcher() {
13
- local folder="$1" marker="$1/.devzone.workspace-ports/restart"
14
- [ -f "$marker" ] || return 0
15
- rm -f "$marker"
16
- if [ -n "${DEVZONE_PORTS_RESTART:-}" ]; then
17
- (cd "$folder" && sh -c "$DEVZONE_PORTS_RESTART")
18
- else
19
- (cd "$folder" && bash manage/workspace.sh ports start)
20
- fi || echo "devzone : port watcher not restarted: devzone workspace ports start" >&2
21
- }
22
-
23
- # Six steps of the update design (#349)
24
- update_workstation() {
25
- local assume_yes="" no_rebuild="" arg folder package installed package_list old_list
26
- local missing rel current expected answer backup base n old f
27
- for arg in "$@"; do
28
- case "$arg" in
29
- --yes) assume_yes=1 ;;
30
- --no-rebuild) no_rebuild=1 ;;
31
- *)
32
- echo "✖ unknown option: $arg (devzone update [--yes] [--no-rebuild])" >&2
33
- exit 1
34
- ;;
35
- esac
36
- done
37
- if [ ! -d .devcontainer ]; then
38
- echo "✖ Run this in a devzone folder (.devcontainer not found here)." >&2
39
- exit 1
40
- fi
41
-
42
- folder="$(pwd)"
43
- package="$(version)"
44
- installed="$(folder_version "$folder")"
45
- work="$(mktemp -d)"
46
- trap 'rm -rf "$work"' EXIT
47
- package_list="$work/package-files"
48
- old_list="$work/old-files"
49
- relative_package_files >"$package_list"
50
- manifest_files "$folder" | LC_ALL=C sort >"$old_list"
51
-
52
- # 1. Versions and package files present
53
- missing=""
54
- while IFS= read -r rel; do
55
- [ -f "$folder/$rel" ] || missing=1
56
- done <"$package_list"
57
- if [ "$installed" = "$package" ] && [ -z "$missing" ]; then
58
- echo "✔ devzone $installed: already up to date."
59
- echo "For the latest published version: npx devzone@latest update"
60
- return 0
61
- fi
62
- echo "devzone: folder $installed, package $package"
63
-
64
- # 2. Files modified locally (checksum different from the manifest; without
65
- # a manifest, different from the one of the package)
66
- : >"$work/modified"
67
- : >"$work/to_save"
68
- : >"$work/to_remove"
69
- : >"$work/old_trees"
70
- while IFS= read -r rel; do
71
- [ -f "$folder/$rel" ] || continue
72
- printf '%s\n' "$rel" >>"$work/to_save"
73
- current="$(checksum "$folder/$rel")"
74
- if expected="$(manifest_checksum "$folder" "$rel")"; then
75
- :
76
- else
77
- expected="$(checksum "$PAQUET/$rel")"
78
- fi
79
- [ "$current" = "$expected" ] || printf '%s\n' "$rel" >>"$work/modified"
80
- done <"$package_list"
81
- while IFS= read -r rel; do
82
- [ -n "$rel" ] || continue
83
- if ! path_allowed "$rel"; then
84
- echo "devzone: manifest: refused path: $rel" >&2
85
- continue
86
- fi
87
- in_list "$package_list" "$rel" && continue
88
- is_protected "$rel" && continue
89
- if ! inside_folder "$folder" "$rel"; then
90
- echo "devzone: manifest: refused path: $rel" >&2
91
- continue
92
- fi
93
- printf '%s\n' "$rel" >>"$work/to_remove"
94
- [ -f "$folder/$rel" ] || continue
95
- printf '%s\n' "$rel" >>"$work/to_save"
96
- if expected="$(manifest_checksum "$folder" "$rel")"; then
97
- [ "$(checksum "$folder/$rel")" = "$expected" ] || printf '%s\n' "$rel" >>"$work/modified"
98
- else
99
- printf '%s\n' "$rel" >>"$work/modified"
100
- fi
101
- done <"$old_list"
102
- for old in core scripts; do
103
- [ -d "$folder/$old" ] || continue
104
- old_tree "$folder" "$old" || continue
105
- printf '%s\n' "$old" >>"$work/old_trees"
106
- while IFS= read -r f; do
107
- rel="${f#"$folder"/}"
108
- if ! path_allowed "$rel"; then
109
- echo "devzone: manifest: refused path: $rel" >&2
110
- continue
111
- fi
112
- is_protected "$rel" && continue
113
- # A file unknown to the old package belongs to the user
114
- known_old_file "$rel" || continue
115
- printf '%s\n' "$rel" >>"$work/to_remove"
116
- printf '%s\n' "$rel" >>"$work/to_save"
117
- # Always confirmed: removing a file of the old package is a change,
118
- # even with a manifest
119
- printf '%s\n' "$rel" >>"$work/modified"
120
- done < <(find "$folder/$old" -type f -print)
121
- done
122
- LC_ALL=C sort -u "$work/modified" >"$work/modified.sorted"
123
- mv "$work/modified.sorted" "$work/modified"
124
- LC_ALL=C sort -u "$work/to_save" >"$work/to_save.sorted"
125
- mv "$work/to_save.sorted" "$work/to_save"
126
-
127
- if [ -s "$work/modified" ]; then
128
- echo "Locally modified files:"
129
- while IFS= read -r rel; do
130
- printf ' %s\n' "$rel"
131
- done <"$work/modified"
132
- if [ -z "$assume_yes" ]; then
133
- if [ ! -t 0 ]; then
134
- echo "✖ Locally modified files: rerun with --yes to replace them (backup in .devzone.backup/)." >&2
135
- exit 1
136
- fi
137
- printf 'Replace these files (backup in .devzone.backup/)? [y/N] ' >&2
138
- read -r answer || answer=""
139
- case "$answer" in
140
- y | Y | yes) ;;
141
- *)
142
- echo "Aborted: nothing was modified."
143
- return 1
144
- ;;
145
- esac
146
- fi
147
- fi
148
-
149
- # 3. Backup of every file replaced or removed
150
- if [ -s "$work/to_save" ]; then
151
- base="$folder/.devzone.backup/$(date +%Y%m%d-%H%M%S)"
152
- backup="$base"
153
- n=1
154
- while [ -e "$backup" ]; do
155
- n=$((n + 1))
156
- backup="$base-$n"
157
- done
158
- while IFS= read -r rel; do
159
- [ -f "$folder/$rel" ] || continue
160
- if ! inside_folder "$folder" "$rel"; then
161
- echo "devzone: manifest: refused path: $rel" >&2
162
- continue
163
- fi
164
- mkdir -p "$backup/$(dirname "$rel")"
165
- # -P: a link is backed up as it is, never its target
166
- cp -P -p "$folder/$rel" "$backup/$rel"
167
- done <"$work/to_save"
168
- echo "Sauvegarde : $backup"
169
- fi
170
-
171
- # 4. Replacement, removal of the old package and of the old layout,
172
- # .gitignore completed without being overwritten
173
- complete_gitignore "$folder"
174
- while IFS= read -r rel; do
175
- mkdir -p "$folder/$(dirname "$rel")"
176
- if ! inside_folder "$folder" "$rel"; then
177
- echo "devzone: manifest: refused path: $rel" >&2
178
- continue
179
- fi
180
- # Never write through a link: the link (not its target) is removed
181
- if [ -L "$folder/$rel" ]; then
182
- rm -f -- "$folder/$rel"
183
- echo "devzone: manifest: link replaced: $rel" >&2
184
- fi
185
- cp -p "$PAQUET/$rel" "$folder/$rel"
186
- if [ -x "$PAQUET/$rel" ]; then
187
- chmod +x "$folder/$rel"
188
- fi
189
- done <"$package_list"
190
- while IFS= read -r rel; do
191
- [ -n "$rel" ] || continue
192
- [ -f "$folder/$rel" ] || continue
193
- if ! inside_folder "$folder" "$rel"; then
194
- echo "devzone: manifest: refused path: $rel" >&2
195
- continue
196
- fi
197
- # rm does not follow links: only the link is removed, never its target
198
- rm -f -- "$folder/$rel"
199
- done <"$work/to_remove"
200
- while IFS= read -r old; do
201
- [ -n "$old" ] || continue
202
- [ -d "$folder/$old" ] || continue
203
- # Only the folders that became empty are removed, never their content
204
- find "$folder/$old" -depth -type d -empty -delete
205
- done <"$work/old_trees"
206
-
207
- # 6. New manifest, then rebuild offered
208
- write_manifest "$folder"
209
- echo "✔ devzone $package: updated."
210
- restart_ports_watcher "$folder"
211
- # the update never changes the workstation mode (#472)
212
- if [ "$(grep -E '^workspace=' "$folder/.devzone.modules" 2>/dev/null | tail -1)" = "workspace=kubernetes" ]; then
213
- return 0
214
- fi
215
- if [ -n "$no_rebuild" ]; then
216
- echo "Rebuild not started (--no-rebuild)."
217
- return 0
218
- fi
219
- if [ ! -t 0 ]; then
220
- echo "Rebuild: make -C $folder rebuild (run in a terminal)"
221
- return 0
222
- fi
223
- printf 'Rebuild the container now? [y/N] ' >&2
224
- read -r answer || answer=""
225
- case "$answer" in
226
- y | Y | yes) ;;
227
- *)
228
- echo "Rebuild not started: make -C $folder rebuild"
229
- return 0
230
- ;;
231
- esac
232
- if [ -n "${DEVZONE_REBUILD:-}" ]; then
233
- sh -c "$DEVZONE_REBUILD"
234
- else
235
- make -C "$folder" rebuild
236
- fi
237
- }
@@ -1,43 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Checks Docker on the host before any call (sourced by uninstall.sh,
3
- # uninstall.sh and the npm command manage/devzone): without it the user
4
- # would only see the raw devcontainers CLI or docker error.
5
- # DEVZONE_DOCKER: docker command name (tests: simulate its absence).
6
- # Compatible with bash 3.2 (macOS).
7
-
8
- # Returns 0 if Docker responds; otherwise prints the cause (missing, no
9
- # permission, stopped) and what to do on stderr, then asks to run $1 again;
10
- # returns 1. Nothing raw on screen.
11
- docker_ready() {
12
- local command_name="$1" docker_cmd="${DEVZONE_DOCKER:-docker}" err
13
- if ! command -v "$docker_cmd" >/dev/null 2>&1; then
14
- {
15
- echo "✖ Docker not found: devzone needs it to run its container."
16
- echo " Install Docker Desktop (macOS, Windows, Linux): https://docs.docker.com/desktop/"
17
- echo " or, on Linux, Docker Engine: https://docs.docker.com/engine/install/"
18
- echo " then run $command_name again"
19
- } >&2
20
- return 1
21
- fi
22
- err="$("$docker_cmd" info 2>&1 >/dev/null)" && return 0
23
- case "$err" in
24
- *"permission denied"* | *"Permission denied"*)
25
- {
26
- echo "✖ Docker denied access: your account is not in the docker group."
27
- echo " sudo usermod -aG docker \$USER, then open a new session (or newgrp docker),"
28
- echo " then run $command_name again"
29
- } >&2
30
- ;;
31
- *)
32
- {
33
- echo "✖ Docker is installed but does not respond (stopped?)."
34
- echo " Start Docker Desktop (on Linux without Docker Desktop: sudo systemctl start docker),"
35
- echo " then run $command_name again"
36
- } >&2
37
- ;;
38
- esac
39
- return 1
40
- }
41
-
42
- # compatibility alias, removed with lot 4 (#509)
43
- docker_pret() { docker_ready "$@"; }
@@ -1,156 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Applications of the Kubernetes workspace relayed on workstation localhost (#611).
3
- # Sourced by ports-workspace.sh, never run alone. One kubectl get svc -A per
4
- # round: each TCP port of each Service gets a local port, kept stable in
5
- # .devzone.workspace-ports/apps/<namespace>.<service>.<port> while the Service
6
- # exists. Only Services with a ready endpoint are relayed (#638): the others keep
7
- # their local port and show as "no ready pod" in status. Output lines:
8
- # "portlocal|namespace|svc/name|remoteport", as pw_services.
9
-
10
- PA_MAX=30
11
- # Namespaces never relayed: system ones, and those of the devzone modules
12
- # (relayed by pw_services, or without any interface).
13
- PA_NS_SKIP=" kube-system kube-public kube-node-lease local-path-storage devzone-system devzone-workspace devzone-postgres devzone-mysql devzone-argocd devzone-gitlab devzone-llm "
14
-
15
- # Services of the cluster, one "namespace|name|clusterIP|type|relay annotation|port/proto," line each.
16
- pa_discover() {
17
- pw_kubectl get svc -A -o 'jsonpath={range .items[*]}{.metadata.namespace}|{.metadata.name}|{.spec.clusterIP}|{.spec.type}|{.metadata.annotations.devzone\.io/relay}|{range .spec.ports[*]}{.port}/{.protocol},{end}{"\n"}{end}' 2>/dev/null
18
- }
19
-
20
- # Services with a ready endpoint, as " namespace/name " items, from one kubectl get
21
- # endpointslices call. A Service with several slices is ready if one of its slices has
22
- # a ready endpoint. Exit 1 if the call fails: the caller then relays every Service.
23
- pa_ready() {
24
- local out ns name conds set=" "
25
- out="$(pw_kubectl get endpointslices -A -o 'jsonpath={range .items[*]}{.metadata.namespace}|{.metadata.labels.kubernetes\.io/service-name}|{range .endpoints[*]}{.conditions.ready},{end}{"\n"}{end}' 2>/dev/null)" || return 1
26
- while IFS='|' read -r ns name conds; do
27
- [ -n "$ns" ] || continue
28
- [ -n "$name" ] || continue
29
- case "$conds" in *true*) set="$set$ns/$name " ;; esac
30
- done <<<"$out"
31
- echo "$set"
32
- }
33
-
34
- # Is the Service excluded? $1 namespace, $2 name, $3 clusterIP, $4 type, $5 relay annotation.
35
- pa_excluded() {
36
- case "$PA_NS_SKIP" in *" $1 "*) return 0 ;; esac
37
- [ "$1/$2" != default/kubernetes ] || return 0
38
- [ "$5" != false ] || return 0
39
- case "$4" in ExternalName) return 0 ;; esac
40
- case "$3" in '' | None) return 0 ;; esac
41
- return 1
42
- }
43
-
44
- # Is local port $1 free for an application? Not in the list "$2" (" p p "), not taken on the workstation.
45
- pa_free() {
46
- case "$2" in *" $1 "*) return 1 ;; esac
47
- ! pw_taken "$1"
48
- }
49
-
50
- # First free local port for Service port $1, given the ports in use "$2": the port itself
51
- # when >= 1024, else $1 + 8000 (80 -> 8080), then incrementing; past 65000, from 20000.
52
- pa_new() {
53
- local c=$(($1 + 8000)) i=0
54
- if [ "$1" -ge 1024 ] && pa_free "$1" "$2"; then
55
- echo "$1"
56
- return 0
57
- fi
58
- [ "$c" -le 65000 ] || c=20000
59
- while [ "$i" -lt 60000 ]; do
60
- if pa_free "$c" "$2"; then
61
- echo "$c"
62
- return 0
63
- fi
64
- c=$((c + 1))
65
- i=$((i + 1))
66
- [ "$c" -le 65000 ] || c=20000
67
- done
68
- return 1
69
- }
70
-
71
- # One round of applications, from the ports already wanted "$1" (services, pod).
72
- # Prints the relay lines; writes apps/ (stable local port), cap/ (cap marker) and
73
- # no-ready-pod/ (Service without ready endpoint, not relayed nor counted), removes the
74
- # state of Services gone, warns once per port over the cap of PA_MAX.
75
- pa_round() {
76
- local d used excl ns name ip type annot ports pp port key stored loc n=0 seen="" f
77
- local ready="" ready_ok=1 unready
78
- d="$(pw_dir)"
79
- mkdir -p "$d/apps" "$d/cap"
80
- excl="$(grep -E '^excluded_ports=' .devzone.modules 2>/dev/null | tail -1 | cut -d= -f2- | tr ',' ' ')"
81
- used=" $1 $excl "
82
- # One kubectl call per round. If it fails, every Service is relayed, warned once.
83
- if ready="$(pa_ready)"; then
84
- rm -f "$d/endpoints-unreadable"
85
- else
86
- ready_ok=0
87
- if [ ! -e "$d/endpoints-unreadable" ]; then
88
- : >"$d/endpoints-unreadable"
89
- echo "⚠ endpointslices unreadable, ready pods not checked" >&2
90
- fi
91
- fi
92
- while IFS='|' read -r ns name ip type annot ports; do
93
- [ -n "$ns" ] || continue
94
- pa_excluded "$ns" "$name" "$ip" "$type" "$annot" && continue
95
- for pp in ${ports//,/ }; do
96
- case "$pp" in */TCP) ;; *) continue ;; esac
97
- port="${pp%/*}"
98
- case "$port" in '' | *[!0-9]*) continue ;; esac
99
- key="$ns.$name.$port"
100
- seen="$seen $key "
101
- stored="$(cat "$d/apps/$key" 2>/dev/null || true)"
102
- loc=""
103
- case "$stored" in '' | *[!0-9]*) ;; *) case "$used" in *" $stored "*) ;; *) loc="$stored" ;; esac ;; esac
104
- if [ -z "$loc" ]; then
105
- loc="$(pa_new "$port" "$used")" || { echo "⚠ no free local port for $ns/$name:$port" >&2; continue; }
106
- fi
107
- used="$used $loc "
108
- [ "$loc" = "$stored" ] || printf '%s' "$loc" >"$d/apps/$key"
109
- unready=0
110
- if [ "$ready_ok" -eq 1 ]; then
111
- case "$ready" in *" $ns/$name "*) ;; *) unready=1 ;; esac
112
- fi
113
- if [ "$unready" -eq 1 ]; then
114
- mkdir -p "$d/no-ready-pod"
115
- : >"$d/no-ready-pod/$key"
116
- continue
117
- fi
118
- rm -f "$d/no-ready-pod/$key"
119
- if [ "$n" -lt "$PA_MAX" ]; then
120
- n=$((n + 1))
121
- rm -f "$d/cap/$key"
122
- echo "$loc|$ns|svc/$name|$port"
123
- elif [ ! -e "$d/cap/$key" ]; then
124
- : >"$d/cap/$key"
125
- echo "⚠ cap of $PA_MAX application relays reached, $ns/$name:$port not relayed" >&2
126
- fi
127
- done
128
- done <<<"$(pa_discover)"
129
- for f in "$d"/apps/* "$d"/cap/* "$d"/no-ready-pod/*; do
130
- [ -e "$f" ] || continue
131
- case "$seen" in *" $(basename "$f") "*) ;; *) rm -f "$f" ;; esac
132
- done
133
- }
134
-
135
- # Status lines of relayed applications: local URL, then namespace/service:port.
136
- # Then the Services without ready pod, with the local URL they would get.
137
- pa_status() {
138
- local d f key rest loc
139
- d="$(pw_dir)"
140
- for f in "$d"/apps/*; do
141
- [ -e "$f" ] || continue
142
- loc="$(cat "$f")"
143
- pw_alive "$(cat "$d/relays/$loc" 2>/dev/null)" || continue
144
- key="$(basename "$f")"
145
- rest="${key#*.}"
146
- echo " http://127.0.0.1:$loc ${key%%.*}/${rest%%.*}:${rest#*.}"
147
- done
148
- for f in "$d"/no-ready-pod/*; do
149
- [ -e "$f" ] || continue
150
- key="$(basename "$f")"
151
- loc="$(cat "$d/apps/$key" 2>/dev/null || true)"
152
- [ -n "$loc" ] || continue
153
- rest="${key#*.}"
154
- echo " http://127.0.0.1:$loc ${key%%.*}/${rest%%.*}:${rest#*.} no ready pod"
155
- done
156
- }
@@ -1,235 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Applications and services of the Kubernetes workspace on workstation localhost (#401).
3
- # Sourced by manage/workspace.sh (ports_* functions); run directly with "loop" or
4
- # "round", it is the workstation watcher: only one, mkdir lock and pid in
5
- # .devzone.workspace-ports/. It relays with kubectl port-forward, on 127.0.0.1
6
- # only, TCP ports >= 1024 listening in the pod, and devzone services of active
7
- # modules in .devzone.modules. Guard: never under bats without
8
- # DEVZONE_PORTS_WORKSPACE_REAL=yes.
9
-
10
- PW_POD="devzone-workspace-0"
11
- PW_NS="devzone-workspace"
12
- # Internal pod ports, never relayed: kubedock and buildkit.
13
- PW_INTERNAL=" 2475 1234 "
14
- # pgAdmin service port: read from the service, without it nothing is relayed.
15
- PW_JSONPATH='jsonpath={.spec.ports[0].port}'
16
- # Cluster Services relayed as applications (#611).
17
- # shellcheck source=manage/scripts/workspace/ports-apps.sh
18
- . "$(dirname "${BASH_SOURCE[0]}")/ports-apps.sh"
19
-
20
- # Watcher state folder: lock, pid, relays, reported ports, log.
21
- pw_dir() { printf '%s' "${DEVZONE_PORTS_WORKSPACE_DIR:-$PWD/.devzone.workspace-ports}"; }
22
-
23
- # kubectl context flag: devzone if it exists, otherwise current context.
24
- pw_ctx() {
25
- if kubectl config get-contexts -o name 2>/dev/null | grep -qx 'devzone'; then
26
- printf -- '--context devzone'
27
- fi
28
- }
29
-
30
- # Watcher kubectl: devzone context if it exists, then given arguments.
31
- pw_kubectl() {
32
- # shellcheck disable=SC2046
33
- kubectl $(pw_ctx) "$@"
34
- }
35
-
36
- # Guard: refuse under bats without DEVZONE_PORTS_WORKSPACE_REAL=yes (message if $1 empty).
37
- pw_guard() {
38
- if [ -n "${BATS_TEST_TMPDIR:-}" ] && [ "${DEVZONE_PORTS_WORKSPACE_REAL:-}" != yes ]; then
39
- [ -n "${1:-}" ] || echo "✖ port watcher refused under bats (DEVZONE_PORTS_WORKSPACE_REAL=yes required)" >&2
40
- return 2
41
- fi
42
- }
43
-
44
- # Is process $1 alive?
45
- pw_alive() { [ -n "${1:-}" ] && kill -0 "$1" 2>/dev/null; }
46
-
47
- # TCP ports >= 1024 listening in the pod (ss -ltnH), excluding internal ports, one per line.
48
- pw_pod_ports() {
49
- local p
50
- pw_kubectl --namespace "$PW_NS" exec "$PW_POD" -c workspace -- ss -ltnH 2>/dev/null |
51
- awk '{n=split($4,a,":"); print a[n]}' | sort -un | while read -r p; do
52
- case "$p" in '' | *[!0-9]*) continue ;; esac
53
- [ "$p" -ge 1024 ] || continue
54
- case "$PW_INTERNAL" in *" $p "*) continue ;; esac
55
- echo "$p"
56
- done
57
- }
58
-
59
- # devzone services to relay, one "port|namespace|target[|remote port]" line per
60
- # active module service: postgres (5432) and pgAdmin if present in namespace,
61
- # mysql (3306), and the Argo CD UI (8443 → 443) when present (#516).
62
- pw_services() {
63
- local port
64
- if grep -qxE 'postgres=(on|true|oui)' .devzone.modules 2>/dev/null; then
65
- echo "5432|devzone-postgres|svc/postgres"
66
- port="$(pw_kubectl --namespace devzone-postgres get svc pgadmin -o "$PW_JSONPATH" 2>/dev/null || true)"
67
- case "$port" in '' | *[!0-9]*) ;; *) echo "$port|devzone-postgres|svc/pgadmin" ;; esac
68
- fi
69
- if grep -qxE 'mysql=(on|true|oui)' .devzone.modules 2>/dev/null; then
70
- echo "3306|devzone-mysql|svc/mysql"
71
- fi
72
- # Argo CD UI: https port of svc/argocd-server on 127.0.0.1:8443, unless argocd=off
73
- if ! grep -qx 'argocd=off' .devzone.modules 2>/dev/null; then
74
- port="$(pw_kubectl --namespace devzone-argocd get svc argocd-server \
75
- -o 'jsonpath={.spec.ports[?(@.name=="https")].port}' 2>/dev/null || true)"
76
- case "$port" in '' | *[!0-9]*) ;; *) echo "${DEVZONE_ARGOCD_PORT:-8443}|devzone-argocd|svc/argocd-server|$port" ;; esac
77
- fi
78
- }
79
-
80
- # Is port $1 already in use on the workstation? (lsof, otherwise nc -z)
81
- pw_taken() {
82
- if command -v lsof >/dev/null 2>&1; then
83
- lsof -nP -iTCP:"$1" -sTCP:LISTEN >/dev/null 2>&1
84
- else
85
- nc -z 127.0.0.1 "$1" >/dev/null 2>&1
86
- fi
87
- }
88
-
89
- # Starts relay of port $1 to port $4 (default: $1) of target $3 in namespace $2,
90
- # unless already alive; port taken on workstation: only one warning line per port.
91
- pw_relay() {
92
- local port="$1" ns="$2" target="$3" remote="${4:-$1}" d pid
93
- d="$(pw_dir)"
94
- pid="$(cat "$d/relays/$port" 2>/dev/null || true)"
95
- pw_alive "$pid" && return 0
96
- if pw_taken "$port"; then
97
- [ -e "$d/taken/$port" ] || { : >"$d/taken/$port"; echo "⚠ port $port already in use on the workstation, not relayed"; }
98
- return 0
99
- fi
100
- # shellcheck disable=SC2046
101
- kubectl $(pw_ctx) --namespace "$ns" port-forward --address 127.0.0.1 "$target" "$port:$remote" </dev/null >/dev/null 2>&1 &
102
- echo $! >"$d/relays/$port"
103
- rm -f "$d/taken/$port"
104
- echo "→ port $port relayed on 127.0.0.1"
105
- }
106
-
107
- # Stops relay of port $1 and forgets its warning.
108
- pw_remove() {
109
- local d pid
110
- d="$(pw_dir)"
111
- pid="$(cat "$d/relays/$1" 2>/dev/null || true)"
112
- if pw_alive "$pid"; then kill "$pid" 2>/dev/null || true; fi
113
- rm -f "$d/relays/$1" "$d/taken/$1"
114
- }
115
-
116
- # One watcher round: relay new ports, remove those that disappeared.
117
- pw_round() {
118
- local d wanted="" p ns target remote l f
119
- d="$(pw_dir)"
120
- mkdir -p "$d/relays" "$d/taken"
121
- while IFS='|' read -r p ns target remote; do
122
- [ -n "$p" ] || continue
123
- wanted="$wanted $p "
124
- pw_relay "$p" "$ns" "$target" "${remote:-$p}"
125
- done <<<"$(pw_services)"
126
- for p in $(pw_pod_ports); do
127
- case "$wanted" in *" $p "*) continue ;; esac
128
- wanted="$wanted $p "
129
- pw_relay "$p" "$PW_NS" "pod/$PW_POD"
130
- done
131
- while IFS='|' read -r p ns target remote; do
132
- [ -n "$p" ] || continue
133
- wanted="$wanted $p "
134
- pw_relay "$p" "$ns" "$target" "$remote"
135
- done <<<"$(pa_round "$wanted")"
136
- for f in "$d"/relays/* "$d"/taken/*; do
137
- [ -e "$f" ] || continue
138
- l="$(basename "$f")"
139
- case "$wanted" in *" $l "*) ;; *) pw_remove "$l" ;; esac
140
- done
141
- }
142
-
143
- # Removes all relays from the state folder.
144
- pw_remove_all() {
145
- local f
146
- for f in "$(pw_dir)"/relays/*; do
147
- if [ -e "$f" ]; then pw_remove "$(basename "$f")"; fi
148
- done
149
- }
150
-
151
- # Watcher loop: one round every 3 s; on stop, removes its relays and lock.
152
- pw_loop() {
153
- local d
154
- d="$(pw_dir)"
155
- trap 'pw_remove_all; rm -rf "$d/lock" "$d/pid"; exit 0' TERM INT
156
- while :; do
157
- pw_round || true
158
- sleep "${DEVZONE_PORTS_WORKSPACE_STEP:-3}" &
159
- wait $!
160
- done
161
- }
162
-
163
- # Starts the watcher, only one: mkdir lock, pid recorded; $1=silence: no guard message.
164
- # shellcheck disable=SC2120
165
- ports_start() {
166
- pw_guard "${1:-}" || return 0
167
- local d pid
168
- d="$(pw_dir)"
169
- mkdir -p "$d"
170
- rm -f "$d/restart"
171
- pid="$(cat "$d/pid" 2>/dev/null || true)"
172
- if pw_alive "$pid"; then
173
- echo "Workspace ports: watcher already running (pid $pid)"
174
- return 0
175
- fi
176
- rm -rf "$d/lock"
177
- mkdir "$d/lock" 2>/dev/null || return 0
178
- nohup bash "${BASH_SOURCE[0]}" loop >>"$d/journal.log" 2>&1 &
179
- echo $! >"$d/pid"
180
- disown 2>/dev/null || true
181
- echo "Workspace ports: watcher started, log $d/journal.log"
182
- }
183
-
184
- # Stops the watcher and all its port-forwards; without watcher, removes leftovers.
185
- ports_stop() {
186
- local d pid
187
- d="$(pw_dir)"
188
- pid="$(cat "$d/pid" 2>/dev/null || true)"
189
- if pw_alive "$pid"; then
190
- kill "$pid" 2>/dev/null || true
191
- for _ in 1 2 3 4 5 6 7 8 9 10; do pw_alive "$pid" || break; sleep 0.2; done
192
- fi
193
- pw_remove_all
194
- rm -rf "$d/lock" "$d/pid" "$d/restart"
195
- }
196
-
197
- # Shows status: watcher, relayed ports, reported taken ports.
198
- ports_status() {
199
- local d pid f
200
- d="$(pw_dir)"
201
- pid="$(cat "$d/pid" 2>/dev/null || true)"
202
- if pw_alive "$pid"; then echo "Port watcher: running (pid $pid)"; else echo "Port watcher: stopped"; fi
203
- for f in "$d"/relays/*; do
204
- if [ -e "$f" ] && pw_alive "$(cat "$f")"; then echo " 127.0.0.1:$(basename "$f") relayed"; fi
205
- done
206
- for f in "$d"/taken/*; do
207
- if [ -e "$f" ]; then echo " ⚠ port $(basename "$f") already in use on the workstation, not relayed"; fi
208
- done
209
- pa_status
210
- return 0
211
- }
212
-
213
- # Subcommand "devzone workspace ports [start|stop|status]" (default: status).
214
- ports() {
215
- case "${1:-status}" in
216
- start) ports_start ;;
217
- stop) ports_stop ;;
218
- status) ports_status ;;
219
- *) echo "✖ devzone workspace ports start|stop|status" >&2; return 1 ;;
220
- esac
221
- }
222
-
223
- # compatibility aliases, removed with lot 4 (#509)
224
- ports_demarrer() { ports_start "$@"; }
225
- ports_arreter() { ports_stop "$@"; }
226
- ports_etat() { ports_status "$@"; }
227
-
228
- # Run directly: watcher loop, or a single round (tests).
229
- if [ "${BASH_SOURCE[0]}" = "$0" ]; then
230
- pw_guard quiet || exit 2
231
- case "${1:-}" in
232
- loop) pw_loop ;;
233
- round) pw_round ;;
234
- esac
235
- fi