guests/photon: Configure networks in one command
This also removes code that was completely unused during network configuration.
This commit is contained in:
parent
2e943428a9
commit
9040ecafeb
|
@ -1,6 +1,3 @@
|
||||||
require 'tempfile'
|
|
||||||
require 'vagrant/util/template_renderer'
|
|
||||||
|
|
||||||
module VagrantPlugins
|
module VagrantPlugins
|
||||||
module GuestPhoton
|
module GuestPhoton
|
||||||
module Cap
|
module Cap
|
||||||
|
@ -8,33 +5,24 @@ module VagrantPlugins
|
||||||
include Vagrant::Util
|
include Vagrant::Util
|
||||||
|
|
||||||
def self.configure_networks(machine, networks)
|
def self.configure_networks(machine, networks)
|
||||||
machine.communicate.tap do |comm|
|
comm = machine.communicate
|
||||||
# Read network interface names
|
|
||||||
interfaces = []
|
|
||||||
comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result|
|
|
||||||
interfaces = result.split("\n")
|
|
||||||
end
|
|
||||||
|
|
||||||
# Configure interfaces
|
commands = []
|
||||||
networks.each do |network|
|
interfaces = []
|
||||||
comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}")
|
|
||||||
end
|
|
||||||
|
|
||||||
primary_machine_config = machine.env.active_machines.first
|
comm.sudo("ifconfig | grep 'eth' | cut -f1 -d' '") do |_, result|
|
||||||
primary_machine = machine.env.machine(*primary_machine_config, true)
|
interfaces = result.split("\n")
|
||||||
|
|
||||||
get_ip = lambda do |machine|
|
|
||||||
ip = nil
|
|
||||||
machine.config.vm.networks.each do |type, opts|
|
|
||||||
if type == :private_network && opts[:ip]
|
|
||||||
ip = opts[:ip]
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
ip
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
networks.each do |network|
|
||||||
|
device = interfaces[network[:interface]]
|
||||||
|
command = "ifconfig #{device}"
|
||||||
|
command << " #{network[:ip]}" if network[:ip]
|
||||||
|
command << " netmast #{network[:netmask]}" if network[:netmask]
|
||||||
|
commands << command
|
||||||
|
end
|
||||||
|
|
||||||
|
comm.sudo(commands.join("\n"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,40 +1,51 @@
|
||||||
# encoding: UTF-8
|
|
||||||
# Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
# Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
||||||
|
|
||||||
require File.expand_path("../../../../../base", __FILE__)
|
require_relative "../../../../base"
|
||||||
|
|
||||||
describe "VagrantPlugins::GuestPhoton::Cap::ConfigureNetworks" do
|
describe "VagrantPlugins::GuestPhoton::Cap:ConfigureNetworks" do
|
||||||
let(:described_class) do
|
let(:caps) do
|
||||||
VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:configure_networks)
|
VagrantPlugins::GuestPhoton::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:photon]
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(machine).to receive(:communicate).and_return(communicator)
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
|
comm.stub_command("ifconfig | grep 'eth' | cut -f1 -d' '",
|
||||||
|
stdout: "eth1\neth2")
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
communicator.verify_expectations!
|
comm.verify_expectations!
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should configure networks' do
|
describe ".configure_networks" do
|
||||||
networks = [
|
let(:cap) { caps.get(:configure_networks) }
|
||||||
{ :type => :static, :ip => '192.168.10.10', :netmask => '255.255.255.0', :interface => 1, :name => 'eth0' },
|
|
||||||
{ :type => :dhcp, :interface => 2, :name => 'eth1' },
|
|
||||||
{ :type => :static, :ip => '10.168.10.10', :netmask => '255.255.0.0', :interface => 3, :name => 'docker0' }
|
|
||||||
]
|
|
||||||
communicator.should_receive(:sudo).with("ifconfig | grep 'eth' | cut -f1 -d' '")
|
|
||||||
communicator.should_receive(:sudo).with('ifconfig 192.168.10.10 netmask 255.255.255.0')
|
|
||||||
communicator.should_receive(:sudo).with('ifconfig netmask ')
|
|
||||||
communicator.should_receive(:sudo).with('ifconfig 10.168.10.10 netmask 255.255.0.0')
|
|
||||||
|
|
||||||
allow_message_expectations_on_nil
|
let(:network_1) do
|
||||||
machine.should_receive(:env).at_least(5).times
|
{
|
||||||
machine.env.should_receive(:active_machines).at_least(:twice)
|
interface: 0,
|
||||||
machine.env.active_machines.should_receive(:first)
|
type: "dhcp",
|
||||||
machine.env.should_receive(:machine)
|
}
|
||||||
|
end
|
||||||
|
|
||||||
described_class.configure_networks(machine, networks)
|
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(/ifconfig eth1/)
|
||||||
|
expect(comm.received_commands[1]).to match(/ifconfig eth2 33.33.33.10 netmast 255.255.0.0/)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue