Class: Roast::HostsFile
- Inherits:
-
Object
- Object
- Roast::HostsFile
- Defined in:
- lib/roast/hosts_file.rb
Constant Summary collapse
- GROUP_PATTERN =
/^## \[([\w\s-]+)\]$/
- HOST_PATTERN =
/^#?\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([a-z0-9\.\-]+)\s*(?:#\s*(\S+))?$/
- DISABLED_PATTERN =
/^# \d+/
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#static_lines ⇒ Object
readonly
Returns the value of attribute static_lines.
Instance Method Summary collapse
- #[](group) ⇒ Object
- #add(group, source, hostname) ⇒ Object
- #delete(entry) ⇒ Object
- #delete_group(group) ⇒ Object
- #delete_host(entry) ⇒ Object
- #disable(entry) ⇒ Object
- #disable_group(group) ⇒ Object
- #enable(entry) ⇒ Object
- #enable_group(group) ⇒ Object
- #find_host(entry) ⇒ Object
- #groups ⇒ Object
- #hosts ⇒ Object
-
#initialize(path = '/etc/hosts') ⇒ HostsFile
constructor
A new instance of HostsFile.
- #list ⇒ Object
- #read ⇒ Object
- #write(output_path = nil) ⇒ Object
Constructor Details
#initialize(path = '/etc/hosts') ⇒ HostsFile
Returns a new instance of HostsFile.
10 11 12 13 14 |
# File 'lib/roast/hosts_file.rb', line 10 def initialize(path = '/etc/hosts') @path = path @static_lines = [] @groups = {} end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/roast/hosts_file.rb', line 7 def path @path end |
#static_lines ⇒ Object (readonly)
Returns the value of attribute static_lines.
8 9 10 |
# File 'lib/roast/hosts_file.rb', line 8 def static_lines @static_lines end |
Instance Method Details
#[](group) ⇒ Object
24 25 26 |
# File 'lib/roast/hosts_file.rb', line 24 def [](group) @groups[group] end |
#add(group, source, hostname) ⇒ Object
82 83 84 85 86 |
# File 'lib/roast/hosts_file.rb', line 82 def add(group, source, hostname) group ||= 'base' @groups[group] ||= Group.new(group) @groups[group] << Host.new(source, hostname) end |
#delete(entry) ⇒ Object
102 103 104 105 106 |
# File 'lib/roast/hosts_file.rb', line 102 def delete(entry) results = delete_host(entry) results end |
#delete_group(group) ⇒ Object
124 125 126 127 128 129 130 |
# File 'lib/roast/hosts_file.rb', line 124 def delete_group(group) return false unless @groups.has_key?(group) @groups.delete(group) true end |
#delete_host(entry) ⇒ Object
78 79 80 |
# File 'lib/roast/hosts_file.rb', line 78 def delete_host(entry) groups.map { |g| g.delete_host(entry) }.flatten end |
#disable(entry) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/roast/hosts_file.rb', line 95 def disable(entry) results = find_host(entry) results.each { |h| h.disable! } results end |
#disable_group(group) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/roast/hosts_file.rb', line 116 def disable_group(group) return false unless @groups.has_key?(group) @groups[group].disable! true end |
#enable(entry) ⇒ Object
88 89 90 91 92 93 |
# File 'lib/roast/hosts_file.rb', line 88 def enable(entry) results = find_host(entry) results.each { |h| h.enable! } results end |
#enable_group(group) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/roast/hosts_file.rb', line 108 def enable_group(group) return false unless @groups.has_key?(group) @groups[group].enable! true end |
#find_host(entry) ⇒ Object
74 75 76 |
# File 'lib/roast/hosts_file.rb', line 74 def find_host(entry) groups.map { |g| g.find_host(entry) }.flatten end |
#groups ⇒ Object
16 17 18 |
# File 'lib/roast/hosts_file.rb', line 16 def groups @groups.values end |
#hosts ⇒ Object
20 21 22 |
# File 'lib/roast/hosts_file.rb', line 20 def hosts groups.map { |g| g.hosts }.flatten end |
#list ⇒ Object
132 133 134 |
# File 'lib/roast/hosts_file.rb', line 132 def list [path, groups] end |
#read ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/roast/hosts_file.rb', line 28 def read in_group = false group = nil File.open(path, 'r') do |file| file.each_line do |line| if group_match = line.match(GROUP_PATTERN) in_group = true group = Group.new(group_match[1]) @groups[group.name] ||= group elsif group && host_match = line.match(HOST_PATTERN) host = Host.new(host_match[1], host_match[2]) host.alias = host_match[3] host.disable! if line =~ DISABLED_PATTERN group << host else static_lines << line end end end self rescue Errno::EACCES puts "Unable to read hosts file: `#{@path}', you might need to `sudo roast'" exit 1 end |
#write(output_path = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/roast/hosts_file.rb', line 55 def write(output_path = nil) output_path = output_path || path temp_file = Tempfile.new('hosts') temp_file << static_lines.join.sub(/\n{3,}\z/, "\n\n") temp_file << groups.map { |g| g.to_hosts_file.chomp }.join("\n\n") File.chmod(0644, temp_file.path) begin FileUtils.cp(path, path + '.roast.bak') if output_path.eql?(path) FileUtils.mv(temp_file.path, output_path, :force => true) rescue Errno::EACCES puts "Unable to write to hosts file: `#{@path}', you might need to `sudo roast'" exit 1 end ensure temp_file.close temp_file.unlink end |