Skip to content

Instantly share code, notes, and snippets.

@arnested
Last active September 7, 2015 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnested/fdc224ccdd2329c830cc to your computer and use it in GitHub Desktop.
Save arnested/fdc224ccdd2329c830cc to your computer and use it in GitHub Desktop.
Docker: setup proxy and drush aliases
#!/bin/bash
##
# Requirements:
#
# brew install jq
# docker and docker-machine
#
##
# Sudoers:
#
# <myuser> ALL=(root) NOPASSWD: /bin/mv * /etc/apache2/other/docker.conf
# <myuser> ALL=(root) NOPASSWD: /usr/sbin/apachectl graceful
#
##
# Config file can hold the following configuration. Values shown are
# the defaults.
#
# {
# "tld": ".dev",
# "dockermachine": "default",
# "apache": "/etc/apache2/other/docker.conf",
# "drush": "~/.drush/docker.aliases.drushrc.php"
# }
#
# Define (and create) config file.
CONFIG_FILE=~/.dock-in.json
if [ ! -f ${CONFIG_FILE} ]; then
echo '{}' > ${CONFIG_FILE}
fi
# Read configs with defaults.
TLD=$(jq -r '.tld // ".dev"' ${CONFIG_FILE})
DOCKER_MACHINE=$(jq -r '.dockermachine // "default"' ${CONFIG_FILE})
APACHE_FILE=$(jq -r '.apache // "/etc/apache2/other/docker.conf"' ${CONFIG_FILE})
DRUSH_FILE=$(jq -r '.drush // "~/.drush/docker.aliases.drushrc.php"' ${CONFIG_FILE})
# Config file default/start content.
APACHE='# This is a generated file.'
DRUSH='<?php // This is a generated file.'
# Eval docker machine environment.
eval "$(docker-machine env "${DOCKER_MACHINE}")"
# Get docker machines IP.
IP=$(docker-machine ip "${DOCKER_MACHINE}")
# Loop over Docker containers.
for NAME in $(docker ps --format={{.Names}}); do
# Apache config.
WWWPORT=$(docker port "${NAME}" 80/tcp | rev | cut -d : -f 1 | rev)
read -r -d '' APACHE <<EOF
$APACHE
<VirtualHost *:80>
ServerName ${NAME}${TLD}
ServerAlias *.${NAME}${TLD}
ProxyPreserveHost On
ProxyPass / http://${IP}:${WWWPORT}/
</VirtualHost>
EOF
# DB config for Drush site aliases.
DBPORT=$(docker port "${NAME}" 3306/tcp | rev | cut -d : -f 1 | rev)
DOCROOT=$(docker inspect "${NAME}" | jq -r '.[].Mounts | .[] | select(.Destination == "/www").Source')
STATUS=$(drush status --root="${DOCROOT}" --show-passwords --pipe)
DB_DRIVER=$(jq -r '."db-driver"' <<< "${STATUS}")
DB_USERNAME=$(jq -r '."db-username"' <<< "${STATUS}")
DB_PASSWORD=$(jq -r '."db-password"' <<< "${STATUS}")
DB_NAME=$(jq -r '."db-name"' <<< "${STATUS}")
read -r -d '' DRUSH <<EOF
$DRUSH
\$aliases['${NAME}'] = array(
'uri' => '${NAME}${TLD}',
'root' => '${DOCROOT}',
'db-url' => '${DB_DRIVER}://${DB_USERNAME}:${DB_PASSWORD}@${IP}:${DBPORT}/${DB_NAME}',
);
EOF
done;
# Write to temporary files.
TEMPAPACHE=$(mktemp -t d-a)
echo "${APACHE}" > "${TEMPAPACHE}"
TEMPDRUSH=$(mktemp -t d-a)
echo "${DRUSH}" > "${TEMPDRUSH}"
# Move into place and restart Apache.
mv "${TEMPDRUSH}" "${DRUSH_FILE}"
sudo mv "${TEMPAPACHE}" "${APACHE_FILE}"
sudo apachectl graceful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment