115 lines
3.6 KiB
Bash
115 lines
3.6 KiB
Bash
|
|
#!/bin/bash
|
||
|
|
# Tier-1 dispatch for forgejo component operations.
|
||
|
|
# Real flow goes through generated run-<op>.sh scripts that source
|
||
|
|
# forgejo-lib.sh and execute methods per manifest_plan entry.
|
||
|
|
# This dispatcher is the CLI fallback when run-*.sh is absent.
|
||
|
|
set -euo pipefail
|
||
|
|
|
||
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||
|
|
cd "$SCRIPT_DIR"
|
||
|
|
|
||
|
|
set -a
|
||
|
|
[ -f "${SCRIPT_DIR}/env-forgejo" ] && source "${SCRIPT_DIR}/env-forgejo"
|
||
|
|
set +a
|
||
|
|
|
||
|
|
# Library exports _kubectl, _method_*, _plan_* helpers.
|
||
|
|
# shellcheck source=/dev/null
|
||
|
|
source "${SCRIPT_DIR}/forgejo-lib.sh"
|
||
|
|
|
||
|
|
CMD_TSK="${CMD_TSK:-${1:-install}}"
|
||
|
|
|
||
|
|
_resolve_kubeconfig() {
|
||
|
|
if [ -n "${KUBECONFIG:-}" ] && [ -f "$KUBECONFIG" ]; then
|
||
|
|
return
|
||
|
|
fi
|
||
|
|
for candidate in /var/lib/k0s/pki/admin.conf /etc/kubernetes/admin.conf /root/.kube/config; do
|
||
|
|
if [ -f "$candidate" ]; then
|
||
|
|
export KUBECONFIG="$candidate"
|
||
|
|
return
|
||
|
|
fi
|
||
|
|
done
|
||
|
|
echo "ERROR: kubeconfig not found — set KUBECONFIG explicitly" >&2
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
_do_install() {
|
||
|
|
_plan_apply_skip "namespace"
|
||
|
|
_method_create-credentials
|
||
|
|
_method_create-cf-secret
|
||
|
|
_plan_apply_skip "clusterissuer"
|
||
|
|
_plan_apply_skip "certificate"
|
||
|
|
_plan_apply "referencegrant"
|
||
|
|
_method_patch-gateway-https
|
||
|
|
_plan_apply "configmap-app-ini"
|
||
|
|
_plan_apply_skip "pvc"
|
||
|
|
_plan_apply "service"
|
||
|
|
_plan_apply "service-ssh"
|
||
|
|
_plan_apply "statefulset"
|
||
|
|
_plan_apply "httproute"
|
||
|
|
_plan_apply "httproute-redirect"
|
||
|
|
_method_wait-ready
|
||
|
|
_method_protect-volume
|
||
|
|
echo "${FORGEJO_NAME} installed in namespace ${FORGEJO_NAMESPACE}"
|
||
|
|
}
|
||
|
|
|
||
|
|
_do_update() {
|
||
|
|
_plan_apply "referencegrant"
|
||
|
|
_method_patch-gateway-https
|
||
|
|
_plan_apply "configmap-app-ini"
|
||
|
|
_plan_apply "service"
|
||
|
|
_plan_apply "service-ssh"
|
||
|
|
_plan_apply "httproute"
|
||
|
|
_plan_apply "httproute-redirect"
|
||
|
|
_plan_apply "statefulset"
|
||
|
|
_plan_rollout_restart "statefulset"
|
||
|
|
_method_wait-ready
|
||
|
|
echo "${FORGEJO_NAME} updated"
|
||
|
|
}
|
||
|
|
|
||
|
|
_do_delete() {
|
||
|
|
_method_remove-gateway-https
|
||
|
|
_plan_delete "httproute"
|
||
|
|
_plan_delete "httproute-redirect"
|
||
|
|
_plan_delete "referencegrant"
|
||
|
|
_plan_delete "statefulset"
|
||
|
|
_plan_delete "service"
|
||
|
|
_plan_delete "service-ssh"
|
||
|
|
_plan_delete "configmap-app-ini"
|
||
|
|
_kubectl delete secret "${FORGEJO_NAME}-credentials" \
|
||
|
|
--namespace "$FORGEJO_NAMESPACE" --ignore-not-found
|
||
|
|
echo "${FORGEJO_NAME} deleted from namespace ${FORGEJO_NAMESPACE} (PVC, ClusterIssuer, Certificate kept)"
|
||
|
|
}
|
||
|
|
|
||
|
|
_do_restart() {
|
||
|
|
_plan_rollout_restart "statefulset"
|
||
|
|
_method_wait-ready
|
||
|
|
echo "${FORGEJO_NAME} restarted"
|
||
|
|
}
|
||
|
|
|
||
|
|
_do_health() {
|
||
|
|
local pod
|
||
|
|
pod=$(_pod_name)
|
||
|
|
if [ -z "$pod" ]; then
|
||
|
|
echo "UNHEALTHY: no pod found for ${FORGEJO_NAME} in ${FORGEJO_NAMESPACE}" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
if _kubectl exec --namespace "$FORGEJO_NAMESPACE" "$pod" -- \
|
||
|
|
sh -c "wget -qO- http://127.0.0.1:${FORGEJO_HTTP_PORT:-3000}/api/healthz" >/dev/null 2>&1; then
|
||
|
|
echo "HEALTHY: ${FORGEJO_NAME} responding at /api/healthz"
|
||
|
|
else
|
||
|
|
echo "UNHEALTHY: ${FORGEJO_NAME} did not respond to /api/healthz" >&2
|
||
|
|
exit 1
|
||
|
|
fi
|
||
|
|
}
|
||
|
|
|
||
|
|
_resolve_kubeconfig
|
||
|
|
|
||
|
|
case "$CMD_TSK" in
|
||
|
|
install) [ -f "$SCRIPT_DIR/run-init.sh" ] && exec bash "$SCRIPT_DIR/run-init.sh"; _do_install ;;
|
||
|
|
update) [ -f "$SCRIPT_DIR/run-update.sh" ] && exec bash "$SCRIPT_DIR/run-update.sh"; _do_update ;;
|
||
|
|
delete) [ -f "$SCRIPT_DIR/run-delete.sh" ] && exec bash "$SCRIPT_DIR/run-delete.sh"; _do_delete ;;
|
||
|
|
restart) [ -f "$SCRIPT_DIR/run-restart.sh" ] && exec bash "$SCRIPT_DIR/run-restart.sh"; _do_restart ;;
|
||
|
|
health) _do_health ;;
|
||
|
|
*) echo "ERROR: unknown CMD_TSK='${CMD_TSK}'. Valid: install|update|delete|restart|health" >&2; exit 1 ;;
|
||
|
|
esac
|