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

Source Code for Module ZenUtils.ZenScriptBase

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