Package Products :: Package ZenUtils :: Module ZCmdBase
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenUtils.ZCmdBase

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 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   
34 -class DataRootError(Exception):pass
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
47 -class ZCmdBase(ZenDaemon):
48 49
50 - def __init__(self, noopts=0, app=None, keeproot=False):
51 ZenDaemon.__init__(self, noopts, keeproot) 52 self.dataroot = None 53 self.app = app 54 self.db = None 55 if not app: 56 try: 57 self.zeoConnect() 58 except ValueError: 59 cache = os.path.join(self.options.pcachedir, 60 '%s.zec' % self.options.pcachename) 61 if os.path.exists(cache): 62 self.log.warning("Deleting corrupted cache %s" % cache) 63 os.unlink(cache) 64 self.zeoConnect() 65 else: 66 raise 67 self.poollock = Lock() 68 self.getDataRoot() 69 self.login() 70 setDescriptors(self.dmd.propertyTransformers)
71
72 - def zeoConnect(self):
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
87 - def logout(self):
88 '''Logs out.''' 89 noSecurityManager()
90 91
92 - def getConnection(self):
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
110 - def closeAll(self):
111 """Close all connections in both free an inuse pools. 112 """ 113 self.db.close()
114 115
116 - def opendb(self):
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
124 - def syncdb(self):
125 self.connection.sync()
126 127
128 - def closedb(self):
129 self.connection.close() 130 #self.db.close() 131 self.app = None 132 self.dataroot = None 133 self.dmd = None
134 135
136 - def getDataRoot(self):
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
157 - def getDmdObj(self, path):
158 """return an object based on a path starting from the dmd""" 159 return getObjByPath(self.app, self.options.dataroot+path)
160 161
162 - def findDevice(self, name):
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):
168 pass
169
170 - def buildOptions(self):
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