From 4e8b6f32b48f1a8754b93f6cff3783b280c2ce02 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 21 Jul 2010 21:01:38 -0700 Subject: [PATCH] Disable host only networks on halt [closes GH-116] --- lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/disable_networks.rb | 26 +++++++++++++ lib/vagrant/action/vm/halt.rb | 4 ++ .../action/vm/disable_networks_test.rb | 39 +++++++++++++++++++ vagrant.gemspec | 3 ++ 5 files changed, 73 insertions(+) create mode 100644 lib/vagrant/action/vm/disable_networks.rb create mode 100644 test/vagrant/action/vm/disable_networks_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index 8617c3957..014155cfe 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -33,6 +33,7 @@ module Vagrant # a restart if fails. halt = Builder.new do use VM::Halt + use VM::DisableNetworks end register :halt, halt diff --git a/lib/vagrant/action/vm/disable_networks.rb b/lib/vagrant/action/vm/disable_networks.rb new file mode 100644 index 000000000..46ec9cf14 --- /dev/null +++ b/lib/vagrant/action/vm/disable_networks.rb @@ -0,0 +1,26 @@ +module Vagrant + class Action + module VM + # Middleware to disable all host only networks on the + # VM + class DisableNetworks + def initialize(app, env) + @app = app + @env = env + end + + def call(env) + env.logger.info "Disabling host only networks..." + + env["vm"].vm.network_adapters.each do |adapter| + next if adapter.attachment_type != :host_only + adapter.enabled = false + adapter.save + end + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant/action/vm/halt.rb b/lib/vagrant/action/vm/halt.rb index 3e441fb7e..23b6d65c3 100644 --- a/lib/vagrant/action/vm/halt.rb +++ b/lib/vagrant/action/vm/halt.rb @@ -20,6 +20,10 @@ module Vagrant env.logger.info "Forcing shutdown of VM..." env["vm"].vm.stop end + + # Sleep for a second to verify that the VM properly + # cleans itself up + sleep 1 end @app.call(env) diff --git a/test/vagrant/action/vm/disable_networks_test.rb b/test/vagrant/action/vm/disable_networks_test.rb new file mode 100644 index 000000000..6fafc6691 --- /dev/null +++ b/test/vagrant/action/vm/disable_networks_test.rb @@ -0,0 +1,39 @@ +require "test_helper" + +class DisableNetworksVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::DisableNetworks + @app, @env = mock_action_data + + @vm = mock("vm") + @env.env.stubs(:vm).returns(@vm) + + @internal_vm = mock("internal") + @vm.stubs(:vm).returns(@internal_vm) + @internal_vm.stubs(:network_adapters).returns([]) + + @instance = @klass.new(@app, @env) + end + + def mock_adapter(type) + adapter = mock("adapter") + adapter.stubs(:attachment_type).returns(type) + + if type == :host_only + adapter.expects(:enabled=).with(false) + adapter.expects(:save) + end + + @internal_vm.network_adapters << adapter + end + + should "remove all network adapters and continue chain" do + mock_adapter(:bridged) + mock_adapter(:host_only) + mock_adapter(:host_only) + + @app.expects(:call).with(@env).once + + @instance.call(@env) + end +end diff --git a/vagrant.gemspec b/vagrant.gemspec index 030c5463b..00bbe6619 100644 --- a/vagrant.gemspec +++ b/vagrant.gemspec @@ -54,6 +54,7 @@ Gem::Specification.new do |s| "lib/vagrant/action/vm/customize.rb", "lib/vagrant/action/vm/destroy.rb", "lib/vagrant/action/vm/destroy_unused_network_interfaces.rb", + "lib/vagrant/action/vm/disable_networks.rb", "lib/vagrant/action/vm/export.rb", "lib/vagrant/action/vm/forward_ports.rb", "lib/vagrant/action/vm/forward_ports_helpers.rb", @@ -144,6 +145,7 @@ Gem::Specification.new do |s| "test/vagrant/action/vm/customize_test.rb", "test/vagrant/action/vm/destroy_test.rb", "test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb", + "test/vagrant/action/vm/disable_networks_test.rb", "test/vagrant/action/vm/export_test.rb", "test/vagrant/action/vm/forward_ports_helpers_test.rb", "test/vagrant/action/vm/forward_ports_test.rb", @@ -230,6 +232,7 @@ Gem::Specification.new do |s| "test/vagrant/action/vm/customize_test.rb", "test/vagrant/action/vm/destroy_test.rb", "test/vagrant/action/vm/destroy_unused_network_interfaces_test.rb", + "test/vagrant/action/vm/disable_networks_test.rb", "test/vagrant/action/vm/export_test.rb", "test/vagrant/action/vm/forward_ports_helpers_test.rb", "test/vagrant/action/vm/forward_ports_test.rb",