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 Products.Five import zcml
25
26 from Utils import getObjByPath, zenPath
27
28 from Exceptions import ZentinelException
29 from ZenDaemon import ZenDaemon
30
31 from Products.ZenRelations.ZenPropertyManager import setDescriptors
32
33 import os
34 defaultCacheDir = zenPath('var')
35
37
38 -def login(context, name='admin', userfolder=None):
39 '''Logs in.'''
40 if userfolder is None:
41 userfolder = context.getPhysicalRoot().acl_users
42 user = userfolder.getUserById(name)
43 if user is None: return
44 if not hasattr(user, 'aq_base'):
45 user = user.__of__(userfolder)
46 newSecurityManager(None, user)
47 return user
48
50
51 - def __init__(self, noopts=0, app=None, keeproot=False):
52 import Products.ZenossStartup
53 zcml.load_site()
54 ZenDaemon.__init__(self, noopts, keeproot)
55 self.dataroot = None
56 self.app = app
57 self.db = None
58 if not app:
59 try:
60 self.zeoConnect()
61 except ValueError:
62 cache = os.path.join(self.options.pcachedir,
63 '%s.zec' % self.options.pcachename)
64 if os.path.exists(cache):
65 self.log.warning("Deleting corrupted cache %s" % cache)
66 os.unlink(cache)
67 self.zeoConnect()
68 else:
69 raise
70 self.poollock = Lock()
71 self.getDataRoot()
72 self.login()
73 setDescriptors(self.dmd.propertyTransformers)
74
76 from ZEO.ClientStorage import ClientStorage
77 storage=ClientStorage((self.options.host, self.options.port),
78 client=self.options.pcachename,
79 var=self.options.pcachedir,
80 cache_size=self.options.pcachesize*1024*1024)
81 from ZODB import DB
82 self.db = DB(storage, cache_size=self.options.cachesize)
83
84
85 - def login(self, name='admin', userfolder=None):
86 '''Logs in.'''
87 login(self.dmd, name, userfolder)
88
89
91 '''Logs out.'''
92 noSecurityManager()
93
94
96 """Return a wrapped app connection from the connection pool.
97 """
98 if not self.db:
99 raise ZentinelException(
100 "running inside zope can't open connections.")
101 try:
102 self.poollock.acquire()
103 connection=self.db.open()
104 root=connection.root()
105 app=root['Application']
106 app = self.getContext(app)
107 app._p_jar.sync()
108 return app
109 finally:
110 self.poollock.release()
111
112
114 """Close all connections in both free an inuse pools.
115 """
116 self.db.close()
117
118
120 if self.app: return
121 self.connection=self.db.open()
122 root=self.connection.root()
123 app=root['Application']
124 self.app = self.getContext(app)
125
126
128 self.connection.sync()
129
130
132 self.connection.close()
133
134 self.app = None
135 self.dataroot = None
136 self.dmd = None
137
138
140 if not self.app: self.opendb()
141 if not self.dataroot:
142 self.dataroot = getObjByPath(self.app, self.options.dataroot)
143 self.dmd = self.dataroot
144
145
146 - def getContext(self, app):
147 from ZPublisher.HTTPRequest import HTTPRequest
148 from ZPublisher.HTTPResponse import HTTPResponse
149 from ZPublisher.BaseRequest import RequestContainer
150 resp = HTTPResponse(stdout=None)
151 env = {
152 'SERVER_NAME':'localhost',
153 'SERVER_PORT':'8080',
154 'REQUEST_METHOD':'GET'
155 }
156 req = HTTPRequest(None, env, resp)
157 return app.__of__(RequestContainer(REQUEST = req))
158
159
161 """return an object based on a path starting from the dmd"""
162 return getObjByPath(self.app, self.options.dataroot+path)
163
164
166 """return a device based on its FQDN"""
167 devices = self.dataroot.getDmdRoot("Devices")
168 return devices.findDevice(name)
169
170 - def sigTerm(self, signum=None, frame=None):
172
174 """basic options setup sub classes can add more options here"""
175 ZenDaemon.buildOptions(self)
176 self.parser.add_option('--host',
177 dest="host",default="localhost",
178 help="hostname of zeo server")
179 self.parser.add_option('--port',
180 dest="port",type="int", default=8100,
181 help="port of zeo server")
182 self.parser.add_option('-R', '--dataroot',
183 dest="dataroot",
184 default="/zport/dmd",
185 help="root object for data load (i.e. /zport/dmd)")
186 self.parser.add_option('--cachesize',
187 dest="cachesize",default=1000, type='int',
188 help="in memory cachesize default: 1000")
189 self.parser.add_option('--pcachename',
190 dest="pcachename",default=None,
191 help="persistent cache file name default:None")
192 self.parser.add_option('--pcachedir',
193 dest="pcachedir",default=defaultCacheDir,
194 help="persistent cache file directory")
195 self.parser.add_option('--pcachesize',
196 dest="pcachesize",default=10, type='int',
197 help="persistent cache file size in MB")
198