devzone 7.2.0 → 7.4.0

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.
@@ -1,219 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Safe write of the module state (#417): the only place that sets, replaces
3
- # or removes a line of $ETAT. A mkdir lock prevents two simultaneous writes,
4
- # the temporary file is created in the state folder (the mv stays atomic: the
5
- # file is never missing or incomplete for a reader) and a backup copy
6
- # $ETAT.prev precedes each replacement. The same file creates the state when
7
- # it is missing: former global state taken over once, backup copy restored,
8
- # state rebuilt from the services cluster, otherwise first installation.
9
- # Sourced library: the caller defines ETAT (file name kept: shared by callers).
10
- # state_write <key> <value> sets or replaces the line <key>=<value>
11
- # state_remove <key> removes the line <key>=
12
- # state_replace <file> replaces the whole state with this file
13
- # state_lock_take / state_lock_release write lock
14
- # state_recovery_* state restored or rebuilt (step 4)
15
- # state_init [modules…] creates the state if it is missing
16
-
17
- # Write lock (#476): state_lock_take, state_lock_release
18
- # shellcheck source=SCRIPTDIR/state-lock.sh
19
- # shellcheck disable=SC1091
20
- . "$(dirname "${BASH_SOURCE[0]}")/state-lock.sh"
21
-
22
- # Abort under lock: the temporary file goes, the lock is released
23
- state_abort() {
24
- rm -f "${1:-}"
25
- state_lock_release
26
- return 1
27
- }
28
-
29
- # Backup copy of the current state, before replacement
30
- state_backup() {
31
- [ -f "$ETAT" ] || return 0
32
- cp "$ETAT" "${ETAT}.prev"
33
- }
34
-
35
- # The state without the line $1, written to the temporary file $2
36
- state_filter() {
37
- local key="$1" dst="$2" code=0
38
- [ -f "$ETAT" ] || return 0
39
- grep -vE "^${key}=" "$ETAT" >"$dst" || code=$?
40
- [ "$code" -le 1 ] || return 1
41
- return 0
42
- }
43
-
44
- # Under lock: replaces the whole state with a file, $ETAT.prev backed up
45
- state_replace() {
46
- local src="$1" tmp
47
- [ -f "$src" ] || return 1
48
- state_lock_take || return 1
49
- state_backup || { state_lock_release; return 1; }
50
- tmp="$(mktemp "${ETAT}.XXXXXX")" || { state_lock_release; return 1; }
51
- cat "$src" >"$tmp" || { state_abort "$tmp"; return 1; }
52
- mv "$tmp" "$ETAT" || { state_abort "$tmp"; return 1; }
53
- state_lock_release
54
- return 0
55
- }
56
-
57
- # Under lock: new value of a key, without losing the other lines
58
- state_write() {
59
- local key="$1" value="$2" tmp
60
- state_lock_take || return 1
61
- state_backup || { state_lock_release; return 1; }
62
- tmp="$(mktemp "${ETAT}.XXXXXX")" || { state_lock_release; return 1; }
63
- state_filter "$key" "$tmp" || { state_abort "$tmp"; return 1; }
64
- printf '%s=%s\n' "$key" "$value" >>"$tmp" || { state_abort "$tmp"; return 1; }
65
- mv "$tmp" "$ETAT" || { state_abort "$tmp"; return 1; }
66
- state_lock_release
67
- return 0
68
- }
69
-
70
- # Under lock: removes a key (#417); without state, nothing to remove
71
- state_remove() {
72
- local key="$1" tmp
73
- state_lock_take || return 1
74
- [ -f "$ETAT" ] || { state_lock_release; return 0; }
75
- state_backup || { state_lock_release; return 1; }
76
- tmp="$(mktemp "${ETAT}.XXXXXX")" || { state_lock_release; return 1; }
77
- state_filter "$key" "$tmp" || { state_abort "$tmp"; return 1; }
78
- mv "$tmp" "$ETAT" || { state_abort "$tmp"; return 1; }
79
- state_lock_release
80
- return 0
81
- }
82
-
83
- # Marks a restored or rebuilt state: the watchers wait (#417)
84
- state_recovery_mark() {
85
- date +%s >"${ETAT}.recouvre" 2>/dev/null || true
86
- }
87
-
88
- # 0 (to wait) less than 60 s after a restore or a rebuild
89
- state_recovery_wait() {
90
- local mark
91
- [ -f "${ETAT}.recouvre" ] || return 1
92
- mark="$(cat "${ETAT}.recouvre" 2>/dev/null || true)"
93
- case "$mark" in
94
- '' | *[!0-9]*) return 1 ;;
95
- esac
96
- [ "$(( $(date +%s) - mark ))" -lt 60 ]
97
- }
98
-
99
- # 0 if the restored or rebuilt state is not yet confirmed (#417)
100
- state_recovery_pending() {
101
- [ -f "${ETAT}.recouvre" ]
102
- }
103
-
104
- # The user confirmed the state (devzone apply): no more guard
105
- state_recovery_confirm() {
106
- rm -f "${ETAT}.recouvre" 2>/dev/null || true
107
- return 0
108
- }
109
-
110
- # First installation (#417): the default state, written in one go
111
- state_default() {
112
- local stage code=0
113
- stage="$(mktemp)" || return 1
114
- printf 'ai=off\nkubernetes=off\ngitlab=off\n' >"$stage"
115
- state_replace "$stage" || code=$?
116
- rm -f "$stage"
117
- return "$code"
118
- }
119
-
120
- # Host context (host KUBECONFIG) that points to the given cluster: the
121
- # cluster= line of the state expects this context, not "devzone" (services cluster)
122
- host_context_of_cluster() {
123
- local cluster="$1" host ctx
124
- host="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}"
125
- while IFS= read -r ctx; do
126
- [ -n "$ctx" ] || continue
127
- # The "devzone" context is set on the services cluster: we look for the
128
- # host context that points to it
129
- [ "$ctx" = devzone ] && continue
130
- if [ "$(KUBECONFIG="$host" kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.cluster}" 2>/dev/null || true)" = "$cluster" ]; then
131
- echo "$ctx"
132
- return 0
133
- fi
134
- done < <(KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null)
135
- return 1
136
- }
137
-
138
- # State missing without backup copy (#417, step 3): rebuilt from the namespaces
139
- # labelled devzone of the services cluster (module on if its namespace exists,
140
- # cluster= of the devzone context); returns 1 otherwise.
141
- state_rebuild() {
142
- local namespaces m v stage seen=0 code=0 cluster_ctx cluster_host label
143
- label="${CLUSTER_LABEL:-app.kubernetes.io/managed-by=devzone}"
144
- command -v kubectl >/dev/null 2>&1 || return 1
145
- namespaces="$(kubectl get namespaces -l "$label" -o name 2>/dev/null || true)"
146
- [ -n "$namespaces" ] || return 1
147
- stage="$(mktemp)" || return 1
148
- for m in "$@"; do
149
- v=off
150
- printf '%s\n' "$namespaces" | grep -qx "namespace/devzone-$m" && { v=on; seen=1; }
151
- printf '%s=%s\n' "$m" "$v" >>"$stage" || code=1
152
- done
153
- # devzone context -> services cluster; the cluster= expects the real host
154
- # context that points to it (e.g. docker-desktop). Without host: no line.
155
- cluster_ctx="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"devzone\")].context.cluster}" 2>/dev/null || true)"
156
- cluster_host=""
157
- if [ -n "$cluster_ctx" ]; then
158
- cluster_host="$(host_context_of_cluster "$cluster_ctx")"
159
- if [ -n "$cluster_host" ]; then
160
- printf 'cluster=%s\n' "$cluster_host" >>"$stage" || code=1
161
- fi
162
- fi
163
- if [ "$code" -eq 0 ] && [ "$seen" -eq 1 ] && state_replace "$stage"; then
164
- echo "devzone : state rebuilt from the namespaces of the services cluster: $ETAT (confirm with devzone apply)" >&2
165
- [ -n "$cluster_host" ] || echo "devzone : no host context points to the services cluster: no cluster= line in $ETAT" >&2
166
- rm -f "$stage"
167
- return 0
168
- fi
169
- rm -f "$stage"
170
- return 1
171
- }
172
-
173
- # First start (no state): former global state taken over once, otherwise backup
174
- # copy restored, otherwise state rebuilt, otherwise everything disabled
175
- state_init() {
176
- [ -f "$ETAT" ] && return 0
177
-
178
- # Migration: former global state (~/.config/devzone/modules) to the state of
179
- # the instance; moved, not copied: the first devzone started after the update
180
- # keeps its choices
181
- if [ -f "$HOME/.config/devzone/modules" ]; then
182
- if state_replace "$HOME/.config/devzone/modules"; then
183
- rm -f "$HOME/.config/devzone/modules"
184
- return 0
185
- fi
186
- fi
187
-
188
- state_lock_take || return 1
189
- if [ -f "$ETAT" ]; then
190
- state_lock_release
191
- return 0
192
- fi
193
- # 1. Backup copy left by the last write (#417)
194
- if [ -f "${ETAT}.prev" ] && state_replace "${ETAT}.prev"; then
195
- echo "devzone : state restored from the backup copy: $ETAT (confirm with devzone apply)" >&2
196
- state_recovery_mark
197
- state_lock_release
198
- return 0
199
- fi
200
- # 2. Rebuild from the services cluster (#417)
201
- if state_rebuild "$@"; then
202
- state_recovery_mark
203
- state_lock_release
204
- return 0
205
- fi
206
- # 3. First installation: everything disabled (#417)
207
- if ! state_default; then
208
- state_lock_release
209
- return 1
210
- fi
211
- state_lock_release
212
- # Connection already set up by another devzone (shared volumes): reported
213
- # once, when the state of this instance is created; stderr: init is also
214
- # called inside $(value …)
215
- if [ -f "$HOME/.claude/.credentials.json" ] || [ -f "$HOME/.local/share/opencode/auth.json" ]; then
216
- echo "devzone : shared Claude Code/opencode connection found: devzone enable ai to use it here" >&2
217
- fi
218
- return 0
219
- }