From 9330ffe456d0c17011f45ad874eb8ada542a41c3 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 9 Nov 2017 17:57:23 +0100 Subject: [PATCH] Creating pidfile and cleaning up --- pp_lib/deploy_zones_from_pdns.py | 47 ++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/pp_lib/deploy_zones_from_pdns.py b/pp_lib/deploy_zones_from_pdns.py index 008dacf..35e2402 100644 --- a/pp_lib/deploy_zones_from_pdns.py +++ b/pp_lib/deploy_zones_from_pdns.py @@ -40,7 +40,7 @@ from .pdns_record import compare_rrsets from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile -__version__ = '0.2.2' +__version__ = '0.2.3' LOG = logging.getLogger(__name__) @@ -292,7 +292,50 @@ class PpDeployZonesApp(PpPDNSApplication): LOG.error(msg) self.exit(1) - LOG.info("Here we go again ...") + try: + self.pidfile.create() + except PidFileError as e: + LOG.error("Could not occupy pidfile: {}".format(e)) + self.exit(7) + return + + try: + + LOG.info("Here we go again ...") + + finally: + self.cleanup() + self.pidfile = None + + # ------------------------------------------------------------------------- + def cleanup(self): + + LOG.info("Cleaning up ...") + + for tgt_file in self.moved_files.keys(): + 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) + + # ----------------------- + def emit_rm_err(function, path, excinfo): + LOG.error("Error removing {!r} - {}: {}".format( + path, excinfo[1].__class__.__name__, excinfo[1])) + + if self.tempdir: + if self.keep_tempdir: + msg = ( + "Temporary directory {!r} will not be removed. " + "It's on yours to remove it manually.").format(self.tempdir) + LOG.warn(msg) + else: + LOG.debug("Destroying temporary directory {!r} ...".format(self.tempdir)) + shutil.rmtree(self.tempdir, False, emit_rm_err) + self.tempdir = None + # ============================================================================= -- 2.39.5