def InstallEgg(dmd, eggPath, link=False): """ Install the given egg and add to the current working set. This does not install the egg as a ZenPack. Return a list of distributions that should be installed as ZenPacks. """ eggPath = os.path.abspath(eggPath) zenPackDir = zenPath('ZenPacks') # Make sure $ZENHOME/ZenPacks exists CreateZenPacksDir() # Install the egg if link: zenPackDir = varPath('ZenPacks') cmd = ('%s setup.py develop ' % binPath('python') + '--site-dirs=%s ' % zenPackDir + '-d %s' % zenPackDir) returncode, out, err = None, '', '' for attempt in range(3): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=eggPath) out, err = p.communicate() p.wait() returncode = p.returncode if returncode: log.info("Error installing the egg (%s): %s" % (returncode, err)) try: DoEasyUninstall(eggPath) except: pass else: break if returncode: raise ZenPackException('Error installing the egg (%s): %s' % (returncode, err)) zpDists = AddDistToWorkingSet(eggPath) else: try: zpDists = DoEasyInstall(eggPath) except: DoEasyUninstall(eggPath) raise # cmd = 'easy_install --always-unzip --site-dirs=%s -d %s %s' % ( # zenPackDir, # zenPackDir, # eggPath) # p = subprocess.Popen(cmd, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # shell=True) # p.wait() # eggName = os.path.split(eggPath)[1] # eggPath = os.path.join(zenPackDir, eggName) return zpDists
def InstallEgg(dmd, eggPath, link=False): """ Install the given egg and add to the current working set. This does not install the egg as a ZenPack. Return a list of distributions that should be installed as ZenPacks. """ eggPath = os.path.abspath(eggPath) zenPackDir = zenPath('ZenPacks') # Make sure $ZENHOME/ZenPacks exists CreateZenPacksDir() # Install the egg if link: zenPackDir = varPath('ZenPacks') cmd = ('%s setup.py develop ' % binPath('python') + '--site-dirs=%s ' % zenPackDir + '-d %s' % zenPackDir) returncode, out, err = None, '', '' for attempt in range(3): p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=eggPath) out, err = p.communicate() p.wait() returncode = p.returncode if returncode: log.info("Error installing the egg (%s): %s" % (returncode, err)) try: DoEasyUninstall(eggPath) except Exception: pass else: break if returncode: raise ZenPackException('Error installing the egg (%s): %s' % (returncode, err)) zpDists = AddDistToWorkingSet(eggPath) else: try: zpDists = DoEasyInstall(eggPath) except Exception: DoEasyUninstall(eggPath) raise # cmd = 'easy_install --always-unzip --site-dirs=%s -d %s %s' % ( # zenPackDir, # zenPackDir, # eggPath) # p = subprocess.Popen(cmd, # stdout=subprocess.PIPE, # stderr=subprocess.PIPE, # shell=True) # p.wait() # eggName = os.path.split(eggPath)[1] # eggPath = os.path.join(zenPackDir, eggName) return zpDists
def manage_addZenPack(self, packId, REQUEST=None, devDir=''): """ Create a new zenpack on the filesystem with the given info. Install the pack. If REQUEST then render the REQUEST otherwise return the new zenpack. """ import Products.ZenUtils.ZenPackCmd as ZenPackCmd if not getattr(self.dmd, 'ZenPackManager'): msg = 'Your Zenoss database appears to be out of date. Try ' \ 'running zenmigrate to update.' if REQUEST: messaging.IMessageSender(self).sendToBrowser( 'Error', msg, priority=messaging.WARNING) return self.callZenScreen(REQUEST) from ZenPack import ZenPackNeedMigrateException raise ZenPackNeedMigrateException(msg) # Make sure a zenpack can be created with given info canCreate, msgOrId = ZenPackCmd.CanCreateZenPack(self, packId) if canCreate: packId = msgOrId else: if REQUEST: messaging.IMessageSender(self).sendToBrowser( 'Add ZenPack', msgOrId) return self.callZenScreen(REQUEST, redirect=False) from ZenPack import ZenPackException raise ZenPackException(msgOrId) if devDir == "": devDir = varPath("ZenPackSource") # Create it zpDir = ZenPackCmd.CreateZenPack(packId, devDir=devDir) # Install it zenPacks = ZenPackCmd.InstallEggAndZenPack(self.dmd, zpDir, link=True, forceRunExternal=True) zenPack = self.packs._getOb(packId, None) audit('UI.ZenPack.Create', packId) if REQUEST: if zenPack: return REQUEST['RESPONSE'].redirect( zenPack.getPrimaryUrlPath()) messaging.IMessageSender(self).sendToBrowser( 'Error', 'There was an error creating the ZenPack.', priority=messaging.WARNING) return self.callZenScreen(REQUEST) return zenPack
def setupWatchdog(self): if self.options.blockingTimeout > 0: self.blockingPlugins = watchdog.get_timeout_entries( timeout_file=varPath('{}.blocked'.format(self.collectorName))) log.info("plugins disabled by watchdog: %r", list(self.blockingPlugins)) log.info("starting watchdog with %.1fs timeout", self.options.blockingTimeout) watchdog.start() else: self.blockingPlugins = set()
def manage_addZenPack(self, packId, REQUEST=None, devDir=''): """ Create a new zenpack on the filesystem with the given info. Install the pack. If REQUEST then render the REQUEST otherwise return the new zenpack. """ import Products.ZenUtils.ZenPackCmd as ZenPackCmd if not getattr(self.dmd, 'ZenPackManager'): msg = 'Your Zenoss database appears to be out of date. Try ' \ 'running zenmigrate to update.' if REQUEST: messaging.IMessageSender(self).sendToBrowser( 'Error', msg, priority=messaging.WARNING) return self.callZenScreen(REQUEST) from ZenPack import ZenPackNeedMigrateException raise ZenPackNeedMigrateException(msg) # Make sure a zenpack can be created with given info canCreate, msgOrId = ZenPackCmd.CanCreateZenPack(self, packId) if canCreate: packId = msgOrId else: if REQUEST: messaging.IMessageSender(self).sendToBrowser( 'Add ZenPack', msgOrId) return self.callZenScreen(REQUEST, redirect=False) from ZenPack import ZenPackException raise ZenPackException(msgOrId) if devDir == "": devDir = varPath("ZenPackSource") # Create it zpDir = ZenPackCmd.CreateZenPack(packId, devDir=devDir) # Install it zenPacks = ZenPackCmd.InstallEggAndZenPack(self.dmd, zpDir, link=True, forceRunExternal=True) zenPack = self.packs._getOb(packId, None) audit('UI.ZenPack.Create', packId) if REQUEST: if zenPack: return REQUEST['RESPONSE'].redirect(zenPack.getPrimaryUrlPath()) messaging.IMessageSender(self).sendToBrowser( 'Error', 'There was an error creating the ZenPack.', priority=messaging.WARNING) return self.callZenScreen(REQUEST) return zenPack
def setupWatchdog(self): if self.options.blockingTimeout > 0: self.blockingPlugins = watchdog.get_timeout_entries( timeout_file=varPath('{}.blocked'.format(self.collectorName))) log.info( "plugins disabled by watchdog: %r", list(self.blockingPlugins)) log.info( "starting watchdog with %.1fs timeout", self.options.blockingTimeout) watchdog.start() else: self.blockingPlugins = set()
from Products.ZenModel.ZenPack import (ZenPack, ZenPackException, ZenPackNotFoundException, ZenPackNeedMigrateException) from Products.ZenUtils.ZenScriptBase import ZenScriptBase from Products.ZenUtils.Utils import cleanupSkins, zenPath, binPath, varPath, get_temp_dir import Products.ZenModel.ZenPackLoader as ZPL from Products.ZenModel.ZenPackLoader import CONFIG_FILE, CONFIG_SECTION_ABOUT from Products.ZenModel.ZVersion import VERSION import ZenPackCmd as EggPackCmd from Products.Zuul import getFacade from zope.component import getUtilitiesFor from Products.ZenUtils.ZenPackInstallFilter import IZenPackInstallFilter ZPHISTORY = zenPath('zphistory.json') UPGRADE_FROM_FILE = varPath('upgrade_from_version.txt') HIGHER_THAN_CRITICAL = 100 LSB_EXITCODE_PROGRAM_IS_NOT_RUNNING = 3 def RemoveZenPack(dmd, packName, log=None, skipDepsCheck=False, leaveObjects=True, deleteFiles=True): if log: log.debug('Removing Pack "%s"', packName) if not skipDepsCheck:
def RemoveZenPack(dmd, packName, filesOnly=False, skipDepsCheck=False, leaveObjects=False, sendEvent=True, forceNoFileDeletion=False, uninstallEgg=True): """ Remove the given ZenPack from Zenoss. Whether the ZenPack will be removed from the filesystem or not depends on the result of the ZenPack's shouldDeleteFilesOnRemoval method. """ try: if filesOnly: skipDepsCheck = True # Check for dependency implications here? if not skipDepsCheck: deps = GetDependents(dmd, packName) if deps: raise ZenPackDependentsException('%s cannot be removed ' % packName + 'because it is required by %s' % ', '.join(deps)) if not filesOnly: # Fetch the zenpack, call its remove() and remove from packs zp = None try: zp = dmd.ZenPackManager.packs._getOb(packName) except AttributeError as ex: raise ZenPackNotFoundException('No ZenPack named %s is installed' % packName) # If zencatalog hasn't finished yet, we get ugly messages that don't # mean anything. Hide them. logFilter = None if not getattr(dmd.zport, '_zencatalog_completed', False): logFilter = CatalogLoggingFilter() logging.getLogger('Zope.ZCatalog').addFilter(logFilter) try: zp.remove(dmd, leaveObjects) dmd.ZenPackManager.packs._delObject(packName) transaction.commit() finally: # Remove our logging filter so we don't hide anything important if logFilter is not None: logging.getLogger('Zope.ZCatalog').removeFilter(logFilter) # Uninstall the egg and possibly delete it # If we can't find the distribution then apparently the zp egg itself is # missing. Continue on with the removal and swallow the # DistributionNotFound exception try: dist = zp.getDistribution() except pkg_resources.DistributionNotFound: dist = None if dist: # Determine deleteFiles before develop -u gets called. Once # it is called the egg has problems figuring out some of it's state. deleteFiles = zp.shouldDeleteFilesOnRemoval() if uninstallEgg: if zp.isDevelopment(): zenPackDir = varPath('ZenPacks') cmd = ('%s setup.py develop -u ' % binPath('python') + '--site-dirs=%s ' % zenPackDir + '-d %s' % zenPackDir) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=zp.eggPath()) out, err = p.communicate() code = p.wait() if code: raise ZenPackException(err) else: DoEasyUninstall(packName) # elif cleanupEasyInstallPth: # # Do we need to call easy_install -m here? It causes problems # # because it tries to install deps. Cleanup easy-install.pth # # ourselves instead. # # We don't want to cleanup easy-install.pth when a newer # # version of the egg has already been installed (when doing # # an upgrade or installing in new location.) # eggLink = './%s' % zp.eggName() # CleanupEasyInstallPth(eggLink) if deleteFiles and not forceNoFileDeletion: eggDir = zp.eggPath() if os.path.islink(eggDir): os.remove(eggDir) else: shutil.rmtree(eggDir) cleanupSkins(dmd) transaction.commit() except ZenPackDependentsException as ex: log.error(ex) except Exception as ex: # Get that exception out there in case it gets blown away by ZPEvent log.exception("Error removing ZenPack %s" % packName) if sendEvent: ZPEvent(dmd, SEVERITY_ERROR, 'Error removing ZenPack %s' % packName, '%s: %s' % sys.exc_info()[:2]) # Don't just raise, because if ZPEvent blew away exception context # it'll be None, which is bad. This manipulates the stack to look like # this is the source of the exception, but we logged it above so no # info is lost. raise ex if sendEvent: ZPEvent(dmd, 2, 'Removed ZenPack %s' % packName) audit('Shell.ZenPack.Remove', packName)
ZenPackNotFoundException, ZenPackNeedMigrateException ) from Products.ZenUtils.ZenScriptBase import ZenScriptBase from Products.ZenUtils.Utils import cleanupSkins, zenPath, binPath, varPath, get_temp_dir import Products.ZenModel.ZenPackLoader as ZPL from Products.ZenModel.ZenPackLoader import CONFIG_FILE, CONFIG_SECTION_ABOUT from Products.ZenModel.ZVersion import VERSION import ZenPackCmd as EggPackCmd from Products.Zuul import getFacade from zope.component import getUtilitiesFor from Products.ZenUtils.ZenPackInstallFilter import IZenPackInstallFilter ZPHISTORY = zenPath('zphistory.json') UPGRADE_FROM_FILE = varPath('upgrade_from_version.txt') HIGHER_THAN_CRITICAL = 100 LSB_EXITCODE_PROGRAM_IS_NOT_RUNNING = 3 def RemoveZenPack(dmd, packName, log=None, skipDepsCheck=False, leaveObjects=True, deleteFiles=True): if log: log.debug('Removing Pack "%s"', packName) if not skipDepsCheck: for pack in dmd.ZenPackManager.packs(): if packName in pack.requires: raise ZenPackException('Pack %s depends on pack %s, ' 'not removing' % (pack.id, packName))
def RemoveZenPack(dmd, packName, filesOnly=False, skipDepsCheck=False, leaveObjects=False, sendEvent=True, forceNoFileDeletion=False, uninstallEgg=True): """ Remove the given ZenPack from Zenoss. Whether the ZenPack will be removed from the filesystem or not depends on the result of the ZenPack's shouldDeleteFilesOnRemoval method. """ try: if filesOnly: skipDepsCheck = True # Check for dependency implications here? if not skipDepsCheck: deps = GetDependents(dmd, packName) if deps: raise ZenPackDependentsException('%s cannot be removed ' % packName + 'because it is required by %s' % ', '.join(deps)) if not filesOnly: # Fetch the zenpack, call its remove() and remove from packs zp = None try: zp = dmd.ZenPackManager.packs._getOb(packName) except AttributeError, ex: raise ZenPackNotFoundException('No ZenPack named %s is installed' % packName) # If zencatalog hasn't finished yet, we get ugly messages that don't # mean anything. Hide them. logFilter = None if not getattr(dmd.zport, '_zencatalog_completed', False): logFilter = CatalogLoggingFilter() logging.getLogger('Zope.ZCatalog').addFilter(logFilter) try: zp.remove(dmd, leaveObjects) dmd.ZenPackManager.packs._delObject(packName) transaction.commit() finally: # Remove our logging filter so we don't hide anything important if logFilter is not None: logging.getLogger('Zope.ZCatalog').removeFilter(logFilter) # Uninstall the egg and possibly delete it # If we can't find the distribution then apparently the zp egg itself is # missing. Continue on with the removal and swallow the # DistributionNotFound exception try: dist = zp.getDistribution() except pkg_resources.DistributionNotFound: dist = None if dist: # Determine deleteFiles before develop -u gets called. Once # it is called the egg has problems figuring out some of it's state. deleteFiles = zp.shouldDeleteFilesOnRemoval() if uninstallEgg: if zp.isDevelopment(): zenPackDir = varPath('ZenPacks') cmd = ('%s setup.py develop -u ' % binPath('python') + '--site-dirs=%s ' % zenPackDir + '-d %s' % zenPackDir) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=zp.eggPath()) out, err = p.communicate() code = p.wait() if code: raise ZenPackException(err) else: DoEasyUninstall(packName) # elif cleanupEasyInstallPth: # # Do we need to call easy_install -m here? It causes problems # # because it tries to install deps. Cleanup easy-install.pth # # ourselves instead. # # We don't want to cleanup easy-install.pth when a newer # # version of the egg has already been installed (when doing # # an upgrade or installing in new location.) # eggLink = './%s' % zp.eggName() # CleanupEasyInstallPth(eggLink) if deleteFiles and not forceNoFileDeletion: eggDir = zp.eggPath() if os.path.islink(eggDir): os.remove(eggDir) else: shutil.rmtree(eggDir) cleanupSkins(dmd) transaction.commit()