Files
2026-07-04 16:00:04 +02:00

157 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)"
CPIF="/usr/local/directadmin/data/admin/custom_package_items.conf"
SUDOERS_FILE="/etc/sudoers.d/directadmin-postgresql-plugin"
CRON_FILE="/etc/cron.d/da-postgresql-jobs"
IS_ROOT=0
if [ "$(id -u)" -eq 0 ]; then
IS_ROOT=1
fi
set_permissions() {
local owner="$1"
local mode="$2"
local path="$3"
if [ -e "$path" ]; then
chown "$owner" "$path" 2>/dev/null || true
chmod "$mode" "$path" 2>/dev/null || true
fi
}
set_mode_if_exists() {
local mode="$1"
local path="$2"
if [ -e "$path" ]; then
chmod "$mode" "$path" 2>/dev/null || true
fi
}
bootstrap_custom_package_items() {
if [ "$IS_ROOT" -ne 1 ]; then
echo "postgresql: warning: uruchom jako root, aby zaktualizować $CPIF" >&2
return 0
fi
touch "$CPIF"
sed -i '/^postgresql_enabled=/d' "$CPIF" || true
sed -i '/^postgresql=/d' "$CPIF" || true
sed -i '/^upostgresql=/d' "$CPIF" || true
sed -i '/^postgresql_max_databases=/d' "$CPIF" || true
sed -i '/^postgresql_unlimited=/d' "$CPIF" || true
sed -i '/^upostgresql_max_databases=/d' "$CPIF" || true
cat >> "$CPIF" <<'EOF'
postgresql_enabled=type=checkbox&string=Enable da-postgresql&desc=Zezwól na dostęp do baz danych PostgreSQL&default=no
postgresql=type=text&string=Bazy PostgreSQL&desc=&default=5
upostgresql=type=checkbox&string=Bez ograniczeń&desc=&default=no&custom=autofocus%20onfocus%3D%22%28function%28c%29%7Bif%28c.dataset.pgInit%3D%3D%3D%271%27%29%7Breturn%3B%7Dc.dataset.pgInit%3D%271%27%3Bvar%20l%3Ddocument.querySelector%28%22input%5Bname%3D%27postgresql%27%5D%22%29%3Bif%28%21l%29%7Breturn%3B%7Dvar%20r%3Dc.closest%28%27tr%27%29%3Bvar%20td%3Dl.closest%28%27td%27%29%3Bif%28r%26%26td%29%7Bvar%20w%3Ddocument.getElementById%28%27da-pg-unlim-wrap%27%29%3Bif%28%21w%29%7Bw%3Ddocument.createElement%28%27label%27%29%3Bw.id%3D%27da-pg-unlim-wrap%27%3Bw.style.display%3D%27inline-flex%27%3Bw.style.alignItems%3D%27center%27%3Bw.style.gap%3D%278px%27%3Bw.style.marginLeft%3D%2712px%27%3Bw.appendChild%28c%29%3Bw.appendChild%28document.createTextNode%28%27Bez%20ogranicze%C5%84%27%29%29%3Btd.style.display%3D%27flex%27%3Btd.style.alignItems%3D%27center%27%3Btd.style.justifyContent%3D%27space-between%27%3Btd.appendChild%28w%29%3B%7Dr.style.display%3D%27none%27%3B%7Dvar%20s%3Dfunction%28%29%7Bl.disabled%3D%21%21c.checked%3B%7D%3Bc.addEventListener%28%27change%27%2Cs%29%3Bs%28%29%3B%7D%29%28this%29%22%20onchange%3D%22var%20l%3Ddocument.querySelector%28%22input%5Bname%3D%27postgresql%27%5D%22%29%3Bif%28l%29%7Bl.disabled%3D%21%21this.checked%3B%7D%22
EOF
set_permissions diradmin:diradmin 640 "$CPIF"
}
bootstrap_sudoers() {
if [ "$IS_ROOT" -ne 1 ]; then
echo "postgresql: warning: uruchom jako root, aby dodać sudoers dla synchronizacji pg_hba" >&2
return 0
fi
cat > "$SUDOERS_FILE" <<SUDO
# DirectAdmin PostgreSQL plugin
diradmin ALL=(root) NOPASSWD: /usr/local/directadmin/plugins/da-postgresql/scripts/setup/pg_hba_sync.sh
SUDO
chmod 440 "$SUDOERS_FILE"
}
bootstrap_worker_cron() {
if [ "$IS_ROOT" -ne 1 ]; then
echo "postgresql: warning: uruchom jako root, aby dodać cron workera backup/restore" >&2
return 0
fi
touch "$CRON_FILE"
if ! grep -Fq '/usr/local/directadmin/plugins/da-postgresql/scripts/worker.sh' "$CRON_FILE"; then
echo '* * * * * root /usr/local/directadmin/plugins/da-postgresql/scripts/worker.sh >/dev/null 2>&1' >> "$CRON_FILE"
fi
chmod 644 "$CRON_FILE" 2>/dev/null || true
}
bootstrap_adminer() {
local adminer_setup="$PLUGIN_DIR/scripts/setup/adminer_install.sh"
if [ "$IS_ROOT" -ne 1 ]; then
echo "postgresql: warning: uruchom jako root, aby zainstalować Adminera." >&2
return 0
fi
if [ ! -f "$adminer_setup" ]; then
echo "postgresql: error: brak skryptu instalacji Adminera: $adminer_setup" >&2
return 1
fi
bash "$adminer_setup"
}
activate_plugin() {
local conf="$PLUGIN_DIR/plugin.conf"
if [ -f "$conf" ]; then
sed -i 's/^active=.*/active=yes/' "$conf" || true
sed -i 's/^installed=.*/installed=yes/' "$conf" || true
fi
}
mkdir -p "$PLUGIN_DIR/data"
mkdir -p "$PLUGIN_DIR/exec" "$PLUGIN_DIR/exec/lib" "$PLUGIN_DIR/exec/handlers"
mkdir -p "$PLUGIN_DIR/data/jobs/pending" "$PLUGIN_DIR/data/jobs/processing" "$PLUGIN_DIR/data/jobs/done"
mkdir -p "$PLUGIN_DIR/data/logs" "$PLUGIN_DIR/data/lock" "$PLUGIN_DIR/data/pids" "$PLUGIN_DIR/data/cancel"
mkdir -p "$PLUGIN_DIR/data/uploads"
if [ "$IS_ROOT" -eq 1 ]; then
chown -R diradmin:diradmin "$PLUGIN_DIR" 2>/dev/null || true
else
echo "postgresql: warning: uruchom jako root, aby ustawić właściciela plików pluginu (w przeciwnym razie może wystąpić biała strona)." >&2
fi
find "$PLUGIN_DIR" -type d -exec chmod 755 {} + 2>/dev/null || true
set_mode_if_exists 700 "$PLUGIN_DIR/data"
set_mode_if_exists 700 "$PLUGIN_DIR/data/jobs"
set_mode_if_exists 700 "$PLUGIN_DIR/data/jobs/pending"
set_mode_if_exists 700 "$PLUGIN_DIR/data/jobs/processing"
set_mode_if_exists 700 "$PLUGIN_DIR/data/jobs/done"
set_mode_if_exists 700 "$PLUGIN_DIR/data/logs"
set_mode_if_exists 700 "$PLUGIN_DIR/data/lock"
set_mode_if_exists 700 "$PLUGIN_DIR/data/pids"
set_mode_if_exists 700 "$PLUGIN_DIR/data/cancel"
set_mode_if_exists 700 "$PLUGIN_DIR/data/uploads"
find "$PLUGIN_DIR" -type f -name '*.html' -exec chmod 755 {} + 2>/dev/null || true
find "$PLUGIN_DIR" -type f -name '*.raw' -exec chmod 755 {} + 2>/dev/null || true
find "$PLUGIN_DIR" -type f -name '*.css' -exec chmod 644 {} + 2>/dev/null || true
find "$PLUGIN_DIR/scripts" -type f -name '*.sh' -exec chmod 755 {} + 2>/dev/null || true
find "$PLUGIN_DIR" -type f -name '*.php' -exec chmod 640 {} + 2>/dev/null || true
set_mode_if_exists 644 "$PLUGIN_DIR/plugin.conf"
set_mode_if_exists 644 "$PLUGIN_DIR/plugin-settings.conf"
set_mode_if_exists 644 "$PLUGIN_DIR/php.ini"
if [ ! -f "$PLUGIN_DIR/data/secret.key" ]; then
tr -dc 'a-f0-9' </dev/urandom | head -c 64 > "$PLUGIN_DIR/data/secret.key" || true
fi
set_mode_if_exists 600 "$PLUGIN_DIR/data/secret.key"
touch "$PLUGIN_DIR/error.log" 2>/dev/null || true
if [ "$IS_ROOT" -eq 1 ]; then
chown diradmin:diradmin "$PLUGIN_DIR/error.log" 2>/dev/null || true
fi
set_mode_if_exists 640 "$PLUGIN_DIR/error.log"
bootstrap_custom_package_items
bootstrap_sudoers
bootstrap_worker_cron
bootstrap_adminer
activate_plugin
echo "da-postgresql: instalacja zakończona."
echo "Przypomnienie: zapisz pakiety i user.conf w DirectAdmin, aby odświeżyć custom package items."