gentoo/guest: #8406 support systemd when configuring net
This commit is contained in:
parent
e06848a649
commit
3a3216ae0c
|
@ -1,4 +1,5 @@
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
|
require "ipaddr"
|
||||||
|
|
||||||
require_relative "../../../../lib/vagrant/util/template_renderer"
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
||||||
|
|
||||||
|
@ -14,12 +15,46 @@ module VagrantPlugins
|
||||||
commands = []
|
commands = []
|
||||||
interfaces = machine.guest.capability(:network_interfaces, "/bin/ip")
|
interfaces = machine.guest.capability(:network_interfaces, "/bin/ip")
|
||||||
|
|
||||||
|
networks.map! { |n| n[:device] = interfaces[n[:interface]]; n }
|
||||||
|
|
||||||
|
if comm.test('[[ `systemctl` =~ -\.mount ]]')
|
||||||
|
# Configure networking for Systemd
|
||||||
|
|
||||||
|
# convert netmasks to CIDR by converting to a binary string and counting the '1's
|
||||||
|
networks.map! { |n| n[:netmask] = IPAddr.new(n[:netmask]).to_i.to_s(2).count("1"); n }
|
||||||
|
|
||||||
|
# glob networks by device, so that we can write one file per device
|
||||||
|
# (result is hash[devicename] = [net, net, net...])
|
||||||
|
networks = networks.map { |n| [n[:device], n] }.reduce({}) { |h, (k, v)| (h[k] ||= []) << v; h }
|
||||||
|
|
||||||
|
# Write one .network file out for each device
|
||||||
|
networks.each_pair do |device_name, device_networks|
|
||||||
|
entry = TemplateRenderer.render('guests/gentoo/network_systemd', networks: device_networks)
|
||||||
|
|
||||||
|
filename = "50_vagrant_#{device_name}.network"
|
||||||
|
tmpfile = "/tmp/#{filename}"
|
||||||
|
destfile = "/etc/systemd/network/#{filename}"
|
||||||
|
|
||||||
|
Tempfile.open('vagrant-gentoo-configure-networks') do |f|
|
||||||
|
f.binmode
|
||||||
|
f.write(entry)
|
||||||
|
f.fsync
|
||||||
|
f.close
|
||||||
|
comm.upload(f.path, tmpfile)
|
||||||
|
end
|
||||||
|
|
||||||
|
commands << "mv #{tmpfile} #{destfile} && chmod 644 #{destfile}"
|
||||||
|
end
|
||||||
|
|
||||||
|
# tell systemd to reload the networking config
|
||||||
|
commands << 'systemctl daemon-reload && systemctl restart systemd-networkd.service'
|
||||||
|
else
|
||||||
|
# Configure networking for OpenRC
|
||||||
|
|
||||||
# Remove any previous network additions to the configuration file.
|
# Remove any previous network additions to the configuration file.
|
||||||
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net"
|
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/conf.d/net"
|
||||||
|
|
||||||
networks.each_with_index do |network, i|
|
networks.each_with_index do |network, i|
|
||||||
network[:device] = interfaces[network[:interface]]
|
|
||||||
|
|
||||||
entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}",
|
entry = TemplateRenderer.render("guests/gentoo/network_#{network[:type]}",
|
||||||
options: network,
|
options: network,
|
||||||
)
|
)
|
||||||
|
@ -44,6 +79,7 @@ module VagrantPlugins
|
||||||
/etc/init.d/net.#{network[:device]} start
|
/etc/init.d/net.#{network[:device]} start
|
||||||
EOH
|
EOH
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
comm.sudo(commands.join("\n"))
|
comm.sudo(commands.join("\n"))
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
[Match]
|
||||||
|
Name=<%= networks[0][:device] %>
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
<%- gateway = nil %>
|
||||||
|
<%- networks.each do |net| %>
|
||||||
|
<%- if net[:type] == 'dhcp' %>
|
||||||
|
DHCP=yes
|
||||||
|
<%- elsif net[:ip] %>
|
||||||
|
Address=<%= net[:ip] %>/<%= net[:netmask] %>
|
||||||
|
<%- end %>
|
||||||
|
<%- gateway ||= net[:gateway] %>
|
||||||
|
<%- end %>
|
||||||
|
<%- if gateway %>
|
||||||
|
Gateway=<%= gateway %>
|
||||||
|
<%- end %>
|
|
@ -0,0 +1,73 @@
|
||||||
|
require_relative "../../../base"
|
||||||
|
|
||||||
|
require "vagrant/util/template_renderer"
|
||||||
|
|
||||||
|
describe "templates/guests/gentoo/network_systemd" do
|
||||||
|
let(:template) { "guests/gentoo/network_systemd" }
|
||||||
|
|
||||||
|
it "renders the template with a static ip" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
type: "dhcp",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
DHCP=yes
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders the template with multiple ips" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
},{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.2.2",
|
||||||
|
netmask: "16",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
Address=1.1.2.2/16
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "renders the template with a static ip" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
|
||||||
|
it "includes the gateway" do
|
||||||
|
result = Vagrant::Util::TemplateRenderer.render(template, networks: [{
|
||||||
|
device: "eth0",
|
||||||
|
ip: "1.1.1.1",
|
||||||
|
netmask: "16",
|
||||||
|
gateway: "1.2.3.4",
|
||||||
|
}])
|
||||||
|
expect(result).to eq <<-EOH.gsub(/^ {6}/, "")
|
||||||
|
[Match]
|
||||||
|
Name=eth0
|
||||||
|
|
||||||
|
[Network]
|
||||||
|
Address=1.1.1.1/16
|
||||||
|
Gateway=1.2.3.4
|
||||||
|
EOH
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue