def __init__(self, validArgCount=0): self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) # Do not log into a file for CLIs LoggingManager.getInstance().setFileLogLevel('CRITICAL') LoggingManager.getInstance().setConsoleLogLevel('CRITICAL') self.parser = CdbOptionParser() self.options = {} self.args = [] self.validArgCount = validArgCount self.optionGroupDict = {} commonGroup = 'Common Options' self.addOptionGroup(commonGroup, None) self.addOptionToGroup(commonGroup, '-h', '--help', action='help', help='Show this help message and exit.') self.addOptionToGroup(commonGroup, '-?', '', action='help', help='Show this help message and exit.') self.addOptionToGroup(commonGroup, '-v', '--version', action='store_true', dest='version', default=False, help='Print version and exit.') self.addOptionToGroup( commonGroup, '-d', '--debug', dest='consoleLogLevel', help= 'Set debug level (valid values: CRITICAL, ERROR, WARNING, INFO, DEBUG). Console log level can also be set via CDB_CONSOLE_LOG_LEVEL environment variable,' ) self.addOptionToGroup( commonGroup, '', '--display-format', dest='displayFormat', default=CdbObject.TEXT_DISPLAY_FORMAT, help= 'Display format for output objects. Possible options are: %s, %s, and %s (default: %s).' % (CdbObject.TEXT_DISPLAY_FORMAT, CdbObject.DICT_DISPLAY_FORMAT, CdbObject.JSON_DISPLAY_FORMAT, CdbObject.TEXT_DISPLAY_FORMAT)) self.addOptionToGroup( commonGroup, '', '--display-keys', dest='displayKeys', default=CdbObject.DEFAULT_KEYS, help= 'List of output object keys to display. Possible options are: %s, %s, and string containing comma-separated keys (default: %s, represents class default keys).' % (CdbObject.DEFAULT_KEYS, CdbObject.ALL_KEYS, CdbObject.DEFAULT_KEYS))
def __init__(self, **kwargs): self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) for col, value in kwargs.items(): if col not in self.columns: raise DbError('Column %s not found in table %s' % (col, self.__class__.__name__)) else: setattr(self, col, value)
def __init__(self): self.sparePartsTask = SparePartsTask() self.cherryPyBackgroundTasks = cherrypy.process.plugins.BackgroundTask( self.BACKGROUND_TASKS_TIME_IN_SECONDS, self.scheduledTasks) self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) self.emailUtility = CdbEmailUtility.getInstance()
def checkCredentials(cls, username, password): """ Verifies credentials for username and password.""" logger = LoggingManager.getInstance().getLogger( 'LoginController:checkCredentials') logger.debug('Checking credential for User: %s' % (username)) #logger.debug('Checking credential for User: %s, Password: %s' % (username, password)) logger.debug('Session id: %s' % cherrypy.serving.session.id) principal = AuthorizationManager.getInstance( ).getAuthorizationPrincipal(username, password) logger.debug('Principal: %s' % (principal)) if principal: cherrypy.session[ LoginController.SESSION_ROLE_KEY] = principal.getRole() logger.debug('Successful login from user: %s (role: %s)' % (username, principal.getRole())) else: logger.debug('Login denied for user: %s' % username) username = cherrypy.session.get( LoginController.SESSION_USERNAME_KEY, None) if username is not None: cherrypy.request.login = None cherrypy.session[ LoginController.INVALID_CDB_SESSION_KEY] = True raise AuthorizationError('Incorrect username or password.') cherrypy.session[ LoginController.SESSION_USER_KEY] = principal.getUserInfo() return principal
def __init__(self, username, password, windchillUrl, icmsUrl, icmsUser, icmsPass): self.username = username self.password = password self.windchillUrl = windchillUrl self.icmsUrl = icmsUrl self.icmsUser = icmsUser self.icmsPass = icmsPass self.windchillWs = None self.windchillWebparts = None self.icmsConnection = None self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) self.versionIndex = None self.iterationIndex = None self.numberIndex = None # searchArgs are used in search functions of Windchill webparts class # Only keyword needs to be changed when searching for a node. # Example: webparts.service.WindchillSearch(**searchArgs) self.searchArgs = { 'keyword': 'notSet', 'START': 0, 'MAX': 1, 'SearchTypes': 'WCTYPE|wt.epm.EPMDocument|gov.anl.aps.DefaultEPMDocument', 'AttributeFilter': '', 'SortBy': 'name', 'SortDirection': 'ASC', 'clientLocale': 'en-US' }
def getPluginRoutes(cls): """ Dynamically loads routes from plugins that have been loaded into web service. Expected directory structure: cdb/cdb_web_service/plugins/[pluginName]/ Plugin package must contain routeDescriptor with class Route descriptor & classmethod getRoutes(). :return: routes for all successfully imported plugins. """ pluginImports = cls.__getPluginImports() pluginRoutes = [] if pluginImports is not None: logger = LoggingManager.getInstance().getLogger(cls.__name__) for pluginImport in pluginImports: logger.debug("Importing routes from: %s" % pluginImport) try: plugin = importlib.import_module(pluginImport) pluginRoutes += plugin.RouteDescriptor.getRoutes() except ImportError as ex: logger.error("Could not import %s: " % pluginImport) logger.error(ex.message) except AttributeError as ex: logger.error("Could not load routes from: %s" % pluginImport) logger.error(ex.message) return pluginRoutes
def prepareServer(self): try: optionParser = self.prepareOptions() (self.options, self.args) = optionParser.parse_args() # Read config file and override with command line options self.readConfigFile(self.options.configFile) self.readCommandLineOptions() # Turn off console log for daemon mode. self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) if self.options.daemonFlag: LoggingManager.getInstance().setConsoleLogLevel('CRITICAL') dispatch = self.routeMapper.setupRoutes() self.logger.debug('Using route dispatch: %s' % dispatch) config = { '/': { 'request.dispatch': dispatch, }, } # No root controller as we provided our own. cherrypy.tree.mount(root=None, config=config) self.initServerLog() self.updateServerConfig() self.logger.info('Using host %s' % self.configurationManager.getServiceHost()) self.logger.info('Using port %s' % self.configurationManager.getServicePort()) self.logger.debug('Using %s request handler threads' % self.options.nServerThreads) # Set up scheduled tasks performed web service. self.backgroundTaskManager = CdbBackgroundTaskManager() except Exception, ex: if self.logger is not None: self.logger.exception(ex) else: import traceback print '\n%s' % sys.exc_info()[1] traceback.print_exc(file=sys.stderr) sys.exit(cdbStatus.CDB_ERROR)
def __init__(self): self.itemDbApi = ItemDbApi() self.userDbApi = UserDbApi() self.propertyDbApi = PropertyDbApi() self.logDbApi = LogDbApi() self.emailUtility = CdbEmailUtility.getInstance() self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance() self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
def getLogger(cls): """ Get an instance of a logger for the class. :return: logger instance. """ logger = LoggingManager.getInstance().getLogger(cls.__name__) return logger
def __init__(self, args, bufsize=0, executable=None, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=None, close_fds=False, shell=True, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0, useExceptions=True, quietMode=False): """ Overrides Popen constructor with more appropriate defaults. """ subprocess.Popen.__init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) self._stdout = None self._stderr = None self._args = args self.useExceptions = useExceptions self.quietMode = quietMode
def parseArgs(self, usage=None): if usage: self.parser.usage = usage try: (self.options, self.args) = self.parser.parse_args() self.processArgs() except SystemExit as rc: sys.stdout.flush() sys.stderr.flush() sys.exit(int(str(rc))) if self.validArgCount < len(self.args): # Positional args are not enabled and we have some msg = 'Invalid positional argument(s):' for arg in self.args[self.validArgCount:]: msg += ' ' + arg msg += ' (This command allows %s positional arguments.)' % self.validArgCount raise InvalidArgument(msg) optDict = self.options.__dict__ if optDict.get('version'): print('CDB Software Version: %s' % (cdb.__version__)) sys.exit(0) # Logging level. First try from command line, then from env variable. consoleLogLevel = optDict.get('consoleLogLevel', None) if consoleLogLevel: LoggingManager.getInstance().setConsoleLogLevel(consoleLogLevel) else: consoleLogLevel = ConfigurationManager.getInstance( ).getConsoleLogLevelFromEnvVar() if consoleLogLevel: LoggingManager.getInstance().setConsoleLogLevel( consoleLogLevel) # Check session cache. configManager = ConfigurationManager.getInstance() try: self.checkSessionCache() except Exception as ex: self.logger.warn('Disabling session cache: %s' % ex) configManager.setSessionCacheFile(None) return (self.options, self.args)
def __init__(self): if CdbPortalUrlUtility.__instance: raise CdbPortalUrlUtility.__instance CdbPortalUrlUtility.__instance = self self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) cm = ConfigurationManager.getInstance() cm.setOptionsFromConfigFile(CdbPortalUrlUtility.CONFIG_SECTION_NAME, CdbPortalUrlUtility.CONFIG_OPTION_NAME_LIST) self.portalWebAddress = cm.getPortalWebAddress()
def __init__(self): self.sessionCookie = None self.host = None self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) self.username = '' self.password = '' self.urlOpener = None cm = ConfigurationManager.getInstance() self.sessionCacheFile = cm.getSessionCacheFile() self.requireSessionCredentials = cm.getRequireSessionCredentials()
def checkAuthorization(cls, *args, **kwargs): """ A tool that looks in config for 'auth.require'. If found and it is not None, a login is required and the entry is evaluated as a list of conditions that the user must fulfill. """ logger = LoggingManager.getInstance().getLogger( 'LoginController:checkAuthorization') conditions = cherrypy.request.config.get('auth.require', None) #logger.debug('Headers: %s' % (cherrypy.request.headers)) #logger.debug('Request params: %s' % (cherrypy.request.params)) #logger.debug('Request query string: %s' % (cherrypy.request.query_string)) method = urllib.parse.quote(cherrypy.request.request_line.split()[0]) params = urllib.parse.quote(cherrypy.request.request_line.split()[1]) if conditions is None: logger.debug('No conditions imposed') return sessionId = cherrypy.serving.session.id sessionCache = cherrypy.session.cache #logger.debug('Session: %s' % ((cherrypy.session.__dict__))) #logger.debug('Session cache length: %s' % (len(sessionCache))) #logger.debug('Session cache: %s' % (sessionCache)) # Check session. if sessionId not in sessionCache: errorMsg = 'Invalid or expired session id: %s.' % sessionId logger.debug(errorMsg) raise CdbHttpError(cdbHttpStatus.CDB_HTTP_UNAUTHORIZED, 'User Not Authorized', InvalidSession(errorMsg)) username = cherrypy.session.get(LoginController.SESSION_USERNAME_KEY) logger.debug('Session id %s is valid (username: %s)' % (sessionId, username)) if username: cherrypy.request.login = username for condition in conditions: # A condition is just a callable that returns true or false if not condition(): logger.debug( 'Authorization check %s failed for username %s' % (condition.__name__, username)) errorMsg = 'Authorization check %s failed for user %s.' % ( condition.__name__, username) raise CdbHttpError(cdbHttpStatus.CDB_HTTP_UNAUTHORIZED, 'User Not Authorized', AuthorizationError(errorMsg)) else: logger.debug('Username is not supplied') raise CdbHttpError(cdbHttpStatus.CDB_HTTP_UNAUTHORIZED, 'User Not Authorized', ex)
def __init__(self): if StorageUtility.__instance: raise StorageUtility.__instance StorageUtility.__instance = self self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) cm = ConfigurationManager.getInstance() cm.setOptionsFromConfigFile(self.CONFIG_SECTION_NAME, self.CONFIG_OPTION_NAME_LIST) self.logAttachmentStoragePath = cm.getLogAttachmentPath() self.storageDirectory = cm.getStorageDirectory()
def __init__(self): CdbModuleManager.__instanceLock.acquire() try: if CdbModuleManager.__instance: raise CdbModuleManager.__instance CdbModuleManager.__instance = self from cdb.common.utility.loggingManager import LoggingManager self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) self.lock = threading.RLock() self.moduleList = [] self.modulesRunning = False finally: CdbModuleManager.__instanceLock.release()
def __init__(self): if StorageUtility.__instance: raise StorageUtility.__instance StorageUtility.__instance = self self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) cm = ConfigurationManager.getInstance() cm.setOptionsFromConfigFile(self.CONFIG_SECTION_NAME, self.CONFIG_OPTION_NAME_LIST) self.logAttachmentStoragePath = cm.getLogAttachmentPath() self.propertyImageStoragePath = cm.getPropertyImagePath() self.storageDirectory = cm.getStorageDirectory() self.installDirectory = cm.getCdbInstallationDirectory() self.galleryUpdateScript = '%s/%s' % (self.installDirectory, self.GALLERY_UPDATE_SCRIPT_PATH)
def __init__(self): DbManager.__lock.acquire() try: if DbManager.__instance is not None: raise DbManager.__instance DbManager.__instance = self self.lock = threading.RLock() self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) cm = ConfigurationManager.getInstance() cm.setOptionsFromConfigFile(DbManager.CONFIG_SECTION_NAME, DbManager.CONFIG_OPTION_NAME_LIST) dbUser = cm.getDbUser() self.logger.debug('Using DB user: %s' % dbUser) dbSchema = cm.getDbSchema() dbPasswordFile = cm.getDbPasswordFile() self.logger.debug('Using DB password file: %s' % dbPasswordFile) dbPassword = open(dbPasswordFile, 'r').readline().strip() dbPort = cm.getDbPort() dbHost = cm.getDbHost() db = cm.getDb() self.logger.debug('DB schema: %s' % dbSchema) self.logger.debug('DB password file: %s' % dbPasswordFile) engineUrl = '%s://%s:%s@%s:%s/%s' % (db, dbUser, dbPassword, dbHost, dbPort, dbSchema) #self.logger.debug('Using engine URL: %s' % engineUrl) self.engine = sqlalchemy.create_engine( engineUrl, pool_size=DbManager.DB_CONNECTION_POOL_SIZE, max_overflow=DbManager.DB_CONNECTION_POOL_MAX_OVERFLOW, pool_recycle=DbManager.DB_CONNECTION_POOL_RECYCYLE_TIME, echo=DbManager.DB_CONNECTION_LOGGING_FLAG, pool_timeout=DbManager.DB_CONNECTION_POOL_TIMEOUT) self.metadata = sqlalchemy.MetaData(engineUrl) self.logger.debug('Mapping DB tables') for (dbTableName, (dbEntityClass, dbRelationDict)) in cdbDbEntityMap.CDB_DB_ENTITY_MAP.items(): self.mapTable(dbEntityClass, dbTableName, dbRelationDict) self.logger.debug('Initialized SQLalchemy engines') finally: DbManager.__lock.release()
def __init__(self): if CdbEmailUtility.__instance: raise CdbEmailUtility.__instance CdbEmailUtility.__instance = self self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) cm = ConfigurationManager.getInstance() cm.setOptionsFromConfigFile(CdbEmailUtility.CONFIG_SECTION_NAME, CdbEmailUtility.CONFIG_OPTION_NAME_LIST) self.mode = cm.getEmailMode() self.__verifyValidMode(self.mode) self.logger.debug("Running in '%s' mode." % self.mode) self.senderEmail = cm.getEmailSenderEmail() self.adminNotificationEmail = cm.getEmailAdminNotificationEmail() self.cdbSubjectStart = cm.getEmailSubject() self.smtpServer = cm.getEmailSmtpServer()
def setupRoutes(cls): """ Setup RESTFul routes. """ logger = LoggingManager.getInstance().getLogger(cls.__name__) contextRoot = ConfigurationManager.getInstance().getContextRoot() logger.debug('Using context root: %s' % contextRoot) # Get routes. routes = LoginRouteDescriptor.getRoutes( ) + UserRouteDescriptor.getRoutes() routes += CdbPluginRouteMapper.getPluginRoutes() routes += ItemRouteDescriptor.getRoutes() # Add routes to dispatcher. d = cherrypy.dispatch.RoutesDispatcher() for route in routes: logger.debug('Connecting route: %s' % route) d.connect(route['name'], route['path'], action=route['action'], controller=route['controller'], conditions=dict(method=route['method'])) return d
def __init__(self): self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__)
def __init__(self): self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) self.dbManager = DbManager.getInstance() self.adminGroupName = None
def getLogger(cls): logger = LoggingManager.getInstance().getLogger(cls.__name__) return logger
def __init__(self, username=None, password=None): self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__)
def __init__(self, signal, oldSignalHandler): self.signal = signal self.oldSignalHandler = oldSignalHandler self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__)
def setFileLogLevel(self, level): try: # We need to override the logger levels and the handler intLevel = self.getIntLogLevel(level.upper()) for handler in self.fileHandlerList: handler.setLevel(intLevel) self.maxIntLevel = intLevel self.logger.setLevel(intLevel) except Exception, ex: raise ConfigurationError(exception=ex) ####################################################################### # Testing. if __name__ == '__main__': lm = LoggingManager.getInstance() logger = lm.getLogger('Main') logger.error('Error In Main') logger.debug('Debug In Main') logger.warn('Warn In Main') logger.info('Info In Main') logger = lm.getLogger('Main') logger.info('Info In Main') logger = lm.getLogger('') logger.info('Info using root logger') logger = lm.getLogger('Main.2') logger.info('Info in Main.2') lm.setConsoleLogLevel('info') logger.debug('You should not see this message') lm.setConsoleLogLevel('debug') logger.debug('Debug in Main.2')
def getInstance(cls): from cdb.common.utility.loggingManager import LoggingManager try: lm = LoggingManager() except LoggingManager, ex: lm = ex
class CdbCli(object): """ Base cdb command line interface class. """ DEFAULT_SESSION_CACHE_FILE = OsUtility.getUserHomeDir() + '/.cdb/.session.cache' ANY_NUMBER_OF_POSITIONAL_ARGS = 10000000 def __init__(self, validArgCount=0): self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__) # Do not log into a file for CLIs LoggingManager.getInstance().setFileLogLevel('CRITICAL') LoggingManager.getInstance().setConsoleLogLevel('CRITICAL') self.parser = CdbOptionParser() self.options = {} self.args = [] self.validArgCount = validArgCount self.optionGroupDict = {} commonGroup = 'Common Options' self.addOptionGroup(commonGroup, None) self.addOptionToGroup(commonGroup, '-h', '--help', action='help', help='Show this help message and exit.') self.addOptionToGroup(commonGroup, '-?', '', action='help', help='Show this help message and exit.') self.addOptionToGroup(commonGroup, '-v', '--version', action='store_true', dest='version', default=False, help='Print version and exit.') self.addOptionToGroup(commonGroup, '-d', '--debug', dest='consoleLogLevel', help='Set debug level (valid values: CRITICAL, ERROR, WARNING, INFO, DEBUG). Console log level can also be set via CDB_CONSOLE_LOG_LEVEL environment variable,') self.addOptionToGroup(commonGroup, '', '--display-format', dest='displayFormat', default=CdbObject.TEXT_DISPLAY_FORMAT, help='Display format for output objects. Possible options are: %s, %s, and %s (default: %s).' % (CdbObject.TEXT_DISPLAY_FORMAT, CdbObject.DICT_DISPLAY_FORMAT, CdbObject.JSON_DISPLAY_FORMAT, CdbObject.TEXT_DISPLAY_FORMAT)) self.addOptionToGroup(commonGroup, '', '--display-keys', dest='displayKeys', default=CdbObject.DEFAULT_KEYS, help='List of output object keys to display. Possible options are: %s, %s, and string containing comma-separated keys (default: %s, represents class default keys).' % (CdbObject.DEFAULT_KEYS, CdbObject.ALL_KEYS, CdbObject.DEFAULT_KEYS)) def getDefaultServiceHost(self): return ConfigurationManager.getInstance().getServiceHost() def getDefaultServicePort(self): return ConfigurationManager.getInstance().getServicePort() def getDefaultServiceProtocol(self): return ConfigurationManager.getInstance().getServiceProtocol() def getUsername(self): return None def getPassword(self): return None def getDisplayFormat(self): return self.options.displayFormat def getDisplayKeys(self): return self.options.displayKeys def getLogger(self): return self.logger def getParser(self): return self.parser def addOption(self, *args, **kwargs): self.parser.add_option(*args, **kwargs) def addOptionToGroup(self, groupName, *args, **kwargs): """ Add group option. Group must be created using addOptionGroup(). """ group = self.optionGroupDict.get(groupName) group.add_option(*args, **kwargs) def addOptionGroup(self, groupName, desc): group = OptionGroup(self.parser, groupName, desc) self.parser.add_option_group(group) self.optionGroupDict[groupName] = group def processArgs(self): pass def parseArgs(self, usage=None): if usage: self.parser.usage = usage try: (self.options, self.args) = self.parser.parse_args() self.processArgs() except SystemExit, rc: sys.stdout.flush() sys.stderr.flush() sys.exit(int(str(rc))) if self.validArgCount < len(self.args): # Positional args are not enabled and we have some msg = 'Invalid positional argument(s):' for arg in self.args[self.validArgCount:]: msg += ' ' + arg msg += ' (This command allows %s positional arguments.)' % self.validArgCount raise InvalidArgument(msg) optDict = self.options.__dict__ if optDict.get('version'): print 'CDB Software Version: %s' % (cdb.__version__) sys.exit(0) # Logging level. First try from command line, then from env variable. consoleLogLevel = optDict.get('consoleLogLevel', None) if consoleLogLevel: LoggingManager.getInstance().setConsoleLogLevel(consoleLogLevel) else: consoleLogLevel = ConfigurationManager.getInstance().getConsoleLogLevelFromEnvVar() if consoleLogLevel: LoggingManager.getInstance().setConsoleLogLevel(consoleLogLevel) # Check session cache. configManager = ConfigurationManager.getInstance() try: self.checkSessionCache() except Exception, ex: self.logger.warn('Disabling session cache: %s' % ex) configManager.setSessionCacheFile(None)
def __init__(self): self.logger = LoggingManager.getInstance().getLogger( self.__class__.__name__) self.lock = threading.RLock()