guests/slackware: Configure networks in one command
This also switches to using new predictable naming for networks.
This commit is contained in:
parent
a7bbb484ad
commit
0bdf6f5ad4
|
@ -1,4 +1,3 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
require "tempfile"
|
require "tempfile"
|
||||||
|
|
||||||
require_relative "../../../../lib/vagrant/util/template_renderer"
|
require_relative "../../../../lib/vagrant/util/template_renderer"
|
||||||
|
@ -10,27 +9,42 @@ module VagrantPlugins
|
||||||
include Vagrant::Util
|
include Vagrant::Util
|
||||||
|
|
||||||
def self.configure_networks(machine, networks)
|
def self.configure_networks(machine, networks)
|
||||||
interfaces = Array.new
|
comm = machine.communicate
|
||||||
machine.communicate.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result|
|
|
||||||
|
commands = []
|
||||||
|
interfaces = []
|
||||||
|
|
||||||
|
comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result|
|
||||||
interfaces = result.split("\n")
|
interfaces = result.split("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
networks.each do |network|
|
# Remove any previous configuration
|
||||||
|
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.d/rc.inet1.conf"
|
||||||
|
|
||||||
|
networks.each.with_index do |network, i|
|
||||||
network[:device] = interfaces[network[:interface]]
|
network[:device] = interfaces[network[:interface]]
|
||||||
|
|
||||||
entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}", options: network)
|
entry = TemplateRenderer.render("guests/slackware/network_#{network[:type]}",
|
||||||
|
i: i+1,
|
||||||
|
options: network,
|
||||||
|
)
|
||||||
|
|
||||||
|
remote_path = "/tmp/vagrant-network-#{network[:device]}-#{Time.now}-#{i}"
|
||||||
Tempfile.open("vagrant-slackware-configure-networks") do |f|
|
Tempfile.open("vagrant-slackware-configure-networks") do |f|
|
||||||
f.binmode
|
f.binmode
|
||||||
f.write(entry)
|
f.write(entry)
|
||||||
f.fsync
|
f.fsync
|
||||||
f.close
|
f.close
|
||||||
machine.communicate.upload(f.path, "/tmp/vagrant_network")
|
comm.upload(f.path, remote_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
machine.communicate.sudo("mv /tmp/vagrant_network /etc/rc.d/rc.inet1.conf")
|
commands << "cat '#{remote_path}' >> /etc/rc.d/rc.inet1.conf"
|
||||||
machine.communicate.sudo("/etc/rc.d/rc.inet1")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Restart networking
|
||||||
|
commands << "/etc/rc.d/rc.inet1"
|
||||||
|
|
||||||
|
comm.sudo(commands.join("\n"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
IPADDR[0]=""
|
#VAGRANT-BEGIN
|
||||||
NETMASK[0]=""
|
# Config for eth<%= i %>
|
||||||
USE_DHCP[0]="yes"
|
USE_DHCP[<%= i %>]="yes"
|
||||||
DHCP_HOSTNAME[0]=""
|
DHCP_HOSTNAME[<%= i %>]=""
|
||||||
|
|
||||||
IPADDR[1]=""
|
<% if options[:gateway] -%>
|
||||||
NETMASK[1]=""
|
GATEWAY="<%= options[:gateway] %>"
|
||||||
USE_DHCP[1]="yes"
|
<% end -%>
|
||||||
DHCP_HOSTNAME[1]=""
|
|
||||||
|
|
||||||
IPADDR[2]=""
|
|
||||||
NETMASK[2]=""
|
|
||||||
USE_DHCP[2]=""
|
|
||||||
DHCP_HOSTNAME[2]=""
|
|
||||||
|
|
||||||
IPADDR[3]=""
|
|
||||||
NETMASK[3]=""
|
|
||||||
USE_DHCP[3]=""
|
|
||||||
DHCP_HOSTNAME[3]=""
|
|
||||||
|
|
||||||
GATEWAY=""
|
|
||||||
|
|
||||||
DEBUG_ETH_UP="no"
|
DEBUG_ETH_UP="no"
|
||||||
|
#VAGRANT-END
|
||||||
|
|
|
@ -1,25 +1,13 @@
|
||||||
IPADDR[0]=""
|
#VAGRANT-BEGIN
|
||||||
NETMASK[0]=""
|
# Config for eth<%= i %>
|
||||||
USE_DHCP[0]="yes"
|
IPADDR[<%= i %>]="<%= options[:ip] %>"
|
||||||
DHCP_HOSTNAME[0]=""
|
NETMASK[<%= i %>]="<%= options[:ip] %>"
|
||||||
|
USE_DHCP[<%= i %>]=""
|
||||||
|
DHCP_HOSTNAME[<%= i %>]=""
|
||||||
|
|
||||||
IPADDR[1]="<%= options[:ip] %>"
|
<% if options[:gateway] -%>
|
||||||
NETMASK[1]=""
|
GATEWAY="<%= options[:gateway] %>"
|
||||||
USE_DHCP[1]=""
|
<% end -%>
|
||||||
DHCP_HOSTNAME[1]=""
|
|
||||||
|
|
||||||
IPADDR[2]=""
|
|
||||||
NETMASK[2]=""
|
|
||||||
USE_DHCP[2]=""
|
|
||||||
DHCP_HOSTNAME[2]=""
|
|
||||||
|
|
||||||
IPADDR[3]=""
|
|
||||||
NETMASK[3]=""
|
|
||||||
USE_DHCP[3]=""
|
|
||||||
DHCP_HOSTNAME[3]=""
|
|
||||||
|
|
||||||
<% if options[:gateway] %>
|
|
||||||
GATEWAY="<%= options[:gateway] %>"
|
|
||||||
<% end %>
|
|
||||||
|
|
||||||
DEBUG_ETH_UP="no"
|
DEBUG_ETH_UP="no"
|
||||||
|
#VAGRANT-END
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
require_relative "../../../../base"
|
||||||
|
|
||||||
|
describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do
|
||||||
|
let(:caps) do
|
||||||
|
VagrantPlugins::GuestSlackware::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:slackware]
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:machine) { double("machine") }
|
||||||
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
|
comm.stub_command("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'",
|
||||||
|
stdout: "eth1\neth2")
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
comm.verify_expectations!
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".configure_networks" do
|
||||||
|
let(:cap) { caps.get(:configure_networks) }
|
||||||
|
|
||||||
|
let(:network_1) do
|
||||||
|
{
|
||||||
|
interface: 0,
|
||||||
|
type: "dhcp",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:network_2) do
|
||||||
|
{
|
||||||
|
interface: 1,
|
||||||
|
type: "static",
|
||||||
|
ip: "33.33.33.10",
|
||||||
|
netmask: "255.255.0.0",
|
||||||
|
gateway: "33.33.0.1",
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "creates and starts the networks" do
|
||||||
|
cap.configure_networks(machine, [network_1, network_2])
|
||||||
|
expect(comm.received_commands[1]).to match(/\/etc\/rc.d\/rc.inet1/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue