mirror of
https://github.com/dredx/prole.git
synced 2026-09-23 11:03:59 +00:00
221 lines
7.0 KiB
Bash
221 lines
7.0 KiB
Bash
#!/usr/bin/env bash
|
|
# Shared helpers for common core init scripts (ArgoCD, OpenBao, OpenTofu, Garage).
|
|
# Responsibilities:
|
|
# - handle [-c|--config] early so knoe_cfg.sh loads the right config file
|
|
# - parse standard actions/options
|
|
# - resolve and apply namespaces consistently
|
|
|
|
# Actions supported by all common core scripts
|
|
COMMON_CORE_ACTIONS="start|stop|status|restart|initialize|update|reload"
|
|
|
|
common_core_default_config_path() {
|
|
local script_dir="${1:-}"
|
|
local mode_hint="${KNOE_MODE:-${DEPLOYMENT_MODE:-${CLUSTER_ENV:-}}}"
|
|
local mode="${mode_hint,,}"
|
|
|
|
if declare -F knoe_normalize_mode >/dev/null 2>&1; then
|
|
mode="$(knoe_normalize_mode "$mode_hint")"
|
|
fi
|
|
|
|
case "$mode" in
|
|
k3d)
|
|
[[ -f "$script_dir/../conf/k3d.cfg" ]] && { printf '%s' "$script_dir/../conf/k3d.cfg"; return 0; }
|
|
;;
|
|
k3s)
|
|
[[ -f "$script_dir/../conf/k3s.cfg" ]] && { printf '%s' "$script_dir/../conf/k3s.cfg"; return 0; }
|
|
;;
|
|
k8s)
|
|
[[ -f "$script_dir/../conf/gke.cfg" ]] && { printf '%s' "$script_dir/../conf/gke.cfg"; return 0; }
|
|
;;
|
|
esac
|
|
|
|
local cfg
|
|
for cfg in k3d.cfg k3s.cfg gke.cfg knoe.cfg; do
|
|
if [[ -f "$script_dir/../conf/$cfg" ]]; then
|
|
printf '%s' "$script_dir/../conf/$cfg"
|
|
return 0
|
|
fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Parse -c/--config before loading knoe_cfg.sh so KNOE_CONF is set in time.
|
|
# Sets:
|
|
# COMMON_CORE_CONFIG_PATH - path to knoe.cfg (if provided)
|
|
# COMMON_CORE_ARGS - original arguments minus -c/--config
|
|
common_core_preparse_config() {
|
|
COMMON_CORE_CONFIG_PATH=""
|
|
COMMON_CORE_ARGS=()
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-c|--config)
|
|
shift
|
|
if [[ -z "${1:-}" ]]; then
|
|
echo "ERROR: -c/--config requires a file path" >&2
|
|
exit 2
|
|
fi
|
|
COMMON_CORE_CONFIG_PATH="$1"
|
|
;;
|
|
-c=*|--config=*)
|
|
COMMON_CORE_CONFIG_PATH="${1#*=}"
|
|
;;
|
|
*)
|
|
COMMON_CORE_ARGS+=("$1")
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [[ -n "$COMMON_CORE_CONFIG_PATH" ]]; then
|
|
if [[ ! -f "$COMMON_CORE_CONFIG_PATH" ]]; then
|
|
echo "ERROR: config file not found: $COMMON_CORE_CONFIG_PATH" >&2
|
|
exit 2
|
|
fi
|
|
local cfg_dir
|
|
cfg_dir=$(cd "$(dirname "$COMMON_CORE_CONFIG_PATH")" && pwd)
|
|
# Preserve an already-set KNOE_CONF when it is a more-specific env subdir of
|
|
# cfg_dir (e.g. conf/service/ vs the injected-default conf/). This allows the
|
|
# parent shell / Python milestone to pass the correct env-specific conf dir and
|
|
# have it survive even when init scripts inject a generic fallback -c path.
|
|
local _current_conf="${KNOE_CONF:-}"
|
|
if [[ -n "$_current_conf" && "$_current_conf" == "${cfg_dir}/"* && ( -f "$_current_conf/k3d.cfg" || -f "$_current_conf/k3s.cfg" || -f "$_current_conf/gke.cfg" || -f "$_current_conf/knoe.cfg" ) ]]; then
|
|
: # keep inherited KNOE_CONF — it is already more specific than cfg_dir
|
|
else
|
|
KNOE_CONF="$cfg_dir"
|
|
export KNOE_CONF
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Parse standard options and action.
|
|
# Sets (always):
|
|
# COMMON_CORE_ACTION
|
|
# COMMON_CORE_NAMESPACE
|
|
# COMMON_CORE_HELP (0/1)
|
|
# COMMON_CORE_PARSE_ERROR (empty or message)
|
|
common_core_parse_args() {
|
|
COMMON_CORE_ACTION=""
|
|
COMMON_CORE_NAMESPACE=""
|
|
COMMON_CORE_HELP=0
|
|
COMMON_CORE_PARSE_ERROR=""
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-m|--mode)
|
|
shift
|
|
knoe_set_mode "${1:-}"
|
|
;;
|
|
-m=*|--mode=*)
|
|
knoe_set_mode "${1#*=}"
|
|
;;
|
|
-n|--namespace)
|
|
shift
|
|
if [[ -z "${1:-}" ]]; then
|
|
COMMON_CORE_PARSE_ERROR="--namespace requires a value"
|
|
break
|
|
fi
|
|
COMMON_CORE_NAMESPACE="$1"
|
|
;;
|
|
-n=*|--namespace=*)
|
|
COMMON_CORE_NAMESPACE="${1#*=}"
|
|
;;
|
|
start|stop|status|restart|initialize|update|reload)
|
|
COMMON_CORE_ACTION="$1"
|
|
;;
|
|
-h|--help)
|
|
COMMON_CORE_HELP=1
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
*)
|
|
if [[ -z "$COMMON_CORE_ACTION" && "$1" != -* ]]; then
|
|
COMMON_CORE_ACTION="$1"
|
|
else
|
|
COMMON_CORE_PARSE_ERROR="Unknown argument: $1"
|
|
break
|
|
fi
|
|
;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [[ -z "$COMMON_CORE_ACTION" && "$COMMON_CORE_HELP" -eq 0 && -z "$COMMON_CORE_PARSE_ERROR" ]]; then
|
|
COMMON_CORE_PARSE_ERROR="Action is required (${COMMON_CORE_ACTIONS//|/, })"
|
|
fi
|
|
}
|
|
|
|
common_core_usage() {
|
|
local prog="${1:-$0}"
|
|
echo "Usage: ${prog##*/} [${COMMON_CORE_ACTIONS//|/|}] [-n namespace] [--mode k3d|k3s|k8s] [-c conf/{k3d|k3s|gke}.cfg]" >&2
|
|
}
|
|
|
|
# Resolve namespace with precedence: CLI override -> SERVICE_NAMESPACE -> NAMESPACE -> provided default
|
|
common_core_resolve_namespace() {
|
|
local default_ns="${1:-default}"
|
|
local ns="${COMMON_CORE_NAMESPACE:-}"
|
|
[[ -z "$ns" && -n "${SERVICE_NAMESPACE:-}" ]] && ns="$SERVICE_NAMESPACE"
|
|
[[ -z "$ns" && -n "${NAMESPACE:-}" ]] && ns="$NAMESPACE"
|
|
[[ -z "$ns" ]] && ns="$default_ns"
|
|
|
|
# In k3s mode, common core services must not default into the `default` namespace.
|
|
# If the caller didn't supply an explicit namespace, normalize `default` -> SERVICE_NAMESPACE
|
|
# from config (Explicit `-n default` is treated as a legacy/mistake and will be corrected).
|
|
local mode="${KNOE_MODE:-}"
|
|
if declare -F knoe_normalize_mode >/dev/null 2>&1; then
|
|
mode="$(knoe_normalize_mode "$mode")"
|
|
fi
|
|
if [[ "$mode" == "k3s" && "$ns" == "default" ]]; then
|
|
if [[ -n "${SERVICE_NAMESPACE:-}" && "${SERVICE_NAMESPACE}" != "default" ]]; then
|
|
ns="$SERVICE_NAMESPACE"
|
|
elif [[ -n "${PROLE_NAMESPACE:-}" && "${PROLE_NAMESPACE}" != "default" ]]; then
|
|
ns="$PROLE_NAMESPACE"
|
|
else
|
|
echo "ERROR: k3s mode requires a non-default service namespace. Set SERVICE_NAMESPACE in knoe.cfg or pass -n." >&2
|
|
exit 2
|
|
fi
|
|
fi
|
|
echo "$ns"
|
|
}
|
|
|
|
# Apply a resolved namespace to both NAMESPACE and SERVICE_NAMESPACE for consistency
|
|
common_core_apply_namespace() {
|
|
local ns="$1"
|
|
if [[ -n "$ns" ]]; then
|
|
NAMESPACE="$ns"
|
|
SERVICE_NAMESPACE="$ns"
|
|
export NAMESPACE SERVICE_NAMESPACE
|
|
fi
|
|
}
|
|
|
|
# Generate a pseudo-random password suitable for non-interactive bootstrapping.
|
|
#
|
|
# Notes:
|
|
# - This is intended for early-install order-of-operations cases where a user-provided
|
|
# secret/password is not available yet.
|
|
# - Callers should treat this as a temporary credential that can be overwritten later.
|
|
knoe_generate_password() {
|
|
local length="${1:-32}"
|
|
local out=""
|
|
|
|
# Prefer OpenSSL CSPRNG when available.
|
|
if command -v openssl >/dev/null 2>&1; then
|
|
# Generate extra bytes and then trim to requested length after making it URL-safe.
|
|
# shellcheck disable=SC2002
|
|
out=$(openssl rand -base64 48 2>/dev/null | tr -d '\n' | tr '+/' '-_' | tr -d '=')
|
|
fi
|
|
|
|
# Fallback to /dev/urandom with a restricted character set.
|
|
if [[ -z "${out:-}" && -r /dev/urandom ]]; then
|
|
out=$(LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c $((length * 2)) 2>/dev/null || true)
|
|
fi
|
|
|
|
# Last resort: time-based (not ideal, but avoids hard failure).
|
|
if [[ -z "${out:-}" ]]; then
|
|
out="$(date +%s%N)-$$-${RANDOM}${RANDOM}"
|
|
fi
|
|
|
|
printf "%s" "${out:0:length}"
|
|
}
|