devzone 1.1.0 → 3.0.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/Dockerfile +4 -4
- package/.devcontainer/{agent-erreurs.sh → agent-errors.sh} +6 -6
- package/.devcontainer/agent-local.sh +2 -2
- package/.devcontainer/{agent-modele.messages → agent-model.messages} +1 -1
- package/.devcontainer/{agent-modele.sh → agent-model.sh} +5 -5
- package/.devcontainer/agent-pod-manifest.sh +4 -4
- package/.devcontainer/{agent-droits.sh → agent-rights.sh} +2 -2
- package/.devcontainer/agent-session.sh +3 -3
- package/.devcontainer/{agent-etat-json.sh → agent-state-json.sh} +6 -6
- package/.devcontainer/agent-workspace.sh +1 -1
- package/.devcontainer/agent.sh +14 -14
- package/.devcontainer/{ia/docker-garde.sh → ai/docker-guard.sh} +1 -1
- package/.devcontainer/{ia → ai}/host-models.sh +2 -2
- package/.devcontainer/{ia/plugins/devzone-garde-v2.js → ai/plugins/devzone-guard-v2.js} +7 -7
- package/.devcontainer/{ia/plugins/devzone-garde.js → ai/plugins/devzone-guard.js} +6 -6
- package/.devcontainer/argocd/argocd.sh +94 -93
- package/.devcontainer/argocd/{catalogue-argocd.sh → catalog-argocd.sh} +3 -3
- package/.devcontainer/argocd/services-hook.sh +8 -8
- package/.devcontainer/argocd/services.sh +1 -1
- package/.devcontainer/catalog/app.sh +209 -0
- package/.devcontainer/catalog/catalog-watcher.sh +130 -0
- package/.devcontainer/{catalogue → catalog}/mysql/app.conf +1 -1
- package/.devcontainer/catalog/mysql/install.sh +87 -0
- package/.devcontainer/catalog/mysql/mysql.sh +262 -0
- package/.devcontainer/catalog/mysql/volume.sh +71 -0
- package/.devcontainer/catalog.sh +231 -0
- package/.devcontainer/chart-direct.sh +1 -1
- package/.devcontainer/cluster.sh +222 -224
- package/.devcontainer/clusters.sh +41 -41
- package/.devcontainer/devcontainer.json +1 -1
- package/.devcontainer/devzone +103 -1506
- package/.devcontainer/devzone-ai-guard.sh +76 -0
- package/.devcontainer/devzone-ai.sh +201 -0
- package/.devcontainer/devzone-cluster.sh +210 -0
- package/.devcontainer/devzone-modules.sh +126 -0
- package/.devcontainer/devzone-platform.sh +189 -0
- package/.devcontainer/devzone-repair.sh +94 -0
- package/.devcontainer/devzone-services.sh +241 -0
- package/.devcontainer/devzone-skill.sh +97 -0
- package/.devcontainer/devzone-status.sh +140 -0
- package/.devcontainer/devzone-version.sh +78 -0
- package/.devcontainer/git-identity.sh +28 -0
- package/.devcontainer/gitlab/argocd.sh +2 -2
- package/.devcontainer/gitlab/chart.sh +2 -2
- package/.devcontainer/gitlab/gitlab.sh +83 -84
- package/.devcontainer/gitlab/installation.sh +111 -111
- package/.devcontainer/gitlab/secrets.sh +47 -47
- package/.devcontainer/gitlab/seed.sh +77 -77
- package/.devcontainer/gitlab/test-gitlab.sh +242 -243
- package/.devcontainer/help.sh +1 -1
- package/.devcontainer/isolation.sh +65 -0
- package/.devcontainer/kind-wrapper.sh +45 -0
- package/.devcontainer/kube-account.sh +199 -0
- package/.devcontainer/kube-config.sh +45 -46
- package/.devcontainer/{invite-kube.sh → kube-prompt.sh} +6 -6
- package/.devcontainer/kube-reachable.sh +64 -0
- package/.devcontainer/{memoire.sh → memory.sh} +1 -1
- package/.devcontainer/metrics-server.sh +2 -2
- package/.devcontainer/opencode.jsonc +2 -2
- package/.devcontainer/packages.sh +228 -0
- package/.devcontainer/password.sh +1 -1
- package/.devcontainer/ports-auto.sh +152 -153
- package/.devcontainer/ports-json.sh +16 -15
- package/.devcontainer/ports.sh +127 -123
- package/.devcontainer/post-create.sh +52 -52
- package/.devcontainer/postgres/postgres.sh +176 -176
- package/.devcontainer/rename-legacy-files.sh +103 -0
- package/.devcontainer/services-admin.sh +5 -5
- package/.devcontainer/skills/devzone/SKILL.md +40 -41
- package/.devcontainer/startup.sh +65 -0
- package/.devcontainer/{etat-gitops.sh → state-gitops.sh} +1 -1
- package/.devcontainer/{etat-json.sh → state-json.sh} +39 -65
- package/.devcontainer/{etat-verrou.sh → state-lock.sh} +1 -1
- package/.devcontainer/{etat.sh → state.sh} +4 -4
- package/.devcontainer/workspace-init.sh +17 -3
- package/Makefile +8 -8
- package/README.md +195 -194
- package/charts/devzone-workspace/templates/statefulset.yaml +1 -1
- package/manage/devzone +8 -10
- package/manage/scripts/argocd-workspace.sh +1 -1
- package/manage/scripts/check-go-layout.sh +40 -0
- package/manage/scripts/check-names.sh +61 -0
- package/manage/scripts/create-cluster.sh +1 -1
- package/manage/scripts/create-mode.sh +2 -2
- package/manage/scripts/devzone-create.sh +5 -5
- package/manage/scripts/devzone-manifest.sh +3 -3
- package/manage/scripts/devzone-update.sh +12 -8
- package/manage/scripts/docker-workstation.sh +2 -2
- package/manage/scripts/docker.sh +2 -2
- package/manage/scripts/{lancer.sh → launch.sh} +10 -51
- package/manage/scripts/{migrer-retour.sh → migrate-revert.sh} +4 -4
- package/manage/scripts/{migrer.sh → migrate.sh} +8 -14
- package/manage/scripts/ports-apps.sh +1 -1
- package/manage/scripts/ports-workspace.sh +7 -7
- package/manage/scripts/{desinstaller.sh → uninstall.sh} +8 -8
- package/manage/scripts/workspace-egress.sh +2 -2
- package/manage/scripts/workspace-update.sh +5 -2
- package/manage/scripts/workspace-wait.sh +2 -2
- package/manage/workspace.sh +4 -4
- package/package.json +1 -1
- package/.devcontainer/catalogue/app.sh +0 -211
- package/.devcontainer/catalogue/catalogue-veilleur.sh +0 -130
- package/.devcontainer/catalogue/mysql/installer.sh +0 -87
- package/.devcontainer/catalogue/mysql/mysql.sh +0 -262
- package/.devcontainer/catalogue/mysql/volume.sh +0 -71
- package/.devcontainer/catalogue.sh +0 -231
- package/.devcontainer/demarrage.sh +0 -66
- package/.devcontainer/docker-relais.sh +0 -149
- package/.devcontainer/docker-sans-hote.sh +0 -12
- package/.devcontainer/git-identite.sh +0 -28
- package/.devcontainer/isolement.sh +0 -65
- package/.devcontainer/kind-enveloppe.sh +0 -45
- package/.devcontainer/kube-compte.sh +0 -200
- package/.devcontainer/kube-joignable.sh +0 -64
- package/.devcontainer/paquets.sh +0 -227
- /package/.devcontainer/{ia/docker-garde.message → ai/docker-guard.message} +0 -0
- /package/.devcontainer/{ia → ai}/host-check.message +0 -0
- /package/.devcontainer/{ia → ai}/host-check.sh +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Catalog hot detection (#414): when .devzone.modules changes, catalog.sh apply
|
|
3
|
+
# runs. A single instance (lock, as #394).
|
|
4
|
+
# catalog-watcher.sh demarrer | arreter | tour
|
|
5
|
+
set -euo pipefail
|
|
6
|
+
|
|
7
|
+
ICI="$(cd "$(dirname "$0")" && pwd)"
|
|
8
|
+
RACINE="${DEVZONE_ROOT:-$(cd "$ICI/../.." && pwd)}"
|
|
9
|
+
ETAT="${DEVZONE_STATE:-$RACINE/.devzone.modules}"
|
|
10
|
+
# shellcheck source=SCRIPTDIR/state.sh
|
|
11
|
+
# shellcheck disable=SC1091
|
|
12
|
+
. "$(dirname "$ICI")/state.sh"
|
|
13
|
+
BASE="${DEVZONE_CATALOG_WATCHER_DIR:-$RACINE/.devzone.catalog-watcher}"
|
|
14
|
+
LOCK_DIR="$BASE/verrou"
|
|
15
|
+
LAST_FILE="$BASE/dernier"
|
|
16
|
+
|
|
17
|
+
refuse() { echo "devzone : catalog-watcher : $*" >&2; exit 2; }
|
|
18
|
+
|
|
19
|
+
# Guard (#293): never under bats, only with DEVZONE_CATALOG_REAL=yes
|
|
20
|
+
guard_watcher() {
|
|
21
|
+
if [ "${DEVZONE_CATALOG_REAL:-}" != yes ]; then
|
|
22
|
+
echo "devzone : catalog-watcher : refused here (DEVZONE_CATALOG_REAL=yes)" >&2
|
|
23
|
+
return 2
|
|
24
|
+
fi
|
|
25
|
+
if [ -n "${BATS_TEST_TMPDIR:-}" ]; then
|
|
26
|
+
echo "devzone : catalog-watcher : refused under bats (tests)" >&2
|
|
27
|
+
return 2
|
|
28
|
+
fi
|
|
29
|
+
return 0
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# Exclusive lock (mkdir) with the pid; a single watcher instance
|
|
33
|
+
lock_take() {
|
|
34
|
+
local start now age pid
|
|
35
|
+
start="$(date +%s)"
|
|
36
|
+
while true; do
|
|
37
|
+
if mkdir "$LOCK_DIR" 2>/dev/null; then echo "$$" >"$LOCK_DIR/pid"; return 0; fi
|
|
38
|
+
now="$(date +%s)"
|
|
39
|
+
age=""; [ -d "$LOCK_DIR" ] && age="$(stat -c %Y "$LOCK_DIR" 2>/dev/null || stat -f %m "$LOCK_DIR" 2>/dev/null || true)"
|
|
40
|
+
pid="$(cat "$LOCK_DIR/pid" 2>/dev/null || true)"
|
|
41
|
+
if [ -n "$age" ] && [ "$(( now - age ))" -gt 30 ]; then rm -rf "$LOCK_DIR"; continue; fi
|
|
42
|
+
if [ -n "$pid" ] && ! kill -0 "$pid" 2>/dev/null; then rm -rf "$LOCK_DIR"; continue; fi
|
|
43
|
+
[ "$(( now - start ))" -ge 5 ] && return 1
|
|
44
|
+
sleep 1
|
|
45
|
+
done
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
# Marks the live lock (date and pid up to date)
|
|
49
|
+
lock_refresh() { touch "$LOCK_DIR" 2>/dev/null || true; echo "$$" >"$LOCK_DIR/pid" 2>/dev/null || true; }
|
|
50
|
+
|
|
51
|
+
# One tick: if .devzone.modules changed since the last tick, apply
|
|
52
|
+
tick() {
|
|
53
|
+
local now previous
|
|
54
|
+
[ -f "$ETAT" ] || return 0
|
|
55
|
+
# Restored or rebuilt state: the watcher does not follow a doubtful state,
|
|
56
|
+
# and never uninstalls until devzone apply has confirmed it
|
|
57
|
+
state_recovery_wait && return 0
|
|
58
|
+
state_recovery_pending && return 0
|
|
59
|
+
mkdir -p "$BASE"
|
|
60
|
+
now="$(stat -c %Y "$ETAT" 2>/dev/null || stat -f %m "$ETAT" 2>/dev/null || true)"
|
|
61
|
+
[ -n "$now" ] || return 0
|
|
62
|
+
previous="$(cat "$LAST_FILE" 2>/dev/null || true)"
|
|
63
|
+
if [ "$now" != "$previous" ]; then
|
|
64
|
+
echo "$now" >"$LAST_FILE"
|
|
65
|
+
echo "devzone : catalog-watcher : .devzone.modules changed, applying" >&2
|
|
66
|
+
bash "$ICI/../catalog.sh" apply
|
|
67
|
+
fi
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
# Loop: one tick every 5 s, lock refreshed on each tick
|
|
71
|
+
loop() {
|
|
72
|
+
mkdir -p "$BASE"
|
|
73
|
+
if ! lock_take; then return 0; fi
|
|
74
|
+
trap 'rm -rf "$LOCK_DIR"' EXIT INT TERM
|
|
75
|
+
while true; do
|
|
76
|
+
[ -f "$ETAT" ] || { rm -rf "$LOCK_DIR"; break; }
|
|
77
|
+
lock_refresh
|
|
78
|
+
tick || true
|
|
79
|
+
sleep 5
|
|
80
|
+
done
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
# Starts the detached loop (#435) and checks that its lock is alive
|
|
84
|
+
start_detached() {
|
|
85
|
+
local i pid stable=0 detache=()
|
|
86
|
+
mkdir -p "$BASE"
|
|
87
|
+
# Same detachment as relay_start: setsid when present, else nohup.
|
|
88
|
+
command -v setsid >/dev/null 2>&1 && detache=(setsid)
|
|
89
|
+
${detache[@]+"${detache[@]}"} nohup env DEVZONE_CATALOG_REAL=yes bash "$0" boucle >"$BASE/veilleur.log" 2>&1 &
|
|
90
|
+
i=0
|
|
91
|
+
while [ "$i" -lt 20 ]; do
|
|
92
|
+
pid="$(cat "$LOCK_DIR/pid" 2>/dev/null || true)"
|
|
93
|
+
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
|
94
|
+
stable=$((stable + 1))
|
|
95
|
+
[ "$stable" -ge 5 ] && { echo "started"; return 0; }
|
|
96
|
+
else
|
|
97
|
+
stable=0
|
|
98
|
+
fi
|
|
99
|
+
sleep 0.1
|
|
100
|
+
i=$((i + 1))
|
|
101
|
+
done
|
|
102
|
+
echo "devzone : catalog-watcher : cannot start, see $BASE/veilleur.log" >&2
|
|
103
|
+
return 1
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
# Stops the watcher and removes the lock
|
|
107
|
+
stop_watcher() {
|
|
108
|
+
local pid
|
|
109
|
+
pid="$(cat "$LOCK_DIR/pid" 2>/dev/null || true)"
|
|
110
|
+
if [ -n "$pid" ]; then kill "$pid" 2>/dev/null || true; fi
|
|
111
|
+
rm -rf "$LOCK_DIR"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case "${1:-}" in
|
|
115
|
+
boucle | loop) if ! guard_watcher; then exit 2; fi; loop ;;
|
|
116
|
+
demarrer | start | lancer)
|
|
117
|
+
if ! guard_watcher; then exit 2; fi
|
|
118
|
+
if [ -f "$LOCK_DIR/pid" ] && kill -0 "$(cat "$LOCK_DIR/pid")" 2>/dev/null; then
|
|
119
|
+
echo "already running"
|
|
120
|
+
else
|
|
121
|
+
start_detached || exit 1
|
|
122
|
+
fi
|
|
123
|
+
;;
|
|
124
|
+
arreter | stop) stop_watcher ; echo "stopped" ;;
|
|
125
|
+
tour | un-tour) tick ;;
|
|
126
|
+
*)
|
|
127
|
+
echo "devzone : catalog-watcher : demarrer | arreter | tour" >&2
|
|
128
|
+
exit 2
|
|
129
|
+
;;
|
|
130
|
+
esac
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# Application MySQL du catalogue (#414) : MySQL local de test pour les projets
|
|
2
2
|
# du workspace, installé dans le cluster des services de devzone.
|
|
3
3
|
# Mémoire et taille du volume : 1 Gi et 5Gi par défaut, modifiables ici.
|
|
4
|
-
|
|
4
|
+
name=mysql
|
|
5
5
|
description=MySQL partagé, une base par projet
|
|
6
6
|
image=mysql:8.4
|
|
7
7
|
port=3306
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# MySQL installation (#440): secret, chart and reinstallation on a volume.
|
|
3
|
+
# shellcheck source=SCRIPTDIR/../../chart-direct.sh
|
|
4
|
+
# shellcheck disable=SC1091
|
|
5
|
+
. "$ICI/../../chart-direct.sh"
|
|
6
|
+
|
|
7
|
+
# Admin secret (mysql-admin): root.env or existing secret, never regenerated.
|
|
8
|
+
mysql_secret() {
|
|
9
|
+
local password file="$SECRETS/admin-password"
|
|
10
|
+
mkdir -p "$SECRETS"
|
|
11
|
+
chmod 700 "$SECRETS"
|
|
12
|
+
if k get secret mysql-admin >/dev/null 2>&1; then
|
|
13
|
+
password="$(k get secret mysql-admin -o jsonpath='{.data.password}' | base64 -d | tr -d '\n')"
|
|
14
|
+
(umask 077 && printf '%s' "$password" >"$file")
|
|
15
|
+
return 0
|
|
16
|
+
fi
|
|
17
|
+
password="$(sed -n 's/^MYSQL_PASSWORD=//p' "$SECRETS/root.env" 2>/dev/null | tail -1 || true)"
|
|
18
|
+
if [ -z "$password" ] && [ -s "$file" ]; then password="$(cat "$file")"; fi
|
|
19
|
+
if [ -z "$password" ]; then
|
|
20
|
+
(umask 077 && openssl rand -hex 24 | tr -d '\n' >"$file")
|
|
21
|
+
k create secret generic mysql-admin --from-file=password="$file" >/dev/null
|
|
22
|
+
elif [ ! -s "$file" ]; then
|
|
23
|
+
(umask 077 && printf '%s' "$password" >"$file")
|
|
24
|
+
fi
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
# Wait for the StatefulSet created by Argo CD (#538): the sync runs in the
|
|
28
|
+
# background, the rollout is polled up to MYSQL_ATTENTE
|
|
29
|
+
mysql_wait() {
|
|
30
|
+
local end=$((SECONDS + MYSQL_ATTENTE))
|
|
31
|
+
while [ "$SECONDS" -lt "$end" ]; do
|
|
32
|
+
if k rollout status statefulset/mysql --timeout=10s >/dev/null 2>&1; then
|
|
33
|
+
return 0
|
|
34
|
+
fi
|
|
35
|
+
sleep 5
|
|
36
|
+
done
|
|
37
|
+
echo "devzone : catalog mysql : not ready after ${MYSQL_ATTENTE}s: kubectl --context $1 -n $NS describe pod mysql-0" >&2
|
|
38
|
+
return 1
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
# MySQL installation by Argo CD (#538): Application devzone-mysql (chart
|
|
42
|
+
# charts/devzone-mysql of the devzone repository), namespace and secret created
|
|
43
|
+
# apart; the claim follows the existing volumes, as the manifest path does
|
|
44
|
+
mysql_install_argocd() {
|
|
45
|
+
local CTX="$1" claim extra=""
|
|
46
|
+
mysql_secret
|
|
47
|
+
claim="$(mysql_claim_choose "$NS")" || return 2
|
|
48
|
+
[ "$claim" = donnees ] || extra="claim=$claim"
|
|
49
|
+
catalogue_argocd_apply mysql "$NS" "$MYSQL_IMAGE" "$MYSQL_PORT" \
|
|
50
|
+
"$MYSQL_MEMOIRE" "$MYSQL_TAILLE" "$extra" || return 1
|
|
51
|
+
mysql_wait "$CTX" || return 1
|
|
52
|
+
mysql_secret
|
|
53
|
+
relay_start mysql "$CTX" "$NS" svc/mysql 3306:3306
|
|
54
|
+
echo "✔ MySQL ready: 127.0.0.1:$MYSQL_PORT in the container"
|
|
55
|
+
regenerer_identifiants
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
# Installs the chart charts/devzone-mysql with the chosen claim (#440, #559)
|
|
59
|
+
mysql_apply_chart() {
|
|
60
|
+
local ns="$1" claim="$2"
|
|
61
|
+
chart_install mysql "$ns" "image=$MYSQL_IMAGE" "port=$MYSQL_PORT" \
|
|
62
|
+
"memory=$MYSQL_MEMOIRE" "size=$MYSQL_TAILLE" "claim=$claim"
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# (Re)installs MySQL on the requested claim, PVC data kept (#440)
|
|
66
|
+
mysql_install_claim() {
|
|
67
|
+
local ns="$1" claim="$2" CTX
|
|
68
|
+
CTX="$(module_context)"
|
|
69
|
+
if ! devzone_context_set "$CTX"; then
|
|
70
|
+
echo "devzone : catalog mysql : cannot set the devzone context on $CTX" >&2
|
|
71
|
+
return 1
|
|
72
|
+
fi
|
|
73
|
+
echo "▶ MySQL on cluster $CTX (namespace $ns, volume ${claim}-mysql-0)…"
|
|
74
|
+
account_namespace "$CTX" "$ns"
|
|
75
|
+
namespace_isolate "$ns" >/dev/null
|
|
76
|
+
mysql_secret
|
|
77
|
+
k delete statefulset mysql --ignore-not-found >/dev/null 2>&1 || true
|
|
78
|
+
mysql_apply_chart "$ns" "$claim" || return 1
|
|
79
|
+
if ! k rollout status statefulset/mysql --timeout="${MYSQL_ATTENTE}s" >/dev/null; then
|
|
80
|
+
echo "devzone : catalog mysql : not ready after ${MYSQL_ATTENTE}s: kubectl --context $CTX -n $ns describe pod mysql-0" >&2
|
|
81
|
+
return 1
|
|
82
|
+
fi
|
|
83
|
+
mysql_secret
|
|
84
|
+
relay_start mysql "$CTX" "$ns" svc/mysql 3306:3306
|
|
85
|
+
echo "✔ MySQL ready: 127.0.0.1:$MYSQL_PORT in the container"
|
|
86
|
+
regenerer_identifiants
|
|
87
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# MySQL of the catalog (#414): create, list, delete, status, relay-start, relay-stop, install, delete-data.
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
ICI="$(cd "$(dirname "$0")" && pwd)"
|
|
5
|
+
RACINE="${DEVZONE_ROOT:-$(dirname "$(dirname "$(dirname "$ICI")")")}"
|
|
6
|
+
ETAT="${DEVZONE_STATE:-$RACINE/.devzone.modules}"
|
|
7
|
+
# shellcheck source=SCRIPTDIR/../../cluster.sh
|
|
8
|
+
# shellcheck disable=SC1091
|
|
9
|
+
. "$ICI/../../cluster.sh"
|
|
10
|
+
# shellcheck source=SCRIPTDIR/../../password.sh
|
|
11
|
+
# shellcheck disable=SC1091
|
|
12
|
+
. "$ICI/../../password.sh"
|
|
13
|
+
# shellcheck source=SCRIPTDIR/../../isolation.sh
|
|
14
|
+
# shellcheck disable=SC1091
|
|
15
|
+
. "$ICI/../../isolation.sh"
|
|
16
|
+
# shellcheck source=SCRIPTDIR/volume.sh
|
|
17
|
+
# shellcheck disable=SC1091
|
|
18
|
+
. "$ICI/volume.sh"
|
|
19
|
+
# shellcheck source=SCRIPTDIR/install.sh
|
|
20
|
+
# shellcheck disable=SC1091
|
|
21
|
+
. "$ICI/install.sh"
|
|
22
|
+
# The argocd module on: MySQL installed by an Application (#538)
|
|
23
|
+
# shellcheck source=SCRIPTDIR/../../argocd/catalog-argocd.sh
|
|
24
|
+
# shellcheck disable=SC1091
|
|
25
|
+
. "$ICI/../../argocd/catalog-argocd.sh"
|
|
26
|
+
NS=devzone-mysql
|
|
27
|
+
SECRETS="$RACINE/workspace/.mysql"
|
|
28
|
+
MYSQL_PORT="${MYSQL_PORT:-3306}"
|
|
29
|
+
MYSQL_IMAGE="${MYSQL_IMAGE:-mysql:8.4}"
|
|
30
|
+
MYSQL_MEMOIRE="${MYSQL_MEMOIRE:-1Gi}"
|
|
31
|
+
MYSQL_TAILLE="${MYSQL_TAILLE:-5Gi}"
|
|
32
|
+
MYSQL_ATTENTE="${MYSQL_ATTENTE:-300}"
|
|
33
|
+
# Standard refusal with a message and code 2
|
|
34
|
+
refuse() { echo "devzone : catalog mysql : $*" >&2; exit 2; }
|
|
35
|
+
# Secret mysql-admin of the services.sh hook: the same as the direct installation
|
|
36
|
+
services_secret_mysql() { mysql_secret; }
|
|
37
|
+
# Regenerates the .password view (non-blocking failure)
|
|
38
|
+
regenerer_identifiants() {
|
|
39
|
+
password_regenerate ||
|
|
40
|
+
echo "devzone : catalog mysql : grouped credentials not updated (devzone passwords)" >&2
|
|
41
|
+
}
|
|
42
|
+
# Shortcut for kubectl in the devzone-mysql namespace
|
|
43
|
+
k() { kubectl --context devzone -n "$NS" "$@"; }
|
|
44
|
+
# Does the devzone-mysql namespace exist? Administrator only (#430)
|
|
45
|
+
installed() { account_admin "$(account_context)" get namespace "$NS" >/dev/null 2>&1; }
|
|
46
|
+
# Context of the module: the one of the devzone cluster
|
|
47
|
+
module_context() {
|
|
48
|
+
local c
|
|
49
|
+
c="$(cluster_saved)"
|
|
50
|
+
[ -n "$c" ] || refuse "no cluster chosen: devzone enable mysql"
|
|
51
|
+
echo "$c"
|
|
52
|
+
}
|
|
53
|
+
# Failure marker of the module (same scheme as devzone's failure_file)
|
|
54
|
+
failure_file() { echo "$ETAT-failed-mysql"; }
|
|
55
|
+
# Project name: a simple SQL identifier, never escaped, not a reserved name
|
|
56
|
+
check_project_name() {
|
|
57
|
+
local p="${1:-}"
|
|
58
|
+
[ -n "$p" ] || refuse "missing project name"
|
|
59
|
+
[[ "$p" =~ ^[a-z][a-z0-9_]{0,62}$ ]] || refuse "invalid project name: $p (lowercase, digits, _; starts with a letter)"
|
|
60
|
+
case "$p" in
|
|
61
|
+
mysql | information_schema | performance_schema | sys) refuse "reserved name: $p" ;;
|
|
62
|
+
esac
|
|
63
|
+
}
|
|
64
|
+
# Random password for a new project
|
|
65
|
+
new_password() { openssl rand -hex 24; }
|
|
66
|
+
# Root password: from root.env of an existing installation, else admin-password
|
|
67
|
+
root_password() {
|
|
68
|
+
local password
|
|
69
|
+
password="$(sed -n 's/^MYSQL_PASSWORD=//p' "$SECRETS/root.env" 2>/dev/null | tail -1 || true)"
|
|
70
|
+
[ -z "$password" ] && [ -f "$SECRETS/admin-password" ] && password="$(cat "$SECRETS/admin-password" 2>/dev/null || true)"
|
|
71
|
+
[ -n "$password" ] || refuse "MySQL root password not found (workspace/.mysql/root.env)"
|
|
72
|
+
echo "$password"
|
|
73
|
+
}
|
|
74
|
+
# mysql as superuser in the pod; the password is never shown
|
|
75
|
+
mysql_admin() { k exec -i mysql-0 -c mysql -- mysql -uroot -p"$(root_password)" "$@"; }
|
|
76
|
+
# Number of databases, system databases excluded
|
|
77
|
+
database_count() {
|
|
78
|
+
local n
|
|
79
|
+
n="$(mysql_admin -N -B -e "SHOW DATABASES" 2>/dev/null | grep -vE '^(information_schema|mysql|performance_schema|sys)$' | grep -c . || true)"
|
|
80
|
+
echo "${n:-0}"
|
|
81
|
+
}
|
|
82
|
+
# Installs the module (the Argo CD path when the argocd module is on)
|
|
83
|
+
install_module() {
|
|
84
|
+
local CTX claim
|
|
85
|
+
CTX="$(module_context)"
|
|
86
|
+
if ! devzone_context_set "$CTX"; then
|
|
87
|
+
echo "devzone : catalog mysql : cannot set the devzone context on $CTX" >&2
|
|
88
|
+
return 1
|
|
89
|
+
fi
|
|
90
|
+
echo "▶ MySQL on cluster $CTX (namespace $NS)…"
|
|
91
|
+
if catalogue_argocd_active mysql; then
|
|
92
|
+
mysql_install_argocd "$CTX"
|
|
93
|
+
return
|
|
94
|
+
fi
|
|
95
|
+
account_namespace "$CTX" "$NS"
|
|
96
|
+
namespace_isolate "$NS" >/dev/null
|
|
97
|
+
if k get statefulset mysql >/dev/null 2>&1; then
|
|
98
|
+
echo "devzone : catalog mysql : existing installation in $NS, adopted (volume and root.env kept)"
|
|
99
|
+
else
|
|
100
|
+
mysql_secret
|
|
101
|
+
claim="$(mysql_claim_choose "$NS")" || return 2
|
|
102
|
+
mysql_apply_chart "$NS" "$claim" || return 1
|
|
103
|
+
if ! k rollout status statefulset/mysql --timeout="${MYSQL_ATTENTE}s" >/dev/null; then
|
|
104
|
+
echo "devzone : catalog mysql : not ready after ${MYSQL_ATTENTE}s: kubectl --context $CTX -n $NS describe pod mysql-0" >&2
|
|
105
|
+
return 1
|
|
106
|
+
fi
|
|
107
|
+
fi
|
|
108
|
+
mysql_secret
|
|
109
|
+
relay_start mysql "$CTX" "$NS" svc/mysql 3306:3306
|
|
110
|
+
echo "✔ MySQL ready: 127.0.0.1:$MYSQL_PORT in the container"
|
|
111
|
+
regenerer_identifiants
|
|
112
|
+
}
|
|
113
|
+
# Deletes the namespace (so the PVCs), keeps workspace/.mysql (#441):
|
|
114
|
+
# terminal confirmation [y/N], --yes required otherwise (refusal, code 2)
|
|
115
|
+
delete_data() {
|
|
116
|
+
local CTX yes="" arg answer
|
|
117
|
+
for arg in "$@"; do
|
|
118
|
+
case "$arg" in
|
|
119
|
+
--yes) yes=1 ;;
|
|
120
|
+
*) refuse "unknown argument: $arg" ;;
|
|
121
|
+
esac
|
|
122
|
+
done
|
|
123
|
+
if [ -z "$yes" ]; then
|
|
124
|
+
cluster_interactive || refuse "confirmation needs a terminal: devzone mysql delete-data --yes"
|
|
125
|
+
printf 'Delete namespace %s and all its volumes? [y/N] ' "$NS" >&2
|
|
126
|
+
read -r answer || answer=""
|
|
127
|
+
case "$answer" in
|
|
128
|
+
y | Y | yes) ;;
|
|
129
|
+
*) echo "Aborted: nothing was deleted."; return 0 ;;
|
|
130
|
+
esac
|
|
131
|
+
fi
|
|
132
|
+
CTX="$(cluster_saved)"
|
|
133
|
+
[ -n "$CTX" ] || refuse "no cluster chosen: devzone enable mysql"
|
|
134
|
+
installed || refuse "module disabled: devzone enable mysql"
|
|
135
|
+
echo "▶ Deleting MySQL and its volumes on ${CTX}…"
|
|
136
|
+
# The Application goes first (#538): otherwise Argo CD recreates the namespace
|
|
137
|
+
catalogue_argocd_delete mysql >/dev/null 2>&1 || true
|
|
138
|
+
# RoleBinding removed, then the namespace deleted by the administrator (#430)
|
|
139
|
+
account_remove "$CTX" "$NS" >/dev/null 2>&1 || true
|
|
140
|
+
account_admin "$CTX" delete namespace "$NS" --wait=true >/dev/null
|
|
141
|
+
relay_stop mysql
|
|
142
|
+
rm -f "$(failure_file)"
|
|
143
|
+
echo "✔ namespace $NS and volume deleted; workspace/.mysql/root.env kept"
|
|
144
|
+
regenerer_identifiants
|
|
145
|
+
}
|
|
146
|
+
# State (disabled, failed, ready, relay stopped): read by devzone, keep the words
|
|
147
|
+
module_status() {
|
|
148
|
+
local CTX
|
|
149
|
+
CTX="$(cluster_saved)"
|
|
150
|
+
if [ -z "$CTX" ] || ! installed; then
|
|
151
|
+
echo "disabled"
|
|
152
|
+
return 0
|
|
153
|
+
fi
|
|
154
|
+
if [ "$(k get statefulset mysql -o jsonpath='{.status.readyReplicas}' 2>/dev/null || true)" != 1 ]; then
|
|
155
|
+
echo "failed on $CTX: devzone apply"
|
|
156
|
+
return 0
|
|
157
|
+
fi
|
|
158
|
+
if ! relay_active mysql; then
|
|
159
|
+
echo "ready on $CTX, relay stopped: devzone apply"
|
|
160
|
+
return 0
|
|
161
|
+
fi
|
|
162
|
+
echo "ready on $CTX: 127.0.0.1:$MYSQL_PORT, $(database_count) database(s)"
|
|
163
|
+
}
|
|
164
|
+
# Creates the user and the database of a project; .env in 600, password kept
|
|
165
|
+
create_project() {
|
|
166
|
+
local project="${1:-}" renew="" file password arg
|
|
167
|
+
check_project_name "$project"
|
|
168
|
+
for arg in "${@:2}"; do
|
|
169
|
+
case "$arg" in
|
|
170
|
+
--new-password) renew=1 ;;
|
|
171
|
+
*) refuse "unknown argument: $arg" ;;
|
|
172
|
+
esac
|
|
173
|
+
done
|
|
174
|
+
CTX="$(module_context)"
|
|
175
|
+
installed || refuse "module disabled: devzone enable mysql"
|
|
176
|
+
file="$SECRETS/$project.env"
|
|
177
|
+
if [ -z "$renew" ] && [ -s "$file" ]; then
|
|
178
|
+
password="$(sed -n 's/^MYSQL_PASSWORD=//p' "$file" 2>/dev/null | tail -1)"
|
|
179
|
+
else
|
|
180
|
+
password="$(new_password)"
|
|
181
|
+
fi
|
|
182
|
+
mysql_admin <<SQL
|
|
183
|
+
CREATE USER IF NOT EXISTS '$project'@'%' IDENTIFIED BY '$password';
|
|
184
|
+
ALTER USER '$project'@'%' IDENTIFIED BY '$password';
|
|
185
|
+
CREATE DATABASE IF NOT EXISTS $project;
|
|
186
|
+
GRANT ALL ON $project.* TO '$project'@'%';
|
|
187
|
+
SQL
|
|
188
|
+
mkdir -p "$SECRETS"
|
|
189
|
+
chmod 700 "$SECRETS"
|
|
190
|
+
(
|
|
191
|
+
umask 077
|
|
192
|
+
cat >"$file" <<ENV
|
|
193
|
+
MYSQL_HOST=127.0.0.1
|
|
194
|
+
MYSQL_PORT=$MYSQL_PORT
|
|
195
|
+
MYSQL_USER=$project
|
|
196
|
+
MYSQL_PASSWORD=$password
|
|
197
|
+
MYSQL_URL=jdbc:mysql://127.0.0.1:$MYSQL_PORT/$project
|
|
198
|
+
ENV
|
|
199
|
+
)
|
|
200
|
+
echo "✔ database $project ready; credentials: $file"
|
|
201
|
+
regenerer_identifiants
|
|
202
|
+
}
|
|
203
|
+
# Lists the databases of the server
|
|
204
|
+
list_databases() {
|
|
205
|
+
local CTX
|
|
206
|
+
CTX="$(module_context)"
|
|
207
|
+
installed || refuse "module disabled: devzone enable mysql"
|
|
208
|
+
# grep -v exits 1 with no user database: under pipefail, the list stays at 0
|
|
209
|
+
mysql_admin -N -B -e "SHOW DATABASES" 2>/dev/null |
|
|
210
|
+
{ grep -vE '^(information_schema|mysql|performance_schema|sys)$' || true; } |
|
|
211
|
+
awk 'BEGIN { n = 0 } { printf " %-30s\n", $1; n++ } END { if (n == 0) print " (no database: devzone mysql create <project>)" }'
|
|
212
|
+
}
|
|
213
|
+
# Deletes the database and the user of a project (confirmation without --yes)
|
|
214
|
+
delete_project() {
|
|
215
|
+
local project="${1:-}" yes="" arg answer
|
|
216
|
+
check_project_name "$project"
|
|
217
|
+
for arg in "${@:2}"; do
|
|
218
|
+
case "$arg" in
|
|
219
|
+
--yes) yes=1 ;;
|
|
220
|
+
*) refuse "unknown argument: $arg" ;;
|
|
221
|
+
esac
|
|
222
|
+
done
|
|
223
|
+
CTX="$(module_context)"
|
|
224
|
+
installed || refuse "module disabled: devzone enable mysql"
|
|
225
|
+
if [ -z "$yes" ]; then
|
|
226
|
+
cluster_interactive || refuse "confirmation needs a terminal: devzone mysql delete $project --yes"
|
|
227
|
+
printf 'Delete database %s and its user? [y/N] ' "$project" >&2
|
|
228
|
+
read -r answer || answer=""
|
|
229
|
+
case "$answer" in
|
|
230
|
+
y | Y | yes) ;;
|
|
231
|
+
*) echo "Aborted: nothing was deleted."; return 0 ;;
|
|
232
|
+
esac
|
|
233
|
+
fi
|
|
234
|
+
mysql_admin <<SQL
|
|
235
|
+
DROP DATABASE IF EXISTS $project;
|
|
236
|
+
DROP USER IF EXISTS '$project'@'%';
|
|
237
|
+
SQL
|
|
238
|
+
rm -f "$SECRETS/$project.env"
|
|
239
|
+
echo "✔ database $project deleted"
|
|
240
|
+
regenerer_identifiants
|
|
241
|
+
}
|
|
242
|
+
# Restarts the relay 127.0.0.1:3306 if the namespace exists
|
|
243
|
+
relay_restart() {
|
|
244
|
+
local CTX
|
|
245
|
+
CTX="$(cluster_saved)"
|
|
246
|
+
[ -n "$CTX" ] && installed && relay_start mysql "$CTX" "$NS" svc/mysql 3306:3306
|
|
247
|
+
}
|
|
248
|
+
case "${1:-}" in
|
|
249
|
+
install) install_module ;;
|
|
250
|
+
delete-data) delete_data "${@:2}" ;;
|
|
251
|
+
status) module_status ;;
|
|
252
|
+
create) create_project "${@:2}" ;;
|
|
253
|
+
list) list_databases ;;
|
|
254
|
+
delete) delete_project "${@:2}" ;;
|
|
255
|
+
volume) mysql_volume "$NS" "${@:2}" ;;
|
|
256
|
+
relay-start) relay_restart ;;
|
|
257
|
+
relay-stop) relay_stop mysql ;;
|
|
258
|
+
*)
|
|
259
|
+
echo "devzone : catalog mysql : create <project> [--new-password] | list | delete <project> [--yes] | volume [<name>] [--delete <name>] [--yes] | delete-data [--yes]" >&2
|
|
260
|
+
exit 1
|
|
261
|
+
;;
|
|
262
|
+
esac
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# MySQL volumes (#440): choice, listing and deletion of the -mysql-0 PVCs.
|
|
3
|
+
|
|
4
|
+
# Lists the PVCs of the namespace whose name ends with -mysql-0
|
|
5
|
+
mysql_volumes() {
|
|
6
|
+
local ns="$1"
|
|
7
|
+
kubectl --context devzone -n "$ns" get pvc -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}' 2>/dev/null |
|
|
8
|
+
grep -E '\-mysql-0$' || true
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
# Claim prefix: 1 PVC -> its prefix, 0 -> donnees, >1 -> refusal (code 2)
|
|
12
|
+
mysql_claim_choose() {
|
|
13
|
+
local ns="$1" list count
|
|
14
|
+
list="$(mysql_volumes "$ns")"
|
|
15
|
+
if [ -z "$list" ]; then echo donnees; return 0; fi
|
|
16
|
+
count="$(printf '%s\n' "$list" | grep -c .)"
|
|
17
|
+
if [ "$count" -eq 1 ]; then echo "${list%-mysql-0}"; return 0; fi
|
|
18
|
+
echo "devzone : catalog mysql : several volumes found:" >&2
|
|
19
|
+
printf '%s\n' "$list" | sed 's/^/ /' >&2
|
|
20
|
+
echo "devzone : catalog mysql : choose one with devzone mysql volume <name>" >&2
|
|
21
|
+
return 2
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
# Claim carried by the StatefulSet (empty if absent)
|
|
25
|
+
mysql_claim_mounted() {
|
|
26
|
+
local ns="$1"
|
|
27
|
+
kubectl --context devzone -n "$ns" get statefulset mysql \
|
|
28
|
+
-o jsonpath='{.spec.volumeClaimTemplates[0].metadata.name}' 2>/dev/null || true
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
# volume subcommand: lists, or selects/deletes a PVC
|
|
32
|
+
mysql_volume() {
|
|
33
|
+
local ns="$1" name="" yes="" delete="" arg answer pvc mounted claim
|
|
34
|
+
shift
|
|
35
|
+
for arg in "$@"; do
|
|
36
|
+
case "$arg" in
|
|
37
|
+
--delete) delete=1 ;;
|
|
38
|
+
--yes) yes=1 ;;
|
|
39
|
+
*) name="$arg" ;;
|
|
40
|
+
esac
|
|
41
|
+
done
|
|
42
|
+
if [ -n "$delete" ]; then
|
|
43
|
+
[ -n "$name" ] || refuse "volume --delete: missing name"
|
|
44
|
+
pvc="$name"
|
|
45
|
+
[[ "$pvc" == *-mysql-0 ]] || pvc="$pvc-mysql-0"
|
|
46
|
+
mounted="$(mysql_claim_mounted "$ns")"
|
|
47
|
+
[ "${pvc%-mysql-0}" != "$mounted" ] || refuse "mounted volume, deletion refused: $name"
|
|
48
|
+
if [ -z "$yes" ]; then
|
|
49
|
+
cluster_interactive || refuse "confirmation needs a terminal: devzone mysql volume --delete $name --yes"
|
|
50
|
+
printf 'Delete volume %s? [y/N] ' "$pvc" >&2
|
|
51
|
+
read -r answer || answer=""
|
|
52
|
+
case "$answer" in
|
|
53
|
+
y | Y | yes) ;;
|
|
54
|
+
*) echo "Aborted: nothing was deleted."; return 0 ;;
|
|
55
|
+
esac
|
|
56
|
+
fi
|
|
57
|
+
kubectl --context devzone -n "$ns" delete pvc "$pvc" >/dev/null
|
|
58
|
+
echo "✔ volume $pvc deleted"
|
|
59
|
+
return 0
|
|
60
|
+
fi
|
|
61
|
+
if [ -z "$name" ]; then
|
|
62
|
+
mounted="$(mysql_claim_mounted "$ns")"
|
|
63
|
+
mysql_volumes "$ns" | awk -v m="$mounted-mysql-0" 'NF { mark = ($0 == m) ? " (mounted)" : ""; printf " %s%s\n", $0, mark; n++ } END { if (!n) print " (no volume)" }'
|
|
64
|
+
return 0
|
|
65
|
+
fi
|
|
66
|
+
module_context >/dev/null
|
|
67
|
+
installed || refuse "module disabled: devzone enable mysql"
|
|
68
|
+
claim="$name"
|
|
69
|
+
[[ "$claim" == *-mysql-0 ]] && claim="${claim%-mysql-0}"
|
|
70
|
+
mysql_install_claim "$ns" "$claim"
|
|
71
|
+
}
|