chore(docker): add .env.example and Docker Compose setup/build/run scripts
This commit is contained in:
parent
5701e77780
commit
a1f26bc61f
12 changed files with 943 additions and 7 deletions
175
scripts/_compose.sh
Executable file
175
scripts/_compose.sh
Executable file
|
|
@ -0,0 +1,175 @@
|
|||
#!/usr/bin/env bash
|
||||
# Shared helpers for GCoordinator Docker Compose wrapper scripts.
|
||||
# Intended to be sourced, not executed directly.
|
||||
|
||||
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
||||
echo "Error: scripts/_compose.sh is a shared helper and should be sourced, not executed directly." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
|
||||
REPO_ROOT="$(cd -- "$SCRIPT_DIR/.." && pwd -P)"
|
||||
DOCKER_DIR="$REPO_ROOT/docker"
|
||||
ENV_FILE="$REPO_ROOT/.env"
|
||||
ENV_EXAMPLE="$REPO_ROOT/.env.example"
|
||||
SECRET_DIR="$DOCKER_DIR/secrets"
|
||||
SECRET_FILE="$SECRET_DIR/gcoord_master_key.txt"
|
||||
DATA_DIR="$REPO_ROOT/data"
|
||||
|
||||
DRY_RUN="${DRY_RUN:-0}"
|
||||
COMPOSE_CMD=()
|
||||
COMPOSE_ARGS=()
|
||||
|
||||
log_info() {
|
||||
printf '[gcoord] %s\n' "$*"
|
||||
}
|
||||
|
||||
log_warn() {
|
||||
printf '[gcoord] WARNING: %s\n' "$*" >&2
|
||||
}
|
||||
|
||||
die() {
|
||||
printf '[gcoord] ERROR: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Safely read KEY from .env without executing arbitrary shell code.
|
||||
env_get() {
|
||||
local key="$1"
|
||||
local file="${2:-$ENV_FILE}"
|
||||
[[ -f "$file" && ! -L "$file" ]] || return 1
|
||||
|
||||
local line val="" found=0
|
||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||
line="${line%$'\r'}"
|
||||
[[ "$line" =~ ^[[:space:]]*# ]] && continue
|
||||
[[ "$line" =~ ^[[:space:]]*$ ]] && continue
|
||||
if [[ "$line" =~ ^[[:space:]]*(export[[:space:]]+)?([A-Za-z_][A-Za-z0-9_]*)=(.*)$ ]]; then
|
||||
if [[ "${BASH_REMATCH[2]}" == "$key" ]]; then
|
||||
val="${BASH_REMATCH[3]}"
|
||||
found=1
|
||||
fi
|
||||
fi
|
||||
done <"$file"
|
||||
|
||||
[[ "$found" -eq 1 ]] || return 1
|
||||
|
||||
# Strip leading/trailing whitespace, then matching outer quotes if present.
|
||||
val="${val#"${val%%[![:space:]]*}"}"
|
||||
val="${val%"${val##*[![:space:]]}"}"
|
||||
if [[ "${#val}" -ge 2 ]]; then
|
||||
if [[ "$val" == \"*\" && "$val" == *\" ]]; then
|
||||
val="${val:1:${#val}-2}"
|
||||
elif [[ "$val" == \'*\' && "$val" == *\' ]]; then
|
||||
val="${val:1:${#val}-2}"
|
||||
fi
|
||||
fi
|
||||
printf '%s' "$val"
|
||||
}
|
||||
|
||||
# Resolve effective setting: process env -> .env -> fallback.
|
||||
env_effective() {
|
||||
local key="$1"
|
||||
local fallback="${2:-}"
|
||||
local val=""
|
||||
|
||||
if [[ -n "${!key:-}" ]]; then
|
||||
printf '%s' "${!key}"
|
||||
return 0
|
||||
fi
|
||||
if val="$(env_get "$key" 2>/dev/null)" && [[ -n "$val" ]]; then
|
||||
printf '%s' "$val"
|
||||
return 0
|
||||
fi
|
||||
printf '%s' "$fallback"
|
||||
}
|
||||
|
||||
detect_compose() {
|
||||
if command -v docker >/dev/null 2>&1 && docker compose version >/dev/null 2>&1; then
|
||||
COMPOSE_CMD=(docker compose)
|
||||
return 0
|
||||
fi
|
||||
if command -v docker-compose >/dev/null 2>&1; then
|
||||
COMPOSE_CMD=(docker-compose)
|
||||
return 0
|
||||
fi
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
COMPOSE_CMD=(docker compose)
|
||||
return 0
|
||||
fi
|
||||
die "Neither 'docker compose' (v2) nor 'docker-compose' (v1) is installed on PATH."
|
||||
}
|
||||
|
||||
validate_mode() {
|
||||
local mode="$1"
|
||||
case "$mode" in
|
||||
lan|cloudflare)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
die "Invalid deployment mode '$mode' (expected 'lan' or 'cloudflare')."
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
resolve_mode() {
|
||||
local explicit="${1:-}"
|
||||
if [[ -n "$explicit" ]]; then
|
||||
validate_mode "$explicit"
|
||||
printf '%s' "$explicit"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local from_env=""
|
||||
if [[ -n "${GCOORD_DEPLOY_MODE:-}" ]]; then
|
||||
from_env="$GCOORD_DEPLOY_MODE"
|
||||
elif from_env="$(env_get GCOORD_DEPLOY_MODE 2>/dev/null)" && [[ -n "$from_env" ]]; then
|
||||
:
|
||||
else
|
||||
from_env=""
|
||||
fi
|
||||
|
||||
if [[ -n "$from_env" ]]; then
|
||||
validate_mode "$from_env"
|
||||
printf '%s' "$from_env"
|
||||
return 0
|
||||
fi
|
||||
|
||||
local cf_token=""
|
||||
cf_token="$(env_effective CLOUDFLARED_TUNNEL_TOKEN "")"
|
||||
if [[ -n "$cf_token" ]]; then
|
||||
printf 'cloudflare'
|
||||
else
|
||||
printf 'lan'
|
||||
fi
|
||||
}
|
||||
|
||||
build_compose_args() {
|
||||
local mode="$1"
|
||||
validate_mode "$mode"
|
||||
|
||||
COMPOSE_ARGS=()
|
||||
if [[ -f "$ENV_FILE" && ! -L "$ENV_FILE" ]]; then
|
||||
COMPOSE_ARGS+=(--env-file "$ENV_FILE")
|
||||
fi
|
||||
COMPOSE_ARGS+=(-f "$DOCKER_DIR/docker-compose.yml")
|
||||
if [[ "$mode" == "lan" ]]; then
|
||||
COMPOSE_ARGS+=(-f "$DOCKER_DIR/docker-compose.lan.yml")
|
||||
fi
|
||||
}
|
||||
|
||||
run_compose() {
|
||||
local mode="$1"
|
||||
shift
|
||||
detect_compose
|
||||
build_compose_args "$mode"
|
||||
|
||||
if [[ "$DRY_RUN" == "1" ]]; then
|
||||
printf '[dry-run]'
|
||||
printf ' %q' "${COMPOSE_CMD[@]}" "${COMPOSE_ARGS[@]}" "$@"
|
||||
printf '\n'
|
||||
return 0
|
||||
fi
|
||||
|
||||
"${COMPOSE_CMD[@]}" "${COMPOSE_ARGS[@]}" "$@"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue