Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
- #
- # NOTE: vagrant-hostmanager plugin is used for private network DNS resolution.
- #
- # vagrant plugin install vagrant-hostmanager
- #
- # Optional: install vagrant-cachier to cache apt packages
- 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' }
- 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
- Vagrant.configure("2") do |config|
- machines = machines_from_file || machines_from_env
- config.vm.box_check_update = false
- config.vm.synced_folder "shared/", "/home/vagrant/shared", create: true, type: "rsync"
- # vagrant-hostmanager configs
- config.hostmanager.enabled = true
- config.hostmanager.manage_host = false
- config.hostmanager.manage_guest = true
- config.hostmanager.ignore_private_ip = false
- config.hostmanager.include_offline = true
- # vagrant-cachier config
- config.cache.scope = :machine if Vagrant.has_plugin? "vagrant-cachier"
- 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"
- 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.cpus = m[:cpu]
- v.memory = m[:memory]*1024
- v.nested = true
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement