SHARE
TWEET

Untitled

a guest Aug 25th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env ruby
  2. # encoding: utf-8
  3. #
  4. # Vagrantfile to bring up one or more VM for testing purposes.
  5. #
  6. # Environment Variables:
  7. #
  8. #   LAB_MACHINES:   Number of machines, ignored if `machines.yml` exits
  9. #   LAB_MEMORY:     Memory in GB each machine, default: 1, ignored if `machines.yml` exists
  10. #   LAB_CPU:        Number of CPU each machine, default: 1, ignored if `machines.yml` exists
  11. #   LAB_RELEASE:    Ubuntu release to be used, default: bionic
  12. #
  13. # NOTE: vagrant-hostmanager plugin is used for private network DNS resolution.
  14. #
  15. #   vagrant plugin install vagrant-hostmanager
  16. #
  17. # Optional: install vagrant-cachier to cache apt packages
  18.  
  19. require 'yaml'
  20.  
  21. MACHINE_FILE="machines.yml"
  22. CPU      = ENV['LAB_CPU'].to_i      == 0 ? 1 : ENV['LAB_CPU'].to_i
  23. MEMORY   = ENV['LAB_MEMORY'].to_i   == 0 ? 1 : ENV['LAB_MEMORY'].to_i
  24. RELEASE  = %w(bionic xenial trusty).include?( ENV['LAB_RELEASE'] ) ? ENV['LAB_RELEASE'] : 'bionic'
  25. RELEASE_VERSIONS = { 'bionic' => '18.04', 'xenial' => '16.04', 'trusty' => '14.04' }
  26.  
  27. def complete_machine_with_env(machine)
  28.   %i( cpu memory release ).each { |k| machine[k] ||= Object.const_get( k.to_s.upcase ) }
  29.   machine
  30. end
  31.  
  32. def machines_from_file
  33.   return nil unless File.file?(MACHINE_FILE)
  34.   machines = YAML.load(open(MACHINE_FILE).read, symbolize_names: true)
  35.   machines.is_a?(Array) ? machines.map{ |m| complete_machine_with_env(m) } : nil
  36. end
  37.  
  38. def machines_from_env
  39.   unless ENV['LAB_MACHINES']
  40.     abort "Error: environment variable LAB_MACHINES not defined! Define this variable or use a machine.yml."
  41.   end
  42.  
  43.   number = ENV['LAB_MACHINES'].to_i == 0 ? 1 : ENV['LAB_MACHINES'].to_i
  44.   machines = []
  45.   number.times { |i| machines.append( complete_machine_with_env({}) ) }
  46.   machines
  47. end
  48.  
  49. Vagrant.configure("2") do |config|
  50.   machines = machines_from_file || machines_from_env
  51.  
  52.   config.vm.box_check_update = false
  53.   config.vm.synced_folder "shared/", "/home/vagrant/shared", create: true, type: "rsync"
  54.  
  55.   # vagrant-hostmanager configs
  56.   config.hostmanager.enabled           = true
  57.   config.hostmanager.manage_host       = false
  58.   config.hostmanager.manage_guest      = true
  59.   config.hostmanager.ignore_private_ip = false
  60.   config.hostmanager.include_offline   = true
  61.  
  62.   # vagrant-cachier config
  63.   config.cache.scope = :machine if Vagrant.has_plugin? "vagrant-cachier"
  64.  
  65.   machines.each_with_index do |m, i|
  66.     index = i + 1
  67.     name = m[:hostname] || "box#{index}"
  68.     config.vm.define( name ) do |config|
  69.       config.vm.hostname = name
  70.       config.vm.network :private_network, ip:  "10.10.100.#{100+index}"
  71.       config.hostmanager.aliases = %W( #{name}.lab )
  72.  
  73.       config.vm.provider "virtualbox" do |v, override|
  74.         override.vm.box = "ubuntu/#{m[:release]}64"
  75.         v.cpus   = m[:cpu]
  76.         v.memory = m[:memory]*1024
  77.       end
  78.  
  79.       config.vm.provider "libvirt" do |v, override|
  80.         override.vm.box = "generic/ubuntu#{RELEASE_VERSIONS[m[:release]].sub('.','')}"
  81.         v.cpus   = m[:cpu]
  82.         v.memory = m[:memory]*1024
  83.         v.nested = true
  84.       end
  85.     end
  86.   end
  87.  
  88. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top