Skip to content

Commit

Permalink
[debops.resolvconf] Clean up fact script
Browse files Browse the repository at this point in the history
  • Loading branch information
drybjed committed Sep 30, 2019
1 parent 9afbc2a commit c3ddf6f
Showing 1 changed file with 16 additions and 20 deletions.
Expand Up @@ -14,9 +14,9 @@ def cmd_exists(cmd):
)


def read_resolv_file(file):
def read_resolv_file(resolv_file):
try:
with open(file, 'r') as f:
with open(resolv_file, 'r') as f:
nameservers = []
domain = ''
search = []
Expand Down Expand Up @@ -56,31 +56,27 @@ if search:

# Get the upstream resolver configuration
upstream_nameservers = []
resolvconf_files = []
resolvconf_path = '/run/resolvconf/interface'
systemdresolved_path = '/run/systemd/resolve/resolv.conf'
if os.path.isdir(resolvconf_path):
resolvconf_files = ([f for f in os.listdir(resolvconf_path)
resolvconf_files = ([os.path.join(resolvconf_path, f)
for f in os.listdir(resolvconf_path)
if os.path.isfile(os.path.join(resolvconf_path, f))])

for element in resolvconf_files:
try:
with open(os.path.join(resolvconf_path, element), 'r') as f:
for line in f:
if line.startswith('nameserver'):
server = line.strip().split()[1]
if not server.startswith('127.'):
upstream_nameservers.append(server)
if os.path.isfile(systemdresolved_path):
resolvconf_files.append(systemdresolved_path)

except Exception:
pass
for element in resolvconf_files:
try:
nameservers, domain, search = read_resolv_file(element)

# Get the upstream resolvers from systemd-resolved
systemdresolved_path = '/run/systemd/resolve/resolv.conf'
if os.path.isfile(systemdresolved_path):
nameservers, domain, search = read_resolv_file(systemdresolved_path)
for server in nameservers:
if not server.startswith('127.'):
upstream_nameservers.append(server)

for server in nameservers:
if not server.startswith('127.'):
upstream_nameservers.append(server)
except Exception:
pass

# If nameservers contains a localhost address, and we've found some
# upstream nameservers, then output the upstream nameservers.
Expand Down

0 comments on commit c3ddf6f

Please sign in to comment.