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

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