]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Starting with method get_named_keys() of class PpDeployZonesApp
authorFrank Brehm <frank@brehm-online.com>
Tue, 7 Dec 2021 14:25:59 +0000 (15:25 +0100)
committerFrank Brehm <frank@brehm-online.com>
Tue, 7 Dec 2021 14:25:59 +0000 (15:25 +0100)
lib/pp_admintools/deploy_zones_from_pdns.py

index b98519475a1e169e0f04b8d749d5cd344145540b..8d6afa9e2a88ee8e3b2f9344d7bdf5b0bc4af9b3 100644 (file)
@@ -130,6 +130,8 @@ class PpDeployZonesApp(PpPDNSApplication):
         self.cmd_start = self.default_cmd_start
         self.cmd_restart = self.default_cmd_restart
 
+        self.named_keys = {}
+
         self.zone_tsig_key = None
 
         self.files2replace = {}
@@ -354,6 +356,11 @@ class PpDeployZonesApp(PpPDNSApplication):
                 "please use 'local' or 'public'"))
             self.exit(1)
 
+        cmd_namedcheckconf = self.get_command('named-checkconf')
+        if not cmd_namedcheckconf:
+            self.exit(1)
+        self.cmd_checkconf = cmd_namedcheckconf
+
     # -------------------------------------------------------------------------
     def _run(self):
 
@@ -369,6 +376,8 @@ class PpDeployZonesApp(PpPDNSApplication):
         LOG.info(_("Starting: {}").format(
             datetime.datetime.now(local_tz).strftime('%Y-%m-%d %H:%M:%S %Z')))
 
+        self.get_named_keys()
+
         try:
             self.pidfile.create()
         except PidFileError as e:
@@ -447,6 +456,39 @@ class PpDeployZonesApp(PpPDNSApplication):
         if self.verbose > 1:
             LOG.debug(_("Temporary zones conf: {!r}").format(self.temp_zones_cfg_file))
 
+    # -------------------------------------------------------------------------
+    def get_named_keys(self):
+
+        LOG.info(_("Trying to get all keys from named.conf ..."))
+
+        LOG.info(_("Checking syntax correctness of named.conf ..."))
+        cmd = shlex.split(self.cmd_checkconf)
+        cmd.append('-p')
+
+        cmd_str = ' '.join(map(lambda x: pipes.quote(x), cmd))
+        LOG.debug(_("Executing: {}").format(cmd_str))
+
+        result = self.run(
+            cmd, stdout=PIPE, stderr=PIPE, timeout=10, check=True, may_simulate=False)
+        if self.verbose > 1:
+            LOG.debug(_("Result:") + '\n' + str(result))
+
+        config = result.stdout
+
+        key_pattern = r'^\s*key\s+("[^"]+"|\S+)\s+\{([^\}]+\}\s*;'
+        re_quotes = re.compile(r'^\s*"([^"]+)"\s*$')
+        re_key = re.compile(key_pattern, re.IGNORECASE | re.MULTILINE | re.DOTALL)
+
+        for match in re_key.finditer(config):
+            match_quotes = re_quotes.match(match[1])
+            if match_quotes:
+                key_name = match_quotes[1]
+            else:
+                key_name = match[1]
+            key_data = match[2].strip()
+            if self.verbose > 1:
+                LOG.debug("Found key {!r}:".format(key_name) + '\n' + key_data)
+
     # -------------------------------------------------------------------------
     def generate_slave_cfg_file(self):