|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +if [ -n "$MYSQL_PORT_3306_TCP" ]; then |
| 5 | + if [ -z "$WORDPRESS_DB_HOST" ]; then |
| 6 | + WORDPRESS_DB_HOST='mysql' |
| 7 | + else |
| 8 | + echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found' |
| 9 | + echo >&2 " Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)" |
| 10 | + echo >&2 ' instead of the linked mysql container' |
| 11 | + fi |
| 12 | +fi |
| 13 | + |
| 14 | +if [ -z "$WORDPRESS_DB_HOST" ]; then |
| 15 | + echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables' |
| 16 | + echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db' |
| 17 | + echo >&2 ' with -e WORDPRESS_DB_HOST=hostname:port?' |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# if we're linked to MySQL, and we're using the root user, and our linked |
| 22 | +# container has a default "root" password set up and passed through... :) |
| 23 | +: ${WORDPRESS_DB_USER:=root} |
| 24 | +if [ "$WORDPRESS_DB_USER" = 'root' ]; then |
| 25 | + : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD} |
| 26 | +fi |
| 27 | +: ${WORDPRESS_DB_NAME:=wordpress} |
| 28 | + |
| 29 | +if [ -z "$WORDPRESS_DB_PASSWORD" ]; then |
| 30 | + echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable' |
| 31 | + echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?' |
| 32 | + echo >&2 |
| 33 | + echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)' |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +if ! [ -e index.php -a -e wp-includes/version.php ]; then |
| 38 | + echo >&2 "WordPress not found in $(pwd) - copying now..." |
| 39 | + if [ "$(ls -A)" ]; then |
| 40 | + echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!" |
| 41 | + ( set -x; ls -A; sleep 10 ) |
| 42 | + fi |
| 43 | + tar cf - --one-file-system -C /usr/src/wordpress . | tar xf - |
| 44 | + echo >&2 "Complete! WordPress has been successfully copied to $(pwd)" |
| 45 | + if [ ! -e .htaccess ]; then |
| 46 | + cat > .htaccess <<-'EOF' |
| 47 | + RewriteEngine On |
| 48 | + RewriteBase / |
| 49 | + RewriteRule ^index\.php$ - [L] |
| 50 | + RewriteCond %{REQUEST_FILENAME} !-f |
| 51 | + RewriteCond %{REQUEST_FILENAME} !-d |
| 52 | + RewriteRule . /index.php [L] |
| 53 | + EOF |
| 54 | + fi |
| 55 | +fi |
| 56 | + |
| 57 | +# TODO handle WordPress upgrades magically in the same way, but only if wp-includes/version.php's $wp_version is less than /usr/src/wordpress/wp-includes/version.php's $wp_version |
| 58 | + |
| 59 | +if [ ! -e wp-config.php ]; then |
| 60 | + awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP' |
| 61 | +// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact |
| 62 | +// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy |
| 63 | +if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { |
| 64 | + $_SERVER['HTTPS'] = 'on'; |
| 65 | +} |
| 66 | +
|
| 67 | +EOPHP |
| 68 | +fi |
| 69 | + |
| 70 | +set_config() { |
| 71 | + key="$1" |
| 72 | + value="$2" |
| 73 | + php_escaped_value="$(php -r 'var_export($argv[1]);' "$value")" |
| 74 | + sed_escaped_value="$(echo "$php_escaped_value" | sed 's/[\/&]/\\&/g')" |
| 75 | + sed -ri "s/((['\"])$key\2\s*,\s*)(['\"]).*\3/\1$sed_escaped_value/" wp-config.php |
| 76 | +} |
| 77 | + |
| 78 | +set_config 'DB_HOST' "$WORDPRESS_DB_HOST" |
| 79 | +set_config 'DB_USER' "$WORDPRESS_DB_USER" |
| 80 | +set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD" |
| 81 | +set_config 'DB_NAME' "$WORDPRESS_DB_NAME" |
| 82 | + |
| 83 | +# allow any of these "Authentication Unique Keys and Salts." to be specified via |
| 84 | +# environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY") |
| 85 | +UNIQUES=( |
| 86 | + AUTH_KEY |
| 87 | + SECURE_AUTH_KEY |
| 88 | + LOGGED_IN_KEY |
| 89 | + NONCE_KEY |
| 90 | + AUTH_SALT |
| 91 | + SECURE_AUTH_SALT |
| 92 | + LOGGED_IN_SALT |
| 93 | + NONCE_SALT |
| 94 | +) |
| 95 | +for unique in "${UNIQUES[@]}"; do |
| 96 | + eval unique_value=\$WORDPRESS_$unique |
| 97 | + if [ "$unique_value" ]; then |
| 98 | + set_config "$unique" "$unique_value" |
| 99 | + else |
| 100 | + # if not specified, let's generate a random value |
| 101 | + current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)" |
| 102 | + if [ "$current_set" = 'put your unique phrase here' ]; then |
| 103 | + set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)" |
| 104 | + fi |
| 105 | + fi |
| 106 | +done |
| 107 | + |
| 108 | +TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP' |
| 109 | +<?php |
| 110 | +// database might not exist, so let's try creating it (just to be safe) |
| 111 | +
|
| 112 | +$stderr = fopen('php://stderr', 'w'); |
| 113 | +
|
| 114 | +list($host, $port) = explode(':', $argv[1], 2); |
| 115 | +
|
| 116 | +$maxTries = 10; |
| 117 | +do { |
| 118 | + $mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port); |
| 119 | + if ($mysql->connect_error) { |
| 120 | + fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n"); |
| 121 | + --$maxTries; |
| 122 | + if ($maxTries <= 0) { |
| 123 | + exit(1); |
| 124 | + } |
| 125 | + sleep(3); |
| 126 | + } |
| 127 | +} while ($mysql->connect_error); |
| 128 | +
|
| 129 | +if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) { |
| 130 | + fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n"); |
| 131 | + $mysql->close(); |
| 132 | + exit(1); |
| 133 | +} |
| 134 | +
|
| 135 | +$mysql->close(); |
| 136 | +EOPHP |
| 137 | + |
| 138 | +chown -R www-data:www-data . |
| 139 | + |
| 140 | +exec "$@" |
0 commit comments