From 86b3d24af723432c41953bdc127a3c01eadb8813 Mon Sep 17 00:00:00 2001 From: Maya Baya Date: Sun, 22 May 2022 22:35:34 +0200 Subject: [PATCH] feat: Add support for plugins and website preinstallation --- Dockerfile | 11 +++- Makefile | 12 +++- README.md | 61 +++++++++++++++++++ container-files/entrypoint-riotkit.sh | 20 +++--- container-files/install-plugins-first-time.sh | 31 ++++++++++ 5 files changed, 121 insertions(+), 14 deletions(-) create mode 100644 container-files/install-plugins-first-time.sh diff --git a/Dockerfile b/Dockerfile index f7e0b2a..e5f00e1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,7 +17,13 @@ ENV AUTO_UPDATE_CRON="0 5 * * TUE" \ PHP_UPLOAD_MAX_FILESIZE="32M" \ PHP_MEMORY_LIMIT="128M" \ HEALTH_CHECK_ALLOWED_SUBNET="" \ - FORCE_UPGRADE=false + FORCE_UPGRADE=false \ + ENABLED_PLUGINS="" \ + WP_PREINSTALL=false \ + WP_SITE_URL=example.org \ + WP_SITE_ADMIN_LOGIN=admin \ + WP_SITE_ADMIN_PASSWORD=riotkit \ + WP_SITE_ADMIN_EMAIL=example@example.org # p2 (jinja2) RUN wget https://github.com/wrouesnel/p2cli/releases/download/r13/p2-linux-x86_64 -O /usr/bin/p2 && chmod +x /usr/bin/p2 @@ -73,7 +79,8 @@ RUN cat /etc/group | grep -v "www-data" > /etc/group.tmp \ # add entrypoints ADD container-files/entrypoint-riotkit.sh /usr/local/bin/ ADD container-files/entrypoint.sh /usr/local/bin/docker-entrypoint.sh -RUN chmod +x /usr/local/bin/entrypoint-riotkit.sh /usr/local/bin/docker-entrypoint.sh +ADD container-files/install-plugins-first-time.sh /usr/local/bin/install-plugins-first-time.sh +RUN chmod +x /usr/local/bin/entrypoint-riotkit.sh /usr/local/bin/docker-entrypoint.sh /usr/local/bin/install-plugins-first-time.sh # high user id number should be more compatible with OpenShift USER 65161 diff --git a/Makefile b/Makefile index ea4d210..30d41e1 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,17 @@ build: run: docker run --rm --name wp-riotkit -p 8090:8080 ${IMAGE} -test: test_installed +test: test_installed test_installs_plugins test_installed: docker run --rm --name wp-riotkit ${IMAGE} /bin/bash -c "echo 'Testing installation...'; test -f /var/www/riotkit/index.php && test -f /var/www/riotkit/wp-admin/index.php" + +test_installs_plugins: + docker rm -f wph-test-mariadb || true + docker network remove wph-test || true + docker network create wph-test + + # MariaDB (dependency) + docker run --rm -d --name wph-test-mariadb -e MARIADB_ROOT_PASSWORD=riotkit -e MARIADB_PASSWORD=riotkit -e MARIADB_USER=wp -e MARIADB_DATABASE=wp --network wph-test --network-alias mariadb.db.svc.cluster.local mariadb:10.7.3 && sleep 15 + docker run --rm --name wp-riotkit -e WP_PREINSTALL=true -e WORDPRESS_DB_HOST=mariadb.db.svc.cluster.local -e WORDPRESS_DB_PASSWORD=riotkit -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_NAME=wp -e ENABLED_PLUGINS="amazon-s3-and-cloudfront,classic-editor" --network wph-test ${IMAGE} /bin/bash -c "/bin/true" + docker rm -f wph-test-mariadb || true diff --git a/README.md b/README.md index 32dccc6..690a9ee 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,8 @@ Hardened version of official Wordpress container, with special support for Kuber - Non-root container - Free from Supervisord, using lightweight [multirun](https://github.com/nicolas-van/multirun) instead - Runtime NGINX and PHP configuration to adjust things like `memory_limit`, `error_reporting` or `post_max_size` +- Preconfiguration of admin account, website name and list of installed plugins +- Possible to upgrade Wordpress together with docker container Roadmap ------- @@ -77,6 +79,7 @@ services: AUTO_UPDATE_CRON: "0 5 * * SAT" XMLRPC_DISABLED: "true" DISABLE_DIRECT_CONTENT_PHP_EXECUTION: "false" + ENABLED_PLUGINS: "amazon-s3-and-cloudfront" # basic auth on administrative endpoints BASIC_AUTH_ENABLED: "true" @@ -91,6 +94,64 @@ services: ``` +Automating installation +----------------------- + +You can skip installation wizard by installing WordPress on container startup. +This container uses `wp-cli` to install WordPress and plugins allowing you to prepare a fully automated website. + +**Example configuration:** +```yaml +WP_PREINSTALL: true +WP_SITE_URL: example.org +WP_SITE_ADMIN_LOGIN: admin +WP_SITE_ADMIN_PASSWORD: riotkit +WP_SITE_ADMIN_EMAIL: example@example.org + +# NOTICE: The plugins will be installed right after WordPress installation is finished, +# this means that when `WP_PREINSTALL=false`, then the entrypoint will wait for user +# to complete the installation wizard, then the plugins will be installed +ENABLED_PLUGINS: "amazon-s3-and-cloudfront,classic-editor" +``` + +**Example log:** + +```bash + >> Checking if autoupdate should be scheduled... [scheduling at '0 5 * * TUE'] + >> Writing to basic auth file - /opt/htpasswd +Adding password for user riotkit + >> Rendering configuration files... + >> Installing Wordpress + >> UID=65161, GID=65161 +WordPress not found in /var/www/riotkit - copying now... +sending incremental file list +index.php +liveness.php +readiness.php +... +wp-includes/widgets/class-wp-widget-text.php + +sent 58,545,704 bytes received 54,312 bytes 39,066,677.33 bytes/sec +total size is 58,341,389 speedup is 1.00 +Complete! WordPress has been successfully copied to /var/www/riotkit +No 'wp-config.php' found in /var/www/riotkit, but 'WORDPRESS_...' variables supplied; copying 'wp-config-docker.php' (WORDPRESS_DB_HOST WORDPRESS_DB_NAME WORDPRESS_DB_PASSWORD WORDPRESS_DB_USER) +Success: WordPress installed successfully. + >> Installing plugin 'amazon-s3-and-cloudfront' +Installing WP Offload Media Lite for Amazon S3, DigitalOcean Spaces, and Google Cloud Storage (2.6.2) +Downloading installation package from https://downloads.wordpress.org/plugin/amazon-s3-and-cloudfront.2.6.2.zip... +Unpacking the package... +Installing the plugin... +Plugin installed successfully. +Success: Installed 1 of 1 plugins. + >> Installing plugin 'classic-editor' +Installing Classic Editor (1.6.2) +Downloading installation package from https://downloads.wordpress.org/plugin/classic-editor.1.6.2.zip... +Unpacking the package... +Installing the plugin... +Plugin installed successfully. +Success: Installed 1 of 1 plugins. +``` + From authors ------------ diff --git a/container-files/entrypoint-riotkit.sh b/container-files/entrypoint-riotkit.sh index f9e76cf..0c36c74 100644 --- a/container-files/entrypoint-riotkit.sh +++ b/container-files/entrypoint-riotkit.sh @@ -3,18 +3,15 @@ set -eo pipefail setupWP() { - # Warning: HACK below :) - # mock php-fpm to not start it immediately by WordPress entrypoint - echo " >> Mock php-fpm" - mv /usr/local/sbin/php-fpm /usr/local/sbin/php-fpm.bckp - ln -s /bin/bash /usr/local/sbin/php-fpm - echo " >> Installing Wordpress" - /usr/local/bin/docker-entrypoint.sh php-fpm || exit 1 + /usr/local/bin/docker-entrypoint.sh || exit 1 +} - echo " >> Cleaning up the mock" - rm /usr/local/sbin/php-fpm - mv /usr/local/sbin/php-fpm.bckp /usr/local/sbin/php-fpm +preinstallWP() { + if [[ "${WP_PREINSTALL}" == "true" ]]; then + wp core install --url=${WP_SITE_URL} --title=${WP_SITE_TITLE} --admin_user=${WP_SITE_ADMIN_LOGIN} --admin_password=${WP_SITE_ADMIN_PASSWORD} --admin_email=${WP_SITE_ADMIN_EMAIL} + /usr/local/bin/install-plugins-first-time.sh no-wait + fi } scheduleAutoupdate() { @@ -46,6 +43,7 @@ scheduleAutoupdate setupBasicAuth setupConfiguration setupWP +preinstallWP # Allows to pass own CMD # Also allows to execute tests on the container @@ -54,4 +52,4 @@ if [[ "${1}" == "exec" ]] || [[ "${1}" == "sh" ]] || [[ "${1}" == "bash" ]] || [ exec "$@" fi -exec multirun "php-fpm" "nginx -c /etc/nginx/nginx.conf" "crond -f -d 6" +exec multirun "php-fpm" "nginx -c /etc/nginx/nginx.conf" "crond -f -d 6" "/usr/local/bin/install-plugins-first-time.sh" diff --git a/container-files/install-plugins-first-time.sh b/container-files/install-plugins-first-time.sh new file mode 100644 index 0000000..7c1eede --- /dev/null +++ b/container-files/install-plugins-first-time.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +installPlugins() { + IFS=, read -ra plugins <<< "${ENABLED_PLUGINS}" + for plugin in "${plugins[@]}"; do + echo " >> Installing plugin '${plugin}'" + wp plugin install "${plugin}" || return 1 + done +} + +if [[ "${1}" == "no-wait" ]]; then + installPlugins +else + while [[ ! -f /var/www/riotkit/wp-content/.plugins-installed ]]; do + if wp core is-installed; then + if installPlugins; then + echo " >> Plugins installed" + break + else + echo " !!! Plugins installation failed" + fi + else + echo " ... Waiting for Wordpress to be installed" + fi + + sleep 20 + done + + echo ">> Fuckwork mode on" + sleep 999999999 +fi