guests/linux: Add shared cap for listing network interfaces

This commit is contained in:
Seth Vargo 2016-06-22 19:08:02 -07:00
parent 66cbe7b41e
commit 8f3b6511f2
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
13 changed files with 98 additions and 92 deletions

View File

@ -12,13 +12,7 @@ module VagrantPlugins
comm = machine.communicate
commands = []
interfaces = []
# The result will be something like:
# eth0\nenp0s8\nenp0s9
comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
interfaces = stdout.split("\n")
end
interfaces = machine.guest.capability(:network_interfaces)
networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]]

View File

@ -13,11 +13,7 @@ module VagrantPlugins
commands = []
entries = []
interfaces = []
comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
interfaces = stdout.split("\n")
end
interfaces = machine.guest.capability(:network_interfaces)
networks.each do |network|
network[:device] = interfaces[network[:interface]]

View File

@ -14,14 +14,10 @@ module VagrantPlugins
comm = machine.communicate
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
interfaces = machine.guest.capability(:network_interface)
interfaces = []
commands = []
comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
interfaces = stdout.split("\n")
end
networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]]

View File

@ -0,0 +1,20 @@
module VagrantPlugins
module GuestLinux
module Cap
class NetworkInterfaces
# Get network interfaces as a list. The result will be something like:
#
# eth0\nenp0s8\nenp0s9
#
# @return [Array<String>]
def self.network_interfaces(machine)
s = ""
machine.communicate.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |type, data|
s << data if type == :stdout
end
s.split("\n")
end
end
end
end
end

View File

@ -46,6 +46,11 @@ module VagrantPlugins
Cap::MountVirtualBoxSharedFolder
end
guest_capability(:linux, :network_interfaces) do
require_relative "cap/network_interfaces"
Cap::NetworkInterfaces
end
guest_capability(:linux, :nfs_client_installed) do
require_relative "cap/nfs_client"
Cap::NFSClient

View File

@ -32,12 +32,8 @@ module VagrantPlugins
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
interfaces = []
commands = []
comm.sudo("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
interfaces = stdout.split("\n")
end
interfaces = machine.guest.capability(:network_interfaces)
networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]]

View File

@ -12,11 +12,7 @@ module VagrantPlugins
comm = machine.communicate
commands = []
interfaces = []
comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, result|
interfaces = result.split("\n")
end
interfaces = machine.guest.capability(:network_interfaces)
# Remove any previous configuration
commands << "sed -i'' -e '/^#VAGRANT-BEGIN/,/^#VAGRANT-END/ d' /etc/rc.d/rc.inet1.conf"

View File

@ -15,11 +15,7 @@ module VagrantPlugins
network_scripts_dir = machine.guest.capability(:network_scripts_dir)
commands = []
interfaces = []
comm.sudo("ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'") do |_, stdout|
interfaces = stdout.split("\n")
end
interfaces = machine.guest.capability(:network_interfaces)
networks.each.with_index do |network, i|
network[:device] = interfaces[network[:interface]]

View File

@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do
.guest_capabilities[:arch]
end
let(:machine) { double("machine") }
let(:guest) { double("guest") }
let(:machine) { double("machine", guest: guest) }
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
@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }
before do
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end
let(:network_1) do
{
interface: 0,
@ -42,15 +46,15 @@ describe "VagrantPlugins::GuestArch::Cap::ConfigureNetworks" do
it "creates and starts the networks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[1]).to match(/mv (.+) '\/etc\/netctl\/eth1'/)
expect(comm.received_commands[1]).to match(/ip link set 'eth1' down/)
expect(comm.received_commands[1]).to match(/netctl restart 'eth1'/)
expect(comm.received_commands[1]).to match(/netctl enable 'eth1'/)
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[1]).to match(/mv (.+) '\/etc\/netctl\/eth2'/)
expect(comm.received_commands[1]).to match(/ip link set 'eth2' down/)
expect(comm.received_commands[1]).to match(/netctl restart 'eth2'/)
expect(comm.received_commands[1]).to match(/netctl enable 'eth2'/)
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

View File

@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do
.guest_capabilities[:debian]
end
let(:machine) { double("machine") }
let(:guest) { double("guest") }
let(:machine) { double("machine", guest: guest) }
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
@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }
before do
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end
let(:network_0) do
{
interface: 0,
@ -43,12 +47,12 @@ describe "VagrantPlugins::GuestDebian::Cap::ConfigureNetworks" do
it "creates and starts the networks" do
cap.configure_networks(machine, [network_0, network_1])
expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth1' || true")
expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth1'")
expect(comm.received_commands[1]).to match("/sbin/ifdown 'eth2' || true")
expect(comm.received_commands[1]).to match("/sbin/ip addr flush dev 'eth2'")
expect(comm.received_commands[1]).to match("/sbin/ifup 'eth1'")
expect(comm.received_commands[1]).to match("/sbin/ifup 'eth2'")
expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth1' || true")
expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth1'")
expect(comm.received_commands[0]).to match("/sbin/ifdown 'eth2' || true")
expect(comm.received_commands[0]).to match("/sbin/ip addr flush dev 'eth2'")
expect(comm.received_commands[0]).to match("/sbin/ifup 'eth1'")
expect(comm.received_commands[0]).to match("/sbin/ifup 'eth2'")
end
end
end

View File

@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
.guest_capabilities[:redhat]
end
let(:machine) { double("machine") }
let(:guest) { double("guest") }
let(:machine) { double("machine", guest: guest) }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
allow(machine).to receive(:communicate).and_return(comm)
comm.stub_command("/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'",
stdout: "eth1\neth2")
end
after do
@ -23,6 +22,16 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }
before do
allow(guest).to receive(:capability)
.with(:network_scripts_dir)
.and_return("/scripts")
allow(guest).to receive(:capability)
.with(:network_interfaces)
.and_return(["eth1", "eth2"])
end
let(:network_1) do
{
interface: 0,
@ -40,21 +49,10 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
}
end
let(:network_scripts_dir) { "/" }
let(:capability) { double("capability") }
before do
allow(machine).to receive(:guest).and_return(capability)
allow(capability).to receive(:capability)
.with(:network_scripts_dir)
.and_return(network_scripts_dir)
end
it "uses fedora for rhel7 configuration" do
require_relative "../../../../../../plugins/guests/fedora/cap/configure_networks"
allow(capability).to receive(:capability)
allow(guest).to receive(:capability)
.with(:flavor)
.and_return(:rhel_7)
allow(VagrantPlugins::GuestFedora::Cap::ConfigureNetworks)
@ -66,15 +64,15 @@ describe "VagrantPlugins::GuestRedHat::Cap::ConfigureNetworks" do
end
it "creates and starts the networks" do
allow(capability).to receive(:capability)
allow(guest).to receive(:capability)
.with(:flavor)
.and_return(:rhel)
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/)
end
end
end

View File

@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do
.guest_capabilities[:slackware]
end
let(:machine) { double("machine") }
let(:guest) { double("guest") }
let(:machine) { double("machine", guest: guest) }
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
@ -23,6 +22,11 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }
before do
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end
let(:network_1) do
{
interface: 0,
@ -42,7 +46,7 @@ describe "VagrantPlugins::GuestSlackware::Cap::ConfigureNetworks" do
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/)
expect(comm.received_commands[0]).to match(/\/etc\/rc.d\/rc.inet1/)
end
end
end

View File

@ -7,13 +7,12 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do
.guest_capabilities[:suse]
end
let(:machine) { double("machine") }
let(:guest) { double("guest") }
let(:machine) { double("machine", guest: guest) }
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
@ -23,6 +22,13 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do
describe ".configure_networks" do
let(:cap) { caps.get(:configure_networks) }
before do
allow(guest).to receive(:capability).with(:network_scripts_dir)
.and_return("/scripts")
allow(guest).to receive(:capability).with(:network_interfaces)
.and_return(["eth1", "eth2"])
end
let(:network_1) do
{
interface: 0,
@ -40,21 +46,12 @@ describe "VagrantPlugins::GuestSUSE::Cap::ConfigureNetworks" do
}
end
let(:guest) { double("guest") }
before do
allow(machine).to receive(:guest).and_return(guest)
allow(guest).to receive(:capability)
.with(:network_scripts_dir)
.and_return("/scripts")
end
it "creates and starts the networks" do
cap.configure_networks(machine, [network_1, network_2])
expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[1]).to match(/\/sbin\/ifup 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth1'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifdown 'eth2'/)
expect(comm.received_commands[0]).to match(/\/sbin\/ifup 'eth2'/)
end
end
end