Class: Sahara::Session::Virtualbox

Inherits:
Object
  • Object
show all
Defined in:
lib/sahara/session/virtualbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Virtualbox

Returns a new instance of Virtualbox.



5
6
7
8
9
10
# File 'lib/sahara/session/virtualbox.rb', line 5

def initialize(machine)
  @machine=machine
  @instance_id = @machine.id
  @vboxcmd=determine_vboxcmd
  @sandboxname="sahara-sandbox"
end

Instance Method Details

#commitObject



58
59
60
61
# File 'lib/sahara/session/virtualbox.rb', line 58

def commit
  `#{@vboxcmd} snapshot "#{@instance_id}" delete "#{@sandboxname}"`
  `#{@vboxcmd} snapshot "#{@instance_id}" take "#{@sandboxname}" --pause`
end

#determine_vboxcmdObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sahara/session/virtualbox.rb', line 12

def determine_vboxcmd
  if windows?
    # On Windows, we use the VBOX_INSTALL_PATH and VBOX_MSI_INSTALL_PATH environmental
    if ENV.has_key?("VBOX_INSTALL_PATH") || ENV.has_key?("VBOX_MSI_INSTALL_PATH")
      # The path usually ends with a \ but we make sure here
      path = ENV["VBOX_INSTALL_PATH"] || ENV["VBOX_MSI_INSTALL_PATH"]
      path = File.join(path, "VBoxManage.exe")
      return "\"#{path}\""
    end
  else
    # for other platforms assume it is on the PATH
    return "VBoxManage"
  end
end

#is_snapshot_mode_on?Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/sahara/session/virtualbox.rb', line 45

def is_snapshot_mode_on?
  snapshots=self.list_snapshots
  return snapshots.include?(@sandboxname)
end

#is_vm_created?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/sahara/session/virtualbox.rb', line 78

def is_vm_created?
  return !@machine.id.nil?
end

#list_snapshotsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/sahara/session/virtualbox.rb', line 34

def list_snapshots
  snapshotlist=Array.new
  output = `#{@vboxcmd} showvminfo --machinereadable "#{@instance_id}"`
  snapshotresult=output.scan(/SnapshotName.*=(.*)/).flatten
  snapshotresult.each do |result|
    clean=result.gsub(/\"/,'').chomp
    snapshotlist << clean
  end
  snapshotlist
end

#offObject



50
51
52
# File 'lib/sahara/session/virtualbox.rb', line 50

def off
  `#{@vboxcmd} snapshot "#{@instance_id}" delete "#{@sandboxname}" `
end

#onObject



54
55
56
# File 'lib/sahara/session/virtualbox.rb', line 54

def on
  `#{@vboxcmd} snapshot "#{@instance_id}" take "#{@sandboxname}" --pause`
end

#rollbackObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sahara/session/virtualbox.rb', line 63

def rollback
  `#{@vboxcmd} controlvm "#{@instance_id}" poweroff `
  sleep 2
  `#{@vboxcmd} snapshot "#{@instance_id}" restore "#{@sandboxname}"`

  gui_boot = @machine.provider_config.gui
  if gui_boot
    boot_mode = "gui"
  else
    boot_mode = "headless"
  end
  # restore boot mode
  `#{@vboxcmd} startvm --type #{boot_mode} "#{@instance_id}" `
end

#windows?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/sahara/session/virtualbox.rb', line 27

def windows?
  %W[mingw mswin].each do |text|
    return true if RbConfig::CONFIG["host_os"].downcase.include?(text)
  end
  false
end