|
| 1 | +#!/bin/bash |
| 2 | +set -eo pipefail |
| 3 | + |
| 4 | +# Nginx server config |
| 5 | +dockerize -template /kool/default.tmpl:/etc/nginx/conf.d/default.conf |
| 6 | + |
| 7 | +# Run as current user |
| 8 | +CURRENT_USER=${ASUSER:-${UID:-0}} |
| 9 | + |
| 10 | +if [ ! -z "$CURRENT_USER" ] && [ "$CURRENT_USER" != "0" ]; then |
| 11 | + usermod -u $CURRENT_USER kool |
| 12 | +fi |
| 13 | + |
| 14 | +# user/group for Wordpress |
| 15 | +user=kool |
| 16 | +group=kool |
| 17 | +uid=$(id -u) |
| 18 | + |
| 19 | +if [ "$1" = "php-fpm" ] || [ "$1" = "supervisord" ]; then |
| 20 | + # Original Wordpress Entrypoint - fresh install if none exists |
| 21 | + if [ ! -e index.php ] && [ ! -e wp-includes/version.php ]; then |
| 22 | + # if the directory exists and WordPress doesn't appear to be installed AND the permissions of it are root:root, let's chown it (likely a Docker-created directory) |
| 23 | + if [ "$uid" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then |
| 24 | + chown "$user:$group" . |
| 25 | + fi |
| 26 | + |
| 27 | + echo >&2 "WordPress not found in $PWD - copying now..." |
| 28 | + if [ -n "$(find -mindepth 1 -maxdepth 1 -not -name wp-content)" ]; then |
| 29 | + echo >&2 "WARNING: $PWD is not empty! (copying anyhow)" |
| 30 | + fi |
| 31 | + sourceTarArgs=( |
| 32 | + --create |
| 33 | + --file - |
| 34 | + --directory /kool/wordpress |
| 35 | + --owner "$user" --group "$group" |
| 36 | + ) |
| 37 | + targetTarArgs=( |
| 38 | + --extract |
| 39 | + --file - |
| 40 | + ) |
| 41 | + if [ "$uid" != '0' ]; then |
| 42 | + # avoid "tar: .: Cannot utime: Operation not permitted" and "tar: .: Cannot change mode to rwxr-xr-x: Operation not permitted" |
| 43 | + targetTarArgs+=( --no-overwrite-dir ) |
| 44 | + fi |
| 45 | + # loop over "pluggable" content in the source, and if it already exists in the destination, skip it |
| 46 | + # https://github.com/docker-library/wordpress/issues/506 ("wp-content" persisted, "akismet" updated, WordPress container restarted/recreated, "akismet" downgraded) |
| 47 | + for contentPath in \ |
| 48 | + /kool/wordpress/.htaccess \ |
| 49 | + /kool/wordpress/wp-content/*/*/ \ |
| 50 | + ; do |
| 51 | + contentPath="${contentPath%/}" |
| 52 | + [ -e "$contentPath" ] || continue |
| 53 | + contentPath="${contentPath#/kool/wordpress/}" # "wp-content/plugins/akismet", etc. |
| 54 | + if [ -e "$PWD/$contentPath" ]; then |
| 55 | + echo >&2 "WARNING: '$PWD/$contentPath' exists! (not copying the WordPress version)" |
| 56 | + sourceTarArgs+=( --exclude "./$contentPath" ) |
| 57 | + fi |
| 58 | + done |
| 59 | + tar "${sourceTarArgs[@]}" . | tar "${targetTarArgs[@]}" |
| 60 | + chown -R "$user:$group" /app |
| 61 | + echo >&2 "Complete! WordPress has been successfully copied to $PWD" |
| 62 | + fi |
| 63 | + |
| 64 | + wpEnvs=( "${!WORDPRESS_@}" ) |
| 65 | + if [ ! -s wp-config.php ] && [ "${#wpEnvs[@]}" -gt 0 ]; then |
| 66 | + for wpConfigDocker in \ |
| 67 | + wp-config-docker.php \ |
| 68 | + /kool/wordpress/wp-config-docker.php \ |
| 69 | + ; do |
| 70 | + if [ -s "$wpConfigDocker" ]; then |
| 71 | + echo >&2 "No 'wp-config.php' found in $PWD, but 'WORDPRESS_...' variables supplied; copying '$wpConfigDocker' (${wpEnvs[*]})" |
| 72 | + # using "awk" to replace all instances of "put your unique phrase here" with a properly unique string (for AUTH_KEY and friends to have safe defaults if they aren't specified with environment variables) |
| 73 | + awk ' |
| 74 | + /put your unique phrase here/ { |
| 75 | + cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\ -f1" |
| 76 | + cmd | getline str |
| 77 | + close(cmd) |
| 78 | + gsub("put your unique phrase here", str) |
| 79 | + } |
| 80 | + { print } |
| 81 | + ' "$wpConfigDocker" > wp-config.php |
| 82 | + if [ "$uid" = '0' ]; then |
| 83 | + # attempt to ensure that wp-config.php is owned by the run user |
| 84 | + # could be on a filesystem that doesn't allow chown (like some NFS setups) |
| 85 | + chown "$user:$group" wp-config.php || true |
| 86 | + fi |
| 87 | + break |
| 88 | + fi |
| 89 | + done |
| 90 | + fi |
| 91 | +fi |
| 92 | + |
| 93 | +exec /kool/entrypoint "$@" |
0 commit comments