]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Replacing config files
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 10 Aug 2017 09:09:18 +0000 (11:09 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 10 Aug 2017 09:09:18 +0000 (11:09 +0200)
pp_lib/config_named_app.py

index 828cfad3f59d3b4d405df3d1bd69fc2c3a267eb7..e22b5e7f13da18aabcf865858134497e7eddabf3 100644 (file)
@@ -26,6 +26,7 @@ import datetime
 import textwrap
 import ipaddress
 import stat
+import shutil
 
 # Third party modules
 import six
@@ -40,7 +41,7 @@ from .cfg_app import PpCfgAppError, PpConfigApplication
 
 from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile
 
-__version__ = '0.6.4'
+__version__ = '0.6.5'
 LOG = logging.getLogger(__name__)
 
 
@@ -281,10 +282,14 @@ class PpConfigNamedApp(PpConfigApplication):
         self.temp_log_cfg_file = None
         self.temp_zones_cfg_file = None
 
+        self.backup_suffix = (
+            '.' + datetime.datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S') + '.bak')
+
         self.reload_necessary = False
         self.restart_necessary = False
 
         self.files2replace = {}
+        self.moved_files = {}
 
         self.acls = {
             'allow-notify': ['dnsmaster.pixelpark.com'],
@@ -828,6 +833,7 @@ class PpConfigNamedApp(PpConfigApplication):
             self.create_temp_files()
             self.compare_files()
             self.check_directories()
+            self.replace_configfiles()
 
         finally:
             if self.tempdir:
@@ -1586,6 +1592,31 @@ class PpConfigNamedApp(PpConfigApplication):
 
         return True
 
+    # -------------------------------------------------------------------------
+    def replace_configfiles(self):
+
+        if not self.files2replace:
+            LOG.debug("No replacement of any config files necessary.")
+            return
+
+        LOG.debug("Start replacing of config files ...")
+
+        for tgt_file in self.files2replace.keys():
+
+            backup_file = tgt_file + self.backup_suffix
+            self.moved_files[tgt_file] = backup_file
+
+            if os.path.exists(tgt_file):
+                LOG.info("Copying {!r} => {!r} ...".format(tgt_file, backup_file))
+                if not self.simulate:
+                    shutil.copy2(tgt_file, backup_file)
+
+        for tgt_file in self.files2replace.keys():
+            src_file = self.files2replace[tgt_file]
+            LOG.info("Copying {!r} => {!r} ...".format(src_file, tgt_file))
+            if not self.simulate:
+                shutil.copy2(src_file, tgt_file)
+
 # =============================================================================
 
 if __name__ == "__main__":