Skip to content

Instantly share code, notes, and snippets.

@jong
Forked from danielrobbins/zenpack-remove.py
Created October 18, 2012 16:45
Show Gist options
  • Save jong/3913153 to your computer and use it in GitHub Desktop.
Save jong/3913153 to your computer and use it in GitHub Desktop.
A script to remove broken ZenPacks
packs = None
if hasattr(dmd, 'ZenPackManager'):
packs = dmd.ZenPackManager.packs
else:
packs = dmd.packs
phase2 = False
for pack in packs():
try:
unused = pack.primaryAq()
print "%s is fine." % pack.id
except AttributeError:
print "Problem with %s ZenPack. Forcing removal." % pack.id
try:
packs._remove(pack)
print "Removed %s ZenPack." % pack.id
except AttributeError:
print "Unable to remove this ZenPack."
phase2 = True
# There were some packs we could not remove using the _remove method. Fall back to
# a deeper removal approach using pack ID.
if phase2:
print
print "Starting Phase 2 removal"
print
all_pack_ids = []
for pack_id in packs._objects:
all_pack_ids.append(pack_id)
valid_pack_ids = []
for pack in packs():
try:
valid_pack_ids.append(pack.id)
except:
pass
# iterate over all packs, if missing from valid packs, remove:
for pack_id in all_pack_ids:
if pack_id not in valid_pack_ids:
print "Forced removal of %s" % pack_id
del packs._objects[pack_id]
commit()
@mailforsachin
Copy link

[zenoss@ds1 zenpack]$ python zenpack-remove.py
Traceback (most recent call last):
File "zenpack-remove.py", line 2, in
if hasattr(dmd, 'ZenPackManager'):
NameError: name 'dmd' is not defined
[zenoss@ds1 zenpack]$ python zenpack-remove.py
Traceback (most recent call last):
File "zenpack-remove.py", line 2, in
if hasattr(dmd, 'ZenPackManager'):
NameError: name 'dmd' is not defined

how to resolve this?

@dsalbert
Copy link

Try this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Globals, os, inspect
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit

conn = ZenScriptBase(connect=True)
dmd = conn.dmd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment