Skip to content

Commit

Permalink
Merge remote-tracking branch 'xteam/auto-site-setup'
Browse files Browse the repository at this point in the history
Conflicts:
	CHANGELOG.md
	README.md
  • Loading branch information
jeremyfelt committed Nov 3, 2013
2 parents 638293c + 5f5497d commit 06c0bce
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -38,6 +38,9 @@
# And we do have a default SQL file that should be included
!/database/init.sql

# And we provide our default host names in a dat file.
!/www/vvv-hosts

# And a few of our web directories are important to share.
!/www/default/
/www/default/webgrind
Expand Down
43 changes: 34 additions & 9 deletions Vagrantfile
Expand Up @@ -38,16 +38,36 @@ Vagrant.configure("2") do |config|
# be aware of the domains specified below. Watch the provisioning script as you may be
# required to enter a password for Vagrant to access your hosts file.
#
# By default, we'll include the domains setup by VVV. A short term goal is to read these in
# from a local config file so that they can be more dynamic to your setup.
# By default, we'll include the domains setup by VVV through the vvv-hosts file
# located in the www/ directory.
#
# Other domains can be automatically added by including a vvv-hosts file containing
# individual domains separated by whitespace in subdirectories of www/.
if defined? VagrantPlugins::HostsUpdater
config.hostsupdater.aliases = [
"vvv.dev",
"local.wordpress.dev",
"local.wordpress-trunk.dev",
"src.wordpress-develop.dev",
"build.wordpress-develop.dev"
]

# Capture the paths to all vvv-hosts files under the www/ directory.
paths = []
Dir.glob(vagrant_dir + '/www/**/vvv-hosts').each do |path|
paths << path
end

# Parse through the vvv-hosts files in each of the found paths and put the hosts
# that are found into a single array.
hosts = []
paths.each do |path|
new_hosts = []
file_hosts = IO.read(path).split( "\n" )
file_hosts.each do |line|
if line[0..0] != "#"
new_hosts << line
end
end
hosts.concat new_hosts
end

# Pass the final hosts array to the hostsupdate plugin so it can perform magic.
config.hostsupdater.aliases = hosts

end

# Default Box IP Address
Expand Down Expand Up @@ -144,6 +164,11 @@ Vagrant.configure("2") do |config|
config.vm.provision :shell, :path => File.join( "provision", "provision.sh" )
end

# auto-site-setup.sh iterates through all sites requiring setup, and sets them up.
if File.exists?(File.join(vagrant_dir,'provision','auto-site-setup.sh')) then
config.vm.provision :shell, :path => File.join( "provision", "auto-site-setup.sh" )
end

# provision-post.sh acts as a post-hook to the default provisioning. Anything that should
# run after the shell commands laid out in provision.sh or provision-custom.sh should be
# put into this file. This provides a good opportunity to install additional packages
Expand Down
36 changes: 36 additions & 0 deletions provision/auto-site-setup.sh
@@ -0,0 +1,36 @@
# auto-site-setup.sh
#
# This script is responsible for finding new sites to setup.

# Kill previously symlinked Nginx configs
# We can't know what sites have been removed, so we have to remove all
# the configs and add them back in again.
find /etc/nginx/custom-sites -name 'vvv-auto-*.conf' -exec rm {} \;

# Look for Nginx vhost files, symlink them into the custom sites dir
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 4 -name 'vvv-nginx.conf'); do
DEST_CONFIG_FILE=${SITE_CONFIG_FILE//\/srv\/www\//}
DEST_CONFIG_FILE=${DEST_CONFIG_FILE//\//\-}
DEST_CONFIG_FILE=${DEST_CONFIG_FILE/%-vvv-nginx.conf/}
DEST_CONFIG_FILE="vvv-auto-$DEST_CONFIG_FILE-$(md5sum <<< $SITE_CONFIG_FILE | cut -c1-32).conf"
# We allow the replacement of the {vvv_path_to_folder} token with
# whatever you want, allowing flexible placement of the site folder
# while still having an Nginx config which works.
DIR=`dirname $SITE_CONFIG_FILE`
sed "s#{vvv_path_to_folder}#$DIR#" $SITE_CONFIG_FILE > /etc/nginx/custom-sites/$DEST_CONFIG_FILE
done

# Look for site setup scripts
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 4 -name 'vvv-init.sh'); do
DIR=`dirname $SITE_CONFIG_FILE`
(
cd $DIR
bash vvv-init.sh
)
done;

# RESTART SERVICES AGAIN
#
# Make sure the services we expect to be running are running.
echo -e "\nRestart Nginx..."
service nginx restart
36 changes: 23 additions & 13 deletions provision/provision.sh
Expand Up @@ -521,19 +521,29 @@ else
echo -e "\nNo network available, skipping network installations"
fi

# Add any custom domains to the virtual machine's hosts file so that it
# is self aware. Enter domains space delimited as shown with the default.
DOMAINS='vvv.dev
local.wordpress.dev
local.wordpress-trunk.dev
src.wordpress-develop.dev
build.wordpress-develop.dev'

if ! grep -q "$DOMAINS" /etc/hosts
then
DOMAINS=$(echo $DOMAINS)
echo "127.0.0.1 $DOMAINS" >> /etc/hosts
fi
# Parse any vvv-hosts file located in www/ or subdirectories of www/
# for domains to be added to the virtual machine's host file so that it is
# self aware.
#
# Domains should be entered on new lines.
echo "Cleaning the virtual machine's /etc/hosts file..."
sed -n '/# vvv-auto$/!p' /etc/hosts > /etc/hosts
echo "Adding domains to the virtual machine's /etc/hosts file..."
find /srv/www/ -maxdepth 4 -name 'vvv-hosts' | \
while read hostfile
do
while IFS='' read -r line || [ -n "$line" ]
do
if [ "#" != ${line:0:1} ]
then
if ! grep -q "^127.0.0.1 $line$" /etc/hosts
then
echo "127.0.0.1 $line # vvv-auto" >> /etc/hosts
echo " * Added $line from $hostfile"
fi
fi
done < $hostfile
done

end_seconds=`date +%s`
echo "-----------------------------"
Expand Down
14 changes: 14 additions & 0 deletions www/vvv-hosts
@@ -0,0 +1,14 @@
# This file contains a list of the domains supported by default
# in Varying Vagrant Vagrants. Both Vagrantfile and provision.sh
# parse this file for domains to add to both your host and guest
# machines as the virtual machines boot.
#
# In addition to this file, other files named vvv-hosts can
# be created with additional domain information. These will be
# automatically parsed as long as they are located in subdirectories
# of the www/ directory.
vvv.dev
local.wordpress.dev
local.wordpress-trunk.dev
src.wordpress-develop.dev
build.wordpress-develop.dev

0 comments on commit 06c0bce

Please sign in to comment.