guests/omnios: Set hostname in one command
This commit is contained in:
parent
90b62a0943
commit
95972c1527
|
@ -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
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue