devzone 7.4.0 → 7.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/.devcontainer/devzone +13 -17
  2. package/.devcontainer/devzone-modules.sh +1 -1
  3. package/.devcontainer/devzone-repair.sh +2 -2
  4. package/.devcontainer/devzone-services.sh +7 -7
  5. package/.devcontainer/devzone-status.sh +5 -5
  6. package/.devcontainer/help.sh +3 -1
  7. package/.devcontainer/versions.env +3 -0
  8. package/.devcontainer/workspace-init.sh +8 -9
  9. package/.gitattributes +8 -0
  10. package/bin/SHA256SUMS +6 -6
  11. package/bin/devzone-darwin-amd64 +0 -0
  12. package/bin/devzone-darwin-arm64 +0 -0
  13. package/bin/devzone-linux-amd64 +0 -0
  14. package/bin/devzone-linux-arm64 +0 -0
  15. package/bin/devzone-windows-amd64.exe +0 -0
  16. package/bin/devzone-windows-arm64.exe +0 -0
  17. package/charts/devzone-mysql/values.yaml +1 -1
  18. package/charts/devzone-postgres/values.yaml +1 -1
  19. package/manage/devzone +2 -2
  20. package/manage/scripts/uninstall.sh +1 -1
  21. package/manage/scripts/workspace/argocd-workspace.sh +2 -2
  22. package/package.json +1 -1
  23. package/.devcontainer/catalog/app.sh +0 -202
  24. package/.devcontainer/catalog/catalog-watcher.sh +0 -127
  25. package/.devcontainer/catalog.sh +0 -208
  26. package/.devcontainer/chart-direct.sh +0 -49
  27. package/.devcontainer/cluster.sh +0 -388
  28. package/.devcontainer/clusters.sh +0 -86
  29. package/.devcontainer/devzone-cluster.sh +0 -209
  30. package/.devcontainer/isolation.sh +0 -65
  31. package/.devcontainer/kube-account.sh +0 -107
  32. package/.devcontainer/kube-prompt.sh +0 -42
  33. package/.devcontainer/memory.sh +0 -113
  34. package/.devcontainer/metrics-server.sh +0 -100
  35. package/.devcontainer/packages.sh +0 -228
  36. package/.devcontainer/password.sh +0 -148
  37. package/.devcontainer/ports-json.sh +0 -31
  38. package/.devcontainer/ports.sh +0 -132
@@ -1,65 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Network isolation and access authorization between devzone namespaces.
3
- # File <= 250 lines: functions namespace_isolate, allow, forbid.
4
- # These functions modify NetworkPolicies in the Kubernetes cluster.
5
- # Loaded by argocd.sh, catalog.sh, the mysql and postgres Go modules, gitlab.sh; shipped with the package.
6
-
7
- # Sets an isolating NetworkPolicy in a namespace.
8
- # Only the pods of the same namespace can reach the services.
9
- namespace_isolate() {
10
- local ns="${1:-}"
11
- [ -n "$ns" ] || { echo "devzone : namespace missing" >&2; return 2; }
12
-
13
- kubectl --context devzone -n "$ns" apply -f - <<EOF
14
- apiVersion: networking.k8s.io/v1
15
- kind: NetworkPolicy
16
- metadata:
17
- name: devzone-meme-namespace
18
- labels:
19
- app.kubernetes.io/managed-by: devzone
20
- spec:
21
- podSelector: {}
22
- ingress:
23
- - from:
24
- - podSelector: {}
25
- EOF
26
- }
27
-
28
- # Adds a namespaceSelector rule to the NetworkPolicy of the target namespace,
29
- # allowing access from outside this namespace.
30
- # Without this rule, an application in another namespace cannot reach the
31
- # devzone services (postgres, etc.).
32
- allow() {
33
- local target_ns="${1:-}" patch output
34
- [ -n "$target_ns" ] || { echo "devzone : target namespace missing" >&2; return 2; }
35
-
36
- kubectl --context devzone -n "$target_ns" get networkpolicy devzone-meme-namespace \
37
- -o yaml 2>/dev/null | kubectl --context devzone -n "$target_ns" apply -f - >/dev/null 2>&1 ||
38
- { echo "devzone : cannot apply the isolating NetworkPolicy in $target_ns" >&2; return 1; }
39
-
40
- patch="$(jq -nc '{"spec":{"ingress":[{"from":[{"podSelector":{},
41
- "namespaceSelector":{"matchLabels":{"app.kubernetes.io/managed-by":"devzone"}}}]}]}}')"
42
- if ! output="$(kubectl --context devzone -n "$target_ns" patch networkpolicy devzone-meme-namespace \
43
- -p "$patch" 2>&1)"; then
44
- echo "devzone : cannot allow access to $target_ns : $output" >&2
45
- return 1
46
- fi
47
-
48
- echo "devzone : rule allowed to $target_ns"
49
- }
50
-
51
- # Removes the namespaceSelector rule from the NetworkPolicy,
52
- # blocking access again from the given namespace.
53
- forbid() {
54
- local target_ns="${1:-}" patch output
55
- [ -n "$target_ns" ] || { echo "devzone : target namespace missing" >&2; return 2; }
56
-
57
- patch="$(jq -nc '{"spec":{"ingress":[{"from":[{"podSelector":{}}]}]}}')"
58
- if ! output="$(kubectl --context devzone -n "$target_ns" patch networkpolicy devzone-meme-namespace \
59
- -p "$patch" 2>&1)"; then
60
- echo "devzone : cannot forbid access to $target_ns : $output" >&2
61
- return 1
62
- fi
63
-
64
- echo "devzone : rule forbidden to $target_ns"
65
- }
@@ -1,107 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Limited Kubernetes account (#430): the devzone context (projects, relays,
3
- # agents) can only act in the devzone-* namespaces. The account is the
4
- # ServiceAccount devzone-agent of the devzone-system namespace, bound to the
5
- # edit ClusterRole (no RBAC, no quotas) in each devzone namespace by a
6
- # RoleBinding. The installers alone use the admin context (host context in
7
- # ~/.kube/hote.config, kind cluster in ~/.kube/config); the shells use
8
- # ~/.kube/config and the agents the limited ~/.kube/agent.config (devzone
9
- # context only, #844).
10
- # Library sourced by cluster.sh.
11
-
12
- ACCOUNT_NS=devzone-system
13
- ACCOUNT_SA=devzone-agent
14
- ACCOUNT_CONFIG="${DEVZONE_KUBE_CONFIG:-$HOME/.kube/config}"
15
-
16
- # kubeconfig file that holds the admin context ctx: hote.config if it lists it
17
- # (host clusters), otherwise ~/.kube/config (kind clusters of the container).
18
- account_admin_config() {
19
- local ctx="$1" host="${DEVZONE_KUBE_HOST:-$HOME/.kube/hote.config}"
20
- if [ -n "$ctx" ] && [ -f "$host" ]; then
21
- if KUBECONFIG="$host" kubectl config get-contexts -o name 2>/dev/null | grep -qx -- "$ctx"; then
22
- echo "$host"
23
- return 0
24
- fi
25
- fi
26
- echo "$ACCOUNT_CONFIG"
27
- }
28
-
29
- # kubectl on the admin context: explicit kubeconfig that holds this context,
30
- # never the combined KUBECONFIG (the admin never goes through the limited
31
- # account). Without a known context: the one kubectl uses.
32
- account_admin() {
33
- local ctx="$1" conf
34
- shift
35
- if [ -z "$ctx" ]; then
36
- ctx="$(kubectl config current-context 2>/dev/null || true)"
37
- fi
38
- conf="$(account_admin_config "$ctx")"
39
- if [ -n "$ctx" ]; then
40
- kubectl --kubeconfig="$conf" --context "$ctx" "$@"
41
- else
42
- kubectl --kubeconfig="$conf" "$@"
43
- fi
44
- }
45
-
46
- # helm on the admin context ctx: the charts create CRDs, ClusterRoles or
47
- # RoleBindings, beyond the limited account's rights (edit).
48
- # Usage: account_helm <ctx> <helm args…>
49
- account_helm() {
50
- local ctx="$1" conf
51
- shift
52
- if [ -z "$ctx" ]; then
53
- ctx="$(kubectl config current-context 2>/dev/null || true)"
54
- fi
55
- conf="$(account_admin_config "$ctx")"
56
- if [ -n "$ctx" ]; then
57
- helm --kubeconfig "$conf" --kube-context "$ctx" "$@"
58
- else
59
- helm --kubeconfig "$conf" "$@"
60
- fi
61
- }
62
-
63
- # Admin context of an installation: the chosen services cluster (cluster_saved
64
- # of cluster.sh), otherwise the current kubectl context.
65
- account_context() {
66
- local ctx
67
- ctx="$(cluster_saved 2>/dev/null || true)"
68
- if [ -n "$ctx" ]; then
69
- echo "$ctx"
70
- return 0
71
- fi
72
- kubectl config current-context 2>/dev/null || true
73
- }
74
-
75
- # Allows the account in namespace ns: devzone-agent RoleBinding to the edit
76
- # ClusterRole (read, write, secrets, NetworkPolicies — no RBAC, no quotas), set
77
- # with the admin context after the namespace is created.
78
- # The account is set up first if missing: devzone-system namespace and
79
- # devzone-agent ServiceAccount, so an installation on a still blank cluster
80
- # makes the account usable at the next token renewal.
81
- account_allow() {
82
- local ctx="$1" ns="$2"
83
- account_admin "$ctx" create namespace "$ACCOUNT_NS" --dry-run=client -o yaml |
84
- account_admin "$ctx" apply -f - >/dev/null || return 1
85
- account_admin "$ctx" -n "$ACCOUNT_NS" create serviceaccount "$ACCOUNT_SA" \
86
- --dry-run=client -o yaml | account_admin "$ctx" apply -f - >/dev/null || return 1
87
- account_admin "$ctx" -n "$ns" create rolebinding "$ACCOUNT_SA" \
88
- --clusterrole=edit --serviceaccount="$ACCOUNT_NS:$ACCOUNT_SA" \
89
- --dry-run=client -o yaml | account_admin "$ctx" apply -f - >/dev/null || return 1
90
- }
91
-
92
- # Removes the account's permission in namespace ns (uninstall): RoleBinding
93
- # deleted with the admin context.
94
- account_remove() {
95
- local ctx="$1" ns="$2"
96
- account_admin "$ctx" -n "$ns" delete rolebinding "$ACCOUNT_SA" \
97
- --ignore-not-found >/dev/null || return 1
98
- }
99
-
100
- # Creates and labels namespace ns as administrator, then allows the account.
101
- account_namespace() {
102
- local ctx="$1" ns="$2"
103
- account_admin "$ctx" create namespace "$ns" --dry-run=client -o yaml |
104
- account_admin "$ctx" apply -f - >/dev/null || return 1
105
- account_admin "$ctx" label namespace "$ns" "$CLUSTER_LABEL" --overwrite >/dev/null || return 1
106
- account_allow "$ctx" "$ns" || return 1
107
- }
@@ -1,42 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Kubernetes prompt of the shell (#337): ⎈ ctx/ns.
3
-
4
- # Renders the Kubernetes prompt (⎈ ctx/ns), with a cache keyed on the mtime.
5
- devzone_kube_prompt() {
6
- local kubeconfig="${KUBECONFIG:-$HOME/.kube/config:$HOME/.kube/hote.config}"
7
- local files=() f sig="" cache mtime ctx ns result ici
8
- ici="$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9
- IFS=':' read -ra files <<<"$kubeconfig"
10
- for f in "${files[@]}"; do
11
- [ -n "$f" ] || continue
12
- if [ -f "$f" ]; then
13
- mtime="$(stat -c '%Y' "$f" 2>/dev/null || echo 0)"
14
- else
15
- mtime="missing"
16
- fi
17
- sig+="${f}:${mtime};"
18
- done
19
- cache="${XDG_CACHE_HOME:-$HOME/.cache}/devzone-kube-prompt"
20
- mkdir -p "$(dirname "$cache")"
21
- if [ -f "$cache" ]; then
22
- if [ "$(sed -n '1p' "$cache" 2>/dev/null)" = "$sig" ]; then
23
- sed -n '2p' "$cache" 2>/dev/null
24
- return 0
25
- fi
26
- fi
27
- ctx="$(kubectl config current-context 2>/dev/null || true)"
28
- if [ -z "$ctx" ]; then
29
- result=""
30
- else
31
- ns="$(kubectl config view -o jsonpath="{.contexts[?(@.name==\"$ctx\")].context.namespace}" 2>/dev/null || true)"
32
- [ -n "$ns" ] || ns=default
33
- # shellcheck disable=SC1090,SC1091
34
- if ( . "$ici/cluster.sh"; cluster_is_local "$ctx" ) >/dev/null 2>&1; then
35
- result="$(printf '\001\033[36m\002⎈ %s/%s\001\033[0m\002' "$ctx" "$ns")"
36
- else
37
- result="$(printf '\001\033[1;31m\002⎈ %s/%s remote\001\033[0m\002' "$ctx" "$ns")"
38
- fi
39
- fi
40
- printf '%s\n%s\n' "$sig" "$result" >"$cache"
41
- printf '%s\n' "$result"
42
- }
@@ -1,113 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Memory check of the services cluster (#608, #612): a module refuses its
3
- # installation when the allocatable memory of the nodes is below its minimum.
4
- # Sourced by the modules (gitlab, llm) and by the tests; bash 3.2 (macOS).
5
- # quantity_ki <quantity> Kubernetes quantity in Ki
6
- # memory_cluster_ki allocatable memory of the cluster, in Ki
7
- # check_module_memory <module> <minimum_gi> [hint]
8
- # Messages in English. Return 0 when the memory is enough, 2 when it is not or
9
- # when the cluster is unreachable.
10
-
11
- # Globals written by memory_cluster_ki (#612) and read by
12
- # check_module_memory, which calls the function directly: a call written
13
- # memory="$(memory_cluster_ki)" would run it in a subshell and lose them.
14
- # Callers that only need the number keep using $(memory_cluster_ki): the
15
- # value is still printed on stdout (argocd, gitlab, llm are unaffected).
16
- # MEMORY_KI same value as on stdout, in Ki
17
- # MEMORY_RULE "maximum" when every node is a kind node (the nodes share
18
- # one VM), "sum" otherwise
19
- MEMORY_KI=""
20
- MEMORY_RULE=""
21
-
22
- # Converts a Kubernetes memory quantity (Ki, Mi, Gi, Ti, K, M, G, bytes)
23
- # into Ki, to total the allocatable memory of the nodes.
24
- quantity_ki() {
25
- local q="${1:-}" number
26
- case "$q" in
27
- *Ki) number="${q%Ki}"; echo $((number)) ;;
28
- *Mi) number="${q%Mi}"; echo $((number * 1024)) ;;
29
- *Gi) number="${q%Gi}"; echo $((number * 1024 * 1024)) ;;
30
- *Ti) number="${q%Ti}"; echo $((number * 1024 * 1024 * 1024)) ;;
31
- *K | *k) number="${q%[Kk]}"; echo $((number * 1000 / 1024)) ;;
32
- *M) number="${q%M}"; echo $((number * 1000 * 1000 / 1024)) ;;
33
- *G) number="${q%G}"; echo $((number * 1000 * 1000 * 1000 / 1024)) ;;
34
- *)
35
- case "$q" in
36
- '' | *[!0-9]*) echo 0 ;;
37
- *) echo $((q / 1024)) ;;
38
- esac
39
- ;;
40
- esac
41
- }
42
-
43
- # Allocatable memory of the services cluster nodes, in Ki. Reading the nodes
44
- # is reserved to the administrator (#430). One kubectl call gives, per node,
45
- # its providerID and its allocatable memory on one "<providerID>|<memory>"
46
- # line (#612). A kind cluster runs on a single Docker daemon: its nodes share
47
- # the memory of one VM and each of them declares the whole VM, so adding them
48
- # up lies. When every providerID starts with "kind://" the greatest node
49
- # memory is returned (MEMORY_RULE=maximum); as soon as one node has no such
50
- # prefix or an empty providerID the sum is returned (MEMORY_RULE=sum), as
51
- # before. Known limit, documented: k3d and minikube in Docker mode share a VM
52
- # too but are not kind, they stay on the sum. A line without the separator
53
- # carries the memory alone, so it is a node without providerID (sum).
54
- memory_cluster_ki() {
55
- local output line providerID memory node total=0 max=0 n=0 all_kind=1
56
- MEMORY_KI=""
57
- MEMORY_RULE=""
58
- output="$(account_admin "$(account_context)" get nodes \
59
- -o jsonpath='{range .items[*]}{.spec.providerID}{"|"}{.status.allocatable.memory}{"\n"}{end}' 2>/dev/null)" || return 1
60
- [ -n "$output" ] || return 1
61
- while IFS= read -r line; do
62
- [ -n "$line" ] || continue
63
- case "$line" in
64
- *'|'*)
65
- providerID="${line%%|*}"
66
- memory="${line#*|}"
67
- ;;
68
- *)
69
- providerID=""
70
- memory="$line"
71
- ;;
72
- esac
73
- node="$(quantity_ki "$memory")"
74
- total=$((total + node))
75
- if [ "$node" -gt "$max" ]; then max="$node"; fi
76
- case "$providerID" in
77
- kind://*) ;;
78
- *) all_kind=0 ;;
79
- esac
80
- n=$((n + 1))
81
- done <<<"$output"
82
- if [ "$n" -gt 0 ] && [ "$all_kind" -eq 1 ]; then
83
- MEMORY_RULE="maximum"
84
- MEMORY_KI="$max"
85
- else
86
- MEMORY_RULE="sum"
87
- MEMORY_KI="$total"
88
- fi
89
- echo "$MEMORY_KI"
90
- }
91
-
92
- # Refuses the installation of <module> under <minimum_gi> GiB of allocatable
93
- # memory. The message says the minimum, the available memory and how to enlarge
94
- # the cluster; [hint] adds an optional hint (for example --preset light).
95
- # On a kind cluster (#612) the available memory is the one of a single node
96
- # and the message says so. Return 0 when the memory is enough, 2 when it is
97
- # not or the cluster is unreachable.
98
- check_module_memory() {
99
- local module_name="$1" minimum_gi="$2" hint="${3:-}" memory precision=""
100
- if ! memory_cluster_ki >/dev/null; then
101
- echo "devzone : ${module_name} : services cluster unreachable: devzone cluster" >&2
102
- return 2
103
- fi
104
- memory="$MEMORY_KI"
105
- if [ "$MEMORY_RULE" = "maximum" ]; then
106
- precision=" (memory of a single node: kind cluster, shared VM)"
107
- fi
108
- if [ "$memory" -lt "$((minimum_gi * 1048576))" ]; then
109
- echo "devzone : ${module_name} : at least ${minimum_gi} GiB of allocatable memory is needed in the cluster (available: $((memory / 1048576)) GiB)${precision} ; enlarge the cluster (Docker Desktop → Settings → Resources) then retry: devzone enable ${module_name}${hint:+ ; hint: ${hint}}" >&2
110
- return 2
111
- fi
112
- return 0
113
- }
@@ -1,100 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Installs metrics-server (CPU/RAM in k9s, kubectl top) on the clusters that
3
- # have no Metrics API. Docker Desktop and kind do not have it by default.
4
- # Usage: metrics-server.sh [context...] | metrics-server.sh retirer [context...]
5
- # (default: reachable local clusters; retirer, the public subcommand
6
- # name kept as is: removal from the clusters where devzone installed it)
7
- # --kubelet-insecure-tls: the kubelet of Docker Desktop and kind has a
8
- # self-signed certificate, without this option the pod never becomes Ready.
9
- # Exits in error if a requested context is unknown or unreachable, or if an
10
- # installation fails.
11
- set -euo pipefail
12
-
13
- # shellcheck source=SCRIPTDIR/cluster.sh
14
- . "$(dirname "$0")/cluster.sh"
15
-
16
- VERSION=v0.9.0
17
- MANIFEST="https://github.com/kubernetes-sigs/metrics-server/releases/download/${VERSION}/components.yaml"
18
-
19
- mapfile -t known < <(kubectl config get-contexts -o name 2>/dev/null || true)
20
-
21
- if [ "${1:-}" = retirer ]; then
22
- shift
23
- # Removal (kubernetes module disabled): only the contexts where devzone
24
- # installed metrics-server (devzone=installe mark) are removed
25
- requested=$#
26
- if [ "$requested" -eq 0 ]; then
27
- # Local clusters only: removal on a remote cluster has nothing to remove
28
- # (devzone never installs there)
29
- mapfile -t contexts < <(printf '%s\n' ${known[@]+"${known[@]}"} |
30
- while IFS= read -r c; do cluster_is_local "$c" && echo "$c"; done || true)
31
- set -- ${contexts[@]+"${contexts[@]}"}
32
- fi
33
- code=0
34
- for context in "$@"; do
35
- # Mark read on the deployment by its name: kubectl refuses a name with
36
- # a selector (-l), the test would always be false (#255)
37
- mark="$(kubectl --context "$context" -n kube-system get deployment metrics-server \
38
- -o jsonpath='{.metadata.labels.devzone}' 2>/dev/null || true)"
39
- if [ "$mark" = installe ]; then
40
- if kubectl --context "$context" delete -f "$MANIFEST" --ignore-not-found >/dev/null 2>&1; then
41
- echo "metrics-server removed ($VERSION) : $context"
42
- else
43
- echo "metrics-server : removal failed: $context" >&2
44
- code=1
45
- fi
46
- elif kubectl --context "$context" -n kube-system get deployment metrics-server >/dev/null 2>&1; then
47
- # Installed by someone else: not up to devzone to remove it
48
- echo "metrics-server present without a devzone mark, left: $context" >&2
49
- fi
50
- done
51
- exit "$code"
52
- fi
53
-
54
- requested=$#
55
- if [ "$requested" -eq 0 ]; then
56
- # Local clusters only: never an automatic installation on a remote cluster
57
- # of the host (prod, staging…); for another one, name it as an argument
58
- mapfile -t contexts < <(printf '%s\n' ${known[@]+"${known[@]}"} |
59
- while IFS= read -r c; do cluster_is_local "$c" && echo "$c"; done || true)
60
- set -- ${contexts[@]+"${contexts[@]}"}
61
- fi
62
-
63
- code=0
64
- reachable=0
65
- for context in "$@"; do
66
- if [ "$requested" -gt 0 ] && ! printf '%s\n' ${known[@]+"${known[@]}"} | grep -qxF -- "$context"; then
67
- echo "metrics-server : unknown context: $context (see kubectl config get-contexts)" >&2
68
- code=1
69
- continue
70
- fi
71
- k=(kubectl --context "$context" --request-timeout=5s)
72
- if ! "${k[@]}" get --raw /readyz >/dev/null 2>&1; then
73
- # Without argument: kind may still be starting, ignored
74
- if [ "$requested" -gt 0 ]; then
75
- echo "metrics-server : cluster unreachable: $context" >&2
76
- code=1
77
- fi
78
- continue
79
- fi
80
- reachable=$((reachable + 1))
81
- if "${k[@]}" get apiservice v1beta1.metrics.k8s.io >/dev/null 2>&1; then
82
- echo "metrics-server : already present: $context"
83
- continue
84
- fi
85
- if "${k[@]}" apply -f "$MANIFEST" >/dev/null &&
86
- "${k[@]}" -n kube-system patch deployment metrics-server --type=json \
87
- -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]' >/dev/null &&
88
- # Mark: make desinstaller only removes what devzone installed
89
- "${k[@]}" -n kube-system label deployment metrics-server devzone=installe app.kubernetes.io/managed-by=devzone --overwrite >/dev/null; then
90
- echo "metrics-server : installed ($VERSION): $context (metrics within ~1 min)"
91
- else
92
- echo "metrics-server : installation failed: $context" >&2
93
- code=1
94
- fi
95
- done
96
-
97
- if [ "$requested" -eq 0 ] && [ "$reachable" -eq 0 ]; then
98
- echo "metrics-server : no reachable local cluster; after kind create cluster: bash .devcontainer/metrics-server.sh"
99
- fi
100
- exit "$code"
@@ -1,228 +0,0 @@
1
- #!/usr/bin/env bash
2
- # System packages kept after a rebuild (#351): a list in the devzone folder
3
- # (<root>/.devzone.packages, a comment header + one package name per line, sorted,
4
- # without duplicates), reinstalled at each container creation.
5
- # A package installed by hand is lost by a rebuild, unless it is in this list.
6
- # packages.sh or packages.sh list list with the state of each package
7
- # packages.sh add <name>… validates, installs, adds to the list
8
- # packages.sh remove <name>… removes from the list and uninstalls (except image packages)
9
- # packages.sh apply installs everything missing
10
- # The state of a package comes from dpkg-query -W -f='${Status}'; the system
11
- # commands go through overridable variables for the tests (sudo, apt-get,
12
- # apt-cache, dpkg-query, mark, timestamp). Compatible with bash 3.2 (macOS).
13
- set -euo pipefail
14
-
15
- HERE="$(cd "$(dirname "$0")" && pwd)"
16
- ROOT="${DEVZONE_ROOT:-$(dirname "$HERE")}"
17
- LIST="${ROOT}/.devzone.packages"
18
- MARK="${DEVZONE_PACKAGES_MARKER:-/tmp/devzone-packages.failed}"
19
- UPDATED="${DEVZONE_PACKAGES_UPDATE:-/tmp/devzone-packages.updated}"
20
- SUDO="${DEVZONE_PACKAGES_SUDO-sudo}"
21
- APT="${DEVZONE_PACKAGES_APT:-apt-get}"
22
- APTCACHE="${DEVZONE_PACKAGES_APTCACHE:-apt-cache}"
23
- DPKGQUERY="${DEVZONE_PACKAGES_DPKG_QUERY:-dpkg-query}"
24
- FORMAT="-f=\${Status}"
25
- # Stored values, kept byte for byte (the users' list file): the header and the
26
- # « already present » mark are written in UTF-8 as byte escapes (contract).
27
- HEADER=$'# paquets ajout\303\251s par devzone packages add ; r\303\251install\303\251s \303\240 chaque cr\303\251ation du conteneur'
28
- PRESENT_MARK=$'# d\303\251j\303\240 pr\303\251sent'
29
-
30
-
31
- refuse() { echo "devzone : packages : $*" >&2; exit 1; }
32
-
33
- as_root() {
34
- if [ -n "$SUDO" ]; then
35
- "$SUDO" "$@"
36
- else
37
- env "$@"
38
- fi
39
- }
40
-
41
- is_installed() {
42
- "$DPKGQUERY" -W "$FORMAT" "$1" 2>/dev/null | grep -q 'install ok installed'
43
- }
44
-
45
- # List lines as they are (name, or name followed by the present mark).
46
- list_lines() {
47
- [ -f "$LIST" ] || return 0
48
- grep -vE '^[[:space:]]*(#|$)' "$LIST" 2>/dev/null |
49
- sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e '/^$/d' |
50
- sort -k1,1 -u || true
51
- }
52
-
53
- # Names only: the end-of-line mark is ignored.
54
- list_names() {
55
- list_lines | sed -e 's/[[:space:]]*#.*$//' | sort -u || true
56
- }
57
-
58
- name_listed() {
59
- local name
60
- while IFS= read -r name; do
61
- [ "$name" = "$1" ] && return 0
62
- done < <(list_names)
63
- return 1
64
- }
65
-
66
- # True if the package was marked as already present in the image.
67
- mark_present() {
68
- grep -qxF -e "$1 $PRESENT_MARK" "$LIST" 2>/dev/null
69
- }
70
-
71
- # Rewrites the list: a single header, then the given lines (sorted by name).
72
- write_list() {
73
- local tmp
74
- tmp="$(mktemp)"
75
- {
76
- echo "$HEADER"
77
- [ "$#" -eq 0 ] || printf '%s\n' "$@" | sort -k1,1 -u
78
- } >"$tmp"
79
- mv "$tmp" "$LIST"
80
- }
81
-
82
- update_index() {
83
- local last
84
- last="$(cat "$UPDATED" 2>/dev/null || true)"
85
- if [ -n "$last" ] && [ $(( $(date +%s) - last )) -lt 3600 ]; then
86
- return 0
87
- fi
88
- as_root "$APT" update || return 1
89
- date +%s >"$UPDATED"
90
- }
91
-
92
- list_packages() {
93
- local packages package
94
- packages="$(list_names)"
95
- if [ -z "$packages" ]; then
96
- echo "no package (devzone packages add <name>)"
97
- return 0
98
- fi
99
- while IFS= read -r package; do
100
- [ -n "$package" ] || continue
101
- if is_installed "$package"; then
102
- echo " ✔ $package"
103
- else
104
- echo " ○ $package"
105
- fi
106
- done < <(printf '%s\n' "$packages")
107
- }
108
-
109
- add_packages() {
110
- local -a a=()
111
- local -a entries=()
112
- local arg name line already=""
113
- for arg in "$@"; do
114
- case "$arg" in
115
- -*) refuse "unknown argument: $arg (devzone packages add <name>…)" ;;
116
- *) a+=("$arg") ;;
117
- esac
118
- done
119
- [ "${#a[@]}" -gt 0 ] || refuse "add expects at least one name: devzone packages add <name>…"
120
- update_index || echo "apt index not updated" >&2
121
- for name in "${a[@]}"; do
122
- if ! "$APTCACHE" show "$name" >/dev/null 2>&1; then
123
- refuse "unknown package: $name (list unchanged)"
124
- fi
125
- done
126
- # What is already installed before the installation comes from the image: marked
127
- for name in "${a[@]}"; do
128
- if is_installed "$name"; then
129
- already="$already $name"
130
- fi
131
- done
132
- if as_root DEBIAN_FRONTEND=noninteractive "$APT" install -y --no-install-recommends "${a[@]}"; then
133
- while IFS= read -r line; do
134
- entries+=("$line")
135
- done < <(list_lines)
136
- for name in "${a[@]}"; do
137
- if name_listed "$name"; then
138
- continue
139
- fi
140
- case " $already " in
141
- *" $name "*)
142
- entries+=("$name $PRESENT_MARK")
143
- echo "$name : already present before devzone packages (marked ; removing will leave it installed)"
144
- ;;
145
- *) entries+=("$name") ;;
146
- esac
147
- done
148
- write_list "${entries[@]}"
149
- else
150
- refuse "installation failed: ${a[*]} (list unchanged)"
151
- fi
152
- }
153
-
154
- remove_packages() {
155
- local -a a=()
156
- local -a removals=()
157
- local -a remaining=()
158
- local arg name line name_line
159
- for arg in "$@"; do
160
- case "$arg" in
161
- -*) refuse "unknown argument: $arg (devzone packages remove <name>…)" ;;
162
- *) a+=("$arg") ;;
163
- esac
164
- done
165
- [ "${#a[@]}" -gt 0 ] || refuse "remove expects at least one name: devzone packages remove <name>…"
166
- for name in "${a[@]}"; do
167
- name_listed "$name" ||
168
- refuse "package not listed: $name (provided by the image?) : devzone packages remove does not add image packages"
169
- if ! mark_present "$name"; then
170
- removals+=("$name")
171
- fi
172
- done
173
- if [ "${#removals[@]}" -gt 0 ] && ! as_root "$APT" remove -y "${removals[@]}"; then
174
- refuse "removal failed: ${removals[*]} (list unchanged)"
175
- fi
176
- for name in "${a[@]}"; do
177
- if mark_present "$name"; then
178
- echo "$name : removed from the list, left installed (present before devzone packages)"
179
- fi
180
- done
181
- while IFS= read -r line; do
182
- name_line="${line%% *}"
183
- case " ${a[*]} " in
184
- *" $name_line "*) ;;
185
- *) remaining+=("$line") ;;
186
- esac
187
- done < <(list_lines)
188
- write_list "${remaining[@]+"${remaining[@]}"}"
189
- }
190
-
191
- apply_packages() {
192
- local package
193
- local -a missing=()
194
- while IFS= read -r package; do
195
- [ -n "$package" ] || continue
196
- if ! is_installed "$package"; then
197
- missing+=("$package")
198
- fi
199
- done < <(printf '%s\n' "$(list_names)")
200
- if [ "${#missing[@]}" -eq 0 ]; then
201
- rm -f "$MARK"
202
- return 0
203
- fi
204
- update_index || echo "apt index not updated" >&2
205
- for package in "${missing[@]}"; do
206
- as_root DEBIAN_FRONTEND=noninteractive "$APT" install -y --no-install-recommends "$package" || true
207
- done
208
- local -a still_missing=()
209
- for package in "${missing[@]}"; do
210
- if ! is_installed "$package"; then
211
- still_missing+=("$package")
212
- fi
213
- done
214
- if [ "${#still_missing[@]}" -gt 0 ]; then
215
- printf '%s\n' "${still_missing[@]}" >"$MARK"
216
- return 1
217
- fi
218
- rm -f "$MARK"
219
- }
220
-
221
- case "${1:-}" in
222
- '' | list) list_packages ;;
223
- add) add_packages "${@:2}" ;;
224
- remove) remove_packages "${@:2}" ;;
225
- apply) apply_packages ;;
226
- -h | --help) awk 'NR==1{next} /^#$/{exit} /^#/{sub(/^# ?/,""); print; next} {exit}' "${BASH_SOURCE[0]}" ;;
227
- *) refuse "unknown command: ${1:-(none)} (devzone packages --help)" ;;
228
- esac