From 81cdc25c8a24c13736bf834489f0022a5b98f932 Mon Sep 17 00:00:00 2001 From: Oleksandr Styhar Date: Thu, 16 Aug 2018 17:09:09 +0300 Subject: [PATCH] ZEN-29068: For upgrades, a separate log file needs to be generated for Monitoring Template changes/conflicts/remediations --- .../zenoss/ZenPackLib/lib/base/DiffSaver.py | 47 +++++++++++++++++++ .../zenoss/ZenPackLib/lib/base/ZenPack.py | 15 +++--- 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 ZenPacks/zenoss/ZenPackLib/lib/base/DiffSaver.py diff --git a/ZenPacks/zenoss/ZenPackLib/lib/base/DiffSaver.py b/ZenPacks/zenoss/ZenPackLib/lib/base/DiffSaver.py new file mode 100644 index 00000000..d3e6406c --- /dev/null +++ b/ZenPacks/zenoss/ZenPackLib/lib/base/DiffSaver.py @@ -0,0 +1,47 @@ +############################################################################## +# +# Copyright (C) Zenoss, Inc. 2018, all rights reserved. +# +# This content is made available according to terms specified in +# License.zenoss under the directory where your Zenoss product is installed. +# +############################################################################## + +import os + +DIFF_DIR = '/home/zenoss/diff/' + + +class DiffSaver(object): + """ + Diff saver - saves ZenPack template diff into a file. + """ + + def __init__(self, log): + self.zpDiffDir = None + self.log = log + + def initZPDiffDir(self, zp): + self.zpDiffDir = '{}{}/'.format(DIFF_DIR, zp.id) + if os.path.isdir(self.zpDiffDir): + old_diffs = os.listdir(self.zpDiffDir) + for f in old_diffs: + os.remove('{}/{}'.format(self.zpDiffDir, f)) + else: + os.makedirs(self.zpDiffDir) + + def saveDiff(self, zp, parent, spec, id, diff): + diffFile = '{}{}.diff'.format(self.zpDiffDir, id) + self.log.info("Existing object {}/{} differs from " + "the newer version included with the {} ZenPack. " + "The existing object will be " + "backed up to '{}'. Please review and reconcile any " + "local changes before deleting the backup".format( + parent.getDmdKey(), spec.name, zp.id, os.path.abspath(diffFile))) + f = open(diffFile, 'w') + f.write(diff) + f.close() + + def remEmptyDir(self): + if not os.listdir(self.zpDiffDir): + os.rmdir(self.zpDiffDir) diff --git a/ZenPacks/zenoss/ZenPackLib/lib/base/ZenPack.py b/ZenPacks/zenoss/ZenPackLib/lib/base/ZenPack.py index ad2957f0..075edbf9 100644 --- a/ZenPacks/zenoss/ZenPackLib/lib/base/ZenPack.py +++ b/ZenPacks/zenoss/ZenPackLib/lib/base/ZenPack.py @@ -15,11 +15,13 @@ from Acquisition import aq_base from Products.ZenModel.ZenPack import ZenPack as ZenPackBase +from .DiffSaver import DiffSaver from ..helpers.Dumper import Dumper from ..helpers.ZenPackLibLog import ZenPackLibLog, new_log from Products.ZenEvents import ZenEventClasses LOG = new_log('zpl.ZenPack') +DiffSaver = DiffSaver(LOG) LOG.setLevel('INFO') ZenPackLibLog.enable_log_stderr(LOG) @@ -61,6 +63,7 @@ def _buildDeviceRelations(self, app, batch=10): def install(self, app): self.createZProperties(app) self.create_device_classes(app) + DiffSaver.initZPDiffDir(self) # Load objects.xml now super(ZenPack, self).install(app) @@ -68,7 +71,7 @@ def install(self, app): self.LOG.info('Adding {} relationships to existing devices'.format(self.id)) self._buildDeviceRelations(app) - # load monitoring templates + # Load monitoring templates for dcname, dcspec in self.device_classes.iteritems(): dcspecparam = self._v_specparams.device_classes.get(dcname) deviceclass = dcspec.get_organizer(app.zport.dmd) @@ -77,6 +80,9 @@ def install(self, app): mtspecparam = dcspecparam.templates.get(mtname) self.update_object(app, deviceclass, 'rrdTemplates', mtname, mtspec, mtspecparam) + # Remove ZP diff folder if it's empty + DiffSaver.remEmptyDir() + # Load event classes for ecname, ecspec in self.event_classes.iteritems(): ec_org = ecspec.create_organizer(app.zport.dmd) @@ -199,12 +205,7 @@ def check_diff(self, app, parent, relname, object, spec, specparam): time_str = time.strftime("%Y%m%d%H%M", time.localtime()) preupgrade_id = "{}-preupgrade-{}".format(object.id, time_str) self.move_object(parent, relname, object.id, preupgrade_id) - LOG.info("Existing object {}/{} differs from " - "the newer version included with the {} ZenPack. " - "The existing object will be " - "backed up to '{}'. Please review and reconcile any " - "local changes before deleting the backup: \n{}".format( - parent.getDmdKey(), spec.name, self.id, preupgrade_id, diff)) + DiffSaver.saveDiff(self, parent, spec, preupgrade_id, diff) return True def get_object(self, parent, relname, object_id):