Skip to content

Commit

Permalink
feat: Add support for plugins and website preinstallation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maya Baya committed May 22, 2022
1 parent 26e17a6 commit 86b3d24
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 14 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
[email protected]

# 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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down Expand Up @@ -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"
Expand All @@ -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: [email protected]

# 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
------------
Expand Down
20 changes: 9 additions & 11 deletions container-files/entrypoint-riotkit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -46,6 +43,7 @@ scheduleAutoupdate
setupBasicAuth
setupConfiguration
setupWP
preinstallWP

# Allows to pass own CMD
# Also allows to execute tests on the container
Expand All @@ -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"
31 changes: 31 additions & 0 deletions container-files/install-plugins-first-time.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 86b3d24

Please sign in to comment.