From e7738efb10f245161acbb33ea770eb6f96164034 Mon Sep 17 00:00:00 2001 From: Nick Carboni Date: Tue, 1 Dec 2015 09:23:08 -0500 Subject: [PATCH] Added a method to set the canonical hostname in `/etc/hosts` Fixes #143 https://bugzilla.redhat.com/show_bug.cgi?id=1286830 --- lib/linux_admin/hosts.rb | 41 +++++++++++++++++++++++++++------------- spec/hosts_spec.rb | 21 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/linux_admin/hosts.rb b/lib/linux_admin/hosts.rb index 950a082..c7cca0c 100644 --- a/lib/linux_admin/hosts.rb +++ b/lib/linux_admin/hosts.rb @@ -22,20 +22,14 @@ def save File.write(@filename, @raw_lines.join("\n")) end - def update_entry(address, hostname, comment = nil) - # Delete entries for this hostname first - @parsed_file.each {|i| i[:hosts].to_a.delete(hostname)} + def add_alias(address, hostname, comment = nil) + add_name(address, hostname, false, comment) + end - # Add entry - line_number = @parsed_file.find_path(address).first + alias_method :update_entry, :add_alias - if line_number.blank? - @parsed_file.push({:address => address, :hosts => [hostname], :comment => comment}) - else - new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname) - @parsed_file.store_path(line_number, :hosts, new_hosts) - @parsed_file.store_path(line_number, :comment, comment) if comment - end + def set_canonical_hostname(address, hostname, comment = nil) + add_name(address, hostname, true, comment) end def hostname=(name) @@ -52,7 +46,28 @@ def hostname result.success? ? result.output.strip : nil end - private + private + + def add_name(address, hostname, fqdn, comment) + # Delete entries for this hostname first + @parsed_file.each { |i| i[:hosts].to_a.delete(hostname) } + + # Add entry + line_number = @parsed_file.find_path(address).first + + if line_number.blank? + @parsed_file.push(:address => address, :hosts => [hostname], :comment => comment) + else + if fqdn + new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.unshift(hostname) + else + new_hosts = @parsed_file.fetch_path(line_number, :hosts).to_a.push(hostname) + end + @parsed_file.store_path(line_number, :hosts, new_hosts) + @parsed_file.store_path(line_number, :comment, comment) if comment + end + end + def parse_file @parsed_file = [] @raw_lines.each { |line| @parsed_file.push(parse_line(line.strip)) } diff --git a/spec/hosts_spec.rb b/spec/hosts_spec.rb index 880a7c3..2591eb3 100644 --- a/spec/hosts_spec.rb +++ b/spec/hosts_spec.rb @@ -32,6 +32,27 @@ end end + describe "#set_canonical_hostname" do + it "removes an existing entry and creates a new one" do + expected_hash = [{:blank => true}, + {:comment => "Some Comment"}, + {:address => "127.0.0.1", :hosts => ["localhost", "localhost.localdomain"], :comment => "with a comment"}, + {:address => "127.0.1.1", :hosts => []}, + {:address => "1.2.3.4", :hosts => ["my.domain.local"], :comment => nil}] + @instance.set_canonical_hostname("1.2.3.4", "my.domain.local") + expect(@instance.parsed_file).to eq(expected_hash) + end + + it "adds the hostname to the start of the hosts list" do + expected_hash = [{:blank => true}, + {:comment => "Some Comment"}, + {:address => "127.0.0.1", :hosts => ["examplehost.example.com", "localhost", "localhost.localdomain"], :comment => "with a comment"}, + {:address => "127.0.1.1", :hosts => ["my.domain.local"]}] + @instance.set_canonical_hostname("127.0.0.1", "examplehost.example.com") + expect(@instance.parsed_file).to eq(expected_hash) + end + end + describe "#save" do before do allow(File).to receive(:write)