Class: VagrantPlugins::GuestCumulus::Cap::ChangeHostName

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-cumulus/cap/change_host_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, new_hostname) ⇒ ChangeHostName



11
12
13
14
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 11

def initialize(machine, new_hostname)
  @machine = machine
  @new_hostname = new_hostname
end

Instance Attribute Details

#machineObject (readonly)

Returns the value of attribute machine.



9
10
11
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 9

def machine
  @machine
end

#new_hostnameObject (readonly)

Returns the value of attribute new_hostname.



9
10
11
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 9

def new_hostname
  @new_hostname
end

Class Method Details

.change_host_name(machine, name) ⇒ Object



5
6
7
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 5

def self.change_host_name(machine, name)
  new(machine, name).change!
end

Instance Method Details

#change!Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 16

def change!
  return unless should_change?

  update_etc_hostname
  update_etc_hosts
  refresh_hostname_service
  update_mailname
  renew_dhcp
  restart_lldp
end

#current_hostnameObject



31
32
33
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 31

def current_hostname
  @current_hostname ||= get_current_hostname
end

#fqdnObject



82
83
84
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 82

def fqdn
  new_hostname
end

#get_current_hostnameObject



35
36
37
38
39
40
41
42
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 35

def get_current_hostname
  hostname = ""
  sudo "hostname -f" do |type, data|
    hostname = data.chomp if type == :stdout && hostname.empty?
  end

  hostname
end

#refresh_hostname_serviceObject



66
67
68
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 66

def refresh_hostname_service
  sudo("hostname -F /etc/hostname")
end

#renew_dhcpObject



74
75
76
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 74

def renew_dhcp
  sudo("ifdown -a; ifup -a; ifup eth0")
end

#restart_lldpObject



78
79
80
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 78

def restart_lldp
  sudo("service lldpd restart")
end

#short_hostnameObject



86
87
88
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 86

def short_hostname
  new_hostname.split('.').first
end

#should_change?Boolean



27
28
29
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 27

def should_change?
  new_hostname != current_hostname
end

#sudo(cmd, &block) ⇒ Object



90
91
92
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 90

def sudo(cmd, &block)
  machine.communicate.sudo(cmd, &block)
end

#test(cmd) ⇒ Object



94
95
96
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 94

def test(cmd)
  machine.communicate.test(cmd)
end

#update_etc_hostnameObject



44
45
46
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 44

def update_etc_hostname
  sudo("echo '#{short_hostname}' > /etc/hostname")
end

#update_etc_hostsObject

/etc/hosts should resemble: 127.0.0.1 localhost 127.0.1.1 host.fqdn.com host.fqdn host



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 51

def update_etc_hosts
  if test("grep '#{current_hostname}' /etc/hosts")
    # Current hostname entry is in /etc/hosts
    ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}'
    search     = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$"
    replace    = "\\1 #{fqdn} #{short_hostname}"
    expression = ['s', search, replace, 'g'].join('@')

    sudo("sed -ri '#{expression}' /etc/hosts")
  else
    # Current hostname entry isn't in /etc/hosts, just append it
    sudo("echo '127.0.1.1 #{fqdn} #{short_hostname}' >>/etc/hosts")
  end
end

#update_mailnameObject



70
71
72
# File 'lib/vagrant-cumulus/cap/change_host_name.rb', line 70

def update_mailname
  sudo("hostname --fqdn > /etc/mailname")
end