Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZEN-29068: For upgrades, a separate log file needs to be generated fo… #544

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions ZenPacks/zenoss/ZenPackLib/lib/base/DiffSaver.py
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 8 additions & 7 deletions ZenPacks/zenoss/ZenPackLib/lib/base/ZenPack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -61,14 +63,15 @@ 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)
if self.NEW_COMPONENT_TYPES:
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand Down