Class: VirtualBox::GuestControl::Machine
- Inherits:
-
Object
- Object
- VirtualBox::GuestControl::Machine
- Includes:
- ActiveSupport::Configurable
- Defined in:
- lib/virtualbox/guest_control/machine.rb
Constant Summary collapse
- VM_MATCHER =
/^"([^"]*)" \{(.*)\}$/
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#uuid ⇒ Object
Returns the value of attribute uuid.
Class Method Summary collapse
Instance Method Summary collapse
- #attach!(usb_device) ⇒ Object
- #attach_usb_devices! ⇒ Object
- #detach!(usb_device) ⇒ Object
- #detach_usb_devices! ⇒ Object
- #environment ⇒ Object
- #guest_additions_run_level ⇒ Object
- #guest_additions_started? ⇒ Boolean
-
#initialize(name, uuid) ⇒ Machine
constructor
A new instance of Machine.
- #restart! ⇒ Object
- #state ⇒ Object
- #usb_devices ⇒ Object
- #wait_until ⇒ Object
- #with_started_machine(shutdown_after = false) ⇒ Object
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
#name ⇒ Object
Returns the value of attribute name.
39 40 41 |
# File 'lib/virtualbox/guest_control/machine.rb', line 39 def name @name end |
#uuid ⇒ Object
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
.all ⇒ Object
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
224 225 226 |
# File 'lib/virtualbox/guest_control/machine.rb', line 224 def attach!(usb_device) Shellter.run!(vbox_manage, "controlvm", ":name", "usbattach", ":uuid", :name => name, :uuid => usb_device.uuid) end |
#attach_usb_devices! ⇒ Object
214 215 216 217 218 |
# File 'lib/virtualbox/guest_control/machine.rb', line 214 def attach_usb_devices! usb_devices.each do |usb_device| attach!(usb_device) end end |
#detach!(usb_device) ⇒ Object
220 221 222 |
# File 'lib/virtualbox/guest_control/machine.rb', line 220 def detach!(usb_device) Shellter.run(vbox_manage, "controlvm", ":name", "usbdetach", ":uuid", :name => name, :uuid => usb_device.uuid) end |
#detach_usb_devices! ⇒ Object
208 209 210 211 212 |
# File 'lib/virtualbox/guest_control/machine.rb', line 208 def detach_usb_devices! usb_devices.each do |usb_device| detach!(usb_device) end end |
#environment ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/virtualbox/guest_control/machine.rb', line 185 def environment 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 end |
#guest_additions_run_level ⇒ Object
165 166 167 |
# File 'lib/virtualbox/guest_control/machine.rb', line 165 def guest_additions_run_level environment[:GuestAdditionsRunLevel].to_i end |
#guest_additions_started? ⇒ Boolean
161 162 163 |
# File 'lib/virtualbox/guest_control/machine.rb', line 161 def guest_additions_started? guest_additions_run_level >= 2 end |
#restart! ⇒ Object
169 170 171 172 |
# File 'lib/virtualbox/guest_control/machine.rb', line 169 def restart! shutdown! start! end |
#state ⇒ Object
157 158 159 |
# File 'lib/virtualbox/guest_control/machine.rb', line 157 def state environment[:VMState] end |
#usb_devices ⇒ Object
204 205 206 |
# File 'lib/virtualbox/guest_control/machine.rb', line 204 def usb_devices UsbDevice.all(vbox_manage) end |
#wait_until ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/virtualbox/guest_control/machine.rb', line 144 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
195 196 197 198 199 200 201 202 |
# File 'lib/virtualbox/guest_control/machine.rb', line 195 def with_started_machine(shutdown_after = false) begin start! yield ensure shutdown! if shutdown_after end end |