getent queries the system resolver for the hostname - but it's not the resolver we're interested in. In fact, the hostname-to-be-set may already exist in DNS (becuase DNS really is a nifty thing and can do a lot of things which are not that possible with /etc/hosts alone), in which case getent will "not fail" and vagrant will believe the hostname had already been set. Instead, query hostnamectl for the "static" hostname - that's the one we will be setting, so we're ok IFF hostnamectl returns exactly what we would be setting.
24 lines
666 B
Ruby
24 lines
666 B
Ruby
module VagrantPlugins
|
|
module GuestSUSE
|
|
module Cap
|
|
class ChangeHostName
|
|
def self.change_host_name(machine, name)
|
|
comm = machine.communicate
|
|
|
|
basename = name.split(".", 2)[0]
|
|
if !comm.test('test "$(hostnamectl --static status)" = "#{basename}"', sudo: false)
|
|
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
|
hostnamectl set-hostname '#{basename}'
|
|
|
|
# Prepend ourselves to /etc/hosts
|
|
grep -w '#{name}' /etc/hosts || {
|
|
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
|
}
|
|
EOH
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|