virtualbox-guestcontrol 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in virtualbox-guestcontrol.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrew Eberbach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Virtualbox::Guestcontrol
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'virtualbox-guestcontrol'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install virtualbox-guestcontrol
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path("../../lib/virtualbox-guestcontrol.rb", __FILE__)
3
+
4
+ class VirtualBoxCommand < Clamp::Command
5
+ option ["-u", "--username"], "USERNAME", "username to log in as"
6
+ option ["-p", "--password"], "PASSWORD", "password to log in as"
7
+
8
+ def virtual_box
9
+ VirtualBox::GuestControl::Runner.configure do |c|
10
+ c.name = name
11
+ c.username = username
12
+ c.password = password
13
+ end
14
+ VirtualBox::GuestControl::Runner.new
15
+ end
16
+
17
+ subcommand "start", "start up the given VM" do
18
+ parameter "NAME", "name of virtual machine"
19
+
20
+ def execute
21
+ virtual_box.start!
22
+ end
23
+ end
24
+
25
+ subcommand "shutdown", "shutdown the given VM" do
26
+ parameter "NAME", "name of virtual machine"
27
+
28
+ def execute
29
+ virtual_box.shutdown!
30
+ end
31
+ end
32
+
33
+ subcommand "restart", "restart the given VM" do
34
+ parameter "NAME", "name of virtual machine"
35
+
36
+ def execute
37
+ virtual_box.restart!
38
+ end
39
+ end
40
+
41
+ subcommand "info", "list all known info about the VM" do
42
+ parameter "NAME", "name of virtual machine"
43
+
44
+ def execute
45
+ virtual_box.state.tap do |map|
46
+ map.keys.sort.each do |key|
47
+ puts [key, map[key]].join(": ")
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ subcommand "status", "says if the vm is booted or not" do
54
+ parameter "NAME", "name of virtual machine"
55
+
56
+ def execute
57
+ if virtual_box.started?
58
+ puts "VM is started"
59
+ else
60
+ puts "VM is shutdown"
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ VirtualBoxCommand.run(ARGV)
67
+
@@ -0,0 +1,136 @@
1
+ require 'timeout'
2
+ require 'shellter'
3
+ require 'active_support/configurable'
4
+ require 'active_support/time'
5
+ require 'active_support/core_ext/hash/indifferent_access'
6
+ require 'clamp'
7
+
8
+ module VirtualBox
9
+ module GuestControl
10
+ class Runner
11
+ include ActiveSupport::Configurable
12
+
13
+ self.config[:default_timeout] = 120.seconds
14
+ self.config[:vbox_manage] = Shellter.which("VBoxManage")
15
+
16
+ config_accessor :vbox_manage
17
+ config_accessor :name
18
+ config_accessor :default_timeout
19
+ config_accessor :username
20
+ config_accessor :password
21
+
22
+ #validate that vbox_manage exists
23
+ #validate that name is a valid name
24
+
25
+ def started?
26
+ state[:GuestAdditionsRunLevel] == "2"
27
+ end
28
+
29
+ def shutdown?
30
+ ["poweroff", "aborted"].include?(state[:VMState])
31
+ end
32
+
33
+ def shutdown!(force = false)
34
+ return if shutdown?
35
+ if force
36
+ Shellter.run(vbox_manage, "controlvm", ":name", "poweroff", :name => name)
37
+ wait_until { shutdown? }
38
+ else
39
+ begin
40
+ Shellter.run(vbox_manage, "controlvm", ":name", "acpipowerbutton", :name => name)
41
+ wait_until { shutdown? }
42
+ rescue TimeoutError
43
+ shutdown_machine(true)
44
+ end
45
+ end
46
+ end
47
+
48
+ def start!
49
+ return if started?
50
+ Shellter.run!(vbox_manage, "startvm", ":name", :name => name)
51
+ wait_until { started? }
52
+ end
53
+
54
+ def restart!
55
+ shutdown!
56
+ start!
57
+ end
58
+
59
+ # VBoxManage guestcontrol <vmname>|<uuid>
60
+ # exec[ute]
61
+ # --image <path to program>
62
+ # --username <name> --password <password>
63
+ # [--dos2unix]
64
+ # [--environment "<NAME>=<VALUE> [<NAME>=<VALUE>]"]
65
+ # [--timeout <msec>] [--unix2dos] [--verbose]
66
+ # [--wait-exit] [--wait-stdout] [--wait-stderr]
67
+ # [-- [<argument1>] ... [<argumentN>]]
68
+ def execute(command, *arguments)
69
+ with_started_machine do
70
+ options = arguments.extract_options!
71
+ params = ["guestcontrol", ":name", "execute", "--image", ":command"]
72
+
73
+ options[:name] = name
74
+ options[:command] = command
75
+
76
+ if username
77
+ params += ["--username", ":username"]
78
+ options[:username] = username
79
+ end
80
+
81
+ if password
82
+ params += ["--password", ":password"]
83
+ options[:password] = password
84
+ end
85
+
86
+ params += ["--wait-stdout"]
87
+ params += ["--timeout", ":timeout"]
88
+ options[:timeout] = default_timeout.to_s
89
+
90
+ unless arguments.empty?
91
+ params += ["--", ":arguments"]
92
+ options[:arguments] = arguments.join(" ")
93
+ end
94
+
95
+ params << options
96
+
97
+ Shellter.run!(vbox_manage, *params)
98
+ end
99
+ end
100
+
101
+ def state
102
+ result = Shellter.run!(vbox_manage, "showvminfo", ":name", "--machinereadable", :name => name)
103
+ {}.with_indifferent_access.tap do |map|
104
+ result.stdout.read.lines.each do |line|
105
+ name, value = line.strip.split("=").map { |y| y.gsub(/(^"|"$)/, "") }
106
+ map[name] = value
107
+ end
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def wait_until
114
+ Timeout::timeout(default_timeout) do
115
+ loop do
116
+ result = yield
117
+ if result
118
+ break
119
+ else
120
+ sleep 5
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def with_started_machine
127
+ begin
128
+ start!
129
+ yield
130
+ ensure
131
+ shutdown!
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,5 @@
1
+ module VirtualBox
2
+ module GuestControl
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/virtualbox-guestcontrol/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Andrew Eberbach"]
6
+ gem.email = ["andrew@ebertech.ca"]
7
+ gem.description = %q{Runs stuff inside of a VirtualBox VM}
8
+ gem.summary = %q{Runs stuff inside of a VirtualBox VM}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "virtualbox-guestcontrol"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = VirtualBox::GuestControl::VERSION
17
+
18
+ gem.add_dependency 'shellter'
19
+ gem.add_dependency 'activesupport', "~> 3.0"
20
+ gem.add_dependency 'clamp'
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtualbox-guestcontrol
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Eberbach
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shellter
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: clamp
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Runs stuff inside of a VirtualBox VM
63
+ email:
64
+ - andrew@ebertech.ca
65
+ executables:
66
+ - rvbox
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - bin/rvbox
76
+ - lib/virtualbox-guestcontrol.rb
77
+ - lib/virtualbox-guestcontrol/version.rb
78
+ - virtualbox-guestcontrol.gemspec
79
+ homepage: ''
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.21
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Runs stuff inside of a VirtualBox VM
103
+ test_files: []