1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""ZenDaemon
15
16 $Id: ZC.py,v 1.9 2004/02/16 17:19:31 edahl Exp $"""
17
18 __version__ = "$Revision: 1.9 $"[11:-2]
19
20 from threading import Lock
21
22 from AccessControl.SecurityManagement import newSecurityManager
23 from AccessControl.SecurityManagement import noSecurityManager
24 from Utils import getObjByPath, zenPath
25
26 from Exceptions import ZentinelException
27 from ZenDaemon import ZenDaemon
28
29 from Products.ZenRelations.ZenPropertyManager import setDescriptors
30
31 import os
32 defaultCacheDir = zenPath('var')
33
35
36 -def login(context, name='admin', userfolder=None):
37 '''Logs in.'''
38 if userfolder is None:
39 userfolder = context.getPhysicalRoot().acl_users
40 user = userfolder.getUserById(name)
41 if user is None: return
42 if not hasattr(user, 'aq_base'):
43 user = user.__of__(userfolder)
44 newSecurityManager(None, user)
45 return user
46
48
49
50 - def __init__(self, noopts=0, app=None, keeproot=False):
71
73 from ZEO.ClientStorage import ClientStorage
74 storage=ClientStorage((self.options.host, self.options.port),
75 client=self.options.pcachename,
76 var=self.options.pcachedir,
77 cache_size=self.options.pcachesize*1024*1024)
78 from ZODB import DB
79 self.db = DB(storage, cache_size=self.options.cachesize)
80
81
82 - def login(self, name='admin', userfolder=None):
83 '''Logs in.'''
84 login(self.dmd, name, userfolder)
85
86
88 '''Logs out.'''
89 noSecurityManager()
90
91
93 """Return a wrapped app connection from the connection pool.
94 """
95 if not self.db:
96 raise ZentinelException(
97 "running inside zope can't open connections.")
98 try:
99 self.poollock.acquire()
100 connection=self.db.open()
101 root=connection.root()
102 app=root['Application']
103 app = self.getContext(app)
104 app._p_jar.sync()
105 return app
106 finally:
107 self.poollock.release()
108
109
111 """Close all connections in both free an inuse pools.
112 """
113 self.db.close()
114
115
117 if self.app: return
118 self.connection=self.db.open()
119 root=self.connection.root()
120 app=root['Application']
121 self.app = self.getContext(app)
122
123
125 self.connection.sync()
126
127
129 self.connection.close()
130
131 self.app = None
132 self.dataroot = None
133 self.dmd = None
134
135
137 if not self.app: self.opendb()
138 if not self.dataroot:
139 self.dataroot = getObjByPath(self.app, self.options.dataroot)
140 self.dmd = self.dataroot
141
142
143 - def getContext(self, app):
144 from ZPublisher.HTTPRequest import HTTPRequest
145 from ZPublisher.HTTPResponse import HTTPResponse
146 from ZPublisher.BaseRequest import RequestContainer
147 resp = HTTPResponse(stdout=None)
148 env = {
149 'SERVER_NAME':'localhost',
150 'SERVER_PORT':'8080',
151 'REQUEST_METHOD':'GET'
152 }
153 req = HTTPRequest(None, env, resp)
154 return app.__of__(RequestContainer(REQUEST = req))
155
156
158 """return an object based on a path starting from the dmd"""
159 return getObjByPath(self.app, self.options.dataroot+path)
160
161
163 """return a device based on its FQDN"""
164 devices = self.dataroot.getDmdRoot("Devices")
165 return devices.findDevice(name)
166
167 - def sigTerm(self, signum=None, frame=None):
169
171 """basic options setup sub classes can add more options here"""
172 ZenDaemon.buildOptions(self)
173 self.parser.add_option('--host',
174 dest="host",default="localhost",
175 help="hostname of zeo server")
176 self.parser.add_option('--port',
177 dest="port",type="int", default=8100,
178 help="port of zeo server")
179 self.parser.add_option('-R', '--dataroot',
180 dest="dataroot",
181 default="/zport/dmd",
182 help="root object for data load (i.e. /zport/dmd)")
183 self.parser.add_option('--cachesize',
184 dest="cachesize",default=1000, type='int',
185 help="in memory cachesize default: 1000")
186 self.parser.add_option('--pcachename',
187 dest="pcachename",default=None,
188 help="persistent cache file name default:None")
189 self.parser.add_option('--pcachedir',
190 dest="pcachedir",default=defaultCacheDir,
191 help="persistent cache file directory")
192 self.parser.add_option('--pcachesize',
193 dest="pcachesize",default=10, type='int',
194 help="persistent cache file size in MB")
195