Class: Beaker::Vagrant
- Inherits:
-
Hypervisor
- Object
- Hypervisor
- Beaker::Vagrant
- Defined in:
- lib/beaker/hypervisor/vagrant.rb
Direct Known Subclasses
VagrantCustom, VagrantDesktop, VagrantFusion, VagrantLibvirt, VagrantParallels, VagrantVirtualbox, VagrantWorkstation
Defined Under Namespace
Classes: MountFolder
Class Method Summary collapse
- .cpus(host, options) ⇒ Object
- .memsize(host, options) ⇒ Object
- .provider_vfile_section(host, options) ⇒ Object
Instance Method Summary collapse
- #cleanup ⇒ Object
- #configure(opts = {}) ⇒ Object
- #connection_preference(_host) ⇒ Object
-
#initialize(vagrant_hosts, options) ⇒ Vagrant
constructor
A new instance of Vagrant.
- #make_vfile(hosts, options = {}) ⇒ Object
- #private_network_generator(host) ⇒ Object
- #provision(provider = nil) ⇒ Object
- #rand_chunk ⇒ Object
- #randip(hypervisor = nil) ⇒ Object
- #set_all_ssh_config ⇒ Object
- #set_ssh_config(host, user) ⇒ Object
- #shell_provisioner_generator(provisioner_config) ⇒ Object
- #vagrant_cmd(args) ⇒ Object
Constructor Details
#initialize(vagrant_hosts, options) ⇒ Vagrant
Returns a new instance of Vagrant.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 202 def initialize(vagrant_hosts, ) require 'tempfile' @options = @logger = [:logger] @hosts = vagrant_hosts vagrant_directory = if [:hosts_file_generated] "beaker_hostgenerator_#{@hosts.map(&:name).join('-')}" else "beaker_#{File.basename([:hosts_file])}" end @vagrant_path = File.(File.join('.vagrant', 'beaker_vagrant_files', vagrant_directory)) @vagrant_file = File.(File.join(@vagrant_path, 'Vagrantfile')) @vagrant_env = {} end |
Class Method Details
.cpus(host, options) ⇒ Object
288 289 290 291 292 293 294 295 296 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 288 def self.cpus(host, ) if host['vagrant_cpus'] host['vagrant_cpus'] elsif ['vagrant_cpus'] ['vagrant_cpus'] else '1' end end |
.memsize(host, options) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 298 def self.memsize(host, ) if host['vagrant_memsize'] host['vagrant_memsize'] elsif ['vagrant_memsize'] ['vagrant_memsize'] elsif /windows/.match?(host['platform']) '2048' else '1024' end end |
.provider_vfile_section(host, options) ⇒ Object
141 142 143 144 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 141 def self.provider_vfile_section(host, ) # Backwards compatibility; default to virtualbox Beaker::VagrantVirtualbox.provider_vfile_section(host, ) end |
Instance Method Details
#cleanup ⇒ Object
258 259 260 261 262 263 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 258 def cleanup @logger.debug 'removing temporary ssh-config files per-vagrant box' @logger.notify 'Destroying vagrant boxes' vagrant_cmd('destroy --force') FileUtils.rm_rf(@vagrant_path) end |
#configure(opts = {}) ⇒ Object
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 217 def configure(opts = {}) unless @options[:provision] unless File.file?(@vagrant_file) raise "Beaker is configured with provision = false but no vagrant file was found at #{@vagrant_file}. You need to enable provision" end set_all_ssh_config end super end |
#connection_preference(_host) ⇒ Object
30 31 32 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 30 def connection_preference(_host) [:hostname] end |
#make_vfile(hosts, options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 46 def make_vfile(hosts, = {}) # HACK: HACK HACK - add checks here to ensure that we have box + box_url # generate the VagrantFile v_file = "Vagrant.configure(\"2\") do |c|\n" v_file << " c.ssh.forward_agent = true\n" if [:forward_ssh_agent] == true v_file << " c.ssh.insert_key = false\n" hosts.each do |host| host.name.tr!('_', '-') # Rewrite Hostname with hyphens instead of underscores to get legal hostname set_host_default_ip(host) v_file << " c.vm.define '#{host.name}' do |v|\n" v_file << " v.vm.hostname = '#{host.name}'\n" v_file << " v.vm.box = '#{host['box']}'\n" v_file << " v.vm.box_url = '#{host['box_url']}'\n" unless host['box_url'].nil? v_file << " v.vm.box_version = '#{host['box_version']}'\n" unless host['box_version'].nil? unless host['box_download_insecure'].nil? v_file << " v.vm.box_download_insecure = '#{host['box_download_insecure']}'\n" end v_file << " v.vm.box_check_update = '#{host['box_check_update'] ||= 'true'}'\n" v_file << " v.vm.synced_folder '.', '/vagrant', disabled: true\n" if host['synced_folder'] == 'disabled' v_file << shell_provisioner_generator(host['shell_provisioner']) if host['shell_provisioner'] v_file << private_network_generator(host) if host['ip'] unless host['mount_folders'].nil? host['mount_folders'].each do |name, folder| mount_folder = Vagrant::MountFolder.new(name, folder) if mount_folder.required_keys_present? v_file << " v.vm.synced_folder '#{mount_folder.from}', '#{mount_folder.to}', create: true\n" else @logger.warn "Using 'mount_folders' requires options 'from' and 'to' for vagrant node, given #{folder.inspect}" end end end unless host['forwarded_ports'].nil? host['forwarded_ports'].each_value do |port| fwd = ' v.vm.network :forwarded_port,' fwd << " protocol: '#{port[:protocol]}'," unless port[:protocol].nil? fwd << " guest_ip: '#{port[:to_ip]}'," unless port[:to_ip].nil? fwd << " guest: #{port[:to]}," fwd << " host_ip: '#{port[:from_ip]}'," unless port[:from_ip].nil? fwd << " host: #{port[:from]}" fwd << "\n" v_file << fwd end end if /windows/i.match?(host['platform']) # due to a regression bug in versions of vagrant 1.6.2, 1.6.3, 1.6.4, >= 1.7.3 ssh fails to forward # automatically (note <=1.6.1, 1.6.5, 1.7.0 - 1.7.2 are uneffected) # Explicitly setting SSH port forwarding due to this bug v_file << " v.vm.network :forwarded_port, guest: 22, host: 2222, id: 'ssh', auto_correct: true\n" v_file << " v.vm.network :forwarded_port, guest: 3389, host: 3389, id: 'rdp', auto_correct: true\n" v_file << " v.vm.network :forwarded_port, guest: 5985, host: 5985, id: 'winrm', auto_correct: true\n" v_file << " v.vm.guest = :windows\n" v_file << " v.vm.communicator = 'winrm'\n" end if /osx/i.match?(host['platform']) v_file << " v.vm.network 'private_network', ip: '10.0.1.10'\n" v_file << " v.vm.synced_folder '.', '/vagrant', :nfs => true\n" end if /freebsd/i.match?(host['platform']) v_file << " v.ssh.shell = 'sh'\n" v_file << " v.vm.guest = :freebsd\n" # FreeBSD NFS has a character restriction of 88 characters # So you can enable it but if your module has a long name it probably won't work... # So to keep things simple let's rsync by default! # # Further reading if interested: # http://www.secnetix.de/olli/FreeBSD/mnamelen.hawk # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=167105 # v_file << if host['vagrant_freebsd_nfs'].nil? " v.vm.synced_folder '.', '/vagrant', type: 'rsync'\n" else " v.vm.synced_folder '.', '/vagrant', :nfs => true\n" end end v_file << self.class.provider_vfile_section(host, ) v_file << " end\n" @logger.debug "created Vagrantfile for VagrantHost #{host.name}" end v_file << "end\n" # In case this is called directly FileUtils.mkdir_p(@vagrant_path) File.write(@vagrant_file, v_file) end |
#private_network_generator(host) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 21 def private_network_generator(host) private_network_string = " v.vm.network :private_network, ip: \"#{host['ip']}\", :netmask => \"#{host['netmask'] ||= '255.255.0.0'}\"" private_network_string << if host['network_mac'] ", :mac => \"#{host['network_mac']}\"\n" else "\n" end end |
#provision(provider = nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 228 def provision(provider = nil) FileUtils.mkdir_p(@vagrant_path) # setting up new vagrant hosts # make sure that any old boxes are dead dead dead begin vagrant_cmd('destroy --force') if File.file?(@vagrant_file) rescue RuntimeError => e # LATER: use <<~MESSAGE once we're on Ruby 2.3 @logger.debug(%( Beaker failed to destroy the existing VM's. If you think this is an error or you upgraded from an older version of beaker try verifying the VM exists and deleting the existing Vagrantfile if you believe it is safe to do so. WARNING: If a VM still exists please run 'vagrant destroy'. cd #{@vagrant_path} vagrant status vagrant destroy # only need to run this is a VM is not created rm #{@vagrant_file} # only do this if all VM's are actually destroyed ).each_line.map(&:strip).join("\n")) raise e end make_vfile @hosts, @options vagrant_cmd("up#{" --provider #{provider}" if provider}") set_all_ssh_config end |
#rand_chunk ⇒ Object
8 9 10 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 8 def rand_chunk rand(2..254).to_s # don't want a 0, 1, or a 255 end |
#randip(hypervisor = nil) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 12 def randip(hypervisor = nil) case hypervisor when /libvirt/ "10.254.#{rand_chunk}.#{rand_chunk}" else "10.255.#{rand_chunk}.#{rand_chunk}" end end |
#set_all_ssh_config ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 146 def set_all_ssh_config @logger.debug 'configure vagrant boxes (set ssh-config, switch to root user, hack etc/hosts)' @hosts.each do |host| if /windows/.match?(host[:platform]) @logger.debug "skip ssh hacks on windows box #{host[:name]}" set_ssh_config host, host['user'] next end default_user = host['user'] set_ssh_config host, 'vagrant' # copy vagrant's keys to roots home dir, to allow for login as root copy_ssh_to_root host, @options # ensure that root login is enabled for this host enable_root_login host, @options # shut down connection, will reconnect on next exec host.close set_ssh_config host, default_user # allow the user to set the env begin host.ssh_permit_user_environment host.close rescue ArgumentError => e @logger.debug("Failed to set SshPermitUserEnvironment. #{e}") end end hack_etc_hosts @hosts, @options end |
#set_ssh_config(host, user) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 180 def set_ssh_config(host, user) return unless Dir.exist?(@vagrant_path) ssh_config = Dir.chdir(@vagrant_path) do stdout, _, status = with_unbundled_env { Open3.capture3(@vagrant_env, 'vagrant', 'ssh-config', host.name) } raise "Failed to 'vagrant ssh-config' for #{host.name}" unless status.success? Tempfile.create do |f| f.write(stdout) f.flush Net::SSH::Config.for(host.name, [f.path]) end end ssh_config[:user] = user ssh_config[:keys_only] = false if @options[:forward_ssh_agent] == true host['ssh'] = host['ssh'].merge(ssh_config) host['user'] = user end |
#shell_provisioner_generator(provisioner_config) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 34 def shell_provisioner_generator(provisioner_config) if provisioner_config['path'].nil? || provisioner_config['path'].empty? raise 'No path defined for shell_provisioner or path empty' end if provisioner_config['args'].nil? " v.vm.provision 'shell', :path => '#{provisioner_config['path']}'\n" else " v.vm.provision 'shell', :path => '#{provisioner_config['path']}', :args => '#{provisioner_config['args']}' \n" end end |
#vagrant_cmd(args) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/beaker/hypervisor/vagrant.rb', line 265 def vagrant_cmd(args) Dir.chdir(@vagrant_path) do retries ||= 0 with_unbundled_env do Open3.popen3(@vagrant_env, "vagrant #{args}") do |_stdin, stdout, stderr, wait_thr| while line = stdout.gets @logger.info(line) end raise "Failed to exec 'vagrant #{args}'. Error was #{stderr.read}" unless wait_thr.value.success? end end rescue StandardError => e if /WinRM/m.match?(e.to_s) sleep(10) retry if (retries += 1) < 6 end raise e end end |