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.
This commit is contained in:
parent
8c095ef172
commit
a444110993
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue