Skip to content

Instantly share code, notes, and snippets.

@zanhsieh
Last active August 29, 2015 14:16
Show Gist options
  • Save zanhsieh/85ebfd1db15225602a6f to your computer and use it in GitHub Desktop.
Save zanhsieh/85ebfd1db15225602a6f to your computer and use it in GitHub Desktop.
common-vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
$IPs = {
"vm1" => "10.0.119.11",
"vm2" => "10.0.119.12",
"vm3" => "10.0.119.13",
"vm4" => "10.0.119.14",
"vm5" => "10.0.119.15"
}
def host_check(ips={})
return ips.map {|k,v|<<-INNER
if [ ! `grep -q #{v} /etc/hosts` ]; then
echo '#{v} #{k}' | sudo tee -a /etc/hosts
fi
INNER
}.join()
end
def firewall_setup(ports=[])
case ports.size
when 0
return
else
return <<-INNER
if [ ! `grep -q '#{ports.join('\|')}' /etc/sysconfig/iptables` ]; then
echo "Open port #{ports.join(',')}"
LN=$(iptables -L --line-numbers | grep REJECT | cut -d ' ' -f1 | head -n 1)
iptables -N allow_services
iptables -I INPUT $LN -j allow_services
iptables -A allow_services -p tcp --match multiport --dports #{ports.join(',')} -j ACCEPT
service iptables save
service iptables restart
fi
INNER
end
end
$vm1_setup = <<-VM1SETUP
#{firewall_setup([80,443,8080,8443])}
if [ ! -f "/var/setup_done" ]; then
# DO WHATEVER YOU WANT HERE
fi
VM1SETUP
$vm2_setup = <<-VM1SETUP
#{firewall_setup([2181,27017])}
if [ ! -f "/var/setup_done" ]; then
# DO WHATEVER YOU WANT HERE
fi
VM1SETUP
Vagrant.configure(2) do |config|
config.vm.box = "centos66"
config.vm.box_check_update = false
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = "box"
config.cache.synced_folder_opts = {
type: "nfs",
mount_options: ['rw', 'vers=3', 'tcp', 'nolock']
}
end
if Vagrant.has_plugin?("vagrant-vbguest")
config.vbguest.auto_update = false
end
#config.vm.synced_folder '.', '/vagrant', nfs: true
$IPs.map do |k,v|
config.vm.define "#{k}" do |m|
m.vm.hostname = "#{k}"
m.vm.network "private_network", ip: "#{v}"
m.vm.provision "shell", inline:<<-SHELL
#{host_check($IPs)}
SHELL
m.vm.provision "shell", inline:<<-SHELL
#{case k
when "vm1"
$vm1_setup
when "vm2"
$vm2_setup
else
end
}
SHELL
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment