Class: PhusionPassenger::Utils::HostsFileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/phusion_passenger/utils/hosts_file_parser.rb

Overview

A /etc/hosts parser. Also supports writing groups of data to the file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io = "/etc/hosts") ⇒ HostsFileParser

Returns a new instance of HostsFileParser.



36
37
38
39
40
41
42
43
44
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 36

def initialize(filename_or_io = "/etc/hosts")
  if filename_or_io.respond_to?(:readline)
    read_and_parse(filename_or_io)
  else
    File.open(filename_or_io, "rb") do |f|
      read_and_parse(f)
    end
  end
end

Class Method Details

.flush_dns_cache!Object



30
31
32
33
34
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 30

def self.flush_dns_cache!
  if RUBY_PLATFORM =~ /darwin/
    system("dscacheutil -flushcache")
  end
end

Instance Method Details

#add_group_data(marker, data) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 67

def add_group_data(marker, data)
  begin_index = find_line(0, "###### BEGIN #{marker} ######")
  end_index = find_line(begin_index + 1, "###### END #{marker} ######") if begin_index
  if begin_index && end_index
    @lines[begin_index + 1 .. end_index - 1] = data.split("\n")
  else
    @lines << "###### BEGIN #{marker} ######"
    @lines.concat(data.split("\n"))
    @lines << "###### END #{marker} ######"
  end
end

#host_countObject



50
51
52
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 50

def host_count
  return @host_names.size
end

#ip_countObject



46
47
48
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 46

def ip_count
  return @ips.size
end

#resolve(host_name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 54

def resolve(host_name)
  if host_name.downcase == "localhost"
    return "127.0.0.1"
  else
    return @host_names[host_name.downcase]
  end
end

#resolves_to_localhost?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 62

def resolves_to_localhost?(hostname)
  ip = resolve(hostname)
  return ip == "127.0.0.1" || ip == "::1" || ip == "0.0.0.0"
end

#write(io) ⇒ Object



79
80
81
82
83
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 79

def write(io)
  @lines.each do |line|
    io.puts(line)
  end
end