prole/etc/init_k8s.sh
chrisfu a7cb5b5fca Add prole_cfg.sh sourcing for configuration management
- Consistently source `prole_cfg.sh` across multiple init scripts for unified configuration handling.
- Enhanced error handling in `screen.py` to manage potential UI exceptions gracefully.
- Improved asynchronous function scheduling using the new `safe_after` method.
2026-01-30 23:37:32 -08:00

208 lines
4.4 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# etc/init_k8s.sh
# Purpose: Manage k3d/k8s environment
# Initialize SCRIPT_DIR
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# Preserve positional args while sourcing env
__PROLE_SAVED_ARGS=("$@")
if [[ -n "${PROLE_HOME:-}" && -f "$PROLE_HOME/env.sh" ]]; then
set --
# shellcheck disable=SC1090
source "$PROLE_HOME/env.sh"
elif [[ -f "$HOME/.prole/env.sh" ]]; then
set --
# shellcheck disable=SC1090
source "$HOME/.prole/env.sh"
fi
set -- "${__PROLE_SAVED_ARGS[@]}"
unset __PROLE_SAVED_ARGS
# Load config values from prole.cfg before applying defaults/CLI overrides.
# shellcheck disable=SC1090
source "$SCRIPT_DIR/prole_cfg.sh"
# Default values
VERBOSE=false
ENVIRONMENT="prod"
HOST=""
CLUSTER_NAME_DEFAULT="prole-dev-cluster"
CLUSTER_PORT_DEFAULT="6443"
usage() {
cat <<EOF
Usage: $0 [initialize|start|stop|restart|status] [options]
Actions:
initialize Create a k3d cluster (dev) or fetch kubeconfig (service/prod)
start Start the k3d cluster
stop Stop the k3d cluster
restart Restart the k3d cluster
status Show status of Docker and k3d cluster
Options:
-v, --verbose Enable verbose output
-e, --environment dev|service|prod (default: $ENVIRONMENT)
-h, --host Remote host for service/prod environments
-n, --name k3d cluster name
EOF
exit 1
}
log() {
echo "[INFO] $*"
}
debug() {
if [[ "$VERBOSE" == "true" ]]; then
echo "[DEBUG] $*"
fi
}
# Parse options
POSITIONAL_ARGS=()
CLUSTER_NAME=""
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-h|--host)
HOST="$2"
shift 2
;;
-n|--name)
CLUSTER_NAME="$2"
shift 2
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
ACTION=${1:-}
if [[ -z "$ACTION" ]]; then
usage
fi
ensure_tools() {
for t in k3d docker; do
command -v "$t" >/dev/null || { echo "ERROR: Missing required tool: $t" >&2; exit 1; }
done
}
ensure_docker() {
if ! docker info >/dev/null 2>&1; then
echo "ERROR: Docker is not running." >&2
exit 1
fi
}
initialize() {
ensure_tools
ensure_docker
if [[ "$ENVIRONMENT" == "dev" ]]; then
if [[ -z "$CLUSTER_NAME" ]]; then
if [[ -t 0 ]]; then
read -p "Enter k3d cluster name [$CLUSTER_NAME_DEFAULT]: " CLUSTER_NAME
CLUSTER_NAME=${CLUSTER_NAME:-$CLUSTER_NAME_DEFAULT}
else
CLUSTER_NAME=$CLUSTER_NAME_DEFAULT
fi
fi
if k3d cluster list "$CLUSTER_NAME" >/dev/null 2>&1; then
log "Cluster '$CLUSTER_NAME' already exists."
else
log "Creating k3d cluster '$CLUSTER_NAME'..."
k3d cluster create "$CLUSTER_NAME" -p "0.0.0.0:$CLUSTER_PORT_DEFAULT:6443@server:0"
fi
else
if [[ -n "$HOST" ]]; then
log "Environment is $ENVIRONMENT. Host is $HOST."
log "TODO: Fetch kubeconfig from $HOST (placeholder)."
else
log "Environment is $ENVIRONMENT. Use -h|--host to specify the remote cluster host."
fi
fi
}
status() {
ensure_tools
log "Checking Docker status..."
if docker info >/dev/null 2>&1; then
echo "Docker is running."
else
echo "Docker is NOT running."
fi
log "Listing k3d clusters..."
k3d cluster list
}
get_cluster_name() {
# If a name was provided or exists in env, use it.
# Otherwise, if there is only one cluster, use it.
# Finally, use default.
if [[ -n "${CLUSTER_NAME:-}" ]]; then
echo "$CLUSTER_NAME"
return
fi
local clusters
clusters=$(k3d cluster list --no-headers | awk '{print $1}')
local count
count=$(echo "$clusters" | grep -c . || true)
if [[ "$count" -eq 1 ]]; then
echo "$clusters"
else
echo "$CLUSTER_NAME_DEFAULT"
fi
}
case "$ACTION" in
initialize)
initialize
;;
status)
status
;;
start)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Starting k3d cluster '$CLUSTER_NAME'..."
k3d cluster start "$CLUSTER_NAME"
;;
stop)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Stopping k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
;;
restart)
ensure_tools
CLUSTER_NAME=$(get_cluster_name)
log "Restarting k3d cluster '$CLUSTER_NAME'..."
k3d cluster stop "$CLUSTER_NAME"
k3d cluster start "$CLUSTER_NAME"
;;
*)
usage
;;
esac