Skip to content

Instantly share code, notes, and snippets.

@madAndroid
Created July 1, 2013 12:19
Show Gist options
  • Save madAndroid/5900335 to your computer and use it in GitHub Desktop.
Save madAndroid/5900335 to your computer and use it in GitHub Desktop.
if File.file?('Vagrantfile') and File.symlink?('../Vagrantfile')
puts
puts "I found a Vagrantfile here, and a symlink in the parent " +
"folder - you probably want to do 'cd ..' and run the " +
"command again.\n"
puts
exit -1
end
def sf_vagrant_config( config, version )
virtual_machines = {
### This small VM facilitates routing/testing between environments,
### and should be spun up first:
:gw => {
:hostname => 'i1-gw.mymedsandme.net',
:ip => '192.168.41.11',
:customize => [ "--memory", "256"],
:sf_roles => [ 'router' ],
:sf_env => 'vagrant',
:sf_location => 'vagrant',
:net_hash => {
'3' => 'mgmt',
'4' => 'auth',
'5' => 'prod',
'6' => 'prodd',
},
:ipadd_hash => {
'eth2' => '10.10.1.1/24',
'eth3' => '10.10.2.1/24',
'eth4' => '10.10.3.1/24',
'eth5' => '10.10.4.1/24',
},
},
### These are shared/management machines, required for auth/vpn/monitoring
:auth_core => {
:hostname => 'i1-auth-ldap1.mymedsandme.net',
:ip => '192.168.41.12',
:sf_roles => [ 'core', 'm3_vagrant' ],
:sf_env => 'auth',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'auth' },
:ipadd_hash => { 'eth2' => '10.10.2.9/24' },
},
:mgmt_core => {
:hostname => 'i1-mgmt-core1.mymedsandme.net',
:ip => '192.168.41.13',
:sf_roles => [ 'core', 'm3_vagrant' ],
:sf_env => 'mgmt',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'mgmt' },
:ipadd_hash => { 'eth2' => '10.10.1.5/24' },
},
:mon => {
:hostname => 'i1-mgmt-mon1.mymedsandme.net',
:ip => '192.168.41.14',
:sf_roles => [ 'monitoring', 'log', 'm3_vagrant' ],
:sf_env => 'mgmt',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'mgmt' },
:ipadd_hash => { 'eth2' => '10.10.1.6/24' },
},
### Env specific machines start here:
:core => {
:hostname => 'i1-prod-core1.mymedsandme.net',
:ip => '192.168.41.15',
:sf_roles => [ 'core' ],
:sf_env => 'prod',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'prod' },
:ipadd_hash => { 'eth2' => '10.10.3.5/24' },
},
:data => {
:hostname => 'i1-prod-data1.mymedsandme.net',
:ip => '192.168.41.16',
:sf_roles => [ 'data' ],
:sf_env => 'prod',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'prod' },
:ipadd_hash => { 'eth2' => '10.10.3.80/24' },
},
:srch => {
:hostname => 'i1-prod-srch1.mymedsandme.net',
:ip => '192.168.41.17',
:sf_roles => [ 'search' ],
:sf_env => 'prod',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'prod' },
:ipadd_hash => { 'eth2' => '10.10.3.90/24' },
},
:app => {
:hostname => 'i1-prod-app1.mymedsandme.net',
:ip => '192.168.41.18',
:sf_roles => [ 'search' ],
:sf_env => 'prod',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'prod' },
:ipadd_hash => { 'eth2' => '10.10.4.40/24' },
},
:sftp => {
:hostname => 'i1-prod-sftp1.mymedsandme.net',
:ip => '192.168.41.19',
:sf_roles => [ 'sftp' ],
:sf_env => 'prod',
:sf_location => 'vagrant',
:customize => [ "--memory", "768"],
:net_hash => { '3' => 'prod' },
:ipadd_hash => { 'eth2' => '10.10.4.120/24' },
},
}
config.vm.box = "ubuntu_server_12042_x64-virtualbox-38"
config.vm.box_url = "http://repo.scalefactory.com/vagrant/ubuntu_server_12042_x64-virtualbox-38.box"
virtual_machines.each do |name,cfg|
config.vm.define name do |vm_config|
if ( version == 1 )
vm_config.vm.customize ["modifyvm", :id, "--memory", (cfg[:memory] || "1024") ]
vm_config.vm.customize ["modifyvm", :id, "--name",
File.basename(File.dirname(__FILE__)) +
"-#{name}" + "_#{Time.now.to_i}"
]
vm_config.vm.share_folder("v-root", "/vagrant", ".", :nfs => true)
vm_config.vm.host_name = cfg[:hostname] if cfg[:hostname]
vm_config.vm.network :hostonly, cfg[:ip], :netmask => "255.255.0.0"
cfg[:net_hash].sort.each { | int_num, int_label |
vm_config.vm.customize ["modifyvm", :id, "--intnet#{int_num}", "#{int_label}"]
vm_config.vm.customize ["modifyvm", :id, "--nic#{int_num}", "intnet"]
} if cfg[:net_hash]
else
vm_config.vm.synced_folder ".", "/vagrant", :nfs => true
vm_config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", (cfg[:memory] || "1024") ]
vb.customize ["modifyvm", :id, "--name",
File.basename(File.dirname(__FILE__)) +
"-#{name}" + "_#{Time.now.to_i}"
]
cfg[:net_hash].sort.each { | int_num, int_label |
vb.customize ["modifyvm", :id, "--intnet#{int_num}", "#{int_label}"]
vb.customize ["modifyvm", :id, "--nic#{int_num}", "intnet"]
} if cfg[:net_hash]
end
vm_config.vm.hostname = cfg[:hostname] if cfg[:hostname]
vm_config.vm.network :private_network, ip: cfg[:ip] if cfg[:ip]
end
vm_config.vm.box = cfg[:box] if cfg[:box]
cfg[:ipadd_hash].sort.each { | eth, ip |
vm_config.vm.provision :shell, :inline => "ip addr show #{eth} | grep #{ip} || "+
"sudo ip addr add #{ip} dev #{eth} && "+
"sudo ip link set #{eth} up"
} if cfg[:ipadd_hash]
apt_partial = "/var/cache/apt/archives/partial"
apt_mount = "var/apt-cache/#{vm_config.vm.host_name}"
FileUtils.mkdir_p(apt_mount) unless File.exists?(apt_mount)
if File.directory?( "var/apt-cache" )
vm_config.vm.share_folder( 'apt-cache', '/var/cache/apt', "#{apt_mount}", :nfs => true, :create => false )
unless RUBY_PLATFORM =~ /darwin/
vm_config.vm.provision :shell, :inline => "[ -d #{apt_partial} ] || mkdir -p #{apt_partial}"
end
end
sf_roles = cfg[:sf_roles].join(',')
sf_environment = cfg[:sf_env]
sf_location = cfg[:sf_location]
### Setup sf_* vars:
vm_config.vm.provision :shell, :inline =>
"echo Setting Environment && "+
"echo '#{sf_roles}' > /etc/sf_roles.conf && "+
"echo '#{sf_environment}' > /etc/sf_environment.conf && "+
"echo '#{sf_location}' > /etc/sf_location.conf"
puppet_base = "/vagrant/infrastructure"
### Configure routing to emulate Iperion environment:
vm_config.vm.provision :shell, :inline =>
"echo Applying vagrant routing && "+
"PATH=/opt/automation/usr/bin:$PATH puppet apply --verbose "+
"--config=#{puppet_base}/vagrant/puppet.conf " +
"#{puppet_base}/mymedsandme.com/manifests/vagrant_routing.pp"
manifests = [ 'bootstrap', 'site' ]
if cfg[:sf_roles].any? { |role| 'router' =~ /#{role}/ }
manifests = []
end
### Main puppet runs: bootstrap first
for ppfile in manifests
vm_config.vm.provision :shell, :inline =>
"PATH=/opt/automation/usr/bin:$PATH puppet apply --verbose "+
"--config=#{puppet_base}/vagrant/puppet.conf " +
"#{puppet_base}/mymedsandme.com/manifests/#{ppfile}.pp"
## squid takes a while to spin up - ensure apt can update on core boxes, which goes via the proxy
if cfg[:sf_roles].any? { |role| 'core' =~ /#{role}/ }
vm_config.vm.provision :shell, :inline =>
"RETURN=1; while [ $RETURN -gt 0 ]; do /etc/init.d/squid3 restart; sleep 3; apt-get clean; sleep 3; apt-get update; RETURN=$?; done"
end
end unless manifests.empty?
## On core machines, sync puppetbase to /etc, and then run agent against manifest set
if cfg[:sf_roles].any? { |role| 'core' =~ /#{role}/ }
vm_config.vm.provision :shell, :inline => "mv /etc/puppet{,.BKP-#{Time.now.to_i}}"
vm_config.vm.provision :shell, :inline => "rsync -ar #{puppet_base}/ /etc/puppet/ --exclude='*.git*'"
vm_config.vm.provision :shell, :inline => "export FACTERLIB=/etc/puppet/scalefactory.com/modules/sf_nodeless/lib/facter:$FACTERLIB"
vm_config.vm.provision :shell, :inline => "puppet agent --test --detailed-exitcodes; if [ $? = 4 -o $? = 6 -o $? = 1 ] ; then exit -99 ; fi "
end
end
end
end
"#{Vagrant::VERSION}" < "1.1.0" and Vagrant::Config.run do |config|
sf_vagrant_config( config, 1 )
end
"#{Vagrant::VERSION}" >= "1.1.0" and Vagrant.configure("2") do |config|
sf_vagrant_config( config, 2 )
end
# vi: set ft=ruby shiftwidth=4 softtabstop=4 tabstop=4 expandtab:
# :indentSize=4:tabSize=4:noTabs=true:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment