rpg-westreiche/westreiche_website/tools/deploy_app.sh

139 lines
4.8 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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
# Vorab-Check: Akzeptiert der DB-Server diese Zugangsdaten? Sonst würde der
# Upload eine vorher laufende App mit einer toten config.local.php ersetzen.
if command -v mysql >/dev/null 2>&1; then
DB_HOST_CLEAN="$(printf '%s' "${DB_HOST}" | tr -d '[:space:]')"
if ! MYSQL_PWD="${DB_PASSWORD}" mysql -h "${DB_HOST_CLEAN}" -u "${DB_USER}" \
--connect-timeout=8 -e 'SELECT 1' "${DB_NAME}" >/dev/null 2>&1; then
echo "FEHLER: DB-Verbindung zu ${DB_USER}@${DB_HOST_CLEAN}/${DB_NAME} schlägt fehl." >&2
echo " DB_HOST in credentials.txt prüfen (richtig ist mxzh.your-database.de)." >&2
exit 1
fi
fi
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