diff --git a/plugins/guests/arch/cap/configure_networks.rb b/plugins/guests/arch/cap/configure_networks.rb index e108d199d..79f14213a 100644 --- a/plugins/guests/arch/cap/configure_networks.rb +++ b/plugins/guests/arch/cap/configure_networks.rb @@ -9,10 +9,12 @@ module VagrantPlugins module Cap class ConfigureNetworks include Vagrant::Util + extend Vagrant::Util::GuestInspection::Linux def self.configure_networks(machine, networks) comm = machine.communicate commands = [] + uses_systemd_networkd = systemd_networkd?(comm) interfaces = machine.guest.capability(:network_interfaces) networks.each.with_index do |network, i| @@ -25,9 +27,15 @@ module VagrantPlugins network[:netmask] = (32-Math.log2((IPAddr.new(network[:netmask], Socket::AF_INET).to_i^0xffffffff)+1)).to_i end - entry = TemplateRenderer.render("guests/arch/network_#{network[:type]}", - options: network, - ) + if uses_systemd_networkd + entry = TemplateRenderer.render("guests/arch/systemd_networkd/network_#{network[:type]}", + options: network, + ) + else + entry = TemplateRenderer.render("guests/arch/default_network/network_#{network[:type]}", + options: network, + ) + end remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now.to_i}-#{i}" @@ -39,7 +47,7 @@ module VagrantPlugins comm.upload(f.path, remote_path) end - if systemd_networkd?(comm) + if uses_systemd_networkd commands << <<-EOH.gsub(/^ {16}/, '').rstrip # Configure #{network[:device]} chmod 0644 '#{remote_path}' && diff --git a/templates/guests/arch/default_network/network_dhcp.erb b/templates/guests/arch/default_network/network_dhcp.erb new file mode 100644 index 000000000..377784574 --- /dev/null +++ b/templates/guests/arch/default_network/network_dhcp.erb @@ -0,0 +1,4 @@ +Description='A basic dhcp ethernet connection' +Interface=<%= options[:device] %> +Connection=ethernet +IP=dhcp diff --git a/templates/guests/arch/default_network/network_static.erb b/templates/guests/arch/default_network/network_static.erb new file mode 100644 index 000000000..52b2f2d77 --- /dev/null +++ b/templates/guests/arch/default_network/network_static.erb @@ -0,0 +1,8 @@ +Connection=ethernet +Description='A basic static ethernet connection' +Interface=<%= options[:device] %> +IP=static +Address=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> +Gateway='<%= options[:gateway] %>' +<% end -%> diff --git a/templates/guests/arch/default_network/network_static6.erb b/templates/guests/arch/default_network/network_static6.erb new file mode 100644 index 000000000..e64ffd5ca --- /dev/null +++ b/templates/guests/arch/default_network/network_static6.erb @@ -0,0 +1,8 @@ +Connection=ethernet +Description='A basic IPv6 ethernet connection' +Interface=<%= options[:device] %> +IP6=static +Address6=('<%= options[:ip]%>/<%= options[:netmask] %>') +<% if options[:gateway] -%> +Gateway6='<%= options[:gateway] %>' +<% end -%> diff --git a/templates/guests/arch/network_dhcp.erb b/templates/guests/arch/systemd_networkd/network_dhcp.erb similarity index 100% rename from templates/guests/arch/network_dhcp.erb rename to templates/guests/arch/systemd_networkd/network_dhcp.erb diff --git a/templates/guests/arch/network_static.erb b/templates/guests/arch/systemd_networkd/network_static.erb similarity index 100% rename from templates/guests/arch/network_static.erb rename to templates/guests/arch/systemd_networkd/network_static.erb diff --git a/templates/guests/arch/network_static6.erb b/templates/guests/arch/systemd_networkd/network_static6.erb similarity index 100% rename from templates/guests/arch/network_static6.erb rename to templates/guests/arch/systemd_networkd/network_static6.erb diff --git a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb index 7f136b268..10304efdb 100644 --- a/test/unit/plugins/guests/arch/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/arch/cap/configure_networks_test.rb @@ -25,6 +25,7 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do before do allow(guest).to receive(:capability).with(:network_interfaces) .and_return(["eth1", "eth2"]) + allow(cap).to receive(:systemd_networkd?).and_return(true) end let(:network_1) do @@ -59,5 +60,23 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do cap.configure_networks(machine, [network_1, network_2]) expect(comm.received_commands[0]).not_to match(/^\s*&&\s*$/) end + + context "network is not contolled by systemd" do + before do + allow(cap).to receive(:systemd_networkd?).and_return(false) + end + + it "creates and stars the networks" do + cap.configure_networks(machine, [network_1, network_2]) + expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth1'/) + expect(comm.received_commands[0]).to match(/ip link set 'eth1' down/) + expect(comm.received_commands[0]).to match(/netctl restart 'eth1'/) + expect(comm.received_commands[0]).to match(/netctl enable 'eth1'/) + expect(comm.received_commands[0]).to match(/mv (.+) '\/etc\/netctl\/eth2'/) + expect(comm.received_commands[0]).to match(/ip link set 'eth2' down/) + expect(comm.received_commands[0]).to match(/netctl restart 'eth2'/) + expect(comm.received_commands[0]).to match(/netctl enable 'eth2'/) + end + end end end diff --git a/test/unit/templates/guests/arch/default_network/network_dhcp_test.rb b/test/unit/templates/guests/arch/default_network/network_dhcp_test.rb new file mode 100644 index 000000000..878d55ee5 --- /dev/null +++ b/test/unit/templates/guests/arch/default_network/network_dhcp_test.rb @@ -0,0 +1,19 @@ +require_relative "../../../../base" + +require "vagrant/util/template_renderer" + +describe "templates/guests/arch/default_network/network_dhcp" do + let(:template) { "guests/arch/default_network/network_dhcp" } + + it "renders the template" do + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + }) + expect(result).to eq <<-EOH.gsub(/^ {6}/, "") + Description='A basic dhcp ethernet connection' + Interface=eth1 + Connection=ethernet + IP=dhcp + EOH + end +end diff --git a/test/unit/templates/guests/arch/default_network/network_static_test.rb b/test/unit/templates/guests/arch/default_network/network_static_test.rb new file mode 100644 index 000000000..e5ae76261 --- /dev/null +++ b/test/unit/templates/guests/arch/default_network/network_static_test.rb @@ -0,0 +1,39 @@ +require_relative "../../../../base" + +require "vagrant/util/template_renderer" + +describe "templates/guests/arch/default_network/network_static" do + let(:template) { "guests/arch/default_network/network_static" } + + it "renders the template" do + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + ip: "1.1.1.1", + netmask: "24", + }) + expect(result).to eq <<-EOH.gsub(/^ {6}/, "") + Connection=ethernet + Description='A basic static ethernet connection' + Interface=eth1 + IP=static + Address=('1.1.1.1/24') + EOH + end + + it "includes the gateway" do + result = Vagrant::Util::TemplateRenderer.render(template, options: { + device: "eth1", + ip: "1.1.1.1", + gateway: "1.2.3.4", + netmask: "24", + }) + expect(result).to eq <<-EOH.gsub(/^ {6}/, "") + Connection=ethernet + Description='A basic static ethernet connection' + Interface=eth1 + IP=static + Address=('1.1.1.1/24') + Gateway='1.2.3.4' + EOH + end +end diff --git a/test/unit/templates/guests/arch/network_dhcp_test.rb b/test/unit/templates/guests/arch/systemd_networkd/network_dhcp_test.rb similarity index 68% rename from test/unit/templates/guests/arch/network_dhcp_test.rb rename to test/unit/templates/guests/arch/systemd_networkd/network_dhcp_test.rb index dbeece1b3..a64652642 100644 --- a/test/unit/templates/guests/arch/network_dhcp_test.rb +++ b/test/unit/templates/guests/arch/systemd_networkd/network_dhcp_test.rb @@ -1,9 +1,9 @@ -require_relative "../../../base" +require_relative "../../../../base" require "vagrant/util/template_renderer" -describe "templates/guests/arch/network_dhcp" do - let(:template) { "guests/arch/network_dhcp" } +describe "templates/guests/arch/systemd_networkd/network_dhcp" do + let(:template) { "guests/arch/systemd_networkd/network_dhcp" } it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: { diff --git a/test/unit/templates/guests/arch/network_static_test.rb b/test/unit/templates/guests/arch/systemd_networkd/network_static_test.rb similarity index 83% rename from test/unit/templates/guests/arch/network_static_test.rb rename to test/unit/templates/guests/arch/systemd_networkd/network_static_test.rb index 215112f69..559b2f584 100644 --- a/test/unit/templates/guests/arch/network_static_test.rb +++ b/test/unit/templates/guests/arch/systemd_networkd/network_static_test.rb @@ -1,9 +1,9 @@ -require_relative "../../../base" +require_relative "../../../../base" require "vagrant/util/template_renderer" -describe "templates/guests/arch/network_static" do - let(:template) { "guests/arch/network_static" } +describe "templates/guests/arch/systemd_networkd/network_static" do + let(:template) { "guests/arch/systemd_networkd/network_static" } it "renders the template" do result = Vagrant::Util::TemplateRenderer.render(template, options: {