devzone 7.3.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.
- package/.devcontainer/devcontainer.json +1 -1
- package/.devcontainer/devzone +1 -4
- package/README.md +1 -1
- package/bin/SHA256SUMS +6 -6
- package/bin/devzone-darwin-amd64 +0 -0
- package/bin/devzone-darwin-arm64 +0 -0
- package/bin/devzone-linux-amd64 +0 -0
- package/bin/devzone-linux-arm64 +0 -0
- package/bin/devzone-windows-amd64.exe +0 -0
- package/bin/devzone-windows-arm64.exe +0 -0
- package/package.json +1 -1
- package/.devcontainer/agent-errors.sh +0 -156
- package/.devcontainer/agent-local.sh +0 -63
- package/.devcontainer/agent-model.sh +0 -113
- package/.devcontainer/agent-pod-egress.sh +0 -36
- package/.devcontainer/agent-pod-manifest.sh +0 -250
- package/.devcontainer/agent-pod.sh +0 -243
- package/.devcontainer/agent-session.sh +0 -242
- package/.devcontainer/agent-state-json.sh +0 -117
- package/.devcontainer/agent-workspace.sh +0 -97
- package/.devcontainer/agent.sh +0 -247
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Manifest and validation helpers for the ephemeral session pod (#222).
|
|
3
|
-
# Sourced by agent-pod.sh.
|
|
4
|
-
# bash 3.2 compatible ; shellcheck -x clean.
|
|
5
|
-
# shellcheck disable=SC1091
|
|
6
|
-
. "$(dirname "${BASH_SOURCE[0]}")/agent-pod-egress.sh"
|
|
7
|
-
|
|
8
|
-
# --- Defaults ---
|
|
9
|
-
|
|
10
|
-
DEVZONE_AGENT_POD_ACTIVE_DEADLINE_SECONDS="${DEVZONE_AGENT_POD_ACTIVE_DEADLINE_SECONDS:-1800}"
|
|
11
|
-
DEVZONE_AGENT_POD_CPU_LIMIT="${DEVZONE_AGENT_POD_CPU_LIMIT:-2}"
|
|
12
|
-
DEVZONE_AGENT_POD_MEM_LIMIT="${DEVZONE_AGENT_POD_MEM_LIMIT:-4Gi}"
|
|
13
|
-
DEVZONE_AGENT_POD_TIMEOUT="${DEVZONE_AGENT_POD_TIMEOUT:-$((DEVZONE_AGENT_POD_ACTIVE_DEADLINE_SECONDS + 120))}"
|
|
14
|
-
# Projects root, read by the launcher only (no trailing slash). The pod keeps
|
|
15
|
-
# /workspaces/projets in its own environment (DEVZONE_ROOT in the manifest).
|
|
16
|
-
AGENT_POD_ROOT="${DEVZONE_ROOT:-/workspaces/projets}"
|
|
17
|
-
# Module state file on the workspace volume (subPath .devzone.modules, read-only in the pod).
|
|
18
|
-
# shellcheck disable=SC2034 # read by agent-pod.sh
|
|
19
|
-
AGENT_POD_MODULES_FILE="${DEVZONE_AGENT_POD_MODULES_FILE:-${AGENT_POD_ROOT}/.devzone.modules}"
|
|
20
|
-
|
|
21
|
-
# --- Path check : under AGENT_POD_ROOT, only [A-Za-z0-9._/-], no . or .. part ---
|
|
22
|
-
|
|
23
|
-
agent_pod_path_ok() {
|
|
24
|
-
printf '%s' "$AGENT_POD_ROOT" | grep -Eq '^/[A-Za-z0-9._/-]*[A-Za-z0-9._-]$' || return 1
|
|
25
|
-
case "$1" in "$AGENT_POD_ROOT"/*) ;; *) return 1 ;; esac
|
|
26
|
-
printf '%s' "$1" | grep -Eq '^[A-Za-z0-9._/-]+$' || return 1
|
|
27
|
-
case "$1/" in */./* | */../* | *//*) return 1 ;; esac
|
|
28
|
-
return 0
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
# --- State filled by agent_pod_validate_folder ---
|
|
32
|
-
|
|
33
|
-
AGENT_POD_WORKTREE_SUBPATH=""
|
|
34
|
-
AGENT_POD_WORKTREE_MOUNTPATH=""
|
|
35
|
-
AGENT_POD_GIT_SUBPATH=""
|
|
36
|
-
AGENT_POD_GIT_MOUNTPATH=""
|
|
37
|
-
AGENT_POD_SESSION_LABEL=""
|
|
38
|
-
AGENT_POD_TICKET=""
|
|
39
|
-
|
|
40
|
-
# --- Helpers : read the workspace pod (devzone-workspace-0, container workspace) ---
|
|
41
|
-
|
|
42
|
-
agent_pod_image_workspace() {
|
|
43
|
-
local result
|
|
44
|
-
result="$(kubectl get pod devzone-workspace-0 -o jsonpath='{.spec.containers[?(@.name=="workspace")].image}' 2>/dev/null)" || true
|
|
45
|
-
echo "${result:-ghcr.io/simbienvenuehoulboumi/devzone-workspace:latest}"
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
agent_pod_path_workspace() {
|
|
49
|
-
local result
|
|
50
|
-
result="$(kubectl get pod devzone-workspace-0 -o jsonpath='{.spec.containers[?(@.name=="workspace")].env[?(@.name=="PATH")].value}' 2>/dev/null)" || true
|
|
51
|
-
echo "${result:-/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin}"
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
agent_pod_pvc_workspace() {
|
|
55
|
-
local result
|
|
56
|
-
result="$(kubectl get pod devzone-workspace-0 -o jsonpath='{.spec.volumes[?(@.name=="workspace")].persistentVolumeClaim.claimName}' 2>/dev/null)" || true
|
|
57
|
-
echo "${result:-workspace-devzone-workspace-0}"
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
agent_pod_pvc_etat() {
|
|
61
|
-
local result
|
|
62
|
-
result="$(kubectl get pod devzone-workspace-0 -o jsonpath='{.spec.volumes[?(@.name=="etat")].persistentVolumeClaim.claimName}' 2>/dev/null)" || true
|
|
63
|
-
echo "${result:-etat-devzone-workspace-0}"
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
# --- Validation of the folder to session ---
|
|
67
|
-
|
|
68
|
-
agent_pod_validate_folder() {
|
|
69
|
-
local folder="$1" gitfile gitdir common base
|
|
70
|
-
if [ -z "$folder" ]; then
|
|
71
|
-
echo "devzone agent --pod : folder missing" >&2
|
|
72
|
-
return 1
|
|
73
|
-
fi
|
|
74
|
-
case "$folder" in
|
|
75
|
-
"$AGENT_POD_ROOT"/*) ;;
|
|
76
|
-
*)
|
|
77
|
-
echo "devzone agent --pod : folder outside the projects root $AGENT_POD_ROOT : $folder" >&2
|
|
78
|
-
return 1
|
|
79
|
-
;;
|
|
80
|
-
esac
|
|
81
|
-
if ! agent_pod_path_ok "$folder"; then
|
|
82
|
-
echo "devzone agent --pod : invalid folder path : $folder" >&2
|
|
83
|
-
return 1
|
|
84
|
-
fi
|
|
85
|
-
gitfile="$folder/.git"
|
|
86
|
-
if [ ! -f "$gitfile" ]; then
|
|
87
|
-
echo "devzone agent --pod : no .git file (not a git worktree) : $folder" >&2
|
|
88
|
-
return 1
|
|
89
|
-
fi
|
|
90
|
-
gitdir="$(grep '^gitdir:' "$gitfile" 2>/dev/null | head -1 | sed 's/^gitdir:[[:space:]]*//;s/[[:space:]]*$//')"
|
|
91
|
-
if [ -z "$gitdir" ]; then
|
|
92
|
-
echo "devzone agent --pod : .git without gitdir line : $folder" >&2
|
|
93
|
-
return 1
|
|
94
|
-
fi
|
|
95
|
-
case "$gitdir" in
|
|
96
|
-
*/worktrees/*) common="${gitdir%/worktrees/*}" ;;
|
|
97
|
-
*)
|
|
98
|
-
echo "devzone agent --pod : gitdir is not a worktree : $gitdir" >&2
|
|
99
|
-
return 1
|
|
100
|
-
;;
|
|
101
|
-
esac
|
|
102
|
-
case "$common" in
|
|
103
|
-
"$AGENT_POD_ROOT"/*) ;;
|
|
104
|
-
*)
|
|
105
|
-
echo "devzone agent --pod : worktree depot outside the projects root $AGENT_POD_ROOT : $common" >&2
|
|
106
|
-
return 1
|
|
107
|
-
;;
|
|
108
|
-
esac
|
|
109
|
-
if ! agent_pod_path_ok "$common"; then
|
|
110
|
-
echo "devzone agent --pod : invalid depot path : $common" >&2
|
|
111
|
-
return 1
|
|
112
|
-
fi
|
|
113
|
-
base="$(basename "$folder")"
|
|
114
|
-
if ! printf '%s' "$base" | grep -Eq '^[A-Za-z0-9]([A-Za-z0-9._-]*[A-Za-z0-9])?$'; then
|
|
115
|
-
echo "devzone agent --pod : invalid label value : $base" >&2
|
|
116
|
-
return 1
|
|
117
|
-
fi
|
|
118
|
-
if [ "${#base}" -gt 63 ]; then
|
|
119
|
-
echo "devzone agent --pod : label value too long : $base" >&2
|
|
120
|
-
return 1
|
|
121
|
-
fi
|
|
122
|
-
AGENT_POD_WORKTREE_MOUNTPATH="$folder"
|
|
123
|
-
AGENT_POD_WORKTREE_SUBPATH="${folder#"$AGENT_POD_ROOT"/}"
|
|
124
|
-
AGENT_POD_GIT_MOUNTPATH="$common"
|
|
125
|
-
AGENT_POD_GIT_SUBPATH="${common#"$AGENT_POD_ROOT"/}"
|
|
126
|
-
AGENT_POD_SESSION_LABEL="$base"
|
|
127
|
-
# Keep the ticket only for an exact dz-<number> name.
|
|
128
|
-
if ! printf '%s' "$base" | grep -Eq '^dz-[0-9]+$'; then
|
|
129
|
-
AGENT_POD_TICKET=""
|
|
130
|
-
else
|
|
131
|
-
AGENT_POD_TICKET="${base#dz-}"
|
|
132
|
-
fi
|
|
133
|
-
return 0
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
# --- Build the session pod manifest ---
|
|
137
|
-
|
|
138
|
-
agent_pod_build_manifest() {
|
|
139
|
-
# $2 (the brief) is never written here: it only goes to the ConfigMap.
|
|
140
|
-
local folder="$1" pod_name="$3"
|
|
141
|
-
local ws_image ws_path pvc_workspace pvc_etat manifest_file ticket_block=""
|
|
142
|
-
|
|
143
|
-
ws_image="$(agent_pod_image_workspace)"
|
|
144
|
-
ws_path="$(agent_pod_path_workspace)"
|
|
145
|
-
pvc_workspace="$(agent_pod_pvc_workspace)"
|
|
146
|
-
pvc_etat="$(agent_pod_pvc_etat)"
|
|
147
|
-
|
|
148
|
-
if [ -n "$AGENT_POD_TICKET" ]; then
|
|
149
|
-
ticket_block=" devzone.io/agent-ticket: \"${AGENT_POD_TICKET}\""
|
|
150
|
-
fi
|
|
151
|
-
|
|
152
|
-
manifest_file="$(mktemp "${TMPDIR:-/tmp}/devzone-agent-pod-manifest.XXXXXX.yaml")"; egress_mode="$(agent_pod_egress_mode)"
|
|
153
|
-
|
|
154
|
-
{
|
|
155
|
-
printf '%s\n' "apiVersion: v1"
|
|
156
|
-
printf '%s\n' "kind: Pod"
|
|
157
|
-
printf '%s\n' "metadata:"
|
|
158
|
-
printf '%s\n' " name: ${pod_name}"
|
|
159
|
-
printf '%s\n' " labels:"
|
|
160
|
-
printf '%s\n' " devzone.io/agent-session: ${AGENT_POD_SESSION_LABEL}"
|
|
161
|
-
if [ -n "$ticket_block" ]; then
|
|
162
|
-
printf '%s\n' "$ticket_block"
|
|
163
|
-
fi
|
|
164
|
-
printf '%s\n' "spec:"
|
|
165
|
-
agent_pod_egress_init_lines "$egress_mode" "$ws_image"
|
|
166
|
-
printf '%s\n' " restartPolicy: Never"
|
|
167
|
-
printf '%s\n' " activeDeadlineSeconds: ${DEVZONE_AGENT_POD_ACTIVE_DEADLINE_SECONDS}"
|
|
168
|
-
printf '%s\n' " automountServiceAccountToken: false"
|
|
169
|
-
printf '%s\n' " securityContext:"
|
|
170
|
-
printf '%s\n' " runAsUser: 1000"
|
|
171
|
-
printf '%s\n' " runAsGroup: 1000"
|
|
172
|
-
printf '%s\n' " runAsNonRoot: true"
|
|
173
|
-
printf '%s\n' " fsGroup: 1000"
|
|
174
|
-
printf '%s\n' " affinity:"
|
|
175
|
-
printf '%s\n' " podAffinity:"
|
|
176
|
-
printf '%s\n' " requiredDuringSchedulingIgnoredDuringExecution:"
|
|
177
|
-
printf '%s\n' " - labelSelector:"
|
|
178
|
-
printf '%s\n' " matchExpressions:"
|
|
179
|
-
printf '%s\n' " - key: statefulset.kubernetes.io/pod-name"
|
|
180
|
-
printf '%s\n' " operator: In"
|
|
181
|
-
printf '%s\n' " values:"
|
|
182
|
-
printf '%s\n' " - devzone-workspace-0"
|
|
183
|
-
printf '%s\n' " topologyKey: kubernetes.io/hostname"
|
|
184
|
-
printf '%s\n' " containers:"
|
|
185
|
-
printf '%s\n' " - name: agent"
|
|
186
|
-
printf '%s\n' " image: ${ws_image}"
|
|
187
|
-
printf '%s\n' " env:"
|
|
188
|
-
printf '%s\n' " - name: HOME"
|
|
189
|
-
printf '%s\n' " value: \"/home/vscode\""
|
|
190
|
-
printf '%s\n' " - name: NPM_CONFIG_PREFIX"
|
|
191
|
-
printf '%s\n' " value: \"/home/vscode/.local/npm\""
|
|
192
|
-
printf '%s\n' " - name: PATH"
|
|
193
|
-
printf '%s\n' " value: \"${ws_path}\""
|
|
194
|
-
printf '%s\n' " - name: DEVZONE_WORKSPACE"
|
|
195
|
-
printf '%s\n' " value: \"kubernetes\""
|
|
196
|
-
printf '%s\n' " - name: DEVZONE_ROOT"
|
|
197
|
-
printf '%s\n' " value: \"/workspaces/projets\""; agent_pod_egress_manifest_lines "$egress_mode"
|
|
198
|
-
printf '%s\n' " resources:"
|
|
199
|
-
printf '%s\n' " limits:"
|
|
200
|
-
printf '%s\n' " cpu: ${DEVZONE_AGENT_POD_CPU_LIMIT}"
|
|
201
|
-
printf '%s\n' " memory: ${DEVZONE_AGENT_POD_MEM_LIMIT}"
|
|
202
|
-
printf '%s\n' " volumeMounts:"
|
|
203
|
-
printf '%s\n' " - name: workspace"
|
|
204
|
-
printf '%s\n' " mountPath: ${AGENT_POD_WORKTREE_MOUNTPATH}"
|
|
205
|
-
printf '%s\n' " subPath: ${AGENT_POD_WORKTREE_SUBPATH}"
|
|
206
|
-
printf '%s\n' " - name: workspace"
|
|
207
|
-
printf '%s\n' " mountPath: ${AGENT_POD_GIT_MOUNTPATH}"
|
|
208
|
-
printf '%s\n' " subPath: ${AGENT_POD_GIT_SUBPATH}"
|
|
209
|
-
printf '%s\n' " - name: workspace"
|
|
210
|
-
printf '%s\n' " mountPath: /workspaces/projets/.devzone.modules"
|
|
211
|
-
printf '%s\n' " subPath: .devzone.modules"
|
|
212
|
-
printf '%s\n' " readOnly: true"
|
|
213
|
-
printf '%s\n' " - name: etat"
|
|
214
|
-
printf '%s\n' " mountPath: /home/vscode/.config/opencode"
|
|
215
|
-
printf '%s\n' " subPath: .config/opencode"
|
|
216
|
-
printf '%s\n' " - name: etat"
|
|
217
|
-
printf '%s\n' " mountPath: /home/vscode/.local/share/opencode"
|
|
218
|
-
printf '%s\n' " subPath: .local/share/opencode"
|
|
219
|
-
printf '%s\n' " - name: etat"
|
|
220
|
-
printf '%s\n' " mountPath: /home/vscode/.local/state/oc-lancer"
|
|
221
|
-
printf '%s\n' " subPath: .local/state/oc-lancer"
|
|
222
|
-
printf '%s\n' " - name: etat"
|
|
223
|
-
printf '%s\n' " mountPath: /home/vscode/.local/npm"
|
|
224
|
-
printf '%s\n' " subPath: .local/npm"
|
|
225
|
-
printf '%s\n' " readOnly: true"
|
|
226
|
-
printf '%s\n' " - name: consigne"
|
|
227
|
-
printf '%s\n' " mountPath: /agent-consigne"
|
|
228
|
-
printf '%s\n' " readOnly: true"
|
|
229
|
-
printf '%s\n' " - name: session-workspace"
|
|
230
|
-
printf '%s\n' " mountPath: /workspaces/projets/workspace"
|
|
231
|
-
printf '%s\n' " command: [\"/bin/bash\", \"-lc\"]"
|
|
232
|
-
printf '%s\n' " args:"
|
|
233
|
-
printf '%s\n' " - 'devzone agent --local ${folder} /agent-consigne/consigne.md; code=\$?; cat \"\$DEVZONE_ROOT/workspace/.agents/${AGENT_POD_SESSION_LABEL}.log\" 2>/dev/null; exit \$code'"
|
|
234
|
-
printf '%s\n' " volumes:"
|
|
235
|
-
printf '%s\n' " - name: workspace"
|
|
236
|
-
printf '%s\n' " persistentVolumeClaim:"
|
|
237
|
-
printf '%s\n' " claimName: ${pvc_workspace}"
|
|
238
|
-
printf '%s\n' " - name: etat"
|
|
239
|
-
printf '%s\n' " persistentVolumeClaim:"
|
|
240
|
-
printf '%s\n' " claimName: ${pvc_etat}"
|
|
241
|
-
printf '%s\n' " - name: consigne"
|
|
242
|
-
printf '%s\n' " configMap:"
|
|
243
|
-
printf '%s\n' " name: ${pod_name}-consigne"
|
|
244
|
-
printf '%s\n' " - name: session-workspace"
|
|
245
|
-
printf '%s\n' " emptyDir:"
|
|
246
|
-
printf '%s\n' " sizeLimit: 256Mi"
|
|
247
|
-
} >"$manifest_file"
|
|
248
|
-
|
|
249
|
-
echo "$manifest_file"
|
|
250
|
-
}
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Ephemeral session pod for devzone agent (#222).
|
|
3
|
-
# Sourced by agent.sh: launches the session pod (devzone agent --pod), waits for
|
|
4
|
-
# its end, keeps its log, deletes the pod and its ConfigMap, lists session pods (status).
|
|
5
|
-
#
|
|
6
|
-
# Constraints:
|
|
7
|
-
# - bash 3.2 compatible
|
|
8
|
-
# - shellcheck -x clean
|
|
9
|
-
# - no real cluster: kubectl is faked in tests
|
|
10
|
-
# - the pod and its ConfigMap are always deleted (EXIT, INT, TERM trap)
|
|
11
|
-
|
|
12
|
-
ICI="${ICI:-$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)}"
|
|
13
|
-
# shellcheck source=agent-pod-manifest.sh disable=SC1091
|
|
14
|
-
. "${ICI}/agent-pod-manifest.sh"
|
|
15
|
-
|
|
16
|
-
# Resources of the current launch, read by the cleanup trap.
|
|
17
|
-
AGENT_POD_ACTIVE_NAME=""
|
|
18
|
-
AGENT_POD_ACTIVE_MANIFEST=""
|
|
19
|
-
AGENT_POD_CLEANED=0
|
|
20
|
-
|
|
21
|
-
# --- Pod and ConfigMap names ---
|
|
22
|
-
|
|
23
|
-
agent_pod_name() {
|
|
24
|
-
local base="${1:-}"
|
|
25
|
-
if [ -z "$base" ]; then
|
|
26
|
-
base="${AGENT_POD_SESSION_LABEL:-session}"
|
|
27
|
-
else
|
|
28
|
-
base="$(basename "$base")"
|
|
29
|
-
fi
|
|
30
|
-
# Kubernetes object names must be lowercase.
|
|
31
|
-
base="$(printf '%s' "$base" | tr '[:upper:]' '[:lower:]')"
|
|
32
|
-
echo "agent-${base}-$RANDOM"
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
agent_pod_configmap_name() {
|
|
36
|
-
echo "$1-consigne"
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
agent_pod_create_configmap() {
|
|
40
|
-
local pod_name="$1" consigne_file="$2"
|
|
41
|
-
kubectl create configmap "$(agent_pod_configmap_name "$pod_name")" \
|
|
42
|
-
--from-file=consigne.md="$consigne_file" >/dev/null 2>&1
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
agent_pod_delete_configmap() {
|
|
46
|
-
local pod_name="$1"
|
|
47
|
-
kubectl delete configmap "$(agent_pod_configmap_name "$pod_name")" >/dev/null 2>&1 || true
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
# --- Create the pod ---
|
|
51
|
-
|
|
52
|
-
agent_pod_create() {
|
|
53
|
-
local manifest_file="$1"
|
|
54
|
-
kubectl create -f "$manifest_file" >/dev/null 2>&1
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
# --- Wait for the pod to finish ---
|
|
58
|
-
|
|
59
|
-
agent_pod_wait() {
|
|
60
|
-
local pod_name="$1"
|
|
61
|
-
local elapsed=0
|
|
62
|
-
local max_wait="$DEVZONE_AGENT_POD_TIMEOUT"
|
|
63
|
-
local phase=""
|
|
64
|
-
|
|
65
|
-
while [ "$elapsed" -lt "$max_wait" ]; do
|
|
66
|
-
phase="$(kubectl get pod "$pod_name" -o jsonpath='{.status.phase}' 2>/dev/null)" || true
|
|
67
|
-
if [ "$phase" = "Succeeded" ] || [ "$phase" = "Failed" ]; then
|
|
68
|
-
echo "$phase"
|
|
69
|
-
return 0
|
|
70
|
-
fi
|
|
71
|
-
sleep 1
|
|
72
|
-
elapsed=$((elapsed + 1))
|
|
73
|
-
done
|
|
74
|
-
|
|
75
|
-
phase="$(kubectl get pod "$pod_name" -o jsonpath='{.status.phase}' 2>/dev/null)" || true
|
|
76
|
-
echo "${phase:-Unknown}"
|
|
77
|
-
return 1
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
# --- Keep the log of the session ---
|
|
81
|
-
|
|
82
|
-
# Init containers run before the agent: their log goes first, so a refused
|
|
83
|
-
# session shows the guard message. A failing `kubectl logs -c` is not fatal.
|
|
84
|
-
agent_pod_fetch_logs() {
|
|
85
|
-
local pod_name="$1" journal_file="$2"
|
|
86
|
-
local init_name
|
|
87
|
-
while IFS= read -r init_name; do
|
|
88
|
-
[ -n "$init_name" ] || continue
|
|
89
|
-
echo "=== init container ${init_name} ===" >>"$journal_file"
|
|
90
|
-
kubectl logs "$pod_name" -c "$init_name" >>"$journal_file" 2>/dev/null || true
|
|
91
|
-
done <<EOF
|
|
92
|
-
$(kubectl get pod "$pod_name" -o jsonpath='{.spec.initContainers[*].name}' 2>/dev/null || true)
|
|
93
|
-
EOF
|
|
94
|
-
kubectl logs "$pod_name" >>"$journal_file" 2>/dev/null || true
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
# Names the init container that stopped the pod on stderr: the launcher already
|
|
98
|
-
# shows the end of the log, which contains the message of the guard. Only the
|
|
99
|
-
# init containers that actually failed (exit code different from 0) are named.
|
|
100
|
-
agent_pod_init_failure() {
|
|
101
|
-
local pod_name="$1"
|
|
102
|
-
local cause name code
|
|
103
|
-
cause="$(kubectl get pod "$pod_name" -o jsonpath='{range .status.initContainerStatuses[?(@.state.terminated.exitCode)]}{.name}{" "}{.state.terminated.exitCode}{"\n"}{end}' 2>/dev/null)" || true
|
|
104
|
-
[ -n "$cause" ] || return 0
|
|
105
|
-
while IFS=' ' read -r name code; do
|
|
106
|
-
[ -n "$name" ] || continue
|
|
107
|
-
[ "${code:-0}" != 0 ] || continue
|
|
108
|
-
echo "devzone agent --pod : init container ${name} failed (exit ${code})" >&2
|
|
109
|
-
done <<EOF
|
|
110
|
-
${cause}
|
|
111
|
-
EOF
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
# Last lines of the log on stderr: the cause of a failure is there.
|
|
115
|
-
agent_pod_show_tail() {
|
|
116
|
-
local journal_file="$1"
|
|
117
|
-
tail -n 15 "$journal_file" >&2 || true
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
# --- Delete the pod ---
|
|
121
|
-
|
|
122
|
-
agent_pod_delete() {
|
|
123
|
-
local pod_name="$1"
|
|
124
|
-
kubectl delete pod "$pod_name" >/dev/null 2>&1 || true
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
# --- Status : list session pods with their limits ---
|
|
128
|
-
|
|
129
|
-
agent_pod_status() {
|
|
130
|
-
kubectl get pods -l "devzone.io/agent-session" \
|
|
131
|
-
-o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].resources.limits.cpu}{"\t"}{.spec.containers[0].resources.limits.memory}{"\n"}{end}' 2>/dev/null || true
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
# --- Cleanup trap: pod, ConfigMap, temporary manifest (not the log) ---
|
|
135
|
-
|
|
136
|
-
agent_pod_cleanup() {
|
|
137
|
-
if [ "$AGENT_POD_CLEANED" = 1 ]; then
|
|
138
|
-
return 0
|
|
139
|
-
fi
|
|
140
|
-
AGENT_POD_CLEANED=1
|
|
141
|
-
if [ -n "$AGENT_POD_ACTIVE_NAME" ]; then
|
|
142
|
-
agent_pod_delete "$AGENT_POD_ACTIVE_NAME"
|
|
143
|
-
agent_pod_delete_configmap "$AGENT_POD_ACTIVE_NAME"
|
|
144
|
-
fi
|
|
145
|
-
if [ -n "$AGENT_POD_ACTIVE_MANIFEST" ]; then
|
|
146
|
-
rm -f "$AGENT_POD_ACTIVE_MANIFEST"
|
|
147
|
-
fi
|
|
148
|
-
return 0
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
# Set before any object is created. INT and TERM leave with 130 / 143;
|
|
152
|
-
# EXIT keeps the exit code of the script.
|
|
153
|
-
agent_pod_set_trap() {
|
|
154
|
-
trap 'agent_pod_cleanup' EXIT
|
|
155
|
-
trap 'agent_pod_cleanup; exit 130' INT
|
|
156
|
-
trap 'agent_pod_cleanup; exit 143' TERM
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
# --- Launch a session agent in an ephemeral pod (devzone agent --pod) ---
|
|
160
|
-
|
|
161
|
-
agent_pod_launch() {
|
|
162
|
-
local folder_raw="${1:-}" consigne_raw="${2:-}"
|
|
163
|
-
local folder consigne pod_name journal_file phase manifest_file state
|
|
164
|
-
local rc=0
|
|
165
|
-
|
|
166
|
-
if [ "${DEVZONE_WORKSPACE:-}" != "kubernetes" ]; then
|
|
167
|
-
echo "devzone agent --pod : must be called from the workspace pod (DEVZONE_WORKSPACE=kubernetes)" >&2
|
|
168
|
-
exit 1
|
|
169
|
-
fi
|
|
170
|
-
|
|
171
|
-
if ! agent_pod_path_ok "$folder_raw"; then
|
|
172
|
-
echo "devzone agent --pod : invalid folder path : ${folder_raw:-}" >&2
|
|
173
|
-
exit 1
|
|
174
|
-
fi
|
|
175
|
-
if [ ! -d "$folder_raw" ]; then
|
|
176
|
-
echo "devzone agent --pod : folder not found : $folder_raw" >&2
|
|
177
|
-
exit 1
|
|
178
|
-
fi
|
|
179
|
-
folder="$(cd "$folder_raw" && pwd -P)" || {
|
|
180
|
-
echo "devzone agent --pod : folder not found : $folder_raw" >&2
|
|
181
|
-
exit 1
|
|
182
|
-
}
|
|
183
|
-
if [ ! -r "${consigne_raw:-}" ]; then
|
|
184
|
-
echo "devzone agent --pod : brief not found : ${consigne_raw:-}" >&2
|
|
185
|
-
exit 1
|
|
186
|
-
fi
|
|
187
|
-
consigne="$(cd "$(dirname "$consigne_raw")" && pwd -P)/$(basename "$consigne_raw")"
|
|
188
|
-
|
|
189
|
-
if ! agent_pod_validate_folder "$folder"; then
|
|
190
|
-
exit 1
|
|
191
|
-
fi
|
|
192
|
-
# The pod mounts this file read-only: refuse before any object is created.
|
|
193
|
-
if [ ! -f "$AGENT_POD_MODULES_FILE" ]; then
|
|
194
|
-
echo "devzone agent --pod : modules file missing : $AGENT_POD_MODULES_FILE (run devzone enable ai on the workspace)" >&2
|
|
195
|
-
exit 1
|
|
196
|
-
fi
|
|
197
|
-
|
|
198
|
-
# The log of the session stays in the agent state directory (shared with the pod).
|
|
199
|
-
state="${AGENT_STATE:-$HOME/.local/state/oc-lancer}"
|
|
200
|
-
mkdir -p "$state"
|
|
201
|
-
journal_file="$state/${AGENT_POD_SESSION_LABEL}.pod.log"
|
|
202
|
-
: >"$journal_file"
|
|
203
|
-
|
|
204
|
-
pod_name="$(agent_pod_name "$folder")"
|
|
205
|
-
AGENT_POD_ACTIVE_NAME="$pod_name"
|
|
206
|
-
agent_pod_set_trap
|
|
207
|
-
|
|
208
|
-
if ! agent_pod_create_configmap "$pod_name" "$consigne"; then
|
|
209
|
-
echo "devzone agent --pod : unable to create the configmap" >&2
|
|
210
|
-
exit 1
|
|
211
|
-
fi
|
|
212
|
-
|
|
213
|
-
manifest_file="$(agent_pod_build_manifest "$folder" "$consigne" "$pod_name")" || {
|
|
214
|
-
echo "devzone agent --pod : unable to build the manifest" >&2
|
|
215
|
-
exit 1
|
|
216
|
-
}
|
|
217
|
-
AGENT_POD_ACTIVE_MANIFEST="$manifest_file"
|
|
218
|
-
|
|
219
|
-
if ! agent_pod_create "$manifest_file"; then
|
|
220
|
-
echo "devzone agent --pod : unable to create the pod (kubectl create -f)" >&2
|
|
221
|
-
echo "devzone agent --pod : try again without --pod" >&2
|
|
222
|
-
exit 1
|
|
223
|
-
fi
|
|
224
|
-
|
|
225
|
-
phase="$(agent_pod_wait "$pod_name")" || rc=$?
|
|
226
|
-
agent_pod_fetch_logs "$pod_name" "$journal_file"
|
|
227
|
-
echo "=== end of agent session in pod (phase: ${phase:-Unknown})" >>"$journal_file"
|
|
228
|
-
if [ "$rc" -ne 0 ] || [ "$phase" = "Failed" ]; then
|
|
229
|
-
agent_pod_init_failure "$pod_name"
|
|
230
|
-
fi
|
|
231
|
-
agent_pod_show_tail "$journal_file"
|
|
232
|
-
|
|
233
|
-
if [ "$rc" -ne 0 ]; then
|
|
234
|
-
echo "devzone agent --pod : pod did not finish in time (phase: ${phase:-Unknown}) ; log : $journal_file" >&2
|
|
235
|
-
exit 1
|
|
236
|
-
fi
|
|
237
|
-
if [ "$phase" = "Failed" ]; then
|
|
238
|
-
echo "devzone agent --pod : session ended in failure ; log : $journal_file" >&2
|
|
239
|
-
exit 1
|
|
240
|
-
fi
|
|
241
|
-
echo "devzone agent --pod : session ended (phase: $phase) ; log : $journal_file" >&2
|
|
242
|
-
exit 0
|
|
243
|
-
}
|