Skip to content

Instantly share code, notes, and snippets.

@demophoon
Created June 5, 2015 18:49
Show Gist options
  • Save demophoon/3d7e07082fcacde7643f to your computer and use it in GitHub Desktop.
Save demophoon/3d7e07082fcacde7643f to your computer and use it in GitHub Desktop.
Vagrant Pooler Helpers
# vi: set ft=ruby :
# This file will need to be modified to meet your requirements
BASE_IP = "192.168.33"
BOXES = [
#{:box => "puppetlabs/ubuntu-14.04-64-nocm", :name => "ubuntu14"},
#{:box => "puppetlabs/ubuntu-12.04-64-nocm", :name => "ubuntu12"},
#{:box => "puppetlabs/centos-6.6-64-nocm", :name => "centos6"},
{:box => "puppetlabs/centos-7.0-64-nocm", :name => "centos7"},
#{:box => "puppetlabs/debian-7.8-64-nocm", :name => "debian7"},
#{:box => "puppetlabs/debian-6.0.10-64-nocm", :name => "debian6"},
]
# Helper Functions
def create_vm(config, box, name)
cpus = 1
memory = 512
ip = 9
boxname = box[:name]
box = box[:box]
hostname = name.to_s
case name.to_s
when /master.*/
memory = 2048
ip = 0
hostname = "master.vm"
when /agent.*/
ip = 1
hostname = "agent.vm"
when /puppetdb.*/
ip = 2
hostname = "puppetdb.vm"
when /console.*/
ip = 3
hostname = "console.vm"
when /compile.*/
memory = 2048
ip = 4
hostname = "compile.vm"
end
ip_modifier = /\S+(\d+).*/.match(name)
if ip_modifier
ip_modifier = ip_modifier[1].to_i
else
ip_modifier = 1
end
ip = 10 * ip_modifier + ip
ip = "#{BASE_IP}.#{ip}"
name = "#{name}-#{boxname}.vm"
config.vm.define name.to_sym do |vbox|
vbox.vm.box = box
vbox.vm.hostname = hostname
vbox.vm.network "private_network", ip: ip
vbox.vm.provider "virtualbox" do |v|
v.cpus = cpus
v.name = name
v.memory = memory
end
if scripts = get_platform_scripts(boxname)
vbox.vm.provision "shell", inline: scripts
end
vbox.vm.provision "shell", inline: <<-SCRIPT
echo "#{ip} #{hostname}" >> /etc/hosts
SCRIPT
vbox.vm.post_up_message = "VM-Name: #{name}, Hostname: #{hostname}, IP: #{ip}"
end
end
def get_platform_scripts(boxname)
el_script = <<-SCRIPT
sudo service firewalld stop
SCRIPT
deb_script = <<-SCRIPT
sudo apt-get update
sudo apt-get install -y build-essential git python vim tmux zsh ruby1.9.1-dev
sudo -H -u vagrant bash -c 'git clone https://github.com/demophoon/dotfiles ~/dotfiles'
sudo -H -u vagrant bash -c 'git clone https://github.com/demophoon/vundle-headless-installer.git ~/vundle-headless-installer'
sudo -H -u vagrant bash -c 'git --git-dir=~/dotfiles submodule update --init'
sudo -H -u vagrant bash -c 'source ~/dotfiles/setup.sh -f'
sudo -H -u vagrant bash -c 'python ~/vundle-headless-installer/install.py'
SCRIPT
case boxname
when /ubuntu.*/
deb_script
when /centos.*|el.*/
el_script
else
nil
end
end
def create_pe_vms(config, box, suffix)
create_vm config, box, "master-#{suffix}"
#create_vm config, box, "agent-#{suffix}"
#create_vm config, box, "puppetdb-#{suffix}"
#create_vm config, box, "console-#{suffix}"
#create_vm config, box, "compile-#{suffix}"
end
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder "/Users/britt/projects", "/local", type: "nfs"
config.vm.synced_folder "/Users/britt/projects/vagrant/ubuntu-master", "/vagrant", type: "nfs"
config.vm.provision "shell", inline: <<-SCRIPT
echo I am provisioning via vagrant.
date > /etc/vagrant_provisioned_at
mkdir -p /root/.ssh/
cat /local/work/yellow/pe_acceptance_tests/id_rsa-acceptance.pub > /root/.ssh/authorized_keys
#echo "192.168.33.10 master.vm" >> /etc/hosts
#echo "192.168.33.11 agent.vm" >> /etc/hosts
#echo "192.168.33.12 puppetdb.vm" >> /etc/hosts
#echo "192.168.33.13 console.vm" >> /etc/hosts
#echo "192.168.33.14 compile.vm" >> /etc/hosts
#echo "192.168.33.20 master2.vm" >> /etc/hosts
#echo "192.168.33.21 agent2.vm" >> /etc/hosts
#echo "192.168.33.22 puppetdb2.vm" >> /etc/hosts
#echo "192.168.33.23 console2.vm" >> /etc/hosts
#echo "192.168.33.24 compile2.vm" >> /etc/hosts
#echo "192.168.33.30 master3.vm" >> /etc/hosts
#echo "192.168.33.31 agent3.vm" >> /etc/hosts
#echo "192.168.33.32 puppetdb3.vm" >> /etc/hosts
#echo "192.168.33.33 console3.vm" >> /etc/hosts
#echo "192.168.33.34 compile3.vm" >> /etc/hosts
#echo "192.168.33.40 master4.vm" >> /etc/hosts
#echo "192.168.33.50 master5.vm" >> /etc/hosts
SCRIPT
config.vm.provision "shell", inline: <<-SCRIPT
echo "Installing PE 3.8.0"
install_dir="/local/work/yellow/pe-installs/versions/el-7-x86_64/3.8.0/"
answers_file="/local/work/yellow/pe-installs/master.vm.answers"
cd ${install_dir} && sudo ${install_dir}/puppet-enterprise-installer -a ${answers_file}
SCRIPT
BOXES.map { |box|
create_pe_vms config, box, "1"
create_pe_vms config, box, "2"
create_pe_vms config, box, "3"
create_pe_vms config, box, "4"
create_pe_vms config, box, "5"
create_pe_vms config, box, "6"
}
end
#!/usr/bin/env ruby
# Run this while in the Vagrant pooler directory to check in your current VM and check out a new VM
output = `curl -d --url http://127.0.0.1:5000/vm/master.vm ;`
match = /\"hostname\": \"(.*)\"/.match(output)
hostname = match[1]
exec("vagrant ssh #{hostname}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment