Dynamically insert entry into /etc/network/interfaces file for Ubuntu

This commit is contained in:
Mitchell Hashimoto 2010-06-03 11:09:31 -07:00
parent 134ebe8405
commit 73e9debefb
8 changed files with 64 additions and 4 deletions

View File

@ -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]]

View File

@ -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
end

View File

@ -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

View File

@ -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.

View File

@ -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
#-------------------------------------------------------------------

View File

@ -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

View File

@ -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")

View File

@ -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