From 6556b5c3095f0582d0b75b5136251ce38c68f7d2 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 12 Jan 2018 15:26:43 +0100 Subject: [PATCH] Retrieving list of all zones --- pp_lib/pdns_migrate_ns.py | 74 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/pp_lib/pdns_migrate_ns.py b/pp_lib/pdns_migrate_ns.py index 8c16939..dc473e9 100644 --- a/pp_lib/pdns_migrate_ns.py +++ b/pp_lib/pdns_migrate_ns.py @@ -18,12 +18,13 @@ from functools import cmp_to_key # Own modules from .common import pp, compare_fqdn, to_str +from .common import RE_DOT_AT_END from .pdns_app import PpPDNSAppError, PpPDNSApplication, PDNSApiNotFoundError, PDNSApiValidationError from .pdns_zone import PdnsApiZone from .pdns_record import compare_rrsets -__version__ = '0.1.0' +__version__ = '0.2.0' LOG = logging.getLogger(__name__) @@ -38,10 +39,25 @@ class PDNSMigrateNsApp(PpPDNSApplication): of all zones of PowerDNS from the old nameservers to the new one. """ + new_public_nameservers = [ + 'ns1.pp-dns.com.', + 'ns2.pp-dns.com.', + 'ns3.pp-dns.com.', + 'ns4.pp-dns.com.', + ] + + new_local_nameservers = [ + 'ns1-local.pixelpark.com', + 'ns2-local.pixelpark.com', + 'ns3-local.pixelpark.com', + ] + # ------------------------------------------------------------------------- def __init__(self, appname=None, version=__version__): self.zones = [] + self.zone_names = [] + self.oneshot = False description = textwrap.dedent('''\ Substituting NS records in all zones by the new ones. @@ -55,12 +71,66 @@ class PDNSMigrateNsApp(PpPDNSApplication): self.initialized = True + # ------------------------------------------------------------------------- + def init_arg_parser(self): + """ + Method to initiate the argument parser. + + This method should be explicitely called by all init_arg_parser() + methods in descendant classes. + """ + + super(PDNSMigrateNsApp, self).init_arg_parser() + + self.arg_parser.add_argument( + "-1", '--oneshot', action="store_true", dest="oneshot", + help="Stop execution after first successful migration." + ) + + self.arg_parser.add_argument( + 'zones', metavar='ZONE', nargs='*', + help=( + "All zones, for which the migration should be executed. " + "If not given, the migration will be executed for all zones."), + ) + + # ------------------------------------------------------------------------- + def perform_arg_parser(self): + """ + Public available method to execute some actions after parsing + the command line parameters. + """ + + super(PDNSMigrateNsApp, self).perform_arg_parser() + + if self.args.oneshot: + self.oneshot = True + + for zone in self.args.zones: + zone_idna = zone + if 'xn--' not in zone: + zone_idna = to_str(zone.encode('idna')) + zone_idna = RE_DOT_AT_END.sub('.', zone_idna).lower() + self.zone_names.append(zone_idna) + # ------------------------------------------------------------------------- def _run(self): LOG.info("Substituting NS records in all zones by the new ones.") - zone_list = self.get_api_zones() + self.zones = self.get_api_zones() + + if not self.zone_names: + for zone in sorted(self.zones, key=lambda x: cmp_to_key(compare_fqdn)(x.name_unicode)): + self.zone_names.append(zone.name) + + for zone_name in self.zone_names: + self.migrate_zone(zone_name) + + # ------------------------------------------------------------------------- + def migrate_zone(self, zone_name): + + LOG.info("Migrating zone {!r} ...".format(zone_name)) # ============================================================================= -- 2.39.5