From 73e9debefb56bdc74f883bb290a3f50420cda958 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 3 Jun 2010 11:09:31 -0700 Subject: [PATCH] Dynamically insert entry into /etc/network/interfaces file for Ubuntu --- lib/vagrant/actions/vm/network.rb | 7 ++++++- lib/vagrant/actions/vm/reload.rb | 4 ++-- lib/vagrant/actions/vm/shared_folders.rb | 4 ++++ lib/vagrant/systems/base.rb | 4 ++++ lib/vagrant/systems/linux.rb | 20 ++++++++++++++++++++ templates/network_entry.erb | 8 ++++++++ test/vagrant/actions/vm/network_test.rb | 19 +++++++++++++++++++ test/vagrant/actions/vm/reload_test.rb | 2 +- 8 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 templates/network_entry.erb diff --git a/lib/vagrant/actions/vm/network.rb b/lib/vagrant/actions/vm/network.rb index 484b56272..f8eb84260 100644 --- a/lib/vagrant/actions/vm/network.rb +++ b/lib/vagrant/actions/vm/network.rb @@ -7,7 +7,12 @@ module Vagrant end def after_boot + if enable_network? + logger.info "Enabling host only network..." + runner.system.prepare_host_only_network + runner.system.enable_host_only_network(runner.env.config.vm.network_options) + end end def enable_network? @@ -17,7 +22,7 @@ module Vagrant # Enables and assigns the host only network to the proper # adapter on the VM, and saves the adapter. def assign_network - logger.info "Enabling host only network..." + logger.info "Preparing host only network..." network_options = runner.env.config.vm.network_options adapter = runner.vm.network_adapters[network_options[:adapter]] diff --git a/lib/vagrant/actions/vm/reload.rb b/lib/vagrant/actions/vm/reload.rb index 22463dbbf..c3dad1640 100644 --- a/lib/vagrant/actions/vm/reload.rb +++ b/lib/vagrant/actions/vm/reload.rb @@ -3,7 +3,7 @@ module Vagrant module VM class Reload < Base def prepare - steps = [Customize, ForwardPorts, SharedFolders, Boot] + steps = [Customize, ForwardPorts, SharedFolders, Network, Boot] steps.unshift(Halt) if @runner.vm.running? steps << Provision if !@runner.env.config.vm.provisioner.nil? @@ -14,4 +14,4 @@ module Vagrant end end end -end \ No newline at end of file +end diff --git a/lib/vagrant/actions/vm/shared_folders.rb b/lib/vagrant/actions/vm/shared_folders.rb index e6bcd1e7d..084669d1e 100644 --- a/lib/vagrant/actions/vm/shared_folders.rb +++ b/lib/vagrant/actions/vm/shared_folders.rb @@ -38,6 +38,10 @@ module Vagrant end @runner.reload! + + # Sleep to allow VirtualBox to clean things up. There seems + # to be no clean API call to do this. + sleep 1 end def create_metadata diff --git a/lib/vagrant/systems/base.rb b/lib/vagrant/systems/base.rb index b01baa0b8..4dc913435 100644 --- a/lib/vagrant/systems/base.rb +++ b/lib/vagrant/systems/base.rb @@ -55,6 +55,10 @@ module Vagrant # wants the folder mounted. def mount_shared_folder(ssh, name, guestpath); end + # Prepares the system for host only networks. This is called + # once prior to any `enable_host_only_network` calls. + def prepare_host_only_network; end + # Setup the system by adding a new host only network. This # method should configure and bring up the interface for the # given options. diff --git a/lib/vagrant/systems/linux.rb b/lib/vagrant/systems/linux.rb index 17e10286c..03867577c 100644 --- a/lib/vagrant/systems/linux.rb +++ b/lib/vagrant/systems/linux.rb @@ -69,6 +69,26 @@ module Vagrant ssh.exec!("sudo rm #{config.vm.rsync_crontab_entry_file}", :error_check => false) end + def prepare_host_only_network + # TODO: Verify ubuntu here, otherwise error and tell user to + # implement this manually or to use ubuntu. + + # Remove any previous host only network additions to the + # interface file. + vm.ssh.execute do |ssh| + ssh.exec!("sudo su -c \"sed -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/network/interfaces > /etc/network/interfaces\"") + end + end + + def enable_host_only_network(net_options) + entry = TemplateRenderer.render('network_entry', :net_options => net_options) + vm.ssh.upload!(StringIO.new(entry), "/tmp/vagrant-network-entry") + + vm.ssh.execute do |ssh| + ssh.exec!("sudo su -c 'cat /tmp/vagrant-network-entry >> /etc/network/interfaces'") + end + end + #------------------------------------------------------------------- # "Private" methods which assist above methods #------------------------------------------------------------------- diff --git a/templates/network_entry.erb b/templates/network_entry.erb new file mode 100644 index 000000000..04def830c --- /dev/null +++ b/templates/network_entry.erb @@ -0,0 +1,8 @@ +#VAGRANT-BEGIN +# The contents below are automatically generated by Vagrant. +# Please do not modify any of these contents. +auto eth<%= net_options[:adapter] %> +iface eth<%= net_options[:adapter] %> inet static + address <%= net_options[:ip] %> + netmask <%= net_options[:netmask] %> +#VAGRANT-END diff --git a/test/vagrant/actions/vm/network_test.rb b/test/vagrant/actions/vm/network_test.rb index 0b1125fb8..4a7672661 100644 --- a/test/vagrant/actions/vm/network_test.rb +++ b/test/vagrant/actions/vm/network_test.rb @@ -23,6 +23,25 @@ class NetworkTest < Test::Unit::TestCase end end + context "after boot" do + setup do + @action.stubs(:enable_network?).returns(true) + end + + should "prepare the host only network, then enable them" do + run_seq = sequence("run") + @runner.system.expects(:prepare_host_only_network).once.in_sequence(run_seq) + @runner.system.expects(:enable_host_only_network).once.in_sequence(run_seq) + @action.after_boot + end + + should "do nothing if network is not enabled" do + @action.stubs(:enable_network?).returns(false) + @runner.system.expects(:prepare_host_only_network).never + @action.after_boot + end + end + context "checking if network is enabled" do should "return true if the network options are set" do @runner.env.config.vm.network("foo") diff --git a/test/vagrant/actions/vm/reload_test.rb b/test/vagrant/actions/vm/reload_test.rb index ebc3e8599..d58551631 100644 --- a/test/vagrant/actions/vm/reload_test.rb +++ b/test/vagrant/actions/vm/reload_test.rb @@ -7,7 +7,7 @@ class ReloadActionTest < Test::Unit::TestCase context "sub-actions" do setup do - @default_order = [Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Boot] + @default_order = [Vagrant::Actions::VM::Customize, Vagrant::Actions::VM::ForwardPorts, Vagrant::Actions::VM::SharedFolders, Vagrant::Actions::VM::Network, Vagrant::Actions::VM::Boot] @vm.stubs(:running?).returns(false) end