-
Notifications
You must be signed in to change notification settings - Fork 0
/
zenobjectcopier.py
executable file
·136 lines (114 loc) · 4.6 KB
/
zenobjectcopier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
###########################################################################
#
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2009, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#
###########################################################################
import os
import sys
import Globals
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
class ZenObjectCopier(ZenScriptBase):
"""
Class that can be used to export and import specific Zobjects out of one
zenoss instance and into another. To be used when a full ZenPack is
overkill for the situation.
Example export command:
zenobjectcopier.py --export \
--object="/zport/dmd/Devices/rrdTemplates/Oracle Tablespaces" \
--file=oracleTablespacesTemplate.xml
Example import command:
zenobjectcopier.py --import --file=oracleTablespacesTemplate.xml
"""
def __init__(self):
ZenScriptBase.__init__(self, connect=True)
def buildOptions(self):
ZenScriptBase.buildOptions(self)
self.parser.add_option('--export', dest='export',
action='store_true', default=False,
help='Perform an XML export of an object')
self.parser.add_option('--object', dest='object',
help='Full path to the object being exported ' +
'(e.g. /zport/dmd/Devices/rrdTemplates/Oracle Tablespaces)')
self.parser.add_option('--import', dest='imprt',
action='store_true', default=False,
help='Perform an import of an object from XML')
self.parser.add_option('--file', dest='file',
help='Filename to of XML to import')
def validateOptions(self):
if self.options.export:
if not self.options.object:
print >> sys.stderr, 'You must specify the object to export.'
sys.exit(1)
if not self.options.file:
print >> sys.stderr, 'You must specify the export file.'
sys.exit(1)
elif self.options.imprt:
if not self.options.file:
print >> sys.stderr, 'You must specify the file to import.'
sys.exit(1)
if not os.path.isfile(self.options.file):
print >> sys.stderr, 'The specified file does not exist.'
sys.exit(1)
else:
print >> sys.stderr, 'You must specify either export or import.'
sys.exit(1)
def doExport(self):
obj = None
try:
obj = self.dmd.getObjByPath(self.options.object)
except:
print >> sys.stderr, 'No object at %s.' % self.options.object
sys.exit(3)
f = None
try:
f = open(self.options.file, 'w')
except:
print >> sys.stderr, 'Error writing to %s.' % self.options.file
sys.exit(2)
# Write the initial XML representation of the object to the file.
print "Writing object XML to %s." % self.options.file
obj.exportXml(f)
f.close()
# Open the file so we can make modifications to the XML.
f = open(self.options.file, 'r')
from xml.dom.minidom import parse
xml = parse(f)
f.close()
# We need to specify the full path to the object so it can be imported.
xml.getElementsByTagName('object')[0].setAttribute(
'id', self.options.object)
# Write the modified XML back to the file.
f = open(self.options.file, 'w')
xml.writexml(f, addindent=" ")
f.close()
def doImport(self):
from Products.ZenRelations.ImportRM import ImportRM
im = ImportRM(noopts=True, app=self.dmd.zport)
f = None
try:
f = open(self.options.file, 'r')
except:
print >> sys.stderr, 'Error reading from %s.' % self.options.file
sys.exit(2)
print "Importing objects from %s." % self.options.file
im.loadObjectFromXML(xmlfile=f)
from transaction import commit
commit()
f.close()
def run(self):
self.validateOptions()
if self.options.export:
self.doExport()
else:
self.doImport()
if __name__ == '__main__':
copier = ZenObjectCopier()
copier.run()