260 lines
7.9 KiB
Bash
260 lines
7.9 KiB
Bash
#!/bin/bash
|
|
# Methods library for lemmy — sourced by install-lemmy.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
|
|
}
|
|
|
|
_wait_for_pod() {
|
|
local timeout="${1:-300}"
|
|
# Wait for lemmy-proxy (nginx) — the external-facing deployment
|
|
_kubectl wait pod \
|
|
--namespace "$LEMMY_NAMESPACE" \
|
|
--selector "app.kubernetes.io/name=lemmy-proxy" \
|
|
--for=condition=Ready \
|
|
--timeout="${timeout}s"
|
|
}
|
|
|
|
_pg_namespace() { echo "core-data"; }
|
|
|
|
_pg_pod() {
|
|
_kubectl get pod --namespace "$(_pg_namespace)" \
|
|
--selector "app.kubernetes.io/name=postgresql" \
|
|
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null
|
|
}
|
|
|
|
# ── 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 LEMMY_DB_PASSWORD
|
|
_require_env LEMMY_ADMIN_PASSWORD
|
|
_require_env PICTRS_API_KEY
|
|
|
|
_kubectl create secret generic "${LEMMY_NAME}-credentials" \
|
|
--namespace "$LEMMY_NAMESPACE" \
|
|
--from-literal=LEMMY_DB_PASSWORD="$LEMMY_DB_PASSWORD" \
|
|
--from-literal=LEMMY_ADMIN_PASSWORD="$LEMMY_ADMIN_PASSWORD" \
|
|
--from-literal=PICTRS_API_KEY="$PICTRS_API_KEY" \
|
|
--dry-run=client -o yaml | _kubectl apply -f -
|
|
echo " [ok] credentials secret '${LEMMY_NAME}-credentials' created in ${LEMMY_NAMESPACE}"
|
|
}
|
|
|
|
_method_init-db() {
|
|
[ -f "${SCRIPT_DIR}/_credentials.env" ] && set -a && source "${SCRIPT_DIR}/_credentials.env" && set +a
|
|
_require_env LEMMY_DB_PASSWORD
|
|
|
|
local pg_ns pg_pod db_user safe_pw safe_usr safe_db
|
|
pg_ns=$(_pg_namespace)
|
|
pg_pod=$(_pg_pod)
|
|
[ -z "$pg_pod" ] && { echo " [error] no postgresql pod found in namespace $pg_ns" >&2; exit 1; }
|
|
|
|
db_user="${LEMMY_DB_USER:-lemmy}"
|
|
safe_pw="${LEMMY_DB_PASSWORD//\'/\'\'}"
|
|
safe_usr="${db_user//\'/\'\'}"
|
|
safe_db="${LEMMY_DB_NAME:-lemmy}"
|
|
safe_db="${safe_db//\'/\'\'}"
|
|
|
|
_kubectl exec -i --namespace "$pg_ns" "$pg_pod" -- psql -U postgres <<SQL
|
|
DO \$\$
|
|
DECLARE
|
|
pw text := '${safe_pw}';
|
|
usr text := '${safe_usr}';
|
|
BEGIN
|
|
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = usr) THEN
|
|
EXECUTE format('CREATE ROLE %I WITH LOGIN PASSWORD %L', usr, pw);
|
|
RAISE NOTICE 'role % created', usr;
|
|
ELSE
|
|
EXECUTE format('ALTER ROLE %I WITH PASSWORD %L', usr, pw);
|
|
RAISE NOTICE 'role % password updated', usr;
|
|
END IF;
|
|
END;
|
|
\$\$;
|
|
SQL
|
|
|
|
local db_exists
|
|
db_exists=$(_kubectl exec --namespace "$pg_ns" "$pg_pod" -- \
|
|
psql -U postgres -tAc \
|
|
"SELECT 1 FROM pg_database WHERE datname = '${safe_db}'" 2>/dev/null || true)
|
|
|
|
if [ "${db_exists}" != "1" ]; then
|
|
_kubectl exec --namespace "$pg_ns" "$pg_pod" -- \
|
|
psql -U postgres -c \
|
|
"CREATE DATABASE \"${safe_db}\"
|
|
WITH OWNER \"${safe_usr}\"
|
|
ENCODING 'UTF8'
|
|
LC_COLLATE 'C'
|
|
LC_CTYPE 'C'
|
|
TEMPLATE template0"
|
|
echo " [ok] database '${safe_db}' created"
|
|
else
|
|
echo " [skip] database '${safe_db}' already exists"
|
|
fi
|
|
|
|
_kubectl exec --namespace "$pg_ns" "$pg_pod" -- \
|
|
psql -U postgres -d "${safe_db}" -c \
|
|
"GRANT ALL ON SCHEMA public TO \"${safe_usr}\""
|
|
echo " [ok] postgresql role '${safe_usr}' and database '${safe_db}' ensured"
|
|
}
|
|
|
|
_method_create-configmaps() {
|
|
[ -f "${SCRIPT_DIR}/_credentials.env" ] && set -a && source "${SCRIPT_DIR}/_credentials.env" && set +a
|
|
_require_env LEMMY_DB_PASSWORD
|
|
_require_env PICTRS_API_KEY
|
|
|
|
local db_host="${LEMMY_DB_HOST:-}"
|
|
local db_port="${LEMMY_DB_PORT:-5432}"
|
|
local db_user="${LEMMY_DB_USER:-lemmy}"
|
|
local db_name="${LEMMY_DB_NAME:-lemmy}"
|
|
local ns="${LEMMY_NAMESPACE:-lemmy}"
|
|
local domain="${LEMMY_DOMAIN:-}"
|
|
|
|
local hjson
|
|
hjson=$(cat <<HJSON
|
|
{
|
|
database: {
|
|
uri: "postgresql://${db_user}:${LEMMY_DB_PASSWORD}@${db_host}:${db_port}/${db_name}"
|
|
}
|
|
hostname: "${domain}"
|
|
bind: "0.0.0.0"
|
|
port: 8536
|
|
tls_enabled: true
|
|
pictrs: {
|
|
url: "http://lemmy-pictrs.${ns}.svc.libre-wuji.local:8080/"
|
|
api_key: "${PICTRS_API_KEY}"
|
|
}
|
|
email: {
|
|
smtp_server: "mail.librecloud.online:587"
|
|
smtp_from_address: "lemmy@librecloud.online"
|
|
tls_type: "starttls"
|
|
}
|
|
}
|
|
HJSON
|
|
)
|
|
|
|
_kubectl create configmap "${LEMMY_NAME}-config" \
|
|
--namespace "$ns" \
|
|
--from-literal="config.hjson=${hjson}" \
|
|
--dry-run=client -o yaml | _kubectl apply -f -
|
|
echo " [ok] lemmy-config ConfigMap created with credentials in ${ns}"
|
|
}
|
|
|
|
_method_wait-pictrs() {
|
|
echo " waiting for pictrs pod to be ready..."
|
|
_kubectl wait pod \
|
|
--namespace "$LEMMY_NAMESPACE" \
|
|
--selector "app.kubernetes.io/name=lemmy-pictrs" \
|
|
--for=condition=Ready \
|
|
--timeout=120s
|
|
echo " [ok] pictrs ready"
|
|
}
|
|
|
|
_method_wait-lemmy() {
|
|
echo " waiting for lemmy backend pod to be ready..."
|
|
_kubectl wait pod \
|
|
--namespace "$LEMMY_NAMESPACE" \
|
|
--selector "app.kubernetes.io/name=lemmy-backend" \
|
|
--for=condition=Ready \
|
|
--timeout=180s
|
|
echo " [ok] lemmy backend ready"
|
|
}
|
|
|
|
_method_wait-ready() {
|
|
_wait_for_pod 300
|
|
}
|
|
|
|
_method_protect-volume() {
|
|
local pvc="${PLAN_PARAM_PVC:-lemmy-pictrs-data}"
|
|
|
|
echo " waiting for PVC '${pvc}' to bind..."
|
|
_kubectl wait pvc "$pvc" \
|
|
--namespace "$LEMMY_NAMESPACE" \
|
|
--for=jsonpath='{.status.phase}'=Bound \
|
|
--timeout=120s
|
|
|
|
local pv_name
|
|
pv_name=$(_kubectl get pvc "$pvc" \
|
|
--namespace "$LEMMY_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
|
|
}
|
|
|