From 59b2b308035a6d6f1f0bfb538a55e135d09af7cc Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 8 Aug 2017 14:25:18 +0200 Subject: [PATCH] Generating named config file for slave zones --- pp_lib/config_named_app.py | 72 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/pp_lib/config_named_app.py b/pp_lib/config_named_app.py index dd78cd5..47b6760 100644 --- a/pp_lib/config_named_app.py +++ b/pp_lib/config_named_app.py @@ -39,7 +39,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile -__version__ = '0.5.6' +__version__ = '0.6.1' LOG = logging.getLogger(__name__) @@ -815,6 +815,7 @@ class PpConfigNamedApp(PpConfigApplication): self.generate_acl_file() self.generate_named_conf() self.generate_log_cfg_file() + self.generate_slave_cfg_file() time.sleep(2) @@ -1161,7 +1162,6 @@ class PpConfigNamedApp(PpConfigApplication): content += '\n'.join(lines) + '\n' - content += '\n};\n' content += '\n// vim: ts=8 filetype=named noet noai\n' @@ -1171,6 +1171,71 @@ class PpConfigNamedApp(PpConfigApplication): if self.verbose > 2: LOG.debug("Generated {!r}:\n{}".format(self.temp_log_cfg_file, content.strip())) + # ------------------------------------------------------------------------- + def generate_slave_cfg_file(self): + + LOG.info("Generating {} ...".format(self.default_named_zones_cfg_file)) + + cur_date = datetime.datetime.now().isoformat(' ') + re_rev = re.compile(r'^rev\.', re.IGNORECASE) + re_trail_dot = re.compile(r'\.+$') + + lines = [] + lines.append('###############################################################') + lines.append('') + lines.append(' Bind9 configuration file for slave sones') + lines.append(' {}'.format(self.named_zones_cfg_file)) + lines.append('') + lines.append(' Generated at: {}'.format(cur_date)) + lines.append('') + lines.append('###############################################################') + header = textwrap.indent('\n'.join(lines), '//', lambda line: True) + '\n' + + content = header + + for zone in self.zones: + + account = str(zone['account']).lower().strip() + + zname = re_trail_dot.sub('', zone['name']) + show_name = zone['canonical_name'] + show_name = re_rev.sub('Reverse ', show_name) + show_name = re_trail_dot.sub('', show_name) + if account.startswith('intern') or account.startswith('local'): + if not self.is_internal: + LOG.debug("Ignoring zone {!r}, because it's an internal zone.".format(zname)) + continue + else: + if self.is_internal: + LOG.debug("Ignoring zone {!r}, because it's a public zone.".format(zname)) + continue + + zfile = os.path.join( + self.named_slavedir_rel, re_trail_dot.sub('', zone['canonical_name']) + '.zone') + + lines = [] + lines.append('') + lines.append('// {}'.format(show_name)) + lines.append('zone "{}" in {{'.format(zname)) + lines.append('\tmasters {') + for master in self.zone_masters: + lines.append('\t\t{};'.format(master)) + lines.append('\t};') + lines.append('\ttype slave;') + lines.append('\tfile "{}";'.format(zfile)) + lines.append('};') + + content += '\n'.join(lines) + '\n' + + content += '\n// vim: ts=8 filetype=named noet noai\n' + + with open(self.temp_zones_cfg_file, 'w', **self.open_args) as fh: + fh.write(content) + + if self.verbose > 2: + LOG.debug("Generated {!r}:\n{}".format(self.temp_zones_cfg_file, content.strip())) + + # ------------------------------------------------------------------------- def get_api_zones(self): @@ -1243,6 +1308,9 @@ class PpConfigNamedApp(PpConfigApplication): if match: prefix = self._get_ipv4_prefix(match.group(1)) if prefix: + if prefix == '127.0.0': + LOG.debug("Pure local zone {!r} will not be considered.".format(prefix)) + continue uni_name = 'rev.' + prefix if not uni_name: -- 2.39.5