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.



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

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



32
33
34
35
36
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 32

def self.flush_dns_cache!
	if PlatformInfo.os_name == "macosx"
		system("dscacheutil -flushcache")
	end
end

Instance Method Details

#add_group_data(marker, data) ⇒ Object



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

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



52
53
54
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 52

def host_count
	return @host_names.size
end

#ip_countObject



48
49
50
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 48

def ip_count
	return @ips.size
end

#resolve(host_name) ⇒ Object



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

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)


64
65
66
67
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 64

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



81
82
83
84
85
# File 'lib/phusion_passenger/utils/hosts_file_parser.rb', line 81

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