Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active December 29, 2018 14:30
Show Gist options
  • Save wpscholar/65e1fa5c571f4be420e6 to your computer and use it in GitHub Desktop.
Save wpscholar/65e1fa5c571f4be420e6 to your computer and use it in GitHub Desktop.
Provision a new site in VVV without having to run `vagrant reload --provision`
#!/bin/sh
# Run with `sudo bash vvv-provision.sh`
DIR=$(basename $PWD)
find /etc/nginx/custom-sites -name "vvv-auto-$DIR-*.conf" -exec rm {} \;
CONF="vvv-auto-$DIR-$(md5sum <<< vvv-nginx.conf | cut -c1-32).conf"
sed "s#{vvv_path_to_folder}#$PWD#" vvv-nginx.conf > /etc/nginx/custom-sites/"$CONF"
echo "Added nginx config file: $CONF"
@mheesters
Copy link

You can also use the function (custom_vvv) from the provision script VVV uses (provision.sh).
Just create a little script for it and run it as root.

#!/bin/bash
#
# provision-custom-vvv.sh
#
custom_vvv(){
  # 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
}

custom_vvv

@tomjn
Copy link

tomjn commented Dec 29, 2018

You don't have to reload, just run vagrant provision and it won't restart the VM

Keep in mind too that all these scripts modify the VM without VVV being awares, which is a great way to break things when VVV updates, or if vagrant destroy gets run for whatever reason. It's very difficult to recover from those scenarios, just stick to the standard VVV vvv-custom.yml config and everything should be fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment