2013-04-04 18:49:26 +00:00
|
|
|
module VagrantPlugins
|
2013-04-04 19:09:40 +00:00
|
|
|
module GuestRedHat
|
2013-04-04 18:49:26 +00:00
|
|
|
module Cap
|
|
|
|
class ChangeHostName
|
|
|
|
def self.change_host_name(machine, name)
|
2013-12-16 03:39:03 +00:00
|
|
|
new(machine, name).change!
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :machine, :new_hostname
|
|
|
|
|
|
|
|
def initialize(machine, new_hostname)
|
|
|
|
@machine = machine
|
|
|
|
@new_hostname = new_hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
def change!
|
|
|
|
return unless should_change?
|
|
|
|
|
|
|
|
update_sysconfig
|
|
|
|
update_hostname
|
|
|
|
update_etc_hosts
|
|
|
|
update_dhcp_hostnames
|
|
|
|
restart_networking
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_change?
|
|
|
|
new_hostname != current_hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_hostname
|
|
|
|
@current_hostname ||= get_current_hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_hostname
|
|
|
|
hostname = ''
|
|
|
|
block = lambda do |type, data|
|
2014-01-08 22:49:54 +00:00
|
|
|
if type == :stdout
|
2013-12-16 03:39:03 +00:00
|
|
|
hostname += data.chomp
|
2013-04-04 18:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
2013-12-16 03:39:03 +00:00
|
|
|
|
|
|
|
execute 'hostname -f', error_check: false, &block
|
|
|
|
execute 'hostname',&block if hostname.empty?
|
|
|
|
/localhost(\..*)?/.match(hostname) ? '' : hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_sysconfig
|
|
|
|
sudo "sed -i 's/\\(HOSTNAME=\\).*/\\1#{fqdn}/' /etc/sysconfig/network"
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_hostname
|
2014-01-08 22:49:54 +00:00
|
|
|
sudo "hostname #{fqdn}"
|
|
|
|
end
|
2013-12-16 03:39:03 +00:00
|
|
|
|
|
|
|
# /etc/hosts should resemble:
|
|
|
|
# 127.0.0.1 host.fqdn.com host localhost ...
|
|
|
|
def update_etc_hosts
|
|
|
|
s = '[[:space:]]'
|
|
|
|
current_fqdn = Regexp.escape(current_hostname)
|
|
|
|
current_short = Regexp.escape(current_hostname.split('.').first.to_s)
|
|
|
|
currents = "\\(#{current_fqdn}#{s}\\+\\|#{current_short}#{s}\\+\\)*" unless current_hostname.empty?
|
|
|
|
local_ip = '127[.]0[.]0[.]1'
|
|
|
|
search = "^\\(#{local_ip}#{s}\\+\\)#{currents}"
|
|
|
|
replace = "\\1#{fqdn} "
|
|
|
|
replace = "#{replace}#{short_hostname} " unless fqdn == short_hostname
|
|
|
|
expression = ['s', search, replace,''].join('@')
|
|
|
|
|
|
|
|
sudo "sed -i '#{expression}' /etc/hosts"
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_dhcp_hostnames
|
|
|
|
sudo "sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{short_hostname}\"/' /etc/sysconfig/network-scripts/ifcfg-*"
|
|
|
|
end
|
|
|
|
|
|
|
|
def restart_networking
|
|
|
|
sudo 'service network restart'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fqdn
|
|
|
|
new_hostname
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_hostname
|
|
|
|
new_hostname.split('.').first
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(cmd, opts=nil, &block)
|
|
|
|
machine.communicate.execute(cmd, opts, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sudo(cmd, opts=nil, &block)
|
|
|
|
machine.communicate.sudo(cmd, opts, &block)
|
2013-04-04 18:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|