Skip to content

Instantly share code, notes, and snippets.

@riywo
Created November 24, 2012 08:25
Show Gist options
  • Save riywo/4138870 to your computer and use it in GitHub Desktop.
Save riywo/4138870 to your computer and use it in GitHub Desktop.
gem "vagrant", :git => "git://github.com/riywo/vagrant.git", :tag => "v1.0.5.1"
#!/usr/bin/env ruby
ip = ARGV[0]
rc_file = "/etc/rc.local"
text = File.read(rc_file)
replace = text.gsub(/^dhclient eth0$/, "#dhclient eth0")
replace = replace.gsub(/^exit 0$/, "route add default gw #{ip} eth1\nexit 0")
File.open(rc_file, "w") { |file| file.puts replace }
interface_file = "/etc/network/interfaces"
text = File.read(interface_file)
replace = text.gsub(/^# The primary network interface$/, "## The primary network interface")
replace = replace.gsub(/^auto eth0$/, "#auto eth0")
replace = replace.gsub(/^iface eth0 inet dhcp$/, "#iface eth0 inet dhcp")
File.open(interface_file, "w") { |file| file.puts replace }
system("reboot")
#!/usr/bin/env ruby
sysctl_file = "/etc/sysctl.conf"
text = File.read(sysctl_file)
replace = text.gsub(/^#net.ipv4.ip_forward=1$/, "net.ipv4.ip_forward=1")
File.open(sysctl_file, "w") { |file| file.puts replace }
rc_file = "/etc/rc.local"
text = File.read(rc_file)
replace = text.gsub(/^exit 0$/, "/sbin/iptables -P FORWARD ACCEPT\n/sbin/iptables --table nat -A POSTROUTING -o eth0 -j MASQUERADE\nexit 0")
File.open(rc_file, "w") { |file| file.puts replace }
system("reboot")
# -*- mode: ruby -*-
# vi: set ft=ruby :
nat_box_ip = "192.168.1.2"
Vagrant::Config.run do |config|
config.vm.define :'test01' do |host_config|
host_config.vm.host_name = "test01"
host_config.ssh.forward_agent = true
host_config.vm.box = "precise64"
host_config.vm.box_url = "http://files.vagrantup.com/precise64.box"
host_config.vm.network :intnet, nat_box_ip, :adapter => 2
host_config.vm.provision :shell do |shell|
shell.path = "init_nat.rb"
end
end
config.vm.define :'test02' do |host_config|
host_config.vm.host_name = "test02"
host_config.ssh.forward_agent = true
host_config.vm.box = "precise64"
host_config.vm.box_url = "http://files.vagrantup.com/precise64.box"
host_config.vm.network :intnet, "192.168.1.3", :adapter => 2
host_config.vm.provision :shell do |shell|
shell.path = "init_default.rb"
shell.args = "#{nat_box_ip}"
end
end
config.vm.define :'test03' do |host_config|
host_config.vm.host_name = "test03"
host_config.ssh.forward_agent = true
host_config.vm.box = "precise64"
host_config.vm.box_url = "http://files.vagrantup.com/precise64.box"
host_config.vm.network :intnet, "192.168.1.4", :adapter => 2
host_config.vm.provision :shell do |shell|
shell.path = "init_default.rb"
shell.args = "#{nat_box_ip}"
end
end
end
@riywo
Copy link
Author

riywo commented Nov 24, 2012

$ bundle install
$ bundle exec vagrant up test01 test02 test03
$ bundle exec vagrant ssh test01

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