virtuoso-prototype 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (C) 2010 3Crowd Technologies, Inc.
2
+
3
+ This program is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU General Public License for more details.
12
+
13
+ You should have received a copy of the GNU General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/README ADDED
@@ -0,0 +1,2 @@
1
+ README for Virtuoso prototype
2
+ =============================
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift(File.expand_path('..',File.dirname(__FILE__))) unless $:.include?(File.expand_path('..',File.dirname(__FILE__)))
4
+
5
+ require 'lib/virtuoso'
6
+
7
+ cli = Virtuoso::CLI.new
8
+ cli.run(ARGV)
@@ -0,0 +1,80 @@
1
+ $:.unshift(File.expand_path(File.dirname(__FILE__))) unless $:.include?(File.expand_path(File.dirname(__FILE__)))
2
+
3
+ require 'virtuoso/errors'
4
+ require 'virtuoso/virtualbox'
5
+
6
+ #Choice, a command line option parser
7
+ require 'choice'
8
+ require 'logger'
9
+
10
+ module Virtuoso
11
+
12
+ class CLI
13
+
14
+ def initialize
15
+ @log = Logger.new(STDOUT)
16
+ @log.level = Logger::DEBUG
17
+ @vm_interface = VirtualBox::Interface.new
18
+ end
19
+
20
+ def run(arguments=[])
21
+ setup_cli_arguments!
22
+ options = parse_arguments(arguments)
23
+ ensure_virtualbox_installed
24
+ perform_action!(options[:operation], options[:vm_name], options[:bond_interface])
25
+ end
26
+
27
+ private
28
+
29
+ def perform_action!(operation, vm_name, bond_interface)
30
+ case operation
31
+ when 'create' then
32
+ @vm_interface.create!(vm_name, bond_interface)
33
+ when 'destroy' then
34
+ @vm_interface.destroy!(vm_name, :delete)
35
+ else
36
+ raise RuntimeError.new("Got operation option that should not be valid: #{operation}")
37
+ end
38
+ end
39
+
40
+ def ensure_virtualbox_installed
41
+ unless @vm_interface.installed?
42
+ raise Error::VirtualBoxNotInstalledError
43
+ end
44
+ end
45
+
46
+ def setup_cli_arguments!
47
+ Choice.options do
48
+ header 'Virtuoso Virtual Manager'
49
+ header 'Specific options:'
50
+
51
+ option :operation do
52
+ short '-o'
53
+ long '--operation=OPERATION'
54
+ desc 'The operation you wish to perform (create, or destroy) (default: create)'
55
+ valid %w[create destroy]
56
+ default 'create'
57
+ end
58
+
59
+ option :vm_name, :required => true do
60
+ short '-n'
61
+ long '--vmname=VMNAME'
62
+ desc 'The virtual machine you wish to operate on (required)'
63
+ end
64
+
65
+ option :bond_interface, :required => true do
66
+ short '-i'
67
+ long '--bond_interface=INTERFACE0'
68
+ desc 'The network interface on the host machine to which the virtual network interface should bond'
69
+ end
70
+ end
71
+ end
72
+
73
+ def parse_arguments(arguments)
74
+ @log.debug("Got choices: #{ Choice.choices.inspect }")
75
+ Choice.choices
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,4 @@
1
+ require 'virtuoso/errors/disk_not_created'
2
+ require 'virtuoso/errors/virtualbox_not_found'
3
+ require 'virtuoso/errors/vm_not_found'
4
+ require 'virtuoso/errors/vm_not_created'
@@ -0,0 +1,8 @@
1
+ module Virtuoso
2
+ module Errors
3
+
4
+ class DiskNotCreatedError < StandardError
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Virtuoso
2
+ module Errors
3
+
4
+ class VirtualBoxNotFoundError < StandardError
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Virtuoso
2
+ module Errors
3
+
4
+ class VMNotCreatedError < StandardError
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Virtuoso
2
+ module Errors
3
+
4
+ class VMNotFoundError < StandardError
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ require 'virtuoso/virtualbox/interface'
@@ -0,0 +1,109 @@
1
+ require 'rubygems'
2
+ require 'virtualbox'
3
+ require 'systemu'
4
+ require 'logger'
5
+ require 'uuid'
6
+
7
+ module Virtuoso
8
+ module VirtualBox
9
+
10
+ class Interface
11
+
12
+ def initialize
13
+ @log = Logger.new(STDOUT)
14
+ @virtualbox = Hash.new
15
+ @virtualbox[:version] = ::VirtualBox.version
16
+ end
17
+
18
+ def create!(vm_name, bond_interface)
19
+ vm = create_vm!(vm_name)
20
+ vm = setup_vm!(vm)
21
+ vm.reload
22
+ storage_controller = add_storage_controller!(vm)
23
+ disk = create_disk!(vm_name)
24
+ attach_disk!(vm, storage_controller, disk)
25
+ add_bridged_network_interface!(vm, 1, bond_interface)
26
+ end
27
+
28
+ def destroy!(vm_name, physically_delete_media=true)
29
+ vm = ::VirtualBox::VM.find(vm_name)
30
+ raise Errors::VMNotFoundError if vm.nil?
31
+ vm.destroy(:destroy_medium => physically_delete_media )
32
+ end
33
+
34
+ def installed?
35
+ @virtualbox[:version].nil? ? false : true
36
+ end
37
+
38
+ private
39
+
40
+ def add_bridged_network_interface!(vm, interface_number, bridge_interface)
41
+ status, stdout, stderr = systemu "VBoxManage modifyvm --nic#{interface_number} bridged --bridgeadapter#{interface_number} #{bridge_interface}"
42
+ @log.debug("Ran VBoxManage add network interface: output (#{status}):")
43
+ @log.debug("Standard out: " + stdout)
44
+ @log.debug("Standard err: " + stderr)
45
+ @ Reload the vm config, to pick up command line changes
46
+ vm.reload
47
+ vm.network_interfaces[interface_number-1]
48
+ end
49
+
50
+ def add_storage_controller!(vm)
51
+ uuid = UUID.new.generate
52
+ #FIXME: we have to shell out here since the virtualbox gem does not provide an easy method for creating storage controllers from scratch
53
+ status, stdout, stderr = systemu "VBoxManage storagectl #{vm.uuid} --name #{uuid} --add sata"
54
+ @log.debug("Ran VBoxManage add storage controller: output (#{status}):")
55
+ @log.debug("Standard out: " + stdout)
56
+ @log.debug("Standard err: " + stderr)
57
+ # Reload the vm config, to pick up the command line changes
58
+ vm.reload
59
+ vm.storage_controllers.select{|controller| controller.name == uuid }.first
60
+ end
61
+
62
+ def attach_disk!(vm, storage_controller, disk)
63
+ @log.debug("Got storage_controller #{storage_controller.inspect}, Got Disk #{disk.inspect}")
64
+ #FIXME: we have to shell out here since the virtualbox gem does not provide an easy to find method for attaching disk images to VMs
65
+ command = "VBoxManage storageattach #{vm.uuid} --storagectl #{storage_controller.name} --port 0 --device 0 --type hdd --medium #{disk.uuid} "
66
+ @log.debug("Running: #{command}")
67
+ status, disk_attach_stdout, disk_attach_stderr = systemu(command)
68
+ @log.debug("Ran VBoxManage storageattach output (#{status}):")
69
+ @log.debug("Standard out: " + disk_attach_stdout)
70
+ @log.debug("Standard err: " + disk_attach_stderr)
71
+ end
72
+
73
+ def setup_vm!(vm)
74
+ vm.vram_size = 24 #megabytes
75
+ vm.memory_size = 512 #megabytes
76
+ vm.save
77
+ return vm
78
+ end
79
+
80
+ def create_vm!(vm_name)
81
+ #FIXME: we have to shell out here since the virtualbox gem does not provide a method for creating new VMs directly through the library
82
+ status, vm_create_output_stdout, vm_create_output_stderr = systemu "VBoxManage createvm --name #{vm_name} --register"
83
+ @log.debug("Ran VBoxManage createvm output (#{status}):")
84
+ @log.debug("Standard out: " + vm_create_output_stdout)
85
+ @log.debug("Standard err: " + vm_create_output_stderr)
86
+ unless status != 0
87
+ return ::VirtualBox::VM.find(vm_name)
88
+ else
89
+ raise Errors::VMNotCreatedError
90
+ end
91
+ end
92
+
93
+ def create_disk!(disk_name)
94
+ disk = ::VirtualBox::HardDrive.new
95
+ disk.format = "VDI"
96
+ disk.location = "#{disk_name}.vdi"
97
+ disk.logical_size = 8000 # in megabytes
98
+ if disk.save
99
+ return disk
100
+ else
101
+ raise Errors::DiskNotCreatedError.new(disk.inspect)
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ end
108
+ end
109
+
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: virtuoso-prototype
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Justin Lynn
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-22 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A quick and dirty prototype for managing virtualbox virtual machines, only scripts VBoxManage to eliminate bringup of a raw VM
23
+ email:
24
+ - eng@3crowd.com
25
+ executables:
26
+ - virtuoso-prototype
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - bin/virtuoso-prototype
33
+ - lib/virtuoso.rb
34
+ - lib/virtuoso/virtualbox/interface.rb
35
+ - lib/virtuoso/errors/vm_not_found.rb
36
+ - lib/virtuoso/errors/disk_not_created.rb
37
+ - lib/virtuoso/errors/virtualbox_not_found.rb
38
+ - lib/virtuoso/errors/vm_not_created.rb
39
+ - lib/virtuoso/virtualbox.rb
40
+ - lib/virtuoso/errors.rb
41
+ - LICENSE
42
+ - README
43
+ has_rdoc: true
44
+ homepage: http://github.com/3Crowd/Virtuoso-prototype
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 23
67
+ segments:
68
+ - 1
69
+ - 3
70
+ - 6
71
+ version: 1.3.6
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.7
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A protoype for managing virtualbox VMs, scripts VBoxManage
79
+ test_files: []
80
+