diff --git a/.gitignore b/.gitignore index e43b0f9..89d61a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ .DS_Store +manta.id_rsa +manta.id_rsa.pub +_env diff --git a/Dockerfile b/Dockerfile index fc3f83e..df940c2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,14 +4,20 @@ RUN a2enmod rewrite # install the PHP extensions we need, and other packages RUN apt-get update \ - && apt-get install -y \ + && apt-get install -y --no-install-recommends \ less \ libpng12-dev \ libjpeg-dev \ unzip \ + curl \ + nfs-common \ + libmemcached-dev \ + vim \ && rm -rf /var/lib/apt/lists/* \ + && pecl install memcached \ && docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr \ - && docker-php-ext-install gd mysqli opcache + && docker-php-ext-install gd mysqli opcache \ + && docker-php-ext-enable memcached # set recommended PHP.ini settings # see https://secure.php.net/manual/en/opcache.installation.php @@ -24,18 +30,20 @@ RUN { \ echo 'opcache.enable_cli=1'; \ } > /usr/local/etc/php/conf.d/opcache-recommended.ini -COPY /bin/docker-entrypoint.sh /entrypoint.sh -# The Containerbuddy helper/glue scripts for this specific app -COPY containerbuddy /opt/containerbuddy +# The our helper/glue scripts and configuration for this specific app +COPY bin /usr/local/bin +COPY etc /etc -# Install Containerbuddy -# Releases at https://github.com/joyent/containerbuddy/releases -ENV CONTAINERBUDDY_VERSION 0.1.1 -ENV CONTAINERBUDDY_SHA1 3163e89d4c95b464b174ba31733946ca247e068e -RUN curl --retry 7 -Lso /tmp/containerbuddy.tar.gz "https://github.com/joyent/containerbuddy/releases/download/${CONTAINERBUDDY_VERSION}/containerbuddy-${CONTAINERBUDDY_VERSION}.tar.gz" \ - && echo "${CONTAINERBUDDY_SHA1} /tmp/containerbuddy.tar.gz" | sha1sum -c \ - && tar zxf /tmp/containerbuddy.tar.gz -C /opt/containerbuddy \ +# Add Containerbuddy and its configuration +ENV CONTAINERBUDDY_VER 1.3.0 +ENV CONTAINERBUDDY file:///etc/containerbuddy.json + +RUN export CONTAINERBUDDY_CHECKSUM=c25d3af30a822f7178b671007dcd013998d9fae1 \ + && curl -Lso /tmp/containerbuddy.tar.gz \ + "https://github.com/joyent/containerbuddy/releases/download/${CONTAINERBUDDY_VER}/containerbuddy-${CONTAINERBUDDY_VER}.tar.gz" \ + && echo "${CONTAINERBUDDY_CHECKSUM} /tmp/containerbuddy.tar.gz" | sha1sum -c \ + && tar zxf /tmp/containerbuddy.tar.gz -C /usr/local/bin \ && rm /tmp/containerbuddy.tar.gz # Install Consul template @@ -47,44 +55,43 @@ RUN curl --retry 7 -Lso /tmp/consul-template.zip "https://releases.hashicorp.com && unzip /tmp/consul-template.zip -d /usr/local/bin \ && rm /tmp/consul-template.zip +# Make the WP uploads directory writeable by the web server +#RUN chown -R www-data:www-data /var/www/html/content/uploads + +# install wp-cli, http://wp-cli.org +ENV WP_CLI_CONFIG_PATH /var/www/html/wp-cli.yml +RUN curl -Ls -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ + && chmod +x wp-cli.phar \ + && mv wp-cli.phar /usr/local/bin/wp \ + && wp --info --allow-root + # copy the WordPress skeleton from this repo into the container # this includes any themes and/or plugins we've added to the content/themes and content/plugins, etc, directories. COPY /var/www/html /var/www/html -# Make the WP uploads directory writeable by the web server -RUN chown -R www-data:www-data /var/www/html/content/uploads - -# install WordPress -# Releases at https://wordpress.org/download/release-archive/ -ENV WORDPRESS_VERSION 4.4.1 -ENV WORDPRESS_SHA1 89bcc67a33aecb691e879c818d7e2299701f30e7 -# upstream tarballs include ./wordpress/ so this gives us /var/www/html/wordpress -RUN curl -o wordpress.tar.gz -SL https://wordpress.org/wordpress-${WORDPRESS_VERSION}.tar.gz \ - && echo "${WORDPRESS_SHA1} *wordpress.tar.gz" | sha1sum -c - \ - && tar -xzf wordpress.tar.gz -C /var/www/html \ - && rm wordpress.tar.gz + +ENV WORDPRESS_VERSION 4.4.2 +# install WordPress via wp-cli & copy the default themes to our content dir +RUN wp --allow-root core download --version=${WORDPRESS_VERSION} \ + && cp -r /var/www/html/wordpress/wp-content/themes/* /var/www/html/content/themes/ + # install HyperDB, https://wordpress.org/plugins/hyperdb # Releases at https://wordpress.org/plugins/hyperdb/developers/ , though no SHA1 fingerprints are published ENV HYPERDB_VERSION 1.1 -RUN curl -Lo /var/www/html/hyperdb.zip https://downloads.wordpress.org/plugin/hyperdb.${HYPERDB_VERSION}.zip \ +RUN curl -Ls -o /var/www/html/hyperdb.zip https://downloads.wordpress.org/plugin/hyperdb.${HYPERDB_VERSION}.zip \ && unzip hyperdb.zip \ && chown -R www-data:www-data /var/www/html/hyperdb \ && mv hyperdb/db.php /var/www/html/content/. \ && rm -rf /var/www/html/hyperdb.zip /var/www/html/hyperdb \ - && touch /var/www/html/wordpress/db-config.php \ - && ln -s /var/www/html/wordpress/db-config.php /var/www/html/. + && touch /var/www/html/content/db-config.php + +# install ztollman's object-cache.php or object caching to memcached +RUN curl -Ls -o /var/www/html/content/object-cache.php https://raw.githubusercontent.com/tollmanz/wordpress-pecl-memcached-object-cache/master/object-cache.php -# install wp-cli, http://wp-cli.org -ENV WP_CLI_CONFIG_PATH /var/www/html/wp-cli.yml -RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ - && chmod +x wp-cli.phar \ - && mv wp-cli.phar /usr/local/bin/wp \ - && wp --info --allow-root # the volume is defined after we install everything VOLUME /var/www/html -# grr, ENTRYPOINT resets CMD now -ENTRYPOINT ["/entrypoint.sh"] -CMD ["apache2-foreground"] +CMD ["/usr/local/bin/containerbuddy", \ + "apache2-foreground"] diff --git a/_env.example b/_env.example new file mode 100644 index 0000000..d632c20 --- /dev/null +++ b/_env.example @@ -0,0 +1,41 @@ +# Environment variables for MySQL service +MYSQL_USER=wpuser +MYSQL_PASSWORD=userpassword +MYSQL_REPL_USER=repluser +MYSQL_REPL_PASSWORD=replpassword +MYSQL_DATABASE=test + +# Environment variables for backups to Manta +MANTA_URL= +MANTA_USER= +MANTA_SUBUSER= +MANTA_ROLE= +MANTA_KEY_ID= +MANTA_PRIVATE_KEY= +MANTA_BUCKET= + +WORDPRESS_VERSION=4.1 +# please include the scheme http:// or https:// in the URL variable +WORDPRESS_URL=http://localhost +WORDPRESS_SITE_TITLE=test site +WORDPRESS_ADMIN_EMAIL=username@domain.com +WORDPRESS_ADMIN_USER=username +WORDPRESS_ADMIN_PASSWORD=password +WORDPRESS_ACTIVE_THEME=theme +WORDPRESS_CACHE_KEY_SALT=some-unique-string +#WORDPRESS_TEST_DATA=False + +# Wordpress unique salts - these must be unique for your install to ensure +# login security. visit the following link to generate a set for your site and +# include them here +# https://api.wordpress.org/secret-key/1.1/salt/ +WORDPRESS_AUTH_KEY= +WORDPRESS_SECURE_AUTH_KEY= +WORDPRESS_LOGGED_IN_KEY= +WORDPRESS_NONCE_KEY= +WORDPRESS_AUTH_SALT= +WORDPRESS_SECURE_AUTH_SALT= +WORDPRESS_LOGGED_IN_SALT= +WORDPRESS_NONCE_SALT= + +CONSUL=consul diff --git a/bin/docker-entrypoint.sh b/bin/docker-entrypoint.sh deleted file mode 100755 index fd58642..0000000 --- a/bin/docker-entrypoint.sh +++ /dev/null @@ -1,164 +0,0 @@ -#!/bin/bash -set -e - -if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then - if [ -n "$MYSQL_PORT_3306_TCP" ]; then - if [ -z "$WORDPRESS_DB_HOST" ]; then - WORDPRESS_DB_HOST='mysql' - else - echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found' - echo >&2 " Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)" - echo >&2 ' instead of the linked mysql container' - fi - fi - - if [ -z "$WORDPRESS_DB_HOST" ]; then - echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables' - echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db' - echo >&2 ' with -e WORDPRESS_DB_HOST=hostname:port?' - exit 1 - fi - - # if we're linked to MySQL and thus have credentials already, let's use them - : ${WORDPRESS_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}} - if [ "$WORDPRESS_DB_USER" = 'root' ]; then - : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD} - fi - : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD} - : ${WORDPRESS_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-wordpress}} - - if [ -z "$WORDPRESS_DB_PASSWORD" ]; then - echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable' - echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?' - echo >&2 - echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)' - exit 1 - fi - - if ! [ -e index.php -a -e wp-includes/version.php ]; then - echo >&2 "WordPress not found in $(pwd) - copying now..." - if [ "$(ls -A)" ]; then - echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!" - ( set -x; ls -A; sleep 10 ) - fi - tar cf - --one-file-system -C /usr/src/wordpress . | tar xf - - echo >&2 "Complete! WordPress has been successfully copied to $(pwd)" - if [ ! -e .htaccess ]; then - # NOTE: The "Indexes" option is disabled in the php:apache base image - cat > .htaccess <<-'EOF' - # BEGIN WordPress - - RewriteEngine On - RewriteBase / - RewriteRule ^index\.php$ - [L] - RewriteCond %{REQUEST_FILENAME} !-f - RewriteCond %{REQUEST_FILENAME} !-d - RewriteRule . /index.php [L] - - # END WordPress - EOF - chown www-data:www-data .htaccess - fi - fi - - # 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 - - if [ ! -e wp-config.php ]; then - awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP' -// If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact -// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy -if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { - $_SERVER['HTTPS'] = 'on'; -} - -EOPHP - chown www-data:www-data wp-config.php - fi - - # see http://stackoverflow.com/a/2705678/433558 - sed_escape_lhs() { - echo "$@" | sed 's/[]\/$*.^|[]/\\&/g' - } - sed_escape_rhs() { - echo "$@" | sed 's/[\/&]/\\&/g' - } - php_escape() { - php -r 'var_export((string) $argv[1]);' "$1" - } - set_config() { - key="$1" - value="$2" - regex="(['\"])$(sed_escape_lhs "$key")\2\s*," - if [ "${key:0:1}" = '$' ]; then - regex="^(\s*)$(sed_escape_lhs "$key")\s*=" - fi - sed -ri "s/($regex\s*)(['\"]).*\3/\1$(sed_escape_rhs "$(php_escape "$value")")/" wp-config.php - } - - set_config 'DB_HOST' "$WORDPRESS_DB_HOST" - set_config 'DB_USER' "$WORDPRESS_DB_USER" - set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD" - set_config 'DB_NAME' "$WORDPRESS_DB_NAME" - - # allow any of these "Authentication Unique Keys and Salts." to be specified via - # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY") - UNIQUES=( - AUTH_KEY - SECURE_AUTH_KEY - LOGGED_IN_KEY - NONCE_KEY - AUTH_SALT - SECURE_AUTH_SALT - LOGGED_IN_SALT - NONCE_SALT - ) - for unique in "${UNIQUES[@]}"; do - eval unique_value=\$WORDPRESS_$unique - if [ "$unique_value" ]; then - set_config "$unique" "$unique_value" - else - # if not specified, let's generate a random value - current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)" - if [ "$current_set" = 'put your unique phrase here' ]; then - set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)" - fi - fi - done - - if [ "$WORDPRESS_TABLE_PREFIX" ]; then - set_config '$table_prefix' "$WORDPRESS_TABLE_PREFIX" - fi - - TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP' -connect_error) { - fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n"); - --$maxTries; - if ($maxTries <= 0) { - exit(1); - } - sleep(3); - } -} while ($mysql->connect_error); - -if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) { - fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n"); - $mysql->close(); - exit(1); -} - -$mysql->close(); -EOPHP -fi - -exec "$@" - diff --git a/bin/health.sh b/bin/health.sh new file mode 100755 index 0000000..4ce9094 --- /dev/null +++ b/bin/health.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +/usr/local/bin/wp --allow-root core is-installed \ + && /usr/local/bin/wp --allow-root option get siteurl \ + && /usr/bin/curl --fail -s -o /dev/null http://localhost/ diff --git a/bin/onchange-db.sh b/bin/onchange-db.sh new file mode 100755 index 0000000..aebb797 --- /dev/null +++ b/bin/onchange-db.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +/usr/local/bin/onchange-wp-config.sh + +consul-template \ + -once \ + -dedup \ + -consul ${CONSUL}:8500 \ + -template "/var/www/html/consul-templates/db-config.php.ctmpl:/var/www/html/content/db-config.php" diff --git a/bin/onchange-memcached.sh b/bin/onchange-memcached.sh new file mode 100755 index 0000000..6f2cd90 --- /dev/null +++ b/bin/onchange-memcached.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +consul-template \ + -once \ + -dedup \ + -consul ${CONSUL}:8500 \ + -template "/var/www/html/consul-templates/memcached-config.php.ctmpl:/var/www/html/memcached-config.php" diff --git a/bin/onchange-nfs.sh b/bin/onchange-nfs.sh new file mode 100755 index 0000000..7c7ab6a --- /dev/null +++ b/bin/onchange-nfs.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +if [[ `curl -s ${CONSUL}:8500/v1/health/state/passing | grep nfs` ]] +then + echo "nfs is healthy, mounting uploads directory...." + mount -t nfs -v -o nolock,vers=3 nfs:/exports /var/www/html/content/uploads + echo "removing no-uploads.php mu-plugin" + rm /var/www/html/content/mu-plugins/no-uploads.php + echo "adding 'upload_files' capability back to default roles" + # only these roles have 'upload_files' cap by default + for role in administrator editor author + do + if [ "$role" != 'role' ] + then + wp --allow-root cap add ${role} upload_files + fi + done +else + echo "nfs is not healthly, umounting uploads directory..." + umount -f -l /var/www/html/content/uploads + echo "creating mu-plugin for NFS error in wp-admin" + echo " + +
+

+
+ + /var/www/html/content/mu-plugins/no-uploads.php + + echo "removing 'upload_files' capability from all roles..." + for role in $(wp --allow-root role list --fields=role --format=csv) + do + if [ "$role" != 'role' ] + then + wp --allow-root cap remove ${role} upload_files + fi + done +fi diff --git a/bin/onchange-wp-config.sh b/bin/onchange-wp-config.sh new file mode 100755 index 0000000..6e38e58 --- /dev/null +++ b/bin/onchange-wp-config.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# The WordPress config file +consul-template \ + -once \ + -dedup \ + -consul ${CONSUL}:8500 \ + -template "/var/www/html/consul-templates/wp-config.php.ctmpl:/var/www/html/wp-config.php" diff --git a/bin/prestart.sh b/bin/prestart.sh new file mode 100755 index 0000000..c2204d4 --- /dev/null +++ b/bin/prestart.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Refresh all config files in order +# +# This script is typically called once at the container start, but +# it can be called manually if WP config details must be changed + +# The database and memcached config files are separate to avoid collisions +# if their backends' onChange handlers are triggered simultaneously +echo "******running preStart script*********" + +until [[ `curl -s ${CONSUL}:8500/v1/health/state/passing | grep mysql-primary` ]] +do + echo "mysql-primary not healthly...." + sleep 5 +done + +until [[ `curl -s ${CONSUL}:8500/v1/health/state/passing | grep nfs` ]] +do + echo "no healthly nfs server avaliable yet...." + sleep 5 +done + +echo "mysql-primary and nfs are now healthly, moving on..." + +/usr/local/bin/onchange-db.sh +/usr/local/bin/onchange-memcached.sh +/usr/local/bin/onchange-nfs.sh + +# The WordPress config file +/usr/local/bin/onchange-wp-config.sh + +if $(wp --allow-root core is-installed) +then + echo "WP is installed" + # run update-db to ensure the database schema is up to date in case the WP version was upgraded in the Docker image + wp --allow-root core update-db +else + echo "WP is NOT installed" + echo "installing now...." + # TODO: check WORDPRESS_URL to ensure it has http:// or https:// at the beginning, if not put it in + wp --allow-root core install --url=$WORDPRESS_URL --title="$WORDPRESS_SITE_TITLE" --admin_user=$WORDPRESS_ADMIN_USER --admin_password=$WORDPRESS_ADMIN_PASSWORD --admin_email=$WORDPRESS_ADMIN_EMAIL --skip-email + # update siteurl to work with our directory structure + # wp option update for siteurl REQUIRES http://, need to determine will we handle that here + # or ask for it in the _env file or test for it above + wp --allow-root option update siteurl `wp --allow-root option get siteurl`/wordpress + if [ $WORDPRESS_TEST_DATA ] + then + echo "installing WP test content" + wp --allow-root plugin install wordpress-importer --activate + curl -OL https://raw.githubusercontent.com/manovotny/wptest/master/wptest.xml + wp --allow-root import wptest.xml --authors=create + wp --allow-root plugin uninstall wordpress-importer --deactivate + rm wptest.xml + fi +fi diff --git a/common-compose.yml b/common-compose.yml new file mode 100644 index 0000000..29884d9 --- /dev/null +++ b/common-compose.yml @@ -0,0 +1,70 @@ +wordpress: + build: . + privileged: true + env_file: _env + environment: + - CONTAINERBUDDY=file:///etc/containerbuddy.json + +nfs: + image: autopilot-nfs + privileged: true + mem_limit: 256m + restart: always + labels: + - triton.cns.services=nfs + expose: + - 111 + - 1892 + - 2049 + environment: + - CONSUL=consul + +# MySQL designed for container-native deployment on Joyent's Triton platform. + +mysql: + image: 0x74696d/triton-mysql:latest + mem_limit: 4g + env_file: _env + environment: + - CONTAINERBUDDY=file:///etc/containerbuddy.json + - MANTA_PRIVATE_KEY + +memcached: + image: autopilot-memcached + mem_limit: 512m + env_file: _env + environment: + - CONTAINERBUDDY=file:///opt/containerbuddy/config.json + ports: + - 11211 + +nginx: + image: 0x74696d/triton-nginx + mem_limit: 512m + ports: + - 80 + env_file: _env + environment: + - NGINX_CONF + command: > + /opt/containerbuddy/containerbuddy -config '{"consul":"{{.CONSUL}}:8500","preStart":"/opt/containerbuddy/reload.sh preStart","services":[{"name":"nginx","port": 80,"publicIp": true,"health":"/usr/bin/curl --fail -s http://localhost/health.txt","poll":10,"ttl":25}],"backends":[{"name":"wordpress","poll":5,"onChange":"/opt/containerbuddy/reload.sh"}]}' + nginx -g "daemon off;" + +# --------------------------------------------------- +# Consul as a service discovery tier + +consul: + image: progrium/consul:latest + command: -server -bootstrap -ui-dir /ui + mem_limit: 128m + ports: + - 8500 + expose: + - 53 + - 8300 + - 8301 + - 8302 + - 8400 + - 8500 + dns: + - 127.0.0.1 diff --git a/containerbuddy/config.json b/containerbuddy/config.json deleted file mode 100644 index 4212ae0..0000000 --- a/containerbuddy/config.json +++ /dev/null @@ -1,25 +0,0 @@ -{ - "consul": "consul:8500", - "services": [ - { - "name": "wordpress", - "port": 80, - "publicIp": true, - "health": "/usr/bin/curl --fail -s http://localhost/health", - "poll": 10, - "ttl": 25 - } - ], - "backends": [ - { - "name": "mysql", - "poll": 7, - "onChange": "/opt/containerbuddy/wp-config.sh" - }, - { - "name": "memcached", - "poll": 11, - "onChange": "/opt/containerbuddy/wp-config.sh" - } - ] -} \ No newline at end of file diff --git a/containerbuddy/health.sh b/containerbuddy/health.sh deleted file mode 100644 index e8e5e64..0000000 --- a/containerbuddy/health.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -/usr/local/bin/wp --allow-root is-installed \ - && /usr/local/bin/wp --allow-root option get site url \ - && /usr/bin/curl --fail -s -o /dev/null http://localhost/ \ No newline at end of file diff --git a/containerbuddy/onstart.sh b/containerbuddy/onstart.sh deleted file mode 100644 index 5aad641..0000000 --- a/containerbuddy/onstart.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash - -# Refresh all config files in order -# -# This script is typically called once at the container start, but -# it can be called manually if WP config details must be changed - -# The database and memcached config files are separate to avoid collisions -# if their backends' onChange handlers are triggered simultaneously -/opt/containerbuddy/reload-db.sh -/opt/containerbuddy/reload-memcached.sh - -# The WordPress config file -consul-template \ - -once \ - -dedup \ - -consul consul:8500 \ - -template "/var/www/html/wp-config.php.ctmpl:/var/www/html/wp-config.php" - -# The WP-CLI config -consul-template \ - -once \ - -dedup \ - -consul consul:8500 \ - -template "/var/www/html/wp-cli.yml.ctmpl:/var/www/html/wp-cli.yml" \ No newline at end of file diff --git a/containerbuddy/reload-db.sh b/containerbuddy/reload-db.sh deleted file mode 100644 index ccdad48..0000000 --- a/containerbuddy/reload-db.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -consul-template \ - -once \ - -dedup \ - -consul consul:8500 \ - -template "/var/www/html/db-config.php.ctmpl:/var/www/html/wordpress/db-config.php" \ No newline at end of file diff --git a/containerbuddy/reload-memcached.sh b/containerbuddy/reload-memcached.sh deleted file mode 100644 index d958a8c..0000000 --- a/containerbuddy/reload-memcached.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -consul-template \ - -once \ - -dedup \ - -consul consul:8500 \ - -template "/var/www/html/memcached-config.php.ctmpl:/var/www/html/memcached-config.php" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..1f9c2cf --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,46 @@ +wordpress: + restart: always + extends: + file: common-compose.yml + service: wordpress + mem_limit: 1g + links: + - consul:consul + - nfs:nfs + ports: + - 80 + +mysql: + extends: + file: common-compose.yml + service: mysql + restart: always + links: + - consul:consul + expose: + - 3306 + +memcached: + extends: + file: common-compose.yml + service: memcached + links: + - consul:consul + restart: always + +# Nginx as a load-balancing tier and reverse proxy +nginx: + extends: + file: common-compose.yml + service: nginx + restart: always + ports: + - "80" + links: + - consul:consul + +consul: + restart: always + extends: + file: common-compose.yml + service: consul diff --git a/etc/containerbuddy.json b/etc/containerbuddy.json new file mode 100644 index 0000000..b4b6bb1 --- /dev/null +++ b/etc/containerbuddy.json @@ -0,0 +1,40 @@ +{ + "consul": "{{ .CONSUL }}:8500", + "preStart": "/usr/local/bin/prestart.sh", + "services": [ + { + "name": "wordpress", + "port": 80, + "interfaces": [ + "eth0", + "eth1[1]", + "192.168.0.0/16", + "2001:db8::/64", + "eth2:inet", + "eth2:inet6", + "inet", + "inet6" + ], + "health": "/usr/local/bin/health.sh", + "poll": 10, + "ttl": 25 + } + ], + "backends": [ + { + "name": "mysql", + "poll": 7, + "onChange": "/usr/local/bin/onchange-db.sh" + }, + { + "name": "nfs", + "poll": 11, + "onChange": "/usr/local/bin/onchange-nfs.sh" + }, + { + "name": "memcached", + "poll": 5, + "onChange": "/usr/local/bin/onchange-memcached.sh" + } + ] +} diff --git a/git2consul.json b/git2consul.json deleted file mode 100644 index 8c28587..0000000 --- a/git2consul.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "version": "0.0.1", - "repos": [{ - "name": "triton-wordpress", - "source_root": "path/in/git/repo", - "mode" : "expand_keys", - "url": "https://github.com/misterbisson/triton-wordpress.git", - "branches": ["progress"], - "include_branch_name" : false, - "hooks": [{ - "type": "polling", - "interval": "1" - }] - }] -} \ No newline at end of file diff --git a/git2consul/wp-config.json b/git2consul/wp-config.json deleted file mode 100644 index 02e6983..0000000 --- a/git2consul/wp-config.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "config": [ - { "site_url": "http://my-site.example.com" }, - - { "SAVEQUERIES": true }, - { "WP_DEBUG": true }, - { "WP_DEBUG_DISPLAY": true }, - - { "AUTH_KEY": "put your unique phrase here" }, - { "SECURE_AUTH_KEY": "put your unique phrase here" }, - { "LOGGED_IN_KEY": "put your unique phrase here" }, - { "NONCE_KEY": "put your unique phrase here" }, - { "AUTH_SALT": "put your unique phrase here" }, - { "SECURE_AUTH_SALT": "put your unique phrase here" }, - { "LOGGED_IN_SALT": "put your unique phrase here" }, - { "NONCE_SALT": "put your unique phrase here" }, - - {} - ] -} diff --git a/local-compose.yml b/local-compose.yml new file mode 100644 index 0000000..fe4c08c --- /dev/null +++ b/local-compose.yml @@ -0,0 +1,61 @@ +wordpress: + extends: + file: common-compose.yml + service: wordpress + links: + - consul:consul + - nfs:nfs + mem_limit: 512m + restart: never + ports: + - "80" + +nfs: + extends: + file: common-compose.yml + service: nfs + environment: + - CONSUL=consul + - CONTAINERBUDDY=file:///etc/containerbuddy.json + links: + - consul:consul + ports: + - "111" + - "1892:1892" + - "2049" + +mysql: + extends: + file: common-compose.yml + service: mysql + restart: never + links: + - consul:consul + ports: + - "3306" + +memcached: + extends: + file: common-compose.yml + service: memcached + links: + - consul:consul + restart: never + +# Nginx as a load-balancing tier and reverse proxy +nginx: + extends: + file: common-compose.yml + service: nginx + ports: + - "80:80" + links: + - consul:consul + +consul: + extends: + file: common-compose.yml + service: consul + restart: never + ports: + - "8500:8500" diff --git a/nginx/containerbuddy.json b/nginx/containerbuddy.json new file mode 100644 index 0000000..aecae4a --- /dev/null +++ b/nginx/containerbuddy.json @@ -0,0 +1 @@ +{"consul":"${CONSUL}:8500","onStart":"/opt/containerbuddy/reload.sh preStart","services":[{"name":"nginx","port": 80,"publicIp": true,"health":"/usr/bin/curl --fail -s http://localhost/health.txt","poll":10,"ttl":25}],"backends":[{"name":"wordpress","poll":5,"onChange":"/opt/containerbuddy/reload.sh"}]} diff --git a/nginx/nginx.conf.ctmpl b/nginx/nginx.conf.ctmpl new file mode 100644 index 0000000..7137e6c --- /dev/null +++ b/nginx/nginx.conf.ctmpl @@ -0,0 +1,54 @@ +user nginx; +worker_processes 1; + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + keepalive_timeout 65; + + + {{ if service "wordpress" }} + upstream wordpress { + # write the address:port pairs for each healthy Customers node + {{range service "wordpress"}} + server {{.Address}}:{{.Port}}; + {{end}} + least_conn; + }{{ end }} + + + server { + listen 80; + server_name _; + + root /usr/share/nginx/html; + + location /health.txt { + add_header content-type "text/html"; + alias /usr/share/nginx/html/index.html; + } + + {{ if service "wordpress" }} + location ^~ / { + proxy_pass http://wordpress; + proxy_redirect off; + }{{end}} + + } +} diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..fa9fc2c --- /dev/null +++ b/setup.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +# run this script to setup your envrionment to start the project +# add the public and private keys you used in your MANTA setup to +# manta.id_rsa dn manta.id_rsa.pub or edit this script + +unset MANTA_PRIVATE_KEY +#unset MANTA_KEY_ID +unset NGINX_CONF +unset NGINX_CONTAINERBUDDY + +MANTA_PRIVATE_KEY=`cat manta.id_rsa` +#MANTA_KEY_ID=`ssh-keygen -lf manta.id_rsa.pub | awk -F ' ' '{print $2}'` +NGINX_CONF=`cat nginx.conf.ctmpl` +NGINX_CONTAINERBUDDY=`cat nginx_containerbuddy.json` diff --git a/var/www/html/db-config.php.ctmpl b/var/www/html/consul-templates/db-config.php.ctmpl similarity index 98% rename from var/www/html/db-config.php.ctmpl rename to var/www/html/consul-templates/db-config.php.ctmpl index bbe2f02..f6e838b 100644 --- a/var/www/html/db-config.php.ctmpl +++ b/var/www/html/consul-templates/db-config.php.ctmpl @@ -25,7 +25,8 @@ * * This has reads turned off, though any reads following a write _will_ be against the master. */ -{{with index (service "dbmaster") 0}} +{{if service "mysql-primary"}} +{{with index (service "mysql-primary") 0}} $wpdb->add_database(array( 'host' => '{{.Address}}:{{.Port}}', 'user' => DB_USER, @@ -35,14 +36,15 @@ $wpdb->add_database(array( 'read' => 0, 'dataset' => 'global', ));{{end}} +{{end}} -{{if service "dbreplica"}} +{{if service "mysql"}} /** * The DB replicas * * These are read-only. */ -{{range service "dbreplica"}} +{{range service "mysql"}} $wpdb->add_database(array( 'host' => '{{.Address}}:{{.Port}}', 'user' => DB_USER, @@ -57,7 +59,7 @@ $wpdb->add_database(array( /** * No DB replicas are healthy, so we're adding the master in as a read-host. */ -{{range service "dbmaster"}} +{{range service "mysql-primary"}} $wpdb->add_database(array( 'host' => '{{.Address}}:{{.Port}}', 'user' => DB_USER, @@ -127,6 +129,8 @@ $wpdb->max_connections = 10; */ $wpdb->check_tcp_responsiveness = true; + + /** Configuration Functions **/ /** diff --git a/var/www/html/consul-templates/memcached-config.php.ctmpl b/var/www/html/consul-templates/memcached-config.php.ctmpl new file mode 100644 index 0000000..0ccc10b --- /dev/null +++ b/var/www/html/consul-templates/memcached-config.php.ctmpl @@ -0,0 +1,31 @@ +