Skip to content

Instantly share code, notes, and snippets.

@swalkinshaw
Last active December 10, 2021 16:45
Show Gist options
  • Save swalkinshaw/5266b6869fcbbedf074d to your computer and use it in GitHub Desktop.
Save swalkinshaw/5266b6869fcbbedf074d to your computer and use it in GitHub Desktop.
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
ANSIBLE_PATH = '.' # path targeting Ansible directory (relative to Vagrantfile)
# Set Ansible roles_path relative to Ansible directory
ENV['ANSIBLE_ROLES_PATH'] = File.join(ANSIBLE_PATH, 'vendor', 'roles')
config_file = File.join(ANSIBLE_PATH, 'group_vars/development')
if File.exists?(config_file)
wordpress_sites = YAML.load_file(config_file)['wordpress_sites']
raise "no sites found in #{config_file}" if wordpress_sites.to_h.empty?
else
raise "#{config_file} file not found. Please set `ANSIBLE_PATH` in Vagrantfile"
end
Vagrant.require_version '>= 1.5.1'
Vagrant.configure('2') do |config|
config.vm.box = 'roots/bedrock'
config.ssh.forward_agent = true
# Required for NFS to work, pick any local IP
config.vm.network :private_network, ip: '192.168.50.5'
hostname, *aliases = wordpress_sites.flat_map { |(_name, site)| site['site_hosts'] }
config.vm.hostname = hostname
if Vagrant.has_plugin? 'vagrant-hostsupdater'
config.hostsupdater.aliases = aliases
else
puts 'vagrant-hostsupdater missing, please install the plugin:'
puts 'vagrant plugin install vagrant-hostsupdater'
end
if Vagrant::Util::Platform.windows?
wordpress_sites.each do |(name, site)|
config.vm.synced_folder local_site_path(site), remote_site_path(name), owner: 'vagrant', group: 'www-data', mount_options: ['dmode=776', 'fmode=775']
end
else
if !Vagrant.has_plugin? 'vagrant-bindfs'
raise Vagrant::Errors::VagrantError.new,
"vagrant-bindfs missing, please install the plugin:\nvagrant plugin install vagrant-bindfs"
else
wordpress_sites.each do |(name, site)|
config.vm.synced_folder local_site_path(site), nfs_path(name), type: 'nfs'
config.bindfs.bind_folder nfs_path(name), remote_site_path(name), u: 'vagrant', g: 'www-data'
end
end
end
if Vagrant::Util::Platform.windows?
playbook_path = File.join(ANSIBLE_PATH, 'dev.yml')
hosts_path = File.join(ANSIBLE_PATH, 'hosts')
config.vm.provision :shell do |sh|
sh.path = 'windows.sh'
sh.args = "#{playbook_path} #{hosts_path}"
end
else
config.vm.provision :ansible do |ansible|
ansible.playbook = File.join(ANSIBLE_PATH, 'dev.yml')
ansible.groups = {
'web' => ['default'],
'development' => ['default']
}
if vars = ENV['ANSIBLE_VARS']
extra_vars = Hash[vars.split(',').map { |pair| pair.split('=') }]
ansible.extra_vars = extra_vars
end
end
end
config.vm.provider 'virtualbox' do |vb|
# Give VM access to all cpu cores on the host
cpus = case RbConfig::CONFIG['host_os']
when /darwin/ then `sysctl -n hw.ncpu`.to_i
when /linux/ then `nproc`.to_i
else 2
end
# Customize memory in MB
vb.customize ['modifyvm', :id, '--memory', 1024]
vb.customize ['modifyvm', :id, '--cpus', cpus]
# Fix for slow external network connections
vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
vb.customize ['modifyvm', :id, '--natdnsproxy1', 'on']
end
end
def local_site_path(site)
File.expand_path(site['local_path'], ANSIBLE_PATH)
end
def nfs_path(site_name)
"/vagrant-nfs-#{site_name}"
end
def remote_site_path(site_name)
File.join('/srv/www/', site_name, 'current')
end
#!/bin/bash
#
# Windows provisioner for bedrock-ansible
# based on KSid/windows-vagrant-ansible
# @author Andrea Brandi
# @version 1.0
ANSIBLE_PLAYBOOK=$1
ANSIBLE_HOSTS=$2
TEMP_HOSTS="/tmp/ansible_hosts"
if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
echo "Ansible playbook not found."
exit 1
fi
if [ ! -f /vagrant/$ANSIBLE_HOSTS ]; then
echo "Ansible hosts not found."
exit 2
fi
# Create an ssh key if not already created.
if [ ! -f ~/.ssh/id_rsa ]; then
echo -e "\n\n\n" | ssh-keygen -t rsa
fi
# Install Ansible and its dependencies if not installed.
if [ ! -f /usr/bin/ansible ]; then
echo "Adding Ansible repository..."
sudo apt-add-repository ppa:rquillo/ansible
echo "Updating system..."
sudo apt-get -y update
echo "Installing Ansible..."
sudo apt-get -y install ansible
fi
cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} && chmod -x ${TEMP_HOSTS}
echo "Running Ansible provisioner defined in Vagrantfile..."
ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --sudo --user=vagrant --extra-vars "default_ssh_user=vagrant is_windows=true" --connection=local
rm ${TEMP_HOSTS}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment