Class: Lab::Drivers::VirtualBoxDriver

Inherits:
VmDriver
  • Object
show all
Defined in:
lib/lab/driver/virtualbox_driver.rb

Instance Attribute Summary collapse

Attributes inherited from VmDriver

#credentials, #os, #tools, #vmid

Instance Method Summary collapse

Methods inherited from VmDriver

#register, #resume

Constructor Details

#initialize(config) ⇒ VirtualBoxDriver

Returns a new instance of VirtualBoxDriver.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lab/driver/virtualbox_driver.rb', line 16

def initialize(config)

  super(config)

  ## Check to see if we already know this vm, if not, go on location
  vmid_list = ::Lab::Controllers::VirtualBoxController::config_list
  unless vmid_list.include? @vmid
    raise "Error, no such vm: #{@vmid}" unless @location
    
    if !File.exist?(@location)
      raise ArgumentError,"Error, no vm at: #{@location}"
    end
    
    # Registering @location
    @vmid = register_and_return_vmid
  end
  
  vmInfo = `VBoxManage showvminfo \"#{@vmid}\" --machinereadable`
  @location = vmInfo.scan(/CfgFile=\"(.*?)\"/).flatten[0].to_s

  if !File.exist?(@location)
    raise ArgumentError,"Couldn't find: " + @location
  end

end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



14
15
16
# File 'lib/lab/driver/virtualbox_driver.rb', line 14

def location
  @location
end

Instance Method Details

#check_file_exists(file) ⇒ Object



120
121
122
123
124
# File 'lib/lab/driver/virtualbox_driver.rb', line 120

def check_file_exists(file)
  file = filter_input(file)

  raise "Not supported by Virtual Box"
end

#cleanupObject



134
135
136
# File 'lib/lab/driver/virtualbox_driver.rb', line 134

def cleanup

end

#copy_from(from, to) ⇒ Object



104
105
106
107
108
109
# File 'lib/lab/driver/virtualbox_driver.rb', line 104

def copy_from(from, to)
  from = filter_input(from)
  to = filter_input(to)

  raise "Not supported by Virtual Box"
end

#copy_to(from, to) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/lab/driver/virtualbox_driver.rb', line 111

def copy_to(from, to)
  from = filter_input(from)
  to = filter_input(to)
  
  command = "VBoxManage guestcontrol copyto \"#{@vmid}\" \"#{from}\"  \"#{to}\" " +
       "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
  system_command(command)
end

#create_directory(directory) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/lab/driver/virtualbox_driver.rb', line 126

def create_directory(directory)
  directory = filter_input(directory)

  command = "VBoxManage guestcontrol createdir \"#{@vmid}\" \"#{directory}\" " + 
       "--username \"#{@vm_user}\" --password \"#{@vm_pass}\""
  system_command(command)
end

#create_snapshot(snapshot) ⇒ Object



80
81
82
83
# File 'lib/lab/driver/virtualbox_driver.rb', line 80

def create_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("VBoxManage snapshot \"#{@vmid}\" take #{snapshot}")
end

#delete_snapshot(snapshot) ⇒ Object



90
91
92
93
# File 'lib/lab/driver/virtualbox_driver.rb', line 90

def delete_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("VBoxManage snapshot \"#{@vmid}\" delete  #{snapshot}")
end

#pauseObject



72
73
74
# File 'lib/lab/driver/virtualbox_driver.rb', line 72

def pause
  system_command("VBoxManage controlvm \"#{@vmid}\" pause")
end

#register_and_return_vmidObject



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lab/driver/virtualbox_driver.rb', line 42

def register_and_return_vmid
  
  vmid = nil
  doc = Document.new(File.open(@location).read)
  doc.elements.each("//Machine"){|x| vmid =  x.attributes['name'] }
  
  #
  # only register if we don't already know the vmid
  #
    system_command("VBoxManage registervm \"#{@location}\"") unless ::Lab::Controllers::VirtualBoxController::config_list.include? vmid
  
  return vmid
end

#resetObject



76
77
78
# File 'lib/lab/driver/virtualbox_driver.rb', line 76

def reset
  system_command("VBoxManage controlvm \"#{@vmid}\" reset")
end

#revert_snapshot(snapshot) ⇒ Object



85
86
87
88
# File 'lib/lab/driver/virtualbox_driver.rb', line 85

def revert_snapshot(snapshot)
  snapshot = filter_input(snapshot)
  system_command("VBoxManage snapshot \"#{@vmid}\" restore #{snapshot}")
end

#run_command(command, arguments = nil) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/lab/driver/virtualbox_driver.rb', line 95

def run_command(command, arguments=nil)
  command = filter_input(command)
  arguments = filter_input(arguments)

  command = "VBoxManage guestcontrol exec \"#{@vmid}\" \"#{command}\" --username \"#{@vm_user}\"" +
       " --password \"#{@vm_pass}\" --arguments \"#{arguments}\""
  system_command(command)
end

#running?Boolean

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/lab/driver/virtualbox_driver.rb', line 138

def running?
  ## Get running Vms
  ::Lab::Controllers::VirtualBoxController::running_list.include? @vmid
end

#startObject



60
61
62
# File 'lib/lab/driver/virtualbox_driver.rb', line 60

def start
  system_command("VBoxManage startvm \"#{@vmid}\"")
end

#stopObject



64
65
66
# File 'lib/lab/driver/virtualbox_driver.rb', line 64

def stop
  system_command("VBoxManage controlvm \"#{@vmid}\" poweroff")
end

#suspendObject



68
69
70
# File 'lib/lab/driver/virtualbox_driver.rb', line 68

def suspend
  system_command("VBoxManage controlvm \"#{@vmid}\" savestate")
end

#unregisterObject



56
57
58
# File 'lib/lab/driver/virtualbox_driver.rb', line 56

def unregister
  system_command("VBoxManage unregistervm \"#{@vmid}\"")
end