From a4441109939ca24403ac255caa621016b291ee1b Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 5 Jun 2016 13:14:02 -0400 Subject: [PATCH] guests/freebsd: Configure hostname in a single command This updates freebsd to set the hostname in a single command and prepend the hostname to the /etc/hosts file. --- .../guests/freebsd/cap/change_host_name.rb | 24 +++++++++-- .../freebsd/cap/change_host_name_test.rb | 40 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb diff --git a/plugins/guests/freebsd/cap/change_host_name.rb b/plugins/guests/freebsd/cap/change_host_name.rb index 6b24440b0..fc194d6d9 100644 --- a/plugins/guests/freebsd/cap/change_host_name.rb +++ b/plugins/guests/freebsd/cap/change_host_name.rb @@ -3,9 +3,27 @@ module VagrantPlugins module Cap class ChangeHostName def self.change_host_name(machine, name) - if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'", {shell: "sh"}) - machine.communicate.sudo("sed -i '' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf", {shell: "sh"}) - machine.communicate.sudo("hostname #{name}", {shell: "sh"}) + options = { shell: "sh" } + comm = machine.communicate + + if !comm.test("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", options) + basename = name.split(".", 2)[0] + command = <<-EOH.gsub(/^ {14}/, '') + # Set the hostname + hostname '#{name}' + sed -i '' 's/^hostname=.*$/hostname=\"#{name}\"/' /etc/rc.conf + + # Remove comments and blank lines from /etc/hosts + sed -i'' -e 's/#.*$//' /etc/hosts + sed -i'' -e '/^$/d' /etc/hosts + + # Prepend ourselves to /etc/hosts + grep -w '#{name}' /etc/hosts || { + echo -e '127.0.0.1\\t#{name}\\t#{basename}' | cat - /etc/hosts > /tmp/tmp-hosts + mv /tmp/tmp-hosts /etc/hosts + } + EOH + comm.sudo(command, options) end end end diff --git a/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb new file mode 100644 index 000000000..fc5c061e9 --- /dev/null +++ b/test/unit/plugins/guests/freebsd/cap/change_host_name_test.rb @@ -0,0 +1,40 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestFreeBSD::Cap::ChangeHostName" do + let(:described_class) do + VagrantPlugins::GuestFreeBSD::Plugin + .components + .guest_capabilities[:freebsd] + .get(:change_host_name) + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".change_host_name" do + let(:name) { "banana-rama.example.com" } + + it "sets the hostname and /etc/hosts" do + comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 1) + described_class.change_host_name(machine, name) + + expect(comm.received_commands[1]).to match(/hostname '#{name}'/) + expect(comm.received_commands[1]).to match(/grep -w '#{name}' \/etc\/hosts/) + expect(comm.received_commands[1]).to match(/echo -e '127.0.0.1\\t#{name}\\tbanana-rama'/) + end + + it "does nothing if the hostname is already set" do + comm.stub_command("hostname -f | grep -w '#{name}' || hostname -s | grep -w '#{name}'", exit_code: 0) + described_class.change_host_name(machine, name) + expect(comm.received_commands.size).to eq(1) + end + end +end