Destroying VMs is now possible
This commit is contained in:
parent
92dd514c4d
commit
c7769661dd
|
@ -10,6 +10,25 @@ class VirtualBox
|
||||||
command("modifyvm #{name} --macaddress1 08002771F257")
|
command("modifyvm #{name} --macaddress1 08002771F257")
|
||||||
end
|
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)
|
def command(cmd)
|
||||||
HOBO_LOGGER.debug "Command: #{cmd}"
|
HOBO_LOGGER.debug "Command: #{cmd}"
|
||||||
result = `VBoxManage #{cmd}`
|
result = `VBoxManage #{cmd}`
|
||||||
|
@ -22,5 +41,20 @@ class VirtualBox
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
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
|
||||||
end
|
end
|
|
@ -5,4 +5,42 @@ class VirtualBoxTest < Test::Unit::TestCase
|
||||||
# Stub out command so nothing actually happens
|
# Stub out command so nothing actually happens
|
||||||
VirtualBox.stubs(:command)
|
VirtualBox.stubs(:command)
|
||||||
end
|
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
|
end
|
Loading…
Reference in New Issue