250 lines
8.5 KiB
Bash
Executable File
250 lines
8.5 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
|
|
PLUGIN_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
|
SETTINGS_FILE="${ALT_MYSQL_SETTINGS_FILE:-$PLUGIN_DIR/plugin-settings.conf}"
|
|
CPIF="${ALT_MYSQL_CUSTOM_PACKAGE_ITEMS_CONF:-/usr/local/directadmin/data/admin/custom_package_items.conf}"
|
|
SUDOERS_FILE="/etc/sudoers.d/directadmin-alt-mysql-plugin"
|
|
CRON_FILE="/etc/cron.d/alt-mysql-jobs"
|
|
IS_ROOT=0
|
|
if [ "${ALT_MYSQL_TEST_ASSUME_ROOT:-0}" = "1" ] || [ "$(id -u)" -eq 0 ]; then
|
|
IS_ROOT=1
|
|
fi
|
|
|
|
read_plugin_setting() {
|
|
local key="$1"
|
|
local default="$2"
|
|
local line value
|
|
|
|
[ -r "$SETTINGS_FILE" ] || {
|
|
printf '%s\n' "$default"
|
|
return 0
|
|
}
|
|
|
|
line="$(grep -E "^${key}=" "$SETTINGS_FILE" | tail -n 1 || true)"
|
|
if [ -z "$line" ]; then
|
|
printf '%s\n' "$default"
|
|
return 0
|
|
fi
|
|
|
|
value="${line#*=}"
|
|
value="${value%%#*}"
|
|
value="${value%\"}"
|
|
value="${value#\"}"
|
|
value="${value%\'}"
|
|
value="${value#\'}"
|
|
value="$(printf '%s' "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
|
|
printf '%s\n' "${value:-$default}"
|
|
}
|
|
|
|
setting_enabled() {
|
|
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
|
|
1|yes|true|on) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
always_enabled() {
|
|
setting_enabled "$(read_plugin_setting always_enabled 1)"
|
|
}
|
|
|
|
remove_custom_package_item() {
|
|
local key="$1"
|
|
local tmp
|
|
|
|
[ -f "$CPIF" ] || return 0
|
|
tmp="$(mktemp)"
|
|
grep -Ev "^${key}=" "$CPIF" > "$tmp" || true
|
|
cat "$tmp" > "$CPIF"
|
|
rm -f "$tmp"
|
|
}
|
|
|
|
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 "mysql: warning: uruchom jako root, aby zaktualizować $CPIF" >&2
|
|
return 0
|
|
fi
|
|
|
|
touch "$CPIF"
|
|
|
|
remove_custom_package_item mysql_enabled
|
|
remove_custom_package_item mysql
|
|
remove_custom_package_item umysql
|
|
remove_custom_package_item mysql_max_databases
|
|
remove_custom_package_item mysql_unlimited
|
|
remove_custom_package_item umysql_max_databases
|
|
|
|
if always_enabled; then
|
|
set_permissions diradmin:diradmin 640 "$CPIF"
|
|
return 0
|
|
fi
|
|
|
|
cat >> "$CPIF" <<'EOF'
|
|
mysql_enabled=type=checkbox&string=Enable mysql&desc=Zezwól na dostęp do baz danych MySQL&default=no
|
|
mysql=type=text&string=Bazy MySQL&desc=&default=5
|
|
umysql=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%27mysql%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%27mysql%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 "mysql: warning: uruchom jako root, aby dodać sudoers dla synchronizacji hostów MySQL" >&2
|
|
return 0
|
|
fi
|
|
|
|
cat > "$SUDOERS_FILE" <<SUDO
|
|
# DirectAdmin alt-mysql plugin
|
|
diradmin ALL=(root) NOPASSWD: /usr/local/directadmin/plugins/alt-mysql/scripts/setup/mysql_host_sync.sh
|
|
SUDO
|
|
|
|
chmod 440 "$SUDOERS_FILE"
|
|
}
|
|
|
|
bootstrap_worker_cron() {
|
|
if [ "$IS_ROOT" -ne 1 ]; then
|
|
echo "mysql: warning: uruchom jako root, aby dodać cron workera backup/restore" >&2
|
|
return 0
|
|
fi
|
|
|
|
touch "$CRON_FILE"
|
|
if ! grep -Fq '/usr/local/directadmin/plugins/alt-mysql/scripts/worker.sh' "$CRON_FILE"; then
|
|
echo '* * * * * root /usr/local/directadmin/plugins/alt-mysql/scripts/worker.sh >/dev/null 2>&1' >> "$CRON_FILE"
|
|
fi
|
|
chmod 644 "$CRON_FILE" 2>/dev/null || true
|
|
}
|
|
|
|
bootstrap_phpmyadmin() {
|
|
local phpmyadmin_setup="$PLUGIN_DIR/scripts/setup/phpmyadmin_private_install.sh"
|
|
|
|
if [ "$IS_ROOT" -ne 1 ]; then
|
|
echo "mysql: warning: uruchom jako root, aby przygotować integrację phpMyAdmin." >&2
|
|
return 0
|
|
fi
|
|
|
|
if [ ! -f "$phpmyadmin_setup" ]; then
|
|
echo "mysql: error: brak skryptu integracji phpMyAdmin: $phpmyadmin_setup" >&2
|
|
return 1
|
|
fi
|
|
|
|
bash "$phpmyadmin_setup"
|
|
}
|
|
|
|
bootstrap_da_integration() {
|
|
local da_setup="$PLUGIN_DIR/scripts/setup/da_integration_install.sh"
|
|
|
|
if [ "$IS_ROOT" -ne 1 ]; then
|
|
echo "mysql: warning: uruchom jako root, aby zainstalować hooki integracji DirectAdmin backup/restore." >&2
|
|
return 0
|
|
fi
|
|
|
|
if [ ! -f "$da_setup" ]; then
|
|
echo "mysql: error: brak skryptu integracji DirectAdmin: $da_setup" >&2
|
|
return 1
|
|
fi
|
|
|
|
bash "$da_setup" install
|
|
}
|
|
|
|
bootstrap_custombuild_hooks() {
|
|
local custombuild_setup="$PLUGIN_DIR/scripts/setup/custombuild_hooks_install.sh"
|
|
|
|
if [ "$IS_ROOT" -ne 1 ]; then
|
|
echo "mysql: warning: uruchom jako root, aby zainstalować hooki CustomBuild po aktualizacjach Roundcube/phpMyAdmin." >&2
|
|
return 0
|
|
fi
|
|
|
|
if [ ! -f "$custombuild_setup" ]; then
|
|
echo "mysql: error: brak skryptu hooków CustomBuild: $custombuild_setup" >&2
|
|
return 1
|
|
fi
|
|
|
|
bash "$custombuild_setup" install
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if [ "${ALT_MYSQL_TEST_ONLY_CUSTOM_PACKAGE_ITEMS:-0}" = "1" ]; then
|
|
bootstrap_custom_package_items
|
|
exit 0
|
|
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 "mysql: 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_phpmyadmin
|
|
bootstrap_da_integration
|
|
bootstrap_custombuild_hooks
|
|
activate_plugin
|
|
|
|
echo "mysql: instalacja zakończona."
|
|
echo "Przypomnienie: zapisz pakiety i user.conf w DirectAdmin, aby odświeżyć custom package items."
|