Merge pull request #11100 from chkpnt/suse-hostname

Use hostnamectl instead of hostname to set the hostname under SUSE
This commit is contained in:
Brian Cain 2019-10-08 09:26:46 -07:00 committed by GitHub
commit ea550289a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 7 deletions

View File

@ -5,11 +5,10 @@ module VagrantPlugins
def self.change_host_name(machine, name)
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
if !comm.test("getent hosts '#{name}'", sudo: false)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
echo '#{basename}' > /etc/HOSTNAME
hostname '#{basename}'
hostnamectl set-hostname '#{basename}'
# Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || {

View File

@ -25,15 +25,15 @@ describe "VagrantPlugins::GuestSUSE::Cap::ChangeHostName" do
let(:basename) { "banana-rama" }
it "sets the hostname" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
comm.stub_command("getent hosts '#{name}'", exit_code: 1)
cap.change_host_name(machine, name)
expect(comm.received_commands[1]).to match(/echo '#{basename}' > \/etc\/HOSTNAME/)
expect(comm.received_commands[1]).to match(/hostname '#{basename}'/)
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{basename}'/)
end
it "does not change the hostname if already set" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
comm.stub_command("getent hosts '#{name}'", exit_code: 0)
cap.change_host_name(machine, name)
expect(comm.received_commands.size).to eq(1)
end