#!/bin/bash # Methods library for cap — sourced by install-cap.sh and generated run-*.sh scripts. # SCRIPT_DIR must be set before sourcing. _kubectl() { if command -v kubectl >/dev/null 2>&1; then command kubectl "$@" elif command -v k0s >/dev/null 2>&1; then k0s kubectl "$@" else echo "ERROR: no kubectl or k0s binary found" >&2; exit 127 fi } _require_env() { local var="$1" if [ -z "${!var:-}" ]; then echo "ERROR: required variable $var is not set" >&2; exit 1 fi } _pod_name() { _kubectl get pod \ --namespace "$CAP_NAMESPACE" \ --selector "app.kubernetes.io/name=${CAP_NAME}" \ -o jsonpath='{.items[0].metadata.name}' 2>/dev/null } _wait_for_pod() { local timeout="${1:-300}" _kubectl wait pod \ --namespace "$CAP_NAMESPACE" \ --selector "app.kubernetes.io/name=${CAP_NAME}" \ --for=condition=Ready \ --timeout="${timeout}s" } # ── Manifest plan helpers ───────────────────────────────────────────────────── _plan_apply() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl apply -f "$path" } _plan_apply_skip() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } if _kubectl get -f "$path" >/dev/null 2>&1; then echo " [skip] ${file} already exists in cluster" return 0 fi _kubectl apply -f "$path" } _plan_rollout_restart() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl rollout restart -f "$path" } _plan_delete() { local file="$1" local path="$SCRIPT_DIR/manifests/${file}.yaml" [ -f "$path" ] || { echo " [skip] ${file}.yaml not in manifests/"; return 0; } _kubectl delete -f "$path" --ignore-not-found } # shellcheck source=lib/gateway-tls.sh source "${SCRIPT_DIR}/lib/gateway-tls.sh" _method_patch-gateway-https() { _lib_gateway_patch_https \ "${TLS_HOOK_GATEWAY_NAME:-libre-wuji}" \ "${TLS_HOOK_GATEWAY_NS:-kube-system}" \ "${TLS_HOOK_LISTENER_NAME}" \ "${TLS_HOOK_DOMAIN}" \ "${TLS_HOOK_TLS_SECRET}" \ "${TLS_HOOK_NAMESPACE}" } _method_remove-gateway-https() { _lib_gateway_remove_https \ "${TLS_HOOK_GATEWAY_NAME:-libre-wuji}" \ "${TLS_HOOK_GATEWAY_NS:-kube-system}" \ "${TLS_HOOK_LISTENER_NAME}" } # ── Component methods ───────────────────────────────────────────────────────── _method_create-credentials() { [ -f "${SCRIPT_DIR}/_credentials.env" ] && set -a && source "${SCRIPT_DIR}/_credentials.env" && set +a _require_env ADMIN_KEY _kubectl create secret generic "${CAP_NAME}-credentials" \ --namespace "$CAP_NAMESPACE" \ --from-literal=ADMIN_KEY="$ADMIN_KEY" \ --dry-run=client -o yaml | _kubectl apply -f - echo " [ok] credentials secret '${CAP_NAME}-credentials' created in ${CAP_NAMESPACE}" } _method_wait-valkey() { local valkey_name="${CAP_NAME}-valkey" echo " waiting for valkey pod ${valkey_name} to be ready..." _kubectl wait pod \ --namespace "$CAP_NAMESPACE" \ --selector "app.kubernetes.io/name=${valkey_name}" \ --for=condition=Ready \ --timeout=120s echo " [ok] valkey ready" } _method_wait-ready() { _wait_for_pod 300 } _method_protect-volume() { local pvc="${PLAN_PARAM_PVC:-${CAP_NAME}-valkey-data}" echo " waiting for PVC '${pvc}' to bind..." _kubectl wait pvc "$pvc" \ --namespace "$CAP_NAMESPACE" \ --for=jsonpath='{.status.phase}'=Bound \ --timeout=120s local pv_name pv_name=$(_kubectl get pvc "$pvc" \ --namespace "$CAP_NAMESPACE" \ -o jsonpath='{.spec.volumeName}') if [ -z "$pv_name" ]; then echo " [warn] could not resolve PV name for PVC '${pvc}' — skipping volume protection" return 0 fi if command -v hcloud >/dev/null 2>&1; then hcloud volume enable-protection "$pv_name" delete 2>/dev/null \ && echo " [ok] volume '${pv_name}' protected" \ || echo " [warn] hcloud protect failed — run manually: hcloud volume enable-protection ${pv_name} delete" else echo " [warn] hcloud CLI not found — skip volume protection for '${pv_name}'" fi }