]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Adding option to keep a backup of old configuration file
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 10 Nov 2017 10:21:50 +0000 (11:21 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 10 Nov 2017 10:21:50 +0000 (11:21 +0100)
pp_lib/deploy_zones_from_pdns.py

index 7ca8579d7fa744fdb8ef895a8100f72f69bece83..560173544eb4eb2e8b4be9e909de8a30901dcc0c 100644 (file)
@@ -34,7 +34,7 @@ import requests
 from six.moves.urllib.parse import urlunsplit
 
 # Own modules
-from .common import pp, compare_fqdn, to_str
+from .common import pp, compare_fqdn, to_str, to_bool
 from .common import RE_DOT_AT_END
 
 from .pdns_app import PpPDNSAppError, PpPDNSApplication
@@ -44,7 +44,7 @@ from .pdns_record import compare_rrsets
 
 from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile
 
-__version__ = '0.4.1'
+__version__ = '0.4.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -120,6 +120,7 @@ class PpDeployZonesApp(PpPDNSApplication):
         self.tempdir = None
         self.temp_zones_cfg_file = None
         self.keep_tempdir = False
+        self.keep_backup = False
 
         self.backup_suffix = (
             '.' + datetime.datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S') + '.bak')
@@ -171,6 +172,11 @@ class PpDeployZonesApp(PpPDNSApplication):
 
         super(PpDeployZonesApp, self).init_arg_parser()
 
+        self.arg_parser.add_argument(
+            '-B', '--backup', dest="keep_backup", action='store_true',
+            help=("Keep a backup file for each changed configuration file."),
+        )
+
         self.arg_parser.add_argument(
             '-K', '--keep-tempdir', dest='keep_tempdir', action='store_true',
             help=(
@@ -190,6 +196,9 @@ class PpDeployZonesApp(PpPDNSApplication):
         if self.args.keep_tempdir:
             self.keep_tempdir = True
 
+        if self.args.keep_backup:
+            self.keep_backup = True
+
     # -------------------------------------------------------------------------
     def perform_config(self):
 
@@ -204,6 +213,10 @@ class PpDeployZonesApp(PpPDNSApplication):
 
             if section_name.lower() == 'app':
                 self._check_path_config(section, section_name, 'pidfile', 'pidfile_name', True)
+                if 'keep-backup' in section:
+                    self.keep_backup = to_bool(section['keep-backup'])
+                if 'keep_backup' in section:
+                    self.keep_backup = to_bool(section['keep_backup'])
 
             if section_name.lower() == 'named':
                 self.set_named_options(section, section_name)
@@ -355,9 +368,12 @@ class PpDeployZonesApp(PpPDNSApplication):
             backup_file = self.moved_files[tgt_file]
             LOG.debug("Searching for {!r}.".format(backup_file))
             if os.path.exists(backup_file):
-                LOG.info("Removing {!r} ...".format(backup_file))
-                if not self.simulate:
-                    os.remove(backup_file)
+                if self.keep_backup:
+                    LOG.info("Keep existing backup file {!r}.".format(backup_file))
+                else:
+                    LOG.info("Removing {!r} ...".format(backup_file))
+                    if not self.simulate:
+                        os.remove(backup_file)
 
         # -----------------------
         def emit_rm_err(function, path, excinfo):