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