(#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.
This commit is contained in:
Brian Cain 2018-05-10 13:02:05 -07:00
parent 02c207b42e
commit 728a9135c8
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
2 changed files with 38 additions and 1 deletions

View File

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

View File

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