From 728a9135c883ebd5593ddb39a2529070a7298c10 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 10 May 2018 13:02:05 -0700 Subject: [PATCH] (#9726) Update netplan config generation to detect NetworkManager Prior to this commit, when setting up private networks on Ubuntu using netplan, it assumed that the guest was using systemd, the suggested default tool to manage networking, and did not take into account devices that could be managed with NetworkManager. This commit fixes that by looking at the devices managed on the guest to see if its managed by NetworkManager, and if so, use that renderer for netplan instead of networkd. --- .../guests/debian/cap/configure_networks.rb | 14 ++++++++++- .../debian/cap/configure_networks_test.rb | 25 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/plugins/guests/debian/cap/configure_networks.rb b/plugins/guests/debian/cap/configure_networks.rb index d53358eb3..6ba4c2ca9 100644 --- a/plugins/guests/debian/cap/configure_networks.rb +++ b/plugins/guests/debian/cap/configure_networks.rb @@ -57,8 +57,20 @@ module VagrantPlugins e_nets[interfaces[network[:interface]]] = e_config end end + + # By default, netplan expects the renderer to be systemd-networkd, + # but if any device is managed by NetworkManager, then we use that renderer + # ref: https://netplan.io/reference + renderer = NETPLAN_DEFAULT_RENDERER + ethernets.keys.each do |k| + if nm_controlled?(comm, k) + renderer = "NetworkManager" + break + end + end + np_config = {"network" => {"version" => NETPLAN_DEFAULT_VERSION, - "renderer" => NETPLAN_DEFAULT_RENDERER, "ethernets" => ethernets}} + "renderer" => renderer, "ethernets" => ethernets}} remote_path = upload_tmp_file(comm, np_config.to_yaml) dest_path = "#{NETPLAN_DIRECTORY}/50-vagrant.yaml" diff --git a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb index 57ed1e6a3..13ccea1b6 100644 --- a/test/unit/plugins/guests/debian/cap/configure_networks_test.rb +++ b/test/unit/plugins/guests/debian/cap/configure_networks_test.rb @@ -65,6 +65,8 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do end before do + allow(comm).to receive(:test).with("nmcli d show eth1").and_return(false) + allow(comm).to receive(:test).with("nmcli d show eth2").and_return(false) allow(comm).to receive(:test).with("ps -o comm= 1 | grep systemd").and_return(false) allow(comm).to receive(:test).with("sudo systemctl status systemd-networkd.service").and_return(false) allow(comm).to receive(:test).with("netplan -h").and_return(false) @@ -118,7 +120,30 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do expect(comm).to receive(:test).with("netplan -h").and_return(true) end + let(:nm_yml) { "---\nnetwork:\n version: 2\n renderer: NetworkManager\n ethernets:\n eth1:\n dhcp4: true\n eth2:\n addresses:\n - 33.33.33.10/16\n gateway4: 33.33.0.1\n" } + let(:networkd_yml) { "---\nnetwork:\n version: 2\n renderer: networkd\n ethernets:\n eth1:\n dhcp4: true\n eth2:\n addresses:\n - 33.33.33.10/16\n gateway4: 33.33.0.1\n" } + + it "uses NetworkManager if detected on device" do + allow(cap).to receive(:nm_controlled?).and_return(true) + allow(comm).to receive(:test).with("nmcli d show eth1").and_return(true) + allow(comm).to receive(:test).with("nmcli d show eth2").and_return(true) + + expect(cap).to receive(:upload_tmp_file).with(comm, nm_yml) + .and_return("/tmp/vagrant-network-entry.1234") + + cap.configure_networks(machine, [network_0, network_1]) + + + expect(comm.received_commands[0]).to match("mv -f '/tmp/vagrant-network-entry.*' '/etc/netplan/.*.yaml'") + expect(comm.received_commands[0]).to match("chown") + expect(comm.received_commands[0]).to match("chmod") + expect(comm.received_commands[0]).to match("netplan apply") + end + it "creates and starts the networks for systemd with netplan" do + expect(cap).to receive(:upload_tmp_file).with(comm, networkd_yml) + .and_return("/tmp/vagrant-network-entry.1234") + cap.configure_networks(machine, [network_0, network_1]) expect(comm.received_commands[0]).to match("mv -f '/tmp/vagrant-network-entry.*' '/etc/netplan/.*.yaml'")