Class: HostManager

Inherits:
Object
  • Object
show all
Defined in:
lib/maphosts/host_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(hostsfile_path, hosts, section_name, ip, verbose = false) ⇒ HostManager

Returns a new instance of HostManager.



6
7
8
9
10
11
12
13
# File 'lib/maphosts/host_manager.rb', line 6

def initialize(hostsfile_path, hosts, section_name, ip, verbose = false)
  @hostsfile = Hosts::File.read(hostsfile_path)
  @hosts = hosts
  @section = init_section("maphosts - #{section_name}")
  @ip = ip
  @dirty = false
  @verbose = verbose
end

Instance Method Details

#patchObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/maphosts/host_manager.rb', line 15

def patch
  migrate_hosts(@section, @hostsfile, @hosts)

  @hosts.each do |hostname|
    host = @section.elements.find { |h| h.respond_to?(:name) && h.name == hostname }

    if !host
      log '+', hostname, "will be added"
      @section.elements << Hosts::Entry.new(@ip, hostname)
      @dirty = true
    else
      if host.address != @ip
        log '*', hostname, "is pointing to #{host.address} instead of #{@ip}"
        host.address = @ip
        @dirty = true
      else
        log '*', hostname, "is already set up correctly"
      end
    end
  end

  if !@dirty
    nil
  else
    @hostsfile.to_s.strip!
  end
end