Module: Pygmy::Resolv
- Defined in:
- lib/pygmy/resolv.rb
Class Method Summary collapse
- .clean ⇒ Object
- .common_resolv_file ⇒ Object
- .configure ⇒ Object
- .contents_has_our_nameserver?(contents) ⇒ Boolean
- .file_comment ⇒ Object
- .file_nameserver_line ⇒ Object
- .has_our_nameserver? ⇒ Boolean
- .nameserver ⇒ Object
- .nameserver_contents ⇒ Object
- .resolv_file ⇒ Object
- .resolv_file_contents ⇒ Object
- .ubuntu_resolv_file ⇒ Object
- .write_to_file(contents) ⇒ Object
Class Method Details
.clean ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/pygmy/resolv.rb', line 59 def self.clean prev_conts = self.resolv_file_contents if self.contents_has_our_nameserver?(prev_conts) prev_conts.gsub!(/#{Regexp.escape(self.nameserver_contents + "\n")}/, '') prev_conts.gsub!(/\s+$/, '') self.write_to_file(prev_conts) end !self.has_our_nameserver? end |
.common_resolv_file ⇒ Object
5 6 7 |
# File 'lib/pygmy/resolv.rb', line 5 def self.common_resolv_file '/etc/resolv.conf' end |
.configure ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pygmy/resolv.rb', line 43 def self.configure # we want to be the first nameserver in the list for performance reasons # we only want to add the nameserver if it isn't already there prev_conts = self.resolv_file_contents unless self.contents_has_our_nameserver?(prev_conts) if prev_conts =~ /nameserver/ prev_conts.sub!(/nameserver/, "#{self.nameserver_contents}\nnameserver") else prev_conts = "#{prev_conts}\n#{self.nameserver_contents}" end prev_conts.gsub!(/\s+$/, '') self.write_to_file(prev_conts) end self.has_our_nameserver? end |
.contents_has_our_nameserver?(contents) ⇒ Boolean
83 84 85 |
# File 'lib/pygmy/resolv.rb', line 83 def self.contents_has_our_nameserver?(contents) !!((contents =~ /#{self.file_comment}/) || (contents =~ /#{self.file_nameserver_line}/)) end |
.file_comment ⇒ Object
15 16 17 |
# File 'lib/pygmy/resolv.rb', line 15 def self.file_comment '# added by pygmy' end |
.file_nameserver_line ⇒ Object
23 24 25 |
# File 'lib/pygmy/resolv.rb', line 23 def self.file_nameserver_line "nameserver #{self.nameserver}" end |
.has_our_nameserver? ⇒ Boolean
79 80 81 |
# File 'lib/pygmy/resolv.rb', line 79 def self.has_our_nameserver? self.contents_has_our_nameserver?(self.resolv_file_contents) end |
.nameserver ⇒ Object
19 20 21 |
# File 'lib/pygmy/resolv.rb', line 19 def self.nameserver "127.0.0.1" end |
.nameserver_contents ⇒ Object
27 28 29 |
# File 'lib/pygmy/resolv.rb', line 27 def self.nameserver_contents "#{self.file_nameserver_line} #{self.file_comment}" end |
.resolv_file ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pygmy/resolv.rb', line 31 def self.resolv_file if Linux.ubuntu? return self.ubuntu_resolv_file if Linux.ubuntu? elsif Linux.fedora? || Linux.arch? || File.exist?(self.common_resolv_file) return self.common_resolv_file else raise RuntimeError.new( "Unable to determine location of resolv file" ) end end |
.resolv_file_contents ⇒ Object
75 76 77 |
# File 'lib/pygmy/resolv.rb', line 75 def self.resolv_file_contents File.read(self.resolv_file) end |
.ubuntu_resolv_file ⇒ Object
9 10 11 12 13 |
# File 'lib/pygmy/resolv.rb', line 9 def self.ubuntu_resolv_file #'/etc/resolvconf/resolv.conf.d/base' # For now, use the common_resolv_file self.common_resolv_file end |
.write_to_file(contents) ⇒ Object
69 70 71 72 73 |
# File 'lib/pygmy/resolv.rb', line 69 def self.write_to_file(contents) # have to use this hack cuz we don't run as root :-( puts "Requesting sudo to write to #{self.resolv_file}".green Bash.run_command("echo -e '#{contents}' | sudo tee #{Shellwords.escape(self.resolv_file)} >/dev/null") end |