Class: VirtualBox::GuestControl::Machine

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/virtualbox/guest_control/machine.rb

Constant Summary collapse

VM_MATCHER =
/^"([^"]*)" \{(.*)\}$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uuid) ⇒ Machine

Returns a new instance of Machine.



34
35
36
37
# File 'lib/virtualbox/guest_control/machine.rb', line 34

def initialize(name, uuid)
  @name = name
  @uuid = uuid
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



39
40
41
# File 'lib/virtualbox/guest_control/machine.rb', line 39

def name
  @name
end

#uuidObject

Returns the value of attribute uuid.



40
41
42
# File 'lib/virtualbox/guest_control/machine.rb', line 40

def uuid
  @uuid
end

Class Method Details

.allObject



18
19
20
21
22
23
# File 'lib/virtualbox/guest_control/machine.rb', line 18

def all
  result = Shellter.run!(vbox_manage, "list", "vms")
  result.stdout.read.lines.map do |line|
    new(*line.scan(VM_MATCHER).first)
  end
end

.find_by_name(name) ⇒ Object



29
30
31
# File 'lib/virtualbox/guest_control/machine.rb', line 29

def find_by_name(name)
  all.detect { |machine| machine.name == name }
end

.find_by_uuid(uuid) ⇒ Object



25
26
27
# File 'lib/virtualbox/guest_control/machine.rb', line 25

def find_by_uuid(uuid)
  all.detect { |machine| machine.uuid == uuid }
end

Instance Method Details

#attach!(usb_device) ⇒ Object



247
248
249
# File 'lib/virtualbox/guest_control/machine.rb', line 247

def attach!(usb_device)
  Shellter.run!(vbox_manage, "controlvm", ":name", "usbattach", ":uuid", :name => name, :uuid => usb_device.uuid)
end

#attach_usb_devices!Object



237
238
239
240
241
# File 'lib/virtualbox/guest_control/machine.rb', line 237

def attach_usb_devices!
  usb_devices.each do |usb_device|
    attach!(usb_device)
  end
end

#detach!(usb_device) ⇒ Object



243
244
245
# File 'lib/virtualbox/guest_control/machine.rb', line 243

def detach!(usb_device)
  Shellter.run(vbox_manage, "controlvm", ":name", "usbdetach", ":uuid", :name => name, :uuid => usb_device.uuid)
end

#detach_usb_devices!Object



231
232
233
234
235
# File 'lib/virtualbox/guest_control/machine.rb', line 231

def detach_usb_devices!
  usb_devices.each do |usb_device|
    detach!(usb_device)
  end
end

#environmentObject



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/virtualbox/guest_control/machine.rb', line 204

def environment
  begin
    result = Shellter.run!(vbox_manage, "showvminfo", ":name", "--machinereadable", :name => name)
    {}.with_indifferent_access.tap do |map|
      result.stdout.read.lines.each do |line|
        name, value = line.strip.split("=").map { |y| y.gsub(/(^"|"$)/, "") }
        map[name] = value
      end
    end
  rescue
    {}
  end
end

#guest_additions_run_levelObject



184
185
186
# File 'lib/virtualbox/guest_control/machine.rb', line 184

def guest_additions_run_level
  environment[:GuestAdditionsRunLevel].to_i
end

#guest_additions_started?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/virtualbox/guest_control/machine.rb', line 180

def guest_additions_started?
  guest_additions_run_level >= 2
end

#restart!Object



188
189
190
191
# File 'lib/virtualbox/guest_control/machine.rb', line 188

def restart!
  shutdown!
  start!
end

#stateObject



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/virtualbox/guest_control/machine.rb', line 167

def state
  state_name = environment[:VMState]
  if state_name == "running"
    if guest_additions_started?
      state_name
    else
      "starting"
    end
  else
    state_name
  end
end

#usb_devicesObject



227
228
229
# File 'lib/virtualbox/guest_control/machine.rb', line 227

def usb_devices
  UsbDevice.all(vbox_manage)
end

#wait_untilObject



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/virtualbox/guest_control/machine.rb', line 154

def wait_until
  Timeout::timeout(default_timeout) do
    loop do
      result = yield
      if result
        break
      else
        sleep 5
      end
    end
  end
end

#with_started_machine(shutdown_after = false) ⇒ Object



218
219
220
221
222
223
224
225
# File 'lib/virtualbox/guest_control/machine.rb', line 218

def with_started_machine(shutdown_after = false)
  begin
    start!
    yield
  ensure
    shutdown! if shutdown_after
  end
end