guests/omnios: Set hostname in one command

This commit is contained in:
Seth Vargo 2016-06-05 16:19:01 -04:00
parent 90b62a0943
commit 95972c1527
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
2 changed files with 58 additions and 5 deletions

View File

@ -3,12 +3,24 @@ module VagrantPlugins
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
su_cmd = machine.config.solaris.suexec_cmd
comm = machine.communicate
# Only do this if the hostname is not already set
if !machine.communicate.test("#{su_cmd} hostname | grep '#{name}'")
machine.communicate.execute("#{su_cmd} sh -c \"echo '#{name}' > /etc/nodename\"")
machine.communicate.execute("#{su_cmd} hostname #{name}")
if !comm.test("hostname | grep -w '#{name}'", sudo: false)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set hostname
echo '#{name}' > /etc/nodename
hostname '#{name}'
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -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
end
end
end

View File

@ -0,0 +1,41 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestOmniOS::Cap:RSync" do
let(:caps) do
VagrantPlugins::GuestOmniOS::Plugin
.components
.guest_capabilities[:omnios]
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(:cap) { caps.get(:change_host_name) }
let(:name) { "banana-rama.example.com" }
it "sets the hostname if unset" do
comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1)
cap.change_host_name(machine, name)
expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/nodename/)
expect(comm.received_commands[1]).to match(/hostname '#{name}'/)
end
it "does not set the hostname if set" do
comm.stub_command("hostname | grep -w '#{name}'", exit_code: 0)
cap.change_host_name(machine, name)
expect(comm.received_commands.size).to eq(1)
end
end
end