Module: Dory::Resolv::Linux

Defined in:
lib/dory/resolv/linux.rb

Class Method Summary collapse

Class Method Details

.cleanObject



64
65
66
67
68
69
70
71
72
# 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_fileObject



6
7
8
# File 'lib/dory/resolv/linux.rb', line 6

def self.common_resolv_file
  '/etc/resolv.conf'
end

.configureObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# 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_commentObject



16
17
18
# File 'lib/dory/resolv/linux.rb', line 16

def self.file_comment
  '# added by dory'
end

.file_nameserver_lineObject

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)



28
29
30
# File 'lib/dory/resolv/linux.rb', line 28

def self.file_nameserver_line
  "nameserver #{self.nameserver}"
end

.has_our_nameserver?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/dory/resolv/linux.rb', line 84

def self.has_our_nameserver?
  self.contents_has_our_nameserver?(self.resolv_file_contents)
end

.nameserverObject



20
21
22
# File 'lib/dory/resolv/linux.rb', line 20

def self.nameserver
  Dory::Config.settings[:dory][:resolv][:nameserver]
end

.nameserver_contentsObject



32
33
34
# File 'lib/dory/resolv/linux.rb', line 32

def self.nameserver_contents
  "#{self.file_nameserver_line}  #{self.file_comment}"
end

.resolv_fileObject



36
37
38
39
40
41
42
43
44
45
46
# 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_contentsObject



80
81
82
# File 'lib/dory/resolv/linux.rb', line 80

def self.resolv_file_contents
  File.read(self.resolv_file)
end

.ubuntu_resolv_fileObject



10
11
12
13
14
# 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) ⇒ Object



74
75
76
77
78
# 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 '#{Bash.escape_single_quotes(contents)}' | sudo /usr/bin/tee #{Shellwords.escape(self.resolv_file)} >/dev/null")
end