From 3ca048a8fac025ff33674a8ef521522298b794dc Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Mon, 30 May 2016 23:58:13 -0400 Subject: [PATCH] guests/coreos: Configure networks in one command This commit configures all the network devices in a single command. --- .../guests/coreos/cap/configure_networks.rb | 71 +++++++++++-------- .../coreos/cap/configure_networks_test.rb | 59 +++++++++++++++ 2 files changed, 100 insertions(+), 30 deletions(-) create mode 100644 test/unit/plugins/guests/coreos/cap/configure_networks_test.rb diff --git a/plugins/guests/coreos/cap/configure_networks.rb b/plugins/guests/coreos/cap/configure_networks.rb index 887d68c8c..9aff20837 100644 --- a/plugins/guests/coreos/cap/configure_networks.rb +++ b/plugins/guests/coreos/cap/configure_networks.rb @@ -10,47 +10,26 @@ module VagrantPlugins def self.configure_networks(machine, networks) machine.communicate.tap do |comm| - # Disable default etcd - comm.sudo("systemctl stop etcd") - # Read network interface names interfaces = [] comm.sudo("ifconfig | grep '(e[n,t][h,s,p][[:digit:]]([a-z][[:digit:]])?' | cut -f1 -d:") do |_, result| interfaces = result.split("\n") 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 = machine.env.machine(*primary_machine_config, true) - 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 - - primary_machine_ip = get_ip.(primary_machine) - current_ip = get_ip.(machine) + primary_machine_ip = get_ip(primary_machine) + current_ip = get_ip(machine) if current_ip == primary_machine_ip entry = TemplateRenderer.render("guests/coreos/etcd.service", options: { - my_ip: current_ip - }) + my_ip: current_ip, + }) else connection_string = "#{primary_machine_ip}:7001" entry = TemplateRenderer.render("guests/coreos/etcd.service", options: { connection_string: connection_string, - my_ip: current_ip + my_ip: current_ip, }) end @@ -62,13 +41,45 @@ module VagrantPlugins comm.upload(f.path, "/tmp/etcd-cluster.service") end - comm.sudo("mv /tmp/etcd-cluster.service /media/state/units/") - comm.sudo("systemctl restart local-enable.service") + # Build a list of commands + commands = [] - # Restart default etcd - comm.sudo("systemctl start etcd") + # Stop default systemd + 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 + + 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 diff --git a/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb b/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb new file mode 100644 index 000000000..28409ea6f --- /dev/null +++ b/test/unit/plugins/guests/coreos/cap/configure_networks_test.rb @@ -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