guests/coreos: Configure networks in one command
This commit configures all the network devices in a single command.
This commit is contained in:
parent
fee0545b23
commit
3ca048a8fa
|
@ -10,47 +10,26 @@ module VagrantPlugins
|
||||||
|
|
||||||
def self.configure_networks(machine, networks)
|
def self.configure_networks(machine, networks)
|
||||||
machine.communicate.tap do |comm|
|
machine.communicate.tap do |comm|
|
||||||
# Disable default etcd
|
|
||||||
comm.sudo("systemctl stop etcd")
|
|
||||||
|
|
||||||
# Read network interface names
|
# Read network interface names
|
||||||
interfaces = []
|
interfaces = []
|
||||||
comm.sudo("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:") do |_, result|
|
comm.sudo("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:") do |_, result|
|
||||||
interfaces = result.split("\n")
|
interfaces = result.split("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Configure interfaces
|
|
||||||
# FIXME: fix matching of interfaces with IP adresses
|
|
||||||
networks.each do |network|
|
|
||||||
comm.sudo("ifconfig #{interfaces[network[:interface].to_i]} #{network[:ip]} netmask #{network[:netmask]}")
|
|
||||||
end
|
|
||||||
|
|
||||||
primary_machine_config = machine.env.active_machines.first
|
primary_machine_config = machine.env.active_machines.first
|
||||||
primary_machine = machine.env.machine(*primary_machine_config, true)
|
primary_machine = machine.env.machine(*primary_machine_config, true)
|
||||||
|
|
||||||
get_ip = lambda do |machine|
|
primary_machine_ip = get_ip(primary_machine)
|
||||||
ip = nil
|
current_ip = get_ip(machine)
|
||||||
machine.config.vm.networks.each do |type, opts|
|
|
||||||
if type == :private_network && opts[:ip]
|
|
||||||
ip = opts[:ip]
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
ip
|
|
||||||
end
|
|
||||||
|
|
||||||
primary_machine_ip = get_ip.(primary_machine)
|
|
||||||
current_ip = get_ip.(machine)
|
|
||||||
if current_ip == primary_machine_ip
|
if current_ip == primary_machine_ip
|
||||||
entry = TemplateRenderer.render("guests/coreos/etcd.service", options: {
|
entry = TemplateRenderer.render("guests/coreos/etcd.service", options: {
|
||||||
my_ip: current_ip
|
my_ip: current_ip,
|
||||||
})
|
})
|
||||||
else
|
else
|
||||||
connection_string = "#{primary_machine_ip}:7001"
|
connection_string = "#{primary_machine_ip}:7001"
|
||||||
entry = TemplateRenderer.render("guests/coreos/etcd.service", options: {
|
entry = TemplateRenderer.render("guests/coreos/etcd.service", options: {
|
||||||
connection_string: connection_string,
|
connection_string: connection_string,
|
||||||
my_ip: current_ip
|
my_ip: current_ip,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -62,13 +41,45 @@ module VagrantPlugins
|
||||||
comm.upload(f.path, "/tmp/etcd-cluster.service")
|
comm.upload(f.path, "/tmp/etcd-cluster.service")
|
||||||
end
|
end
|
||||||
|
|
||||||
comm.sudo("mv /tmp/etcd-cluster.service /media/state/units/")
|
# Build a list of commands
|
||||||
comm.sudo("systemctl restart local-enable.service")
|
commands = []
|
||||||
|
|
||||||
# Restart default etcd
|
# Stop default systemd
|
||||||
comm.sudo("systemctl start etcd")
|
commands << "systemctl stop etcd"
|
||||||
|
|
||||||
|
# Configure interfaces
|
||||||
|
# FIXME: fix matching of interfaces with IP adresses
|
||||||
|
networks.each do |network|
|
||||||
|
iface = interfaces[network[:interface].to_i]
|
||||||
|
commands << "ifconfig #{iface} #{network[:ip]} netmask #{network[:netmask]}".squeeze(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
commands << <<-EOH.gsub(/^ {14}/, '')
|
||||||
|
mv /tmp/etcd-cluster.service /media/state/units/
|
||||||
|
systemctl restart local-enable.service
|
||||||
|
|
||||||
|
# Restart default etcd
|
||||||
|
systemctl start etcd
|
||||||
|
EOH
|
||||||
|
|
||||||
|
# Run all network configuration commands in one communicator session.
|
||||||
|
comm.sudo(commands.join("\n"))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.get_ip(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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
require_relative "../../../../base"
|
||||||
|
|
||||||
|
describe "VagrantPlugins::GuestCoreOS::Cap::ConfigureNetworks" do
|
||||||
|
let(:described_class) do
|
||||||
|
VagrantPlugins::GuestCoreOS::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:coreos]
|
||||||
|
.get(:configure_networks)
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:machine) { double("machine") }
|
||||||
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
|
let(:env) do
|
||||||
|
double("env", machine: machine, active_machines: [machine])
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
|
allow(machine).to receive(:env).and_return(env)
|
||||||
|
|
||||||
|
allow(described_class).to receive(:get_ip).and_return("1.2.3.4")
|
||||||
|
|
||||||
|
comm.stub_command("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:",
|
||||||
|
stdout: "eth1\neth2")
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
comm.verify_expectations!
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".configure_networks" do
|
||||||
|
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
|
||||||
|
described_class.configure_networks(machine, [network_1, network_2])
|
||||||
|
expect(comm.received_commands[1]).to match(/systemctl stop etcd/)
|
||||||
|
expect(comm.received_commands[1]).to match(/ifconfig eth1 netmask/)
|
||||||
|
expect(comm.received_commands[1]).to match(/ifconfig eth2 33.33.33.10 netmask 255.255.0.0/)
|
||||||
|
expect(comm.received_commands[1]).to match(/systemctl restart local-enable.service/)
|
||||||
|
expect(comm.received_commands[1]).to match(/systemctl start etcd/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue