Merge pull request #5721 from mitchellh/sethvargo/network_gateway

Add support for network gateways
This commit is contained in:
Seth Vargo 2015-05-30 12:35:42 -07:00
commit 1df0680bcf
27 changed files with 714 additions and 4 deletions

View File

@ -3,3 +3,6 @@ Description='A basic static ethernet connection'
Interface=<%= options[:device] %>
IP=static
Address=('<%= options[:ip]%>/24')
<% if options[:gateway] %>
Gateway='<%= options[:gateway] %>'
<% end %>

View File

@ -4,4 +4,7 @@ auto eth<%= options[:interface] %>
iface eth<%= options[:interface] %> inet static
address <%= options[:ip] %>
netmask <%= options[:netmask] %>
<% if options[:gateway] %>
gateway <%= options[:gateway] %>
<% end %>
#VAGRANT-END

View File

@ -6,7 +6,11 @@ ONBOOT=yes
IPADDR=<%= options[:ip] %>
NETMASK=<%= options[:netmask] %>
DEVICE=<%= options[:device] %>
<%= options[:gateway] ? "GATEWAY=#{options[:gateway]}" : '' %>
<%= options[:mac_address] ? "HWADDR=#{options[:mac_address]}" : '' %>
<% if options[:gateway] %>
GATEWAY=<%= options[:gateway] %>
<% end %>
<% if options[:mac_address] %>
HWADDR=<%= options[:mac_address] %>
<% end %>
PEERDNS=no
#VAGRANT-END

View File

@ -1,3 +1,6 @@
#VAGRANT-BEGIN
ifconfig_<%= ifname %>="inet <%= options[:ip] %> netmask <%= options[:netmask] %>"
<% if options[:gateway] %>
default_router="<%= options[:gateway] %>"
<% end %>
#VAGRANT-END

View File

@ -2,5 +2,9 @@
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='<%= options[:ip] %>/<%= options[:netmask] %>'
<%= [:gateway, :nameservers, :domain, :route, :gateway6, :route6, :mtu].reduce([]) { |a,i| a.push("#{i}=#{options[i]}") if options[i]; a }.join("\n") %>
<% [:gateway, :nameservers, :domain, :route, :gateway6, :route6, :mtu].each do |key| %>
<% if options[key] %>
<%= key %>='<%= options[key] %>'
<% end %>
<% end %>
#VAGRANT-END

View File

@ -1,4 +1,7 @@
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_eth<%= options[:interface] %>=("<%= options[:ip] %> netmask <%= options[:netmask] %>")
<% if options[:gateway] %>
gateways_eth<%= options[:interface] %>="<%= options[:gateway] %>"
<% end %>
#VAGRANT-END

View File

@ -6,5 +6,8 @@ ONBOOT=yes
IPADDR=<%= options[:ip] %>
NETMASK=<%= options[:netmask] %>
DEVICE=eth<%= options[:interface] %>
<% if options[:gateway] %>
GATEWAY=<%= options[:gateway] %>
<% end %>
PEERDNS=no
#VAGRANT-END

View File

@ -4,7 +4,10 @@ BOOTPROTO='static'
IPADDR='<%= options[:ip] %>'
NETMASK='<%= options[:netmask] %>'
DEVICE='eth<%= options[:interface] %>'
PEERDNS=no
<% if options[:gateway] %>
GATEWAY='<%= options[:gateway] %>'
<% end %>
PEERDNS='no'
STARTMODE='auto'
USERCONTROL='no'
#VAGRANT-END

View File

@ -0,0 +1,19 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/network_dhcp" do
let(:template) { "guests/arch/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Description='A basic dhcp ethernet connection'
Interface=en0
Connection=ethernet
IP=dhcp
EOH
end
end

View File

@ -0,0 +1,37 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/arch/network_static" do
let(:template) { "guests/arch/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Connection=ethernet
Description='A basic static ethernet connection'
Interface=en0
IP=static
Address=('1.1.1.1/24')
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
gateway: "1.2.3.4",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
Connection=ethernet
Description='A basic static ethernet connection'
Interface=en0
IP=static
Address=('1.1.1.1/24')
Gateway='1.2.3.4'
EOH
end
end

View File

@ -0,0 +1,41 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/debian/network_dhcp" do
let(:template) { "guests/debian/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth
iface eth inet dhcp
post-up route del default dev $IFACE || true
#VAGRANT-END
EOH
end
context "when use_dhcp_assigned_default_route is set" do
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
use_dhcp_assigned_default_route: true,
})
expect(result).to eq <<-EOH.gsub(/^ {8}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth
iface eth inet dhcp
# We need to disable eth0, see GH-2648
post-up route del default dev eth0
post-up dhclient $IFACE
pre-down route add default dev eth0
#VAGRANT-END
EOH
end
end
end

View File

@ -0,0 +1,43 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/debian/network_static" do
let(:template) { "guests/debian/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth
iface eth inet static
address 1.1.1.1
netmask 255.255.0.0
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway: "1.2.3.4",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth
iface eth inet static
address 1.1.1.1
netmask 255.255.0.0
gateway 1.2.3.4
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,21 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/fedora/network_dhcp" do
let(:template) { "guests/fedora/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=en0
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,71 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/fedora/network_static" do
let(:template) { "guests/fedora/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=1.1.1.1
NETMASK=255.255.0.0
DEVICE=en0
PEERDNS=no
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway: "1.2.3.4",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=1.1.1.1
NETMASK=255.255.0.0
DEVICE=en0
GATEWAY=1.2.3.4
PEERDNS=no
#VAGRANT-END
EOH
end
it "includes the mac_address" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
mac_address: "abcd1234",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=1.1.1.1
NETMASK=255.255.0.0
DEVICE=en0
HWADDR=abcd1234
PEERDNS=no
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,16 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/freebsd/network_dhcp" do
let(:template) { "guests/freebsd/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, ifname: "vtneten0")
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
ifconfig_vtneten0="DHCP"
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,39 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/freebsd/network_static" do
let(:template) { "guests/freebsd/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template,
ifname: "vtneten0",
options: {
ip: "1.1.1.1",
netmask: "255.255.0.0",
},
)
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
ifconfig_vtneten0="inet 1.1.1.1 netmask 255.255.0.0"
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template,
ifname: "vtneten0",
options: {
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway: "1.2.3.4",
},
)
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
ifconfig_vtneten0="inet 1.1.1.1 netmask 255.255.0.0"
default_router="1.2.3.4"
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,19 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/funtoo/network_dhcp" do
let(:template) { "guests/funtoo/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='dhcp'
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,141 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/funtoo/network_static" do
let(:template) { "guests/funtoo/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway: "1.2.3.4",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
gateway='1.2.3.4'
#VAGRANT-END
EOH
end
it "includes the nameservers" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
nameservers: "ns1.company.com",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
nameservers='ns1.company.com'
#VAGRANT-END
EOH
end
it "includes the domain" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
domain: "company.com",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
domain='company.com'
#VAGRANT-END
EOH
end
it "includes the route" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
route: "5.6.7.8",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
route='5.6.7.8'
#VAGRANT-END
EOH
end
it "includes the gateway6" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway6: "aaaa:0000",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
gateway6='aaaa:0000'
#VAGRANT-END
EOH
end
it "includes the route6" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
route6: "bbbb:1111",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
route6='bbbb:1111'
#VAGRANT-END
EOH
end
it "includes the mtu" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
device: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
mtu: "1",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
template='interface'
ipaddr='1.1.1.1/255.255.0.0'
mtu='1'
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,19 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/gentoo/network_dhcp" do
let(:template) { "guests/gentoo/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_ethen0="dhcp"
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,37 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/gentoo/network_static" do
let(:template) { "guests/gentoo/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_ethen0=("1.1.1.1 netmask 255.255.0.0")
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
gateway: "1.2.3.4",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
config_ethen0=("1.1.1.1 netmask 255.255.0.0")
gateways_ethen0="1.2.3.4"
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,18 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/netbsd/network_dhcp" do
let(:template) { "guests/netbsd/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
ifconfig_wmen0=dhcp
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,20 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/netbsd/network_static" do
let(:template) { "guests/netbsd/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
ifconfig_wmen0="media autoselect up;inet 1.1.1.1 netmask 255.255.0.0"
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,21 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/redhat/network_dhcp" do
let(:template) { "guests/redhat/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=ethen0
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,49 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/redhat/network_static" do
let(:template) { "guests/redhat/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=1.1.1.1
NETMASK=255.255.0.0
DEVICE=ethen0
PEERDNS=no
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
gateway: "1.2.3.4",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=1.1.1.1
NETMASK=255.255.0.0
DEVICE=ethen0
GATEWAY=1.2.3.4
PEERDNS=no
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,21 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/suse/network_dhcp" do
let(:template) { "guests/suse/network_dhcp" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
BOOTPROTO=dhcp
ONBOOT=yes
DEVICE=ethen0
#VAGRANT-END
EOH
end
end

View File

@ -0,0 +1,49 @@
require_relative "../../../base"
require "vagrant/util/template_renderer"
describe "templates/guests/suse/network_static" do
let(:template) { "guests/suse/network_static" }
it "renders the template" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
BOOTPROTO='static'
IPADDR='1.1.1.1'
NETMASK='255.255.0.0'
DEVICE='ethen0'
PEERDNS='no'
STARTMODE='auto'
USERCONTROL='no'
#VAGRANT-END
EOH
end
it "includes the gateway" do
result = Vagrant::Util::TemplateRenderer.render(template, options: {
interface: "en0",
ip: "1.1.1.1",
gateway: "1.2.3.4",
netmask: "255.255.0.0",
})
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
BOOTPROTO='static'
IPADDR='1.1.1.1'
NETMASK='255.255.0.0'
DEVICE='ethen0'
GATEWAY='1.2.3.4'
PEERDNS='no'
STARTMODE='auto'
USERCONTROL='no'
#VAGRANT-END
EOH
end
end

View File

@ -63,6 +63,9 @@ the [reserved private address space](http://en.wikipedia.org/wiki/Private_networ
and most routers actually block traffic from going to them from the
outside world.
For some operating systems, additional configuration options for the static
IP address are available such as setting the default gateway or MTU.
## Disable Auto-Configuration
If you want to manually configure the network interface yourself, you