Skip to content

Instantly share code, notes, and snippets.

@kamidzi
Created September 26, 2015 21:27
Show Gist options
  • Save kamidzi/904eb07491481757b207 to your computer and use it in GitHub Desktop.
Save kamidzi/904eb07491481757b207 to your computer and use it in GitHub Desktop.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# This is a Vagrantfile to automatically provision a local BCPC cluster.
require 'openssl' # used to validate CA certificates
require 'uri' # used to parse the local mirror if one is given
Vagrant.require_version ">= 1.7.0"
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
# if being run via BOOT_GO.sh, ENV['REPO_ROOT'] will be the root of the repo;
# if it is not set, set it ourselves
ENV['REPO_ROOT'] ||= %x{git rev-parse --show-toplevel}.strip
# pull in the bootstrap config defaults and overrides (even though these would already
# be set by BOOT_GO.sh, do it again here so that Vagrant always has access to all
# configuration variables)
def extract_and_export_envvar(line)
matches = line.match(/^\s*(?!#)(export |)(?<name>.+)=(?<value>.+)$/)
unless matches.nil?
name, value = matches[:name], matches[:value]
# this blob interpolates existing environment variables in
interpolated_value = value.dup
envvars_to_interpolate = interpolated_value.scan /\$\w+/
envvars_to_interpolate.flatten.each {|v| interpolated_value.gsub!(v, ENV[v.tr('$', '')])}
explanation = (ENV[name] ? "Overriding " : "Setting ") + "#{name} = #{interpolated_value}"
explanation << " (originally #{value})" if interpolated_value != value
# puts explanation
ENV[name] = interpolated_value
end
end
# list any package names to be automatically removed here
$packages_to_remove = []
# if proxies are provided, configure them before configuring any local mirror override
# RUN THIS SCRIPT WITH sudo AND privileged = false so that $HOME is set to the
# value of the non-root user
$proxy_configuration_script = <<-EOH
sudo touch /etc/apt/apt.conf
touch $HOME/proxy_config.sh
EOH
# this script tests proxy servers from inside the bootstrap node
$testing_proxy_servers_script = <<-EOH
. $HOME/proxy_config.sh
FAILED=0
HTTP_PROXY_TEST=$(curl http://www.google.com 2>&1)
if [[ $? != 0 ]]; then
echo "HTTP proxy test failed, check HTTP proxy configuration:"
echo -e "$HTTP_PROXY_TEST"
FAILED=1
fi
HTTPS_PROXY_TEST=$(curl https://github.com 2>&1)
if [[ $? != 0 ]]; then
echo -e "HTTPS proxy test failed, check HTTPS proxy configuration and SSL certificates:"
echo -e "$HTTPS_PROXY_TEST"
FAILED=1
fi
exit $FAILED
EOH
# compute the repository configuration script
$repos_script = <<-EOH
#!/bin/bash
hash -r
install -d -m0755 -g adm /var/log/vagrant
exec &>>/var/log/vagrant/provision.log
date --rfc-3339=s
EOH
# try to apt-get update immediately so that the bootstrap does not wander along for a while
# and waste everyone's time in case our repo configuration set above is broken
$repos_script << <<-EOH
apt-get update
EOH
# END repository configuration script
# since these boxes do not come with swap, this script will add a swap file on disk
# (repartionining /dev/sda would be a dreadful chore)
$add_swap_script = <<-EOH
#!/bin/bash
fallocate -l 8192M /swap
chmod 600 /swap
mkswap /swap
swapon /swap
echo '/swap none swap defaults 0 0' | tee -a /etc/fstab
EOH
Vagrant.configure("2") do |config|
vms_to_build = ['lxchost']
vms_to_build.each_with_index do |vm, idx|
config.vm.synched_folder ".", "/vagrant", disabled: true
config.vm.define vm do |m|
bootstrap_domain = (ENV['BCPC_HYPERVISOR_DOMAIN'] or "bcpc.example.com")
m.vm.hostname = "lxchost.#{bootstrap_domain}"
m.vm.network :private_network, ip: "172.16.40.2", netmask: "255.255.255.0", adapter_ip: "172.16.40.1"
# fix no-tty error
m.vm.provision "fix-no-tty", type: "shell" do |s|
s.privileged = false
s.inline = "sudo sed -i '/tty/!s/mesg n/tty -s \\&\\& mesg n/' /root/.profile"
end
# # set up repositories
# m.vm.provision "configure-repositories", type: "shell" do |s|
# s.inline = $repos_script
# end
#
# # clean up some packages installed in this image by default
# m.vm.provision "clean-up-unnecessary-packages", type: "shell" do |s|
# s.inline = "apt-get remove -y #{$packages_to_remove.join(' ')}"
# end if $packages_to_remove.length > 0
# add swap space
m.vm.provision "add-swap-space", type: "shell" do |s|
s.inline = $add_swap_script
end
m.vm.box = "trusty64"
m.vm.box_url = "#{ENV['BOOTSTRAP_CACHE_DIR']}/trusty-server-cloudimg-amd64-vagrant-disk1.box"
memory = ( ENV["CLUSTER_VM_MEM"] or "8192" )
cpus = ( ENV["CLUSTER_VM_CPUS"] or "2" )
disk_size = ( ENV["CLUSTER_VM_DRIVE_SIZE"] or "65536" )
m.vm.provider :virtualbox do |vb|
vb.name = "lxchost"
vb.memory = memory
vb.cpus = cpus
vb.customize ["modifyvm", :id, "--nictype2", "82543GC"]
vb.customize ["modifyvm", :id, "--vram", "16"]
vb.customize ["modifyvm", :id, "--largepages", "on"]
vb.customize ["modifyvm", :id, "--nestedpaging", "on"]
vb.customize ["modifyvm", :id, "--vtxvpid", "on"]
vb.customize ["modifyvm", :id, "--hwvirtex", "on"]
vb.customize ["modifyvm", :id, "--ioapic", "on"]
vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
vb.customize ["modifyvm", :id, "--uart2", "0x2F8", "3"]
vb.customize ["modifyvm", :id, "--uartmode1", "disconnected"]
vb.customize ["modifyvm", :id, "--uartmode2", "disconnected"]
unless idx == 0
# this is an unpleasing hack to locate the VM on disk, so that additional disks can be stored with it
# this assumes that all VMs will be going into the default VirtualBox folder
begin
default_vm_config_file = %x[VBoxManage list systemproperties | grep 'Default machine folder']
default_vm_loc = default_vm_config_file.match(/^Default machine folder:\s+(.+)$/)[1]
vm_dir = File.join(default_vm_loc, vm)
rescue
fail "Unable to locate VM #{vm} on disk, terminating"
end
['b'].each_with_index do |disk, disk_idx|
disk_file = File.join(vm_dir, "#{vm}-#{disk}.vdi")
unless File.exist?(disk_file)
vb.customize ["createhd", "--filename", disk_file, "--size", disk_size]
vb.customize ["storageattach", :id, "--storagectl", "SATAController", "--device", "0", "--port", "#{disk_idx+1}", "--type", "hdd", "--medium", disk_file]
end # File.exist
end
end # if (1..3).include? idx
end # machine.vm.provider :virtualbox do |vb|
end # config.vm.define vm do |machine|
end # $vms_to_build.each_with_index
end # Vagrant.configure("2") do |config|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment