virtualbox-guestcontrol 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rvbox CHANGED
@@ -53,6 +53,24 @@ class VirtualBoxCommand < Clamp::Command
53
53
  end
54
54
  end
55
55
  end
56
+
57
+ subcommand "usblist", "list usb devices on host" do
58
+ def execute
59
+ VirtualBox::GuestControl::Runner.usb_devices.each do |device|
60
+ puts device.to_s
61
+ end
62
+ end
63
+ end
64
+
65
+ subcommand "usbdetach", "detach usb devices on host" do
66
+ parameter "NAME", "name of virtual machine"
67
+
68
+ def execute
69
+ VirtualBox::GuestControl::Runner.usb_devices.each do |device|
70
+ device.detach(name)
71
+ end
72
+ end
73
+ end
56
74
 
57
75
  subcommand "status", "says if the vm is booted or not" do
58
76
  parameter "NAME", "name of virtual machine"
@@ -65,6 +83,33 @@ class VirtualBoxCommand < Clamp::Command
65
83
  end
66
84
  end
67
85
  end
86
+
87
+ subcommand "list", "lists the available VMs" do
88
+ option ["--uuid"], :flag, "show only uuid", :default => false
89
+ option ["--name"], :flag, "show only name", :default => false
90
+
91
+ def execute
92
+ VirtualBox::GuestControl::Runner.virtual_machines.each do |name, uuid|
93
+ if uuid?
94
+ puts uuid
95
+ elsif name?
96
+ puts name
97
+ else
98
+ puts [name, uuid].join("\t")
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ subcommand "execute", "execute a given command" do
105
+ parameter "NAME", "name of virtual machine"
106
+ parameter "[COMMAND_PARAMETERS] ...", "command and parameters to pass to the VM", :attribute_name => :command_parameters
107
+
108
+ def execute
109
+ image_name, arguments = command_parameters
110
+ virtual_box.execute *command_parameters
111
+ end
112
+ end
68
113
  end
69
114
 
70
115
  VirtualBoxCommand.run(ARGV)
@@ -10,7 +10,7 @@ module VirtualBox
10
10
  class Runner
11
11
  include ActiveSupport::Configurable
12
12
 
13
- self.config[:default_timeout] = 120.seconds
13
+ self.config[:default_timeout] = 240.seconds
14
14
  self.config[:vbox_manage] = Shellter.which("VBoxManage")
15
15
 
16
16
  config_accessor :vbox_manage
@@ -19,19 +19,23 @@ module VirtualBox
19
19
  config_accessor :username
20
20
  config_accessor :password
21
21
 
22
- #validate that vbox_manage exists
23
- #validate that name is a valid name
22
+ def valid?
23
+
24
+ end
24
25
 
25
26
  def started?
26
- state[:GuestAdditionsRunLevel] == "2"
27
+ state[:GuestAdditionsRunLevel] == "2" || state[:GuestAdditionsRunLevel] == "3"
27
28
  end
28
29
 
29
30
  def shutdown?
30
- ["poweroff", "aborted"].include?(state[:VMState])
31
+ ["poweroff", "aborted", "saved"].include?(state[:VMState])
31
32
  end
32
33
 
33
34
  def shutdown!(force = false)
34
35
  return if shutdown?
36
+
37
+ detach_usb_devices
38
+
35
39
  if force
36
40
  Shellter.run(vbox_manage, "controlvm", ":name", "poweroff", :name => name)
37
41
  wait_until { shutdown? }
@@ -83,18 +87,19 @@ module VirtualBox
83
87
  options[:password] = password
84
88
  end
85
89
 
86
- params += ["--wait-stdout"]
87
- params += ["--timeout", ":timeout"]
88
- options[:timeout] = default_timeout.to_s
90
+ # params += ["--timeout", ":timeout"]
91
+ params += ["--wait-exit", "--wait-stdout"]
92
+ # options[:timeout] = default_timeout.to_s
89
93
 
90
94
  unless arguments.empty?
91
- params += ["--", ":arguments"]
92
- options[:arguments] = arguments.join(" ")
95
+ params += ["--"] + arguments
93
96
  end
94
97
 
95
98
  params << options
99
+
100
+ result = Shellter.run(vbox_manage, *params)
101
+ puts result.stdout.read
96
102
 
97
- Shellter.run!(vbox_manage, *params)
98
103
  end
99
104
  end
100
105
 
@@ -107,6 +112,83 @@ module VirtualBox
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ class UsbDevice
117
+ class << self
118
+ def parse(output, vbox_manage)
119
+ [].tap do |devices|
120
+ device = nil
121
+ output.lines.each do |line|
122
+ if device
123
+ if line.strip.blank?
124
+ devices << device
125
+ device = nil
126
+ else
127
+ key, value = line.split(":")
128
+ key = key.strip.gsub(" ", "_").underscore
129
+ value = value.strip
130
+ device.send(:"#{key}=", value)
131
+ end
132
+ else
133
+ next unless line =~ /UUID:/
134
+ value = line.split(":").last.strip
135
+ device = new(value, vbox_manage)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ attr_accessor :uuid, :vendor_id, :product_id, :revision
143
+ attr_accessor :manufacturer, :product, :address, :current_state
144
+ attr_accessor :serial_number
145
+ attr_accessor :vbox_manage
146
+
147
+ def initialize(uuid, vbox_manage)
148
+ self.vbox_manage = vbox_manage
149
+ self.uuid = uuid
150
+ end
151
+
152
+ def detach(name)
153
+ result = Shellter.run(vbox_manage, "controlvm", ":name", "usbdetach", ":uuid", :name => name, :uuid => uuid)
154
+ end
155
+
156
+ def attach(name)
157
+ Shellter.run!(vbox_manage, "controlvm", ":name", "usbattach", ":uuid", :name => name, :uuid => uuid)
158
+ end
159
+
160
+ def to_s
161
+ "#{product} (#{uuid}): #{current_state}"
162
+ end
163
+ end
164
+
165
+ def detach_usb_devices
166
+ self.class.usb_devices.each do |device|
167
+ device.detach(name)
168
+ end
169
+ end
170
+
171
+ def attach_usb_devices
172
+ self.class.usb_devices.each do |device|
173
+ device.attach(name)
174
+ end
175
+ end
176
+
177
+ VM_MATCHER = /^"([^"]*)" \{(.*)\}$/
178
+
179
+ class << self
180
+ def virtual_machines
181
+ result = Shellter.run!(vbox_manage, "list", "vms")
182
+ result.stdout.read.lines.map do |line|
183
+ line.scan(VM_MATCHER).first
184
+ end
185
+ end
186
+
187
+ def usb_devices
188
+ result = Shellter.run!(vbox_manage, "list", "usbhost")
189
+ UsbDevice.parse(result.stdout, vbox_manage)
190
+ end
191
+ end
110
192
 
111
193
  private
112
194
 
@@ -123,12 +205,12 @@ module VirtualBox
123
205
  end
124
206
  end
125
207
 
126
- def with_started_machine
208
+ def with_started_machine(shutdown_after = false)
127
209
  begin
128
210
  start!
129
211
  yield
130
212
  ensure
131
- shutdown!
213
+ shutdown! if shutdown_after
132
214
  end
133
215
  end
134
216
  end
@@ -1,5 +1,5 @@
1
1
  module VirtualBox
2
2
  module GuestControl
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtualbox-guestcontrol
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-28 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shellter