]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Retrieving list of all zones
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 14:26:43 +0000 (15:26 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 12 Jan 2018 14:26:43 +0000 (15:26 +0100)
pp_lib/pdns_migrate_ns.py

index 8c169395a6b49d3511d7c66e5b10c86886074930..dc473e9b58b9927c39176b302baab6099aabebdf 100644 (file)
@@ -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))
 
 # =============================================================================