From 3210cc265e2ef1b0b5cb2c2fc1dcb6c827898b63 Mon Sep 17 00:00:00 2001 From: xZero707 Date: Sat, 25 May 2024 02:25:33 +0200 Subject: [PATCH] Add support for WordPress init - automated install --- docker-compose.yml | 16 +++++- .../dependencies.d/init-install-wordpress | 0 .../dependencies.d/base | 0 .../dependencies.d/init-verify-wordpress | 0 .../s6-rc.d/init-install-wordpress/run | 56 +++++++++++++++++++ .../s6-rc.d/init-install-wordpress/type | 1 + .../s6-rc.d/init-install-wordpress/up | 1 + .../dependencies.d/init-install-wordpress | 0 .../dependencies.d/init-install-wordpress | 0 .../user/contents.d/init-install-wordpress | 0 10 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/crond/dependencies.d/init-install-wordpress create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/base create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/init-verify-wordpress create mode 100755 rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/run create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/type create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/up create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/init-webuser-permissions/dependencies.d/init-install-wordpress create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/unitd/dependencies.d/init-install-wordpress create mode 100644 rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-install-wordpress diff --git a/docker-compose.yml b/docker-compose.yml index b6ad5d8..3571595 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,12 +22,23 @@ x-wordpress-configuration-env: &wordpress-configuration-env $$_SERVER['REQUEST_SCHEME'] = 'https'; # $_SERVER definitions above are set to trick WP that it's accessed over HTTPS. This is typically useful only behind reverse proxy and should be avoided in production +x-wordpress-init-env: &wordpress-init-env + WORDPRESS_INIT_ENABLE: "true" + WORDPRESS_INIT_ADMIN_USER: admin + WORDPRESS_INIT_ADMIN_PASSWORD: admin + WORDPRESS_INIT_ADMIN_EMAIL: admin@example.com + WORDPRESS_INIT_SITE_TITLE: "Example.com" + WORDPRESS_INIT_SITE_URL: "https://www.example.com" + + networks: default: secrets: database_root_password: file: ./.secrets/database_root_password.txt + wordpress_database_password: + file: ./.secrets/wordpress_database_password.txt services: wordpress: @@ -44,7 +55,7 @@ services: - ./.secrets/wp-salts.env - ./.secrets/wp-database.env environment: - <<: [ *wordpress-configuration-env ] + <<: [ *wordpress-configuration-env, *wordpress-init-env ] CRON_ENABLED: "false" labels: # This configures traefik - if you have it. You also need to make sure that this service is in the same network with Traefik instance - "traefik.enable=true" @@ -108,12 +119,15 @@ services: PUID: 1000 PGID: 1000 MARIADB_INIT_DATABASES: wordpress + MARIADB_INIT_USERS: wordpress FILE__MARIADB_ROOT_PASSWORD: /run/secrets/database_root_password + FILE__MARIADB_USER_wordpress_PASSWORD: /run/secrets/wordpress_database_password FORCE_CONFIG_OVERWRITE: 1 volumes: - ./data/database/config:/config - ./data/database/data:/var/lib/mysql secrets: - database_root_password + - wordpress_database_password networks: default: diff --git a/rootfs/etc/s6-overlay/s6-rc.d/crond/dependencies.d/init-install-wordpress b/rootfs/etc/s6-overlay/s6-rc.d/crond/dependencies.d/init-install-wordpress new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/base b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/base new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/init-verify-wordpress b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/dependencies.d/init-verify-wordpress new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/run b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/run new file mode 100755 index 0000000..b282a33 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/run @@ -0,0 +1,56 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +await_database() { + # Settings + local interval="1" + local status + + # For loop + for i in {1..30} + do + # Check if database is reachable + status="$(wp core is-installed 2>&1)" + + # Check if status contains database connection (partial match) + if echo "${status}" | grep -q "Error establishing"; then + echo "Database is not reachable, retrying in ${interval} seconds [${i}/30]" + sleep "${interval}" + continue + fi + + return 0 + done +} + +# init-install-wordpress main +main() { + # This will prepend service name to all output from here + exec > >(while read -r line; do echo "[init-install-wordpress] ${line}"; done) 2>&1 + + if [ "${WORDPRESS_INIT_ENABLE:-false}" = "false" ]; then + echo "WordPress init is disabled" + return 0 + fi + + # Check if WordPress is already installed + if wp core is-installed 2>&1; then + echo "WordPress is already installed" + return 0 + fi + + if ! await_database; then + echo "Error: Database is not reachable" + return 1 + fi + + wp core install \ + --url="${WORDPRESS_INIT_URL:?}" \ + --title="${WORDPRESS_INIT_TITLE:-WordPress}" \ + --admin_user="${WORDPRESS_INIT_ADMIN_USER:?}" \ + --admin_password="${WORDPRESS_INIT_ADMIN_PASSWORD:?}" \ + --admin_email="${WORDPRESS_INIT_ADMIN_EMAIL:?}" \ + --skip-email + +} +main diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/type b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/type new file mode 100644 index 0000000..3d92b15 --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/type @@ -0,0 +1 @@ +oneshot \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/up b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/up new file mode 100644 index 0000000..5de8a4f --- /dev/null +++ b/rootfs/etc/s6-overlay/s6-rc.d/init-install-wordpress/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/init-install-wordpress/run \ No newline at end of file diff --git a/rootfs/etc/s6-overlay/s6-rc.d/init-webuser-permissions/dependencies.d/init-install-wordpress b/rootfs/etc/s6-overlay/s6-rc.d/init-webuser-permissions/dependencies.d/init-install-wordpress new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/unitd/dependencies.d/init-install-wordpress b/rootfs/etc/s6-overlay/s6-rc.d/unitd/dependencies.d/init-install-wordpress new file mode 100644 index 0000000..e69de29 diff --git a/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-install-wordpress b/rootfs/etc/s6-overlay/s6-rc.d/user/contents.d/init-install-wordpress new file mode 100644 index 0000000..e69de29