Skip to content

Instantly share code, notes, and snippets.

@topdown
Created August 5, 2016 18:56
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 topdown/edae3d23a3944c484530fa56740f2482 to your computer and use it in GitHub Desktop.
Save topdown/edae3d23a3944c484530fa56740f2482 to your computer and use it in GitHub Desktop.
This file is a custom provision script for VVV that skips all updates and update checks
#!/bin/bash
#
# provision.sh
#
# Customized by Jeff Behnke @topdown on GitHub
#
# This file is a custom provision script for VVV that skips all updates and update checks
# It only does the absolute tasks that I know are needed for vagrant up, vagrant reload, vagrant provision and vv create
# It currently runs vagrant provision tasks in roughly 8 secs.
# You should be using VV for creating sites, then replace its custom-provision.sh with this one once you have a working VVV system.
# Later if you want to have things update, just remove/move this script so the provision.sh will run which will do the default updates.
#
#
#
# This file is specified in Vagrantfile and is loaded by Vagrant as the primary
# provisioning script whenever the commands `vagrant up`, `vagrant provision`,
# or `vagrant reload` are used. It provides all of the default packages and
# configurations included with Varying Vagrant Vagrants.
# By storing the date now, we can calculate the duration of provisioning at the
# end of this script.
start_seconds="$(date +%s)"
# Network Detection
#
# Make an HTTP request to google.com to determine if outside access is available
# to us. If 3 attempts with a timeout of 5 seconds are not successful, then we'll
# skip a few things further in provisioning rather than create a bunch of errors.
if [[ "$(wget --tries=3 --timeout=5 --spider http://google.com 2>&1 | grep 'connected')" ]]; then
echo "Network connection detected..."
ping_result="Connected"
else
echo "Network connection not detected. Unable to reach google.com..."
ping_result="Not Connected"
fi
# MySQL
#
# Use debconf-set-selections to specify the default password for the root MySQL
# account. This runs on every provision, even if MySQL has been installed. If
# MySQL is already installed, it will not affect anything.
echo mysql-server mysql-server/root_password password root | debconf-set-selections
echo mysql-server mysql-server/root_password_again password root | debconf-set-selections
# Postfix
#
# Use debconf-set-selections to specify the selections in the postfix setup. Set
# up as an 'Internet Site' with the host name 'vvv'. Note that if your current
# Internet connection does not allow communication over port 25, you will not be
# able to send mail, even with postfix installed.
echo postfix postfix/main_mailer_type select Internet Site | debconf-set-selections
echo postfix postfix/mailname string vvv | debconf-set-selections
# Disable ipv6 as some ISPs/mail servers have problems with it
echo "inet_protocols = ipv4" >> /etc/postfix/main.cf
# Used to to ensure proper services are started on `vagrant up`
cp /srv/config/init/vvv-start.conf /etc/init/vvv-start.conf
echo " * Copied /srv/config/init/vvv-start.conf to /etc/init/vvv-start.conf"
# Copy nginx configuration from local
cp /srv/config/nginx-config/nginx.conf /etc/nginx/nginx.conf
cp /srv/config/nginx-config/nginx-wp-common.conf /etc/nginx/nginx-wp-common.conf
if [[ ! -d /etc/nginx/custom-sites ]]; then
mkdir /etc/nginx/custom-sites/
fi
rsync -rvzh --delete /srv/config/nginx-config/sites/ /etc/nginx/custom-sites/
echo " * Copied /srv/config/nginx-config/nginx.conf to /etc/nginx/nginx.conf"
echo " * Copied /srv/config/nginx-config/nginx-wp-common.conf to /etc/nginx/nginx-wp-common.conf"
echo " * Rsync'd /srv/config/nginx-config/sites/ to /etc/nginx/custom-sites"
# Copy php-fpm configuration from local
#cp /srv/config/php5-fpm-config/php5-fpm.conf /etc/php5/fpm/php5-fpm.conf
#cp /srv/config/php5-fpm-config/www.conf /etc/php5/fpm/pool.d/www.conf
#cp /srv/config/php5-fpm-config/php-custom.ini /etc/php5/fpm/conf.d/php-custom.ini
#cp /srv/config/php5-fpm-config/opcache.ini /etc/php5/fpm/conf.d/opcache.ini
#cp /srv/config/php5-fpm-config/xdebug.ini /etc/php5/mods-available/xdebug.ini
# Find the path to Xdebug and prepend it to xdebug.ini
#XDEBUG_PATH=$( find /usr -name 'xdebug.so' | head -1 )
#sed -i "1izend_extension=\"$XDEBUG_PATH\"" /etc/php5/mods-available/xdebug.ini
#echo " * Copied /srv/config/php5-fpm-config/php5-fpm.conf to /etc/php5/fpm/php5-fpm.conf"
#echo " * Copied /srv/config/php5-fpm-config/www.conf to /etc/php5/fpm/pool.d/www.conf"
#echo " * Copied /srv/config/php5-fpm-config/php-custom.ini to /etc/php5/fpm/conf.d/php-custom.ini"
#echo " * Copied /srv/config/php5-fpm-config/opcache.ini to /etc/php5/fpm/conf.d/opcache.ini"
#echo " * Copied /srv/config/php5-fpm-config/xdebug.ini to /etc/php5/mods-available/xdebug.ini"
# Copy memcached configuration from local
#cp /srv/config/memcached-config/memcached.conf /etc/memcached.conf
#echo " * Copied /srv/config/memcached-config/memcached.conf to /etc/memcached.conf"
# Copy custom dotfiles and bin file for the vagrant user from local
#cp /srv/config/bash_profile /home/vagrant/.bash_profile
#cp /srv/config/bash_aliases /home/vagrant/.bash_aliases
#cp /srv/config/vimrc /home/vagrant/.vimrc
#if [[ ! -d /home/vagrant/.subversion ]]; then
# mkdir /home/vagrant/.subversion
#fi
#cp /srv/config/subversion-servers /home/vagrant/.subversion/servers
#if [[ ! -d /home/vagrant/bin ]]; then
# mkdir /home/vagrant/bin
#fi
#rsync -rvzh --delete /srv/config/homebin/ /home/vagrant/bin/
#echo " * Copied /srv/config/bash_profile to /home/vagrant/.bash_profile"
#echo " * Copied /srv/config/bash_aliases to /home/vagrant/.bash_aliases"
#echo " * Copied /srv/config/vimrc to /home/vagrant/.vimrc"
#echo " * Copied /srv/config/subversion-servers to /home/vagrant/.subversion/servers"
#echo " * rsync'd /srv/config/homebin to /home/vagrant/bin"
# If a bash_prompt file exists in the VVV config/ directory, copy to the VM.
#if [[ -f /srv/config/bash_prompt ]]; then
# cp /srv/config/bash_prompt /home/vagrant/.bash_prompt
# echo " * Copied /srv/config/bash_prompt to /home/vagrant/.bash_prompt"
#fi
# RESTART SERVICES
#
# Make sure the services we expect to be running are running.
echo -e "\nRestart services..."
service nginx restart
service memcached restart
# Disable PHP Xdebug module by default
# php5dismod xdebug
# Enable PHP mcrypt module by default
php5enmod mcrypt
service php5-fpm restart
# If MySQL is installed, go through the various imports and service tasks.
exists_mysql="$(service mysql status)"
if [[ "mysql: unrecognized service" != "${exists_mysql}" ]]; then
echo -e "\nSetup MySQL configuration file links..."
# Copy mysql configuration from local
cp /srv/config/mysql-config/my.cnf /etc/mysql/my.cnf
cp /srv/config/mysql-config/root-my.cnf /home/vagrant/.my.cnf
echo " * Copied /srv/config/mysql-config/my.cnf to /etc/mysql/my.cnf"
echo " * Copied /srv/config/mysql-config/root-my.cnf to /home/vagrant/.my.cnf"
# MySQL gives us an error if we restart a non running service, which
# happens after a `vagrant halt`. Check to see if it's running before
# deciding whether to start or restart.
if [[ "mysql stop/waiting" == "${exists_mysql}" ]]; then
echo "service mysql start"
service mysql start
else
echo "service mysql restart"
service mysql restart
fi
# IMPORT SQL
#
# Create the databases (unique to system) that will be imported with
# the mysqldump files located in database/backups/
if [[ -f /srv/database/init-custom.sql ]]; then
mysql -u root -proot < /srv/database/init-custom.sql
echo -e "\nInitial custom MySQL scripting..."
else
echo -e "\nNo custom MySQL scripting found in database/init-custom.sql, skipping..."
fi
# Setup MySQL by importing an init file that creates necessary
# users and databases that our vagrant setup relies on.
mysql -u root -proot < /srv/database/init.sql
echo "Initial MySQL prep..."
# Process each mysqldump SQL file in database/backups to import
# an initial data set for MySQL.
/srv/database/import-sql.sh
else
echo -e "\nMySQL is not installed. No databases imported."
fi
# Run wp-cli, tar, and npm as `vagrant` user instead of `root`
if (( $EUID == 0 )); then
wp() { sudo -EH -u vagrant -- wp "$@"; }
tar() { sudo -EH -u vagrant -- tar "$@"; }
npm() { sudo -EH -u vagrant -- npm "$@"; }
fi
# Find 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 site setup scripts
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 5 -name 'vvv-init.sh'); do
DIR="$(dirname $SITE_CONFIG_FILE)"
(
cd "$DIR"
source vvv-init.sh
)
done
# Look for Nginx vhost files, symlink them into the custom sites dir
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 5 -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
# 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 > /tmp/hosts
mv /tmp/hosts /etc/hosts
echo "Adding domains to the virtual machine's /etc/hosts file..."
find /srv/www/ -maxdepth 5 -name 'vvv-hosts' | \
while read hostfile; do
while IFS='' read -r line || [ -n "$line" ]; do
if [[ "#" != ${line:0:1} ]]; then
if [[ -z "$(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 "-----------------------------"
echo "Provisioning complete in "$(expr $end_seconds - $start_seconds)" seconds"
if [[ $ping_result == "Connected" ]]; then
echo "External network connection established, packages up to date."
else
echo "No external network available. Package installation and maintenance skipped."
fi
echo "For further setup instructions, visit http://vvv.dev"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment