Skip to content

Instantly share code, notes, and snippets.

@roylez
Last active May 13, 2020 23:44
Show Gist options
  • Save roylez/071568b0d963fa4f6fcde7416a5ed2c1 to your computer and use it in GitHub Desktop.
Save roylez/071568b0d963fa4f6fcde7416a5ed2c1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
# Vagrantfile to bring up one or more VM for testing purposes.
#
# Environment Variables:
#
# LAB_MACHINES: Number of machines, ignored if `machines.yml` exits
# LAB_MEMORY: Memory in GB each machine, default: 1, ignored if `machines.yml` exists
# LAB_CPU: Number of CPU each machine, default: 1, ignored if `machines.yml` exists
# LAB_RELEASE: Ubuntu release to be used, default: bionic
#
# Optional: install vagrant-cachier to cache apt packages
#
# Example of machines.yml
# ---
# - cpu: 2
# memory: 1
# release: bionic
# - cpu: 1
# memory: 1
# release: focal
# hostname: focal
require 'yaml'
MACHINE_FILE="machines.yml"
CPU = ENV['LAB_CPU'].to_i == 0 ? 1 : ENV['LAB_CPU'].to_i
MEMORY = ENV['LAB_MEMORY'].to_i == 0 ? 1 : ENV['LAB_MEMORY'].to_i
RELEASE = %w(bionic xenial trusty).include?( ENV['LAB_RELEASE'] ) ? ENV['LAB_RELEASE'] : 'bionic'
RELEASE_VERSIONS = { 'bionic' => '18.04', 'xenial' => '16.04', 'trusty' => '14.04', 'focal' => "20.04" }
TAME_SCRIPT = <<-SHELL
APT="env DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical apt-get --assume-yes -q --no-install-recommends -o Dpkg::Options::=--force-confnew "
echo "== Remove hard coded DNS"
sed -i -e 's/^#\\?DNS=.*$/DNS=/g;s/^#\\?DNSSEC=.*$/DNSSEC=no/g' /etc/systemd/resolved.conf
systemctl restart systemd-resolved
echo "== Use local mirror for APT"
sed -i 's/us.archive/au.archive/g' /etc/apt/sources.list
$APT update
echo "== Install avahi to resolve hostnames"
$APT install avahi-daemon libnss-mdns
sed -ie '/deny-interfaces/a deny-interfaces=eth1' /etc/avahi/avahi-daemon.conf
systemctl restart avahi-daemon.s*
echo "== Make shell friendlier"
cat << EOF > /home/vagrant/.inputrc
set completion-ignore-case on
set show-all-if-ambiguous on
set show-all-if-unmodified on
set completion-map-case on
set completion-prefix-display-length 2
"\t": menu-complete
"": history-search-backward
"": history-search-forward
EOF
chown vagrant:vagrant /home/vagrant/.inputrc
echo "== Copy ssh keys"
cp /home/vagrant/shared/id_* /home/vagrant/.ssh/
cat /home/vagrant/.ssh/id_*.pub >> /home/vagrant/.ssh/authorized_keys
echo "StrictHostKeyChecking no" >> /home/vagrant/.ssh/config
chown -R vagrant:vagrant /home/vagrant/.ssh
SHELL
def complete_machine_with_env(machine)
%i( cpu memory release ).each { |k| machine[k] ||= Object.const_get( k.to_s.upcase ) }
machine
end
def machines_from_file
return nil unless File.file?(MACHINE_FILE)
machines = YAML.load(open(MACHINE_FILE).read, symbolize_names: true)
machines.is_a?(Array) ? machines.map{ |m| complete_machine_with_env(m) } : nil
end
def machines_from_env
unless ENV['LAB_MACHINES']
abort "Error: environment variable LAB_MACHINES not defined! Define this variable or use a machine.yml."
end
number = ENV['LAB_MACHINES'].to_i == 0 ? 1 : ENV['LAB_MACHINES'].to_i
machines = []
number.times { |i| machines.append( complete_machine_with_env({}) ) }
machines
end
ENV['VAGRANT_NO_PARALLEL'] = 'yes'
Vagrant.configure("2") do |config|
machines = machines_from_file || machines_from_env
# vagrant-cachier config
config.cache.scope = :machine if Vagrant.has_plugin? "vagrant-cachier"
# disable nfs synced_folder all together
config.nfs.functional = false
config.nfs.verify_installed = false
# disable autoupdate of images
config.vm.box_check_update = false
# default synced_folder
config.vm.synced_folder "shared/", "/home/vagrant/shared", create: true, type: "rsync"
# disalbe hard coded DNS
config.vm.provision "shell", inline: TAME_SCRIPT
machines.each_with_index do |m, i|
index = i + 1
name = m[:hostname] || "box#{index}"
config.vm.define( name ) do |config|
config.vm.hostname = name
config.vm.network :private_network, ip: "10.10.100.#{100+index}"
# config.hostmanager.aliases = %W( #{name}.lab )
config.vm.provider "virtualbox" do |v, override|
override.vm.box = "ubuntu/#{m[:release]}64"
# do not create console log for vms
v.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ]
v.cpus = m[:cpu]
v.memory = m[:memory]*1024
end
config.vm.provider "libvirt" do |v, override|
override.vm.box = "generic/ubuntu#{RELEASE_VERSIONS[m[:release]].sub('.','')}"
v.storage :file, size: '5G'
v.nested = true
v.cpu_mode = "host-passthrough"
v.graphics_type = "none"
v.cpus = m[:cpu]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment