diff --git a/lib/hobo/virtual_box.rb b/lib/hobo/virtual_box.rb index 97894fa64..814cec827 100644 --- a/lib/hobo/virtual_box.rb +++ b/lib/hobo/virtual_box.rb @@ -10,6 +10,25 @@ class VirtualBox 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}` @@ -22,5 +41,20 @@ class VirtualBox 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 \ No newline at end of file diff --git a/test/hobo/virtual_box_test.rb b/test/hobo/virtual_box_test.rb index d982a1ce5..683552f89 100644 --- a/test/hobo/virtual_box_test.rb +++ b/test/hobo/virtual_box_test.rb @@ -5,4 +5,42 @@ class VirtualBoxTest < Test::Unit::TestCase # 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 \ No newline at end of file