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
196
scripts/setup.sh
Executable file
196
scripts/setup.sh
Executable file
|
|
@ -0,0 +1,196 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# shellcheck source=scripts/_compose.sh
|
||||
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)/_compose.sh"
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: ./scripts/setup.sh [options]
|
||||
|
||||
Initialize `.env`, generate `docker/secrets/gcoord_master_key.txt` (if absent),
|
||||
and prepare the persistent `data/` directory with secure permissions.
|
||||
|
||||
Options:
|
||||
--mode <lan|cloudflare> Set GCOORD_DEPLOY_MODE in `.env`
|
||||
--throttle <low-resource|balanced|unthrottled>
|
||||
Set GCOORD_THROTTLE_MODE in `.env`
|
||||
--force-env Overwrite `.env` from `.env.example`
|
||||
-h, --help Show this help message
|
||||
EOF
|
||||
}
|
||||
|
||||
REQ_MODE=""
|
||||
REQ_THROTTLE=""
|
||||
FORCE_ENV=0
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--mode)
|
||||
[[ $# -ge 2 ]] || die "--mode requires an argument ('lan' or 'cloudflare')."
|
||||
REQ_MODE="$2"
|
||||
validate_mode "$REQ_MODE"
|
||||
shift 2
|
||||
;;
|
||||
--mode=*)
|
||||
REQ_MODE="${1#*=}"
|
||||
validate_mode "$REQ_MODE"
|
||||
shift
|
||||
;;
|
||||
--lan)
|
||||
REQ_MODE="lan"
|
||||
shift
|
||||
;;
|
||||
--cloudflare)
|
||||
REQ_MODE="cloudflare"
|
||||
shift
|
||||
;;
|
||||
--throttle)
|
||||
[[ $# -ge 2 ]] || die "--throttle requires 'low-resource', 'balanced', or 'unthrottled'."
|
||||
REQ_THROTTLE="$2"
|
||||
shift 2
|
||||
;;
|
||||
--throttle=*)
|
||||
REQ_THROTTLE="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--force-env)
|
||||
FORCE_ENV=1
|
||||
shift
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*)
|
||||
die "Unknown option: $1 (run with --help)"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [[ -n "$REQ_THROTTLE" ]]; then
|
||||
case "$REQ_THROTTLE" in
|
||||
low-resource|balanced|unthrottled) ;;
|
||||
*) die "Invalid --throttle value '$REQ_THROTTLE' (expected low-resource, balanced, or unthrottled)." ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
[[ -f "$ENV_EXAMPLE" && ! -L "$ENV_EXAMPLE" ]] || die "Missing template file: $ENV_EXAMPLE"
|
||||
[[ ! -L "$ENV_FILE" ]] || die "Refusing to operate on symlinked .env: $ENV_FILE"
|
||||
[[ ! -L "$SECRET_FILE" ]] || die "Refusing to operate on symlinked secret file: $SECRET_FILE"
|
||||
[[ ! -L "$DATA_DIR" ]] || die "Refusing to operate on symlinked data dir: $DATA_DIR"
|
||||
|
||||
set_env_var() {
|
||||
local file="$1"
|
||||
local key="$2"
|
||||
local val="$3"
|
||||
local tmp_file
|
||||
tmp_file="$(mktemp "${file}.tmp.XXXXXX")"
|
||||
chmod 0600 "$tmp_file"
|
||||
TARGET_KEY="$key" TARGET_VAL="$val" awk '
|
||||
BEGIN { updated = 0 }
|
||||
{
|
||||
if ($0 ~ "^[[:space:]]*" ENVIRON["TARGET_KEY"] "=") {
|
||||
print ENVIRON["TARGET_KEY"] "=" ENVIRON["TARGET_VAL"]
|
||||
updated = 1
|
||||
} else {
|
||||
print $0
|
||||
}
|
||||
}
|
||||
END {
|
||||
if (!updated) {
|
||||
print ENVIRON["TARGET_KEY"] "=" ENVIRON["TARGET_VAL"]
|
||||
}
|
||||
}
|
||||
' "$file" >"$tmp_file"
|
||||
mv -f "$tmp_file" "$file"
|
||||
chmod 0600 "$file"
|
||||
}
|
||||
|
||||
generate_hex_key() {
|
||||
if command -v openssl >/dev/null 2>&1; then
|
||||
openssl rand -hex 32
|
||||
return 0
|
||||
fi
|
||||
if command -v node >/dev/null 2>&1; then
|
||||
node -e "process.stdout.write(require('crypto').randomBytes(32).toString('hex') + '\n')"
|
||||
return 0
|
||||
fi
|
||||
if command -v python3 >/dev/null 2>&1; then
|
||||
python3 -c "import secrets; print(secrets.token_hex(32))"
|
||||
return 0
|
||||
fi
|
||||
if [[ -r /dev/urandom ]]; then
|
||||
od -An -tx1 -N32 /dev/urandom | tr -d ' \n'
|
||||
printf '\n'
|
||||
return 0
|
||||
fi
|
||||
die "Unable to generate random master key (need openssl, node, python3, or /dev/urandom)."
|
||||
}
|
||||
|
||||
if [[ ! -f "$ENV_FILE" || "$FORCE_ENV" -eq 1 ]]; then
|
||||
tmp_env="$(mktemp "${ENV_FILE}.tmp.XXXXXX")"
|
||||
chmod 0600 "$tmp_env"
|
||||
cp "$ENV_EXAMPLE" "$tmp_env"
|
||||
chmod 0600 "$tmp_env"
|
||||
mv -f "$tmp_env" "$ENV_FILE"
|
||||
chmod 0600 "$ENV_FILE"
|
||||
log_info "Created $ENV_FILE (0600) from .env.example."
|
||||
else
|
||||
chmod 0600 "$ENV_FILE" || true
|
||||
log_info "Keeping existing $ENV_FILE (use --force-env to reset from .env.example)."
|
||||
fi
|
||||
|
||||
if [[ -n "$REQ_MODE" ]]; then
|
||||
set_env_var "$ENV_FILE" "GCOORD_DEPLOY_MODE" "$REQ_MODE"
|
||||
log_info "Set GCOORD_DEPLOY_MODE=$REQ_MODE in .env."
|
||||
fi
|
||||
|
||||
if [[ -n "$REQ_THROTTLE" ]]; then
|
||||
set_env_var "$ENV_FILE" "GCOORD_THROTTLE_MODE" "$REQ_THROTTLE"
|
||||
log_info "Set GCOORD_THROTTLE_MODE=$REQ_THROTTLE in .env."
|
||||
fi
|
||||
|
||||
mkdir -p -m 0700 "$SECRET_DIR"
|
||||
chmod 0700 "$SECRET_DIR"
|
||||
|
||||
if [[ -s "$SECRET_FILE" ]]; then
|
||||
chmod 0600 "$SECRET_FILE" || true
|
||||
log_info "Keeping existing Vault master key at $SECRET_FILE (0600)."
|
||||
else
|
||||
key_hex="$(generate_hex_key | tr -d '[:space:]')"
|
||||
[[ "$key_hex" =~ ^[0-9a-fA-F]{64}$ ]] || die "Generated master key has unexpected format."
|
||||
tmp_key="$(mktemp "${SECRET_FILE}.tmp.XXXXXX")"
|
||||
chmod 0600 "$tmp_key"
|
||||
printf '%s\n' "$key_hex" >"$tmp_key"
|
||||
mv -f "$tmp_key" "$SECRET_FILE"
|
||||
chmod 0600 "$SECRET_FILE"
|
||||
log_info "Generated new 256-bit Vault master key at $SECRET_FILE (0600)."
|
||||
log_warn "Back up $SECRET_FILE securely offline; losing both the master key and backups makes vault.enc unrecoverable."
|
||||
fi
|
||||
|
||||
mkdir -p -m 0700 "$DATA_DIR"
|
||||
chmod 0700 "$DATA_DIR"
|
||||
cur_uid="$(id -u)"
|
||||
if [[ "$cur_uid" -eq 0 ]]; then
|
||||
chown 1000:1000 "$DATA_DIR"
|
||||
log_info "Prepared $DATA_DIR owned by 1000:1000 (0700)."
|
||||
elif [[ "$cur_uid" -ne 1000 ]]; then
|
||||
log_info "Prepared $DATA_DIR (0700)."
|
||||
log_warn "Container runs as UID:GID 1000:1000, while your host UID is $cur_uid. If GCoordinator fails with EACCES on /data, run: sudo chown -R 1000:1000 \"$DATA_DIR\""
|
||||
else
|
||||
log_info "Prepared $DATA_DIR (0700, UID 1000)."
|
||||
fi
|
||||
|
||||
effective_mode="$(resolve_mode "$REQ_MODE")"
|
||||
effective_throttle="$(env_effective GCOORD_THROTTLE_MODE low-resource)"
|
||||
|
||||
printf '\nSetup complete (mode=%s, throttle=%s).\n' "$effective_mode" "$effective_throttle"
|
||||
printf 'Next steps:\n'
|
||||
if [[ "$effective_mode" == "cloudflare" ]]; then
|
||||
printf ' 1. Set CLOUDFLARED_TUNNEL_TOKEN in %s\n' "$ENV_FILE"
|
||||
printf ' 2. ./scripts/build.sh --cloudflare && ./scripts/up.sh --cloudflare\n'
|
||||
else
|
||||
printf ' 1. Review %s (GCOORD_LAN_BIND, ports, GCOORD_PROXY_PORT, GCOORD_THROTTLE_MODE)\n' "$ENV_FILE"
|
||||
printf ' 2. ./scripts/build.sh && ./scripts/up.sh\n'
|
||||
fi
|
||||
Loading…
Add table
Add a link
Reference in a new issue