Class: Vagrant::Solaris10::Cap::ChangeHostName

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

Class Method Summary collapse

Class Method Details

.change_host_name(machine, name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vagrant-solaris10/cap/change_host_name.rb', line 8

def self.change_host_name(machine, name)

  # Only do this if the hostname is not already set
  machine.communicate.tap do |comm|
    if !comm.test("hostname | grep '#{name}", sudo: true)
      ifconfig = nil
      # Get ifconfig output
      comm.execute("ifconfig -a") do |type, data|
        if type == :stdout
          ifconfig ||= ""
          ifconfig += data
        end
      end

      broadcasting_interface = ifconfig.scan(/^(\w+).*BROADCAST/)[0][0]
      broadcasting_ip = ifconfig.scan(/inet\s(\S+).*broadcast/)[0][0]

      comm.sudo("sh -c \"echo '#{name}' > /etc/nodename\"")
      comm.sudo("sh -c \"echo '#{name}' > /etc/hostname.#{broadcasting_interface}\"")
      comm.sudo("sh -c \"gsed -i -e 's/\\(#{broadcasting_ip}\\s*\\).*/\\1#{name}/g' /etc/hosts\"")

      comm.sudo("uname -S #{name}")
    end
  end

end