lib/instalatron.rb in instalatron-0.1.3 vs lib/instalatron.rb in instalatron-0.1.4

- old
+ new

@@ -2,12 +2,34 @@ require 'yaml' require 'virtualbox' module Instalatron - VERSION = '0.1.3' + VERSION = '0.1.4' + # + # NIC is nic1, nic2, etc + # device is eth0, eth1, etc + # mode is either :nat, :bridged, :hostonly + # + def self.set_nic_mode(vm_name, nic, device, mode) + err = `VBoxManage controlvm #{vm_name} #{nic} #{mode.to_s} #{device} 2>&1` + puts err + raise Exception.new(err) if $? != 0 + end + + def self.add_ssh_nat_mapping(vm_name, guest_port, host_port) + vm=VirtualBox::VM.find(vm_name) + port = VirtualBox::NATForwardedPort.new + port.name = "guestssh" + port.guestport = guest_port + port.hostport = host_port + vm.network_adapters[0].nat_driver.forwarded_ports << port + port.save + vm.save + end + def self.destroy_vm(vm_name) `VBoxManage controlvm '#{vm_name}' poweroff > /dev/null 2>&1` # dumb sleep 1 `VBoxManage unregistervm '#{vm_name}' --delete > /dev/null 2>&1` @@ -21,10 +43,11 @@ end os_type = params[:os_type] || 'RedHat_64' vboxcmd = params[:vboxcmd] || 'VBoxManage' vm_memory = params[:vm_memory] || 512 vm_cpus = params[:vm_cpus] || 1 + vm_disk_size = params[:vm_disk_size].to_i || 8192 if params[:headless].nil? params[:headless] = false end headless = params[:headless] ? 'headless':'gui' # listing os types @@ -42,11 +65,11 @@ else exit 1 end vm=VirtualBox::VM.find(vm_name) - vm.memory_size= vm_memory + vm.memory_size = vm_memory.to_i vm.os_type_id = os_type vm.cpu_count = vm_cpus vm.name = vm_name vm.boot_order[0]=:dvd @@ -58,11 +81,11 @@ # Create DISK place = `#{vboxcmd} list systemproperties|grep '^Default machine'|cut -d ':' -f 2|sed -e 's/^[ ]*//'`.strip.chomp disk_file = "#{place}/#{vm_name}/#{vm_name}.vdi" - `#{vboxcmd} createhd --filename '#{disk_file}' --size 8192 --format VDI >/dev/null 2>&1` + `#{vboxcmd} createhd --filename '#{disk_file}' --size #{vm_disk_size} --format VDI >/dev/null 2>&1` # Add IDE/Sata Controllers `#{vboxcmd} storagectl '#{vm_name}' --name 'SATA Controller' --add sata --hostiocache off >/dev/null 2>&1` `#{vboxcmd} storagectl '#{vm_name}' --name 'IDE Controller' --add ide >/dev/null 2>&1` @@ -81,21 +104,21 @@ seq.each do |str| keycodes = string_to_keycode str keycodes.split.each do |k| `VBoxManage controlvm #{vm_name} keyboardputscancode '#{k}' >/dev/null 2>&1` end - sleep key_press_delay.to_i + sleep key_press_delay.to_f end end def self.grab_screenshot(vm_name, dest_file = nil) if dest_file.nil? dest_file = vm_name + "_#{Time.now.to_f}.png" end `VBoxManage controlvm #{vm_name} screenshotpng #{dest_file} >/dev/null 2>&1` end - def self.same_image?(ref_image, new_img, threshold = 1500) + def self.same_image?(ref_image, new_img, threshold = 2000) `file #{ref_image}` =~ /(\d+\sx\s\d+)/ geom1 = $1 `file #{new_img}` =~ /(\d+\sx\s\d+)/ geom2 = $1