ops: add new deployment and operator automation scripts
This commit is contained in:
76
scripts/operator/sync-pmg-webui-password-to-dotenv.sh
Executable file
76
scripts/operator/sync-pmg-webui-password-to-dotenv.sh
Executable file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
# Fetch Proxmox Mail Gateway (LXC 100) web UI password from the container and upsert
|
||||
# it into the repo .env as PMG_WEBUI_PASSWORD="..."
|
||||
#
|
||||
# Usage (from repo root):
|
||||
# bash scripts/operator/sync-pmg-webui-password-to-dotenv.sh
|
||||
# PROXMOX_SSH=root@192.168.11.11 PMG_VMID=100 bash ...
|
||||
#
|
||||
# Does not print the password. Backs up .env to .env.bak.pmg.<timestamp> before edit.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
||||
PROXMOX_SSH="${PROXMOX_SSH:-root@192.168.11.11}"
|
||||
PMG_VMID="${PMG_VMID:-100}"
|
||||
KEY="${PMG_ENV_KEY:-PMG_WEBUI_PASSWORD}"
|
||||
DOTENV="${DOTENV_FILE:-$PROJECT_ROOT/.env}"
|
||||
PW_PATH="${PMG_PASSWORD_FILE:-/root/PMG_WEBUI_password.txt}"
|
||||
|
||||
if [ ! -e "$DOTENV" ]; then
|
||||
: > "$DOTENV"
|
||||
echo "Created $DOTENV"
|
||||
fi
|
||||
|
||||
if ! command -v ssh >/dev/null; then
|
||||
echo "ssh not found" >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! command -v python3 >/dev/null; then
|
||||
echo "python3 not found" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
PW=$(
|
||||
ssh -o ConnectTimeout=15 -o BatchMode=yes "$PROXMOX_SSH" "pct exec $PMG_VMID -- cat $PW_PATH" 2>/dev/null | tr -d '\r' || true
|
||||
)
|
||||
# Trim leading/trailing whitespace only
|
||||
PW="${PW#"${PW%%[![:space:]]*}"}"
|
||||
PW="${PW%"${PW##*[![:space:]]}"}"
|
||||
|
||||
if [ -z "$PW" ]; then
|
||||
echo "Failed to read password (empty or ssh failed). Check: ssh $PROXMOX_SSH 'pct exec $PMG_VMID -- test -f $PW_PATH'" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TS=$(date +%Y%m%d_%H%M%S)
|
||||
if [ -f "$DOTENV" ] && [ -s "$DOTENV" ]; then
|
||||
cp -a "$DOTENV" "$DOTENV.bak.pmg.$TS"
|
||||
echo "Backup: $DOTENV.bak.pmg.$TS"
|
||||
fi
|
||||
|
||||
export DOTENV_PATH="$DOTENV" DOTENV_KEY="$KEY"
|
||||
# shellcheck disable=SC2016,SC2090
|
||||
python3 -c '
|
||||
import os, re
|
||||
import sys
|
||||
path = os.environ["DOTENV_PATH"]
|
||||
key = os.environ["DOTENV_KEY"]
|
||||
pw = sys.argv[1]
|
||||
|
||||
def dquote(s: str) -> str:
|
||||
return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"") + "\""
|
||||
|
||||
line = key + "=" + dquote(pw) + "\n"
|
||||
with open(path) as f:
|
||||
lines = f.readlines()
|
||||
|
||||
out_lines = [ln for ln in lines if not re.match(r"^" + re.escape(key) + r"\s*=", ln)]
|
||||
out_lines.append(line)
|
||||
with open(path, "w") as f:
|
||||
f.writelines(out_lines)
|
||||
print("Wrote " + key + " to " + path + " (value not shown).")
|
||||
' -- "$PW"
|
||||
unset DOTENV_PATH DOTENV_KEY
|
||||
|
||||
echo "Done."
|
||||
Reference in New Issue
Block a user