rpg-westreiche/westreiche_website/tools/deploy_app.sh

128 lines
4.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# Westreiche – Charakterverwalter per FTPS auf den Server laden.
#
# Ziel: public_html/<APP_DIRECTORY>/ (APP_DIRECTORY aus credentials.txt).
# Die Zugangsdaten kommen aus ../../credentials.txt (gitignored).
#
# ./tools/deploy_app.sh --dry-run # nur zeigen, was hochgeht
# ./tools/deploy_app.sh # hochladen
set -euo pipefail
APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
REPO_ROOT="$(cd "${APP_DIR}/.." && pwd)"
CREDENTIALS="${REPO_ROOT}/credentials.txt"
DRY_RUN=0
[[ "${1:-}" == "--dry-run" ]] && DRY_RUN=1
if [[ ! -f "${CREDENTIALS}" ]]; then
echo "credentials.txt fehlt: ${CREDENTIALS}" >&2
exit 1
fi
# credentials.txt zeilenweise einlesen (KEY=VALUE, optional in Anführungs-
# zeichen) – nicht per `source`, damit Passwörter mit Sonderzeichen wie
# ( ) ; ? $ nicht die Shell sprengen.
load_credentials() {
local file="$1" line key value
while IFS= read -r line || [[ -n "${line}" ]]; do
line="${line%$'\r'}"
[[ -z "${line//[[:space:]]/}" || "${line}" == \#* ]] && continue
[[ "${line}" != *=* ]] && continue
key="${line%%=*}"
value="${line#*=}"
key="${key//[[:space:]]/}"
[[ "${key}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue
if [[ ${#value} -ge 2 && ( "${value}" == \"*\" || "${value}" == \'*\' ) ]]; then
value="${value:1:${#value}-2}"
fi
value="${value%"${value##*[![:space:]]}"}"
export "${key}=${value}"
done < "${file}"
}
load_credentials "${CREDENTIALS}"
: "${FTP_URL:?FTP_URL fehlt}" "${FTP_USER:?FTP_USER fehlt}" "${FTP_PASSWORD:?FTP_PASSWORD fehlt}"
APP_DIRECTORY="${APP_DIRECTORY:-westreiche_website}"
TARGET="/public_html/${APP_DIRECTORY}"
curl_ftp() {
curl -s -S --ssl-reqd --user "${FTP_USER}:${FTP_PASSWORD}" "$@"
}
# config.local.php aus den credentials bauen (nicht im Repo ablegen)
CONFIG_TMP="$(mktemp)"
trap 'rm -f "${CONFIG_TMP}"' EXIT
python3 - "${CONFIG_TMP}" <<'PY'
import os, sys
path = sys.argv[1]
def q(key, default=""):
value = os.environ.get(key, default) or default
return value.replace("\\", "\\\\").replace("'", "\\'")
app_dir = os.environ.get("APP_DIRECTORY") or "westreiche_website"
public_url = os.environ.get("APP_PUBLIC_URL") or "https://zauberturm.de"
content = f"""<?php
return [
'db' => [
'host' => '{q('DB_HOST').strip()}',
'port' => {int(os.environ.get('DB_PORT', '3306'))},
'name' => '{q('DB_NAME')}',
'user' => '{q('DB_USER')}',
'password' => '{q('DB_PASSWORD')}',
'charset' => 'utf8mb4',
],
'mail' => [
'host' => '{q('MAIL_HOST').strip()}',
'port' => {int(os.environ.get('MAIL_PORT', '587'))},
'secure' => '{os.environ.get('MAIL_SECURE', 'tls').strip()}',
'user' => '{q('MAIL_USER').strip()}',
'password' => '{q('MAIL_PASSWORD')}',
'from' => '{q('MAIL_FROM').strip()}',
'from_name' => '{q('MAIL_FROM_NAME').strip()}',
'timeout' => 15,
],
'app' => [
'base_path' => '/{app_dir}',
'public_url' => '{public_url}',
'storage_path' => dirname(__DIR__) . '/storage',
'install_token' => '{os.environ.get('INSTALL_TOKEN') or ''}',
'session_idle_seconds' => 30 * 86400,
'session_absolute_seconds' => 90 * 86400,
'remember_seconds' => 90 * 86400,
'login_window_seconds' => 900,
'login_max_per_user' => 5,
'login_max_per_ip' => 15,
],
];
"""
open(path, "w", encoding="utf-8").write(content)
PY
upload() {
local src="$1" dst="$2"
if [[ ${DRY_RUN} -eq 1 ]]; then
echo "PUT ${src#${APP_DIR}/} -> ${dst}"
return
fi
curl_ftp --ftp-create-dirs -T "${src}" "ftp://${FTP_URL}${dst}"
}
echo "Ziel: ftp://${FTP_URL}${TARGET}"
# Verzeichnisse anlegen (FTP braucht MKD je Ebene; --ftp-create-dirs erledigt das)
find "${APP_DIR}" -type f \
! -name 'config.local.php' \
! -name '.DS_Store' \
| sort | while read -r file; do
rel="${file#${APP_DIR}/}"
upload "${file}" "${TARGET}/${rel}"
done
if [[ ${DRY_RUN} -eq 1 ]]; then
echo "PUT app/config.local.php -> ${TARGET}/app/config.local.php"
else
curl_ftp --ftp-create-dirs -T "${CONFIG_TMP}" "ftp://${FTP_URL}${TARGET}/app/config.local.php"
echo "Fertig. Installation: https://…${TARGET}/install.php?token=${INSTALL_TOKEN:-<INSTALL_TOKEN>}"
fi