Goodbye virtual_box.rb, hello virtualbox gem
This commit is contained in:
parent
6ea3fe39b9
commit
6a2619be02
4
Gemfile
4
Gemfile
|
@ -1,7 +1,9 @@
|
|||
# Gems required for the lib to even run
|
||||
gem "virtualbox", ">= 0.4.0"
|
||||
gem "net-ssh", ">= 2.0.19"
|
||||
gem "jashmenn-git-style-binaries", ">= 0.1.10"
|
||||
|
||||
source "http://gems.github.com"
|
||||
gem "jashmenn-git-style-binaries", ">= 0.1.10"
|
||||
|
||||
# Gems required for testing only. To install run
|
||||
# gem bundle test
|
||||
|
|
|
@ -7,7 +7,6 @@ require 'ftools'
|
|||
require 'logger'
|
||||
require 'hobo/config'
|
||||
require 'hobo/env'
|
||||
require 'hobo/virtual_box'
|
||||
require 'hobo/ssh'
|
||||
|
||||
# TODO: Make this configurable
|
||||
|
|
|
@ -1,60 +0,0 @@
|
|||
class VirtualBox
|
||||
class <<self
|
||||
def create(name, options = {})
|
||||
# To create the VM, we simply import the base OVF which takes care
|
||||
# of matching up the hardware and setting up the configuration.
|
||||
command("import #{File.expand_path("~/.hobo/base/base.ovf")} --vsys 0 --vmname #{name}")
|
||||
|
||||
# We must manually modify the mac address to match the base VM
|
||||
# so that eth0 will still work
|
||||
command("modifyvm #{name} --macaddress1 08002771F257")
|
||||
end
|
||||
|
||||
def destroy(name)
|
||||
# We must first get the vm info to parse which disk images this
|
||||
# vm is using, since we'll have to remove those as well.
|
||||
vminfo = parse_kv_pairs(command("showvminfo #{name} --machinereadable"))
|
||||
|
||||
# Detach mediums associated with VM so we can delete
|
||||
# TODO: Make this use the vminfo returned to be flexible enough to destroy
|
||||
# all mediums for any machine
|
||||
command("storageattach #{name} --storagectl \"IDE Controller\" --port 0 --device 0 --medium none")
|
||||
command("storageattach #{name} --storagectl \"IDE Controller\" --port 1 --device 0 --medium none")
|
||||
command("storageattach #{name} --storagectl \"Floppy Controller\" --port 0 --device 0 --medium none")
|
||||
|
||||
# Remove the disk associated with the VM
|
||||
command("closemedium disk #{vminfo["IDE Controller-0-0"]} --delete")
|
||||
|
||||
# Remove and delete the VM (unregister)
|
||||
command("unregistervm #{name} --delete")
|
||||
end
|
||||
|
||||
def command(cmd)
|
||||
HOBO_LOGGER.debug "Command: #{cmd}"
|
||||
result = `VBoxManage #{cmd}`
|
||||
|
||||
if $?.to_i >= 1
|
||||
HOBO_LOGGER.error "VBoxManage command failed: #{cmd}"
|
||||
# TODO: Raise error here that other commands can catch?
|
||||
raise Exception.new("Failure: #{result}")
|
||||
end
|
||||
|
||||
return result
|
||||
end
|
||||
|
||||
# Parses the key value pairs from the VBoxManage key=value pair
|
||||
# format and returns as a Ruby Hash
|
||||
def parse_kv_pairs(raw)
|
||||
parsed = {}
|
||||
|
||||
raw.lines.each do |line|
|
||||
# Some lines aren't configuration, we just ignore them
|
||||
next unless line =~ /^"?(.+?)"?="?(.+?)"?$/
|
||||
|
||||
parsed[$1] = $2.strip
|
||||
end
|
||||
|
||||
parsed
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,46 +0,0 @@
|
|||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
|
||||
class VirtualBoxTest < Test::Unit::TestCase
|
||||
setup do
|
||||
# Stub out command so nothing actually happens
|
||||
VirtualBox.stubs(:command)
|
||||
end
|
||||
|
||||
context "parsing key value pairs" do
|
||||
should "not parse the lines which don't contain key value pairs" do
|
||||
result = VirtualBox.parse_kv_pairs("I'm not a key value pair")
|
||||
assert result.empty?
|
||||
end
|
||||
|
||||
should "parse the lines which contain key value pairs" do
|
||||
result = VirtualBox.parse_kv_pairs("foo=bar")
|
||||
assert_equal 1, result.length
|
||||
assert_equal "bar", result["foo"]
|
||||
end
|
||||
|
||||
should "ignore surrounding double quotes on keys and values" do
|
||||
result = VirtualBox.parse_kv_pairs('"foo"="a value"')
|
||||
assert_equal 1, result.length
|
||||
assert_equal "a value", result["foo"]
|
||||
end
|
||||
|
||||
should "trim the values" do
|
||||
result = VirtualBox.parse_kv_pairs("foo=bar ")
|
||||
assert_equal 1, result.length
|
||||
assert_equal "bar", result["foo"]
|
||||
end
|
||||
|
||||
should "parse multiple lines" do
|
||||
result = VirtualBox.parse_kv_pairs(<<-raw)
|
||||
This is some header
|
||||
|
||||
foo=bar
|
||||
"bar"=baz
|
||||
raw
|
||||
|
||||
assert_equal 2, result.length
|
||||
assert_equal "bar", result["foo"]
|
||||
assert_equal "baz", result["bar"]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue