Update omnios cap for setting hostname

This commit is contained in:
sophia 2020-07-20 13:34:02 -05:00
parent 169d7b4011
commit cf130d07a2
2 changed files with 32 additions and 30 deletions

View File

@ -1,24 +1,18 @@
require_relative '../../linux/cap/change_host_name'
module VagrantPlugins
module GuestOmniOS
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
comm = machine.communicate
extend VagrantPlugins::GuestLinux::Cap::ChangeHostName
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set hostname
echo '#{name}' > /etc/nodename
hostname '#{name}'
# 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
end
def self.change_name_command(name)
basename = name.split(".", 2)[0]
return <<-EOH.gsub(/^ {14}/, "")
# Set hostname
echo '#{name}' > /etc/nodename
hostname '#{name}'
EOH
end
end
end

View File

@ -1,14 +1,17 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do
let(:caps) do
describe "VagrantPlugins::GuestOmniOS::Cap::ChangeHostName" do
let(:described_class) do
VagrantPlugins::GuestOmniOS::Plugin
.components
.guest_capabilities[:omnios]
.get(:change_host_name)
end
let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:name) { "banana-rama.example.com" }
let(:basename) { "banana-rama" }
before do
allow(machine).to receive(:communicate).and_return(comm)
@ -19,23 +22,28 @@ describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do
end
describe ".change_host_name" do
let(:cap) { caps.get(:change_host_name) }
context "minimal network config" do
let(:networks) { [
[:forwarded_port, {:guest=>22, :host=>2222, :host_ip=>"127.0.0.1", :id=>"ssh", :auto_correct=>true, :protocol=>"tcp"}]
] }
let(:name) { "banana-rama.example.com" }
before do
allow(machine).to receive_message_chain(:config, :vm, :networks).and_return(networks)
end
it "sets the hostname if unset" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
cap.change_host_name(machine, name)
it "sets the hostname" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/nodename/)
expect(comm.received_commands[1]).to match(/hostname '#{name}'/)
end
described_class.change_host_name(machine, name)
expect(comm.received_commands[2]).to match(/hostname '#{name}'/)
end
it "does not set the hostname if set" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
cap.change_host_name(machine, name)
it "does not change the hostname if already set" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
expect(comm.received_commands.size).to eq(1)
described_class.change_host_name(machine, name)
expect(comm).to_not receive(:sudo).with(/hostname '#{name}'/)
end
end
end
end