1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZenScriptBase
15 """
16
17 from AccessControl.SecurityManagement import newSecurityManager
18 from AccessControl.SecurityManagement import noSecurityManager
19 from threading import Lock
20 from transaction import commit
21 from Utils import getObjByPath, zenPath, set_context
22 from CmdBase import CmdBase
23 from Products.Five import zcml
24
25 from Products.ZenRelations.ZenPropertyManager import setDescriptors
26 from Exceptions import ZentinelException
27
28 defaultCacheDir = zenPath('var')
29
31
33
34 - def __init__(self, noopts=0, app=None, connect=False):
35 import Products.ZenossStartup
36 zcml.load_site()
37 CmdBase.__init__(self, noopts)
38 self.dataroot = None
39 self.app = app
40 self.db = None
41 if connect:
42 self.connect()
43
45 if not self.app:
46 from ZEO import ClientStorage
47 from ZODB import DB
48 addr = (self.options.host, self.options.port)
49 storage=ClientStorage.ClientStorage(addr,
50 client=self.options.pcachename,
51 var=self.options.pcachedir,
52 cache_size=self.options.pcachesize*1024*1024)
53 self.db=DB(storage, cache_size=self.options.cachesize)
54 self.poollock = Lock()
55 self.getDataRoot()
56 self.login()
57 if getattr(self.dmd, 'propertyTransformers', None) is None:
58 self.dmd.propertyTransformers = {}
59 commit()
60 setDescriptors(self.dmd.propertyTransformers)
61
62
63 - def login(self, name='admin', userfolder=None):
64 '''Logs in.'''
65 if userfolder is None:
66 userfolder = self.app.acl_users
67 user = userfolder.getUserById(name)
68 if user is None: return
69 if not hasattr(user, 'aq_base'):
70 user = user.__of__(userfolder)
71 newSecurityManager(None, user)
72
73
75 '''Logs out.'''
76 noSecurityManager()
77
78
80 """Return a wrapped app connection from the connection pool.
81 """
82 if not self.db:
83 raise ZentinelException(
84 "running inside zope can't open connections.")
85 try:
86 self.poollock.acquire()
87 connection=self.db.open()
88 root=connection.root()
89 app=root['Application']
90 app = set_context(app)
91 app._p_jar.sync()
92 return app
93 finally:
94 self.poollock.release()
95
96
98 """Close all connections in both free an inuse pools.
99 """
100 self.db.close()
101
102
104 if self.app: return
105 self.connection=self.db.open()
106 root=self.connection.root()
107 app = root['Application']
108 self.app = set_context(app)
109 self.app._p_jar.sync()
110
111
113 self.connection.sync()
114
115
117 self.connection.close()
118
119 self.app = None
120 self.dataroot = None
121 self.dmd = None
122
123
125 if not self.app: self.opendb()
126 if not self.dataroot:
127 self.dataroot = getObjByPath(self.app, self.options.dataroot)
128 self.dmd = self.dataroot
129
130
132 """return an object based on a path starting from the dmd"""
133 return getObjByPath(self.app, self.options.dataroot+path)
134
135
137 """return a device based on its FQDN"""
138 devices = self.dataroot.getDmdRoot("Devices")
139 return devices.findDevice(name)
140
141
143 """basic options setup sub classes can add more options here"""
144 CmdBase.buildOptions(self)
145 self.parser.add_option('--host',
146 dest="host",default="localhost",
147 help="hostname of zeo server")
148 self.parser.add_option('--port',
149 dest="port",type="int", default=8100,
150 help="port of zeo server")
151 self.parser.add_option('-R', '--dataroot',
152 dest="dataroot",
153 default="/zport/dmd",
154 help="root object for data load (i.e. /zport/dmd)")
155 self.parser.add_option('--cachesize',
156 dest="cachesize",default=1000, type='int',
157 help="in memory cachesize default: 1000")
158 self.parser.add_option('--pcachename',
159 dest="pcachename",default=None,
160 help="persistent cache file name default:None")
161 self.parser.add_option('--pcachedir',
162 dest="pcachedir",default=defaultCacheDir,
163 help="persistent cache file directory")
164 self.parser.add_option('--pcachesize',
165 dest="pcachesize",default=10, type='int',
166 help="persistent cache file size in MB")
167