Class: Beaker::Vagrant
- Inherits:
-
Hypervisor
- Object
- Hypervisor
- Beaker::Vagrant
- Defined in:
- lib/beaker/hypervisor/vagrant.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
- #copy_ssh_to_root(host) ⇒ Object
- #hack_etc_hosts(hosts) ⇒ Object
-
#initialize(vagrant_hosts, options) ⇒ Vagrant
constructor
A new instance of Vagrant.
- #make_vfile(hosts) ⇒ Object
- #provision ⇒ Object
- #rand_chunk ⇒ Object
- #randip ⇒ Object
-
#randmac ⇒ String
Return a random mac address.
- #set_ssh_config(host, user) ⇒ Object
- #vagrant_cmd(args) ⇒ Object
Methods inherited from Hypervisor
Constructor Details
#initialize(vagrant_hosts, options) ⇒ Vagrant
Returns a new instance of Vagrant.
83 84 85 86 87 88 89 90 91 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 83 def initialize(vagrant_hosts, ) require 'tempfile' @options = @logger = [:logger] @temp_files = [] @vagrant_hosts = vagrant_hosts @vagrant_path = File.(File.join(File.basename(__FILE__), '..', 'vagrant_files', [:hosts_file])) end |
Instance Method Details
#cleanup ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 117 def cleanup @logger.debug "removing temporory ssh-config files per-vagrant box" @temp_files.each do |f| f.close() end @logger.notify "Destroying vagrant boxes" vagrant_cmd("destroy --force") end |
#copy_ssh_to_root(host) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 56 def copy_ssh_to_root host #make is possible to log in as root by copying the ssh dir to root's account @logger.debug "Give root a copy of vagrant's keys" if host['platform'] =~ /windows/ host.exec(Command.new('sudo su -c "cp -r .ssh /home/Administrator/."')) else host.exec(Command.new('sudo su -c "cp -r .ssh /root/."')) end end |
#hack_etc_hosts(hosts) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 46 def hack_etc_hosts hosts etc_hosts = "127.0.0.1\tlocalhost localhost.localdomain\n" hosts.each do |host| etc_hosts += "#{host['ip'].to_s}\t#{host.name}\n" end hosts.each do |host| set_etc_hosts(host, etc_hosts) end end |
#make_vfile(hosts) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 21 def make_vfile hosts #HACK HACK HACK - add checks here to ensure that we have box + box_url #generate the VagrantFile vagrant_file = "Vagrant.configure(\"2\") do |c|\n" hosts.each do |host| host['ip'] ||= randip #use the existing ip, otherwise default to a random ip vagrant_file << " c.vm.define '#{host.name}' do |v|\n" vagrant_file << " v.vm.hostname = '#{host.name}'\n" vagrant_file << " v.vm.box = '#{host['box']}'\n" vagrant_file << " v.vm.box_url = '#{host['box_url']}'\n" unless host['box_url'].nil? vagrant_file << " v.vm.base_mac = '#{randmac}'\n" vagrant_file << " v.vm.network :private_network, ip: \"#{host['ip'].to_s}\", :netmask => \"255.255.0.0\"\n" vagrant_file << " end\n" @logger.debug "created Vagrantfile for VagrantHost #{host.name}" end vagrant_file << " c.vm.provider :virtualbox do |vb|\n" vagrant_file << " vb.customize [\"modifyvm\", :id, \"--memory\", \"1024\"]\n" vagrant_file << " end\n" vagrant_file << "end\n" FileUtils.mkdir_p(@vagrant_path) File.open(File.(File.join(@vagrant_path, "Vagrantfile")), 'w') do |f| f.write(vagrant_file) end end |
#provision ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 93 def provision make_vfile @vagrant_hosts #stop anything currently running, that way vagrant up will re-do networking on existing boxes vagrant_cmd("halt") vagrant_cmd("up") @logger.debug "configure vagrant boxes (set ssh-config, switch to root user, hack etc/hosts)" @vagrant_hosts.each do |host| default_user = host['user'] set_ssh_config host, 'vagrant' copy_ssh_to_root host #shut down connection, will reconnect on next exec host.close set_ssh_config host, default_user end hack_etc_hosts @vagrant_hosts end |
#rand_chunk ⇒ Object
13 14 15 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 13 def rand_chunk (2 + rand(252)).to_s #don't want a 0, 1, or a 255 end |
#randip ⇒ Object
17 18 19 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 17 def randip "10.255.#{rand_chunk}.#{rand_chunk}" end |
#randmac ⇒ String
Return a random mac address
9 10 11 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 9 def randmac "080027" + (1..3).map{"%0.2X"%rand(256)}.join end |
#set_ssh_config(host, user) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 66 def set_ssh_config host, user f = Tempfile.new("#{host.name}") ssh_config = Dir.chdir(@vagrant_path) do stdin, stdout = Open3.popen3('vagrant', 'ssh-config', host.name) stdout.read end #replace hostname with ip ssh_config = ssh_config.gsub(/#{host.name}/, host['ip']) #set the user ssh_config = ssh_config.gsub(/User vagrant/, "User #{user}") f.write(ssh_config) f.rewind host['ssh'] = {:config => f.path()} host['user'] = user @temp_files << f end |
#vagrant_cmd(args) ⇒ Object
126 127 128 129 130 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 126 def vagrant_cmd(args) Dir.chdir(@vagrant_path) do system("vagrant #{args}") end end |