Class: HostManager
- Inherits:
-
Object
- Object
- HostManager
- Defined in:
- lib/maphosts/host_manager.rb
Instance Method Summary collapse
-
#initialize(hostsfile_path, hosts, section_name, ip, verbose = false) ⇒ HostManager
constructor
A new instance of HostManager.
- #patch ⇒ Object
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
#patch ⇒ Object
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 42 43 44 45 46 47 48 49 50 |
# 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 # Strip whitespace at end of file if !@hostsfile.elements.last.nil? && @hostsfile.elements.last.kind_of?(Hosts::EmptyElement) @hostsfile.elements.delete @hostsfile.elements.last end if @hostsfile.to_s.empty? raise "The patched hostsfile is empty. This is most likely due to a bug. No worry, the file has not been written." end @hostsfile end end |