module Pygmy::Resolv

Public Class Methods

clean() click to toggle source
# File lib/pygmy/resolv.rb, line 65
def self.clean
  if Linux.osx?
    return ResolvOsx.clean?
  end
  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/pygmy/resolv.rb, line 7
def self.common_resolv_file
  '/etc/resolv.conf'
end
configure() click to toggle source
# File lib/pygmy/resolv.rb, line 46
def self.configure
  if Linux.osx?
    return ResolvOsx.create_resolver?
  end
  # 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) click to toggle source
# File lib/pygmy/resolv.rb, line 95
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/pygmy/resolv.rb, line 17
def self.file_comment
  '# added by amazee.io pygmy'
end
file_nameserver_line() click to toggle source
# File lib/pygmy/resolv.rb, line 25
def self.file_nameserver_line
  "nameserver #{self.nameserver}"
end
has_our_nameserver?() click to toggle source
# File lib/pygmy/resolv.rb, line 88
def self.has_our_nameserver?
  if Linux.osx?
    return ResolvOsx.resolver_file_exists?
  end
  self.contents_has_our_nameserver?(self.resolv_file_contents)
end
nameserver() click to toggle source
# File lib/pygmy/resolv.rb, line 21
def self.nameserver
  "127.0.0.1"
end
nameserver_contents() click to toggle source
# File lib/pygmy/resolv.rb, line 29
def self.nameserver_contents
  "#{self.file_nameserver_line}  #{self.file_comment}"
end
resolv_file() click to toggle source
# File lib/pygmy/resolv.rb, line 33
def self.resolv_file
  if Linux.ubuntu?
    return self.ubuntu_resolv_file
  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() click to toggle source
# File lib/pygmy/resolv.rb, line 84
def self.resolv_file_contents
  File.read(self.resolv_file)
end
ubuntu_resolv_file() click to toggle source
# File lib/pygmy/resolv.rb, line 11
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/pygmy/resolv.rb, line 78
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