Skip to content

Commit

Permalink
update /etc/hosts on rhel 7
Browse files Browse the repository at this point in the history
the previous fix for hashicorp#4465 assumed that the NetworkManager takes care of updating /etc/hosts with
the new hostname, but testing shows it doesn't. This change ensures that the change is always added
to /etc/hosts when the name changes.
  • Loading branch information
Xylakant committed Sep 20, 2014
1 parent 6747f12 commit 96b011b
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions plugins/guests/redhat/cap/change_host_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ module GuestRedHat
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
case machine.guest.capability("flavor")
when :rhel_7
change_host_name_rhel7(machine, name)
else
new(machine, name).change!
end
end

def self.change_host_name_rhel7(machine, name)
machine.communicate.sudo("hostnamectl set-hostname #{name}")
new(machine, name).change!
end

attr_reader :machine, :new_hostname
Expand All @@ -25,11 +16,17 @@ def initialize(machine, new_hostname)
def change!
return unless should_change?

update_sysconfig
update_hostname
update_etc_hosts
update_dhcp_hostnames
restart_networking
case machine.guest.capability("flavor")
when :rhel_7
update_hostname_rhel7
update_etc_hosts
else
update_sysconfig
update_hostname
update_etc_hosts
update_dhcp_hostnames
restart_networking
end
end

def should_change?
Expand Down Expand Up @@ -61,6 +58,10 @@ def update_hostname
sudo "hostname #{fqdn}"
end

def update_hostname_rhel7
sudo "hostnamectl set-hostname #{fqdn}"
end

# /etc/hosts should resemble:
# 127.0.0.1 host.fqdn.com host localhost ...
def update_etc_hosts
Expand Down

0 comments on commit 96b011b

Please sign in to comment.