module Dory::Resolv::Linux

Public Class Methods

clean() click to toggle source
# File lib/dory/resolv/linux.rb, line 64
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() click to toggle source
# File lib/dory/resolv/linux.rb, line 6
def self.common_resolv_file
  '/etc/resolv.conf'
end
configure() click to toggle source
# File lib/dory/resolv/linux.rb, line 48
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!(/^\s*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) click to toggle source
# File lib/dory/resolv/linux.rb, line 88
def self.contents_has_our_nameserver?(contents)
 !!((contents =~ /#{self.file_comment}/) && (contents =~ /#{self.file_nameserver_line}/))
end
file_comment() click to toggle source
# File lib/dory/resolv/linux.rb, line 16
def self.file_comment
  '# added by dory'
end
file_nameserver_line() click to toggle source

Note that we ignore any ports present in the config file because only port 53 is supported on linux (and there's not a way to specify it in the resolv.conf even if we wanted to, which someday hopefully we can)

# File lib/dory/resolv/linux.rb, line 28
def self.file_nameserver_line
  "nameserver #{self.nameserver}"
end
has_our_nameserver?() click to toggle source
# File lib/dory/resolv/linux.rb, line 84
def self.has_our_nameserver?
  self.contents_has_our_nameserver?(self.resolv_file_contents)
end
nameserver() click to toggle source
# File lib/dory/resolv/linux.rb, line 20
def self.nameserver
  Dory::Config.settings[:dory][:resolv][:nameserver]
end
nameserver_contents() click to toggle source
# File lib/dory/resolv/linux.rb, line 32
def self.nameserver_contents
  "#{self.file_nameserver_line}  #{self.file_comment}"
end
resolv_file() click to toggle source
# File lib/dory/resolv/linux.rb, line 36
def self.resolv_file
  if Os.ubuntu?
    return self.ubuntu_resolv_file if Os.ubuntu?
  elsif Os.fedora? || Os.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() click to toggle source
# File lib/dory/resolv/linux.rb, line 80
def self.resolv_file_contents
  File.read(self.resolv_file)
end
ubuntu_resolv_file() click to toggle source
# File lib/dory/resolv/linux.rb, line 10
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) click to toggle source
# File lib/dory/resolv/linux.rb, line 74
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 /usr/bin/tee #{Shellwords.escape(self.resolv_file)} >/dev/null")
end