]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Evaluating /etc/resolv.conf for default nameservers
authorFrank Brehm <frank@brehm-online.com>
Wed, 31 Aug 2022 10:57:58 +0000 (12:57 +0200)
committerFrank Brehm <frank@brehm-online.com>
Wed, 31 Aug 2022 10:57:58 +0000 (12:57 +0200)
lib/cr_vmware_tpl/config.py

index 3e016c6748f744754f37383bd2c99d2c1babb3fb..a294297058c640a9828d3ffb348b4cefbd916c5f 100644 (file)
@@ -13,6 +13,8 @@ import logging
 import re
 import copy
 import crypt
+import os
+import ipaddress
 
 # Third party modules
 from pathlib import Path
@@ -31,7 +33,7 @@ from . import DEFAULT_CONFIG_DIR, DEFAULT_DISTRO_ARCH, MAX_PORT_NUMBER
 
 from .xlate import XLATOR
 
-__version__ = '2.1.2'
+__version__ = '2.2.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -750,15 +752,18 @@ class CrTplConfiguration(BaseMultiConfig):
     default_cobbler_rootdir = Path('/var/lib/cobbler')
     default_cobbler_profile_repos = ['pp-centos8-baseos']
     default_cobbler_nameservers = [
-        '93.188.109.11',
+        '93.188.104.82',
         '93.188.109.12',
-        '93.188.109.13',
+        '217.66.52.10',
     ]
     default_cobbler_dns_search = [
         'pixelpark.net',
         'pixelpark.com',
         'pixelpark.de',
     ]
+    resolv_conf = Path('/etc/resolv.conf')
+    evaluated_resolv_conf = False
+
     default_cobbler_ws_base_url = 'http://cobbler.pixelpark.com'
     default_cobbler_ws_docroot = Path('/var/www/html')
     default_cobbler_ws_rel_filesdir = Path('custom/vmware-template-files')
@@ -794,6 +799,7 @@ class CrTplConfiguration(BaseMultiConfig):
     default_bind_dn = 'uid=readonly,ou=People,o=isp'
 
     re_ldap_section_w_name = re.compile(r'^\s*ldap\s*:\s*(\S+)')
+    re_resolv_ns_entry = re.compile(r'^\s*nameserver\s+(\S+)')
 
     # -------------------------------------------------------------------------
     def __init__(
@@ -802,6 +808,8 @@ class CrTplConfiguration(BaseMultiConfig):
             additional_config_file=None, additional_cfgdirs=None, encoding=DEFAULT_ENCODING,
             ensure_privacy=False, use_chardet=True, initialized=False):
 
+        self.eval_resolv_conf()
+
         add_stems = []
         if additional_stems:
             if is_sequence(additional_stems):
@@ -1017,6 +1025,7 @@ class CrTplConfiguration(BaseMultiConfig):
         res['data_size_mb'] = self.data_size_mb
         res['data_size_kb'] = self.data_size_kb
         res['data_size'] = self.data_size
+        res['default_cobbler_nameservers'] = self.default_cobbler_nameservers
         res['ram_gb'] = self.ram_gb
         res['system_ks'] = self.system_ks
         res['snippets_dir'] = self.snippets_dir
@@ -1063,6 +1072,57 @@ class CrTplConfiguration(BaseMultiConfig):
         if not self.template_name_given:
             self.template_name = self.current_distro.shortname + '-template'
 
+    # -------------------------------------------------------------------------
+    @classmethod
+    def eval_resolv_conf(cls):
+
+        if cls.evaluated_resolv_conf:
+            return True
+
+        if not cls.resolv_conf.exists():
+            LOG.error(_("File {!r} not found on current host.").format(str(cls.resolv_conf)))
+            cls.evaluated_resolv_conf = True
+            return False
+
+        if not cls.resolv_conf.is_file():
+            LOG.error(_("Path {!r} is not a regular file.").format(str(cls.resolv_conf)))
+            cls.evaluated_resolv_conf = True
+            return False
+
+        if not os.access(cls.resolv_conf, os.R_OK):
+            LOG.error(_("File {!r} is not readable.").format(str(cls.resolv_conf)))
+            cls.evaluated_resolv_conf = True
+            return False
+
+        LOG.info(_("Evaluating {!r} for nameservers.").format(str(ls.resolv_conf)))
+
+        nameservers = []
+        file_content = cls.resolv_conf.read_text()
+        lines = file_content.splitlines()
+
+        for line in lines:
+            match = cls.re_resolv_ns_entry.match(line)
+            if match:
+                try:
+                    ns_address = ipaddress.ip_address(match.group(1))
+                    nameservers.append(str(ns_address))
+                except ValueError as e:
+                    msg = _("Found invalid IP address {addr!r} as a nameserver in {file!r}:")
+                    msg = msg.format(addr=match.group(1), file=str(cls.resolv_conf))
+                    msg += ' ' + str(e)
+                    LOG.warn(msg)
+
+        cls.evaluated_resolv_conf = True
+        msg = _("Found nameservers in {!r}:").format(str(cls.resolv_conf))
+        msg += ' {}'.format(pp(nameservers))
+        LOG.debug(msg)
+
+        if len(nameservers):
+            cls.default_cobbler_nameservers = nameservers
+            return True
+
+        return False
+
     # -------------------------------------------------------------------------
     def verify_cobbler_distros(self):