def reloadConfig(self): """ Called after a SIGHUP to reload config from db. """ stdMsg('reloading configuration') libCache = lib.cache.Cache() libCache.get('dbPriceList')._load() # Reload crash report skip list libCache.get('modCrash')._getSkipList()
def __init__(self, *args): SimpleJSONRPCServer.__init__(self, addr=(args[0], args[1]), requestHandler=JsonRpcDispatch, logRequests=0) self.oParent = args[2] stdMsg('Started JSON-RPC Server')
def _dispatch(self, sRequest, rgoParam, sRemoteIP): # Okay, let's check our subclasses try: libCache = lib.cache.Cache() # Parse Request sModule, sMethod = self._parseRequest(sRequest) oModule = libCache.get(sModule) if oModule is None: raise AttributeError # Make sure this method isn't private if sMethod[0:1] == '_': stdMsg('cannot call private method "%s"' % sRequest) raise Exception, 'cannot call private method "%s"' % sRequest # If this is not a function that can be called by Everyone, check session and proceed if not self._checkEverybody(sModule, sMethod): # Discard any attempt to proceed further without a session or magic if len(rgoParam) == 0: return (True, 'noauth') sSessID, rgoParam = self._getSession(rgoParam) if self._checkPrivate(sModule, sMethod): return (True, 'noauth') elif sSessID == MAGIC and self._checkMagic(sModule, sMethod): pass # Okay, just check the session elif not libCache.get('dbSessionList').checkExists( sSessID=sSessID): return (True, 'noauth') # Inject Remote IP into UpdateIP call if sModule == 'modStats' and (sMethod == 'updateIP' or sMethod == 'newserver'): rgoParam = list(rgoParam) rgoParam.insert(1, sRemoteIP) rgoParam = tuple(rgoParam) # Alright, let's call it oFunc = getattr(oModule, sMethod) # Skip logging these functions since they are chatty rgsSkipLog = [ 'stats.updateIP', 'stats.cameraFail', 'stats.isAlive' ] except AttributeError, e: errMsg(e) # Oops it wasn't found, throw an error sMsg = 'method "%s" is not supported' % sRequest stdMsg(sMsg) return (False, 'method "%s" is not supported' % sRequest)
def savePID(): # Save pidfile try: pid = os.getpid() if os.access(PIDFILE, os.F_OK): os.unlink(PIDFILE) f = open(PIDFILE, 'w') f.write('%d' % pid) f.close() except IOError: stdMsg('could not create pid file')
def updateDVSLog(self, bSerial=None, bEventID=None, sData=None): """ Update the Log """ try: if sData == 'None': sData = None if bEventID == 'None': bEventID = None try: bSerial = int(bSerial) except ValueError: raise Exception('bad serial') try: if bEventID is not None: bEventID = int(bEventID) except ValueError: raise Exception('bad event id') # See if we should skip anything for this serial oMatchTest = re.compile( '.*test.*') # Regex to skip any "test" systems oServer = self._dbServerList.getServer(bSerial=bSerial) if oServer is None: return False # server does not exist? if oServer.checkHasSkip(): return False if oMatchTest.match(oServer.getCategories()): return False if oServer.getMaintenance() == 'no': return False sTimeStamp = time.time() self._dbDVSLogList.addDVSLogEntry(bSerial, bEventID, sData, sTimeStamp) #=================================================================== # oDVSLogEntry.setSerial(bSerial) # oDVSLogEntry.setEventID(bEventID) # oDVSLogEntry.setData(sData) # oDVSLogEntry.setTimestamp( time.time() ) # # self._dbDVSLogList.setDVSLogEntry( oDVSLogEntry ) #=================================================================== stdMsg('DVSLog Entry added for serial-[%3s] event-[%s]' % (bSerial, bEventID)) return True except Exception, e: errMsg('error updating DVSLog [%s]' % e) return False
def __init__(self, *args): # Since SimpleXMLRPCServer extends SocketServer.TCPServer we can set the TCPServer.address_family property to socket.AF_INET6 for IPV6 support # if this fails (i.e. because the machine does not support IPV6) we fallback on IPV4 # NOTE: AF_INET6 is supposed to support both IPV6, and V4 try: self.address_family = socket.AF_INET6 SimpleXMLRPCServer.__init__(self, addr=(args[0], args[1]), requestHandler=XmlRpcDispatch, logRequests=0) except: self.address_family = socket.AF_INET SimpleXMLRPCServer.__init__(self, addr=(args[0], args[1]), requestHandler=XmlRpcDispatch, logRequests=0) self.oParent = args[2] stdMsg('Started XML-RPC Server')
def upgradedvs( self, bSerial, sOS, sVersion ): """ DEPRECATED Upgrade DVS repo to 3.0. This function is no longer used >=3.1. Instead rda-backend will communicate the OS and DVS version to us periodically like a heartbeat. It will call setupRepo instead. Params are: bSerial, sOS {8.0,co4,co5}, sVersion {dvs-2.0,dvs-2.5,dvs-3.0} """ try: # Parse sVersion and send to setupRepo to check and update if not self.setupRepo( bSerial, sOS, sVersion.split( 'dvs-' )[ 1 ] ): raise Exception, 'could not upgrade dvs' stdMsg( 'serial updated serial-[%d] os-[%s] version-[%s]' % ( bSerial, sOS, sVersion ) ) return True except Exception, e: errMsg( 'error upgrading DVS repo [%s]' % e ) raise Exception, "System error upgrading DVS repo."
def run(self): try: self.fRunning = True self.oServerXML = XmlRpcServer('', SERVER_PORT, self) if ENABLE_JSON: self.oServerJSON = JsonRpcServer('', SERVER_PORT_JSON, self) # Make sure our children do not inherit our socket # Found at http://mail.python.org/pipermail/python-list/2007-July/621680.html if sys.modules.has_key('fcntl'): bFlags = fcntl.fcntl(self.oServerXML.fileno(), fcntl.F_GETFD) fcntl.fcntl(self.oServerXML.fileno(), fcntl.F_SETFD, bFlags | fcntl.FD_CLOEXEC) if ENABLE_JSON: bFlags = fcntl.fcntl(self.oServerJSON.fileno(), fcntl.F_GETFD) fcntl.fcntl(self.oServerJSON.fileno(), fcntl.F_SETFD, bFlags | fcntl.FD_CLOEXEC) while not self.fStop: rgbSocket = [self.oServerXML.socket] if ENABLE_JSON: rgbSocket.append(self.oServerJSON.socket) rgbRead, rgbWrite, rgbErr = select.select(rgbSocket, [], [], 5) if len(rgbRead) > 0: if self.oServerXML.socket in rgbRead: self.oServerXML.handle_request() elif self.oServerJSON is not None and self.oServerJSON.socket in rgbRead: self.oServerJSON.handle_request() self.fRunning = False except Exception, e: self.fRunning = False errMsg('%s encountered an error [%s]' % (self.sName, e)) stdMsg('%s terminating' % self.sName)
def logoutUser(self, sSessID): """ Logout this user session """ try: oSession = self._dbSessionList.getSession(sSessID=sSessID) if oSession is None: # Session does not exist return False sName = self._dbUserList.getUser(bID=oSession.getUser()).getName() fStatus = self._dbSessionList.delUser(oSession.getUser()) if fStatus: self._libUtil.logMsg('logout succeeded [%s]' % sName) stdMsg('logout succeeded [%s]' % sName) else: self._libUtil.logMsg('logout failed [%s]' % sName) stdMsg('logout failed [%s]' % sName) return fStatus except Exception, e: self._libUtil.logMsg('logout failed [%s]' % sName) errMsg('error while attempting to logout user [%s]' % e) raise Exception, "System error while logging out."
def loginUser(self, sName, sPass, fForce=False): """ Validate user and create new session object """ try: oUser = None # Auth against LDAP if USE_LDAP: if not self._authUserLdap(sName, sPass): self._libUtil.logMsg('login failed [%s] [noauth]' % sName) stdMsg('login failed user-[%s] reason-[noauth]' % sName) return 'noauth' # Get User oUser = self._dbUserList.getUser(sName=sName) if oUser is None: if not CACHE_LDAP: # User not found stdMsg( 'login succeeded for user-[%s], but caching disabled ... denied' % sName) return 'noauth' try: # User is missing, but we need to Cache from Ldap lookup oUser = self._dbUserList.addUser( sName, sPass, '%s LDAP import' % sName, 20) # Create default access entry self._dbAccessList.addEntry( oUser.getID(), [self._dbRightList.getValueByName('access')]) stdMsg('new user cached on system from ldap [%s]' % sName) except Exception, e: errMsg('error caching user from ldap [%s]' % e) return 'noauth' # Auth against internal DB only else:
def main(argv): global oXmlRpc, fReady try: os.environ[ 'HOME' ] = '/root' if parseArgs(sys.argv) != 0: return 0 signal.signal(signal.SIGINT, hdlrSignal) signal.signal(signal.SIGTERM, hdlrSignal) signal.signal(signal.SIGHUP, hdlrSignal) # Detect is running if checkIsRunning(): stdMsg('Already running ... aborting') return 0 # Create our server and start listening if optDaemon: # Run as a daemon in the background. if enterDaemonMode('/dev/null','/var/log/monarch-backend.log','/var/log/monarch-backend.log') != 0: errMsg('error with daemon mode') return 1 stdMsg('starting daemon') else: stdMsg('starting') # Save our pid file savePID() # Initialize Threads rgoThread = {} rgoThread['ProcessControl'] = thread_process_control.ThreadProcessControl('ProcessControl') rgoThread['Bugzilla'] = thread_bugzilla.ThreadBugzilla('Bugzilla') rgoThread['CheckSick'] = thread_check_sick.ThreadCheckSick('CheckSick') oXmlRpc = thread_xmlrpc.ThreadXmlRpc('XmlRpc Server', rgoThread) oXmlRpc.start() rgoThread['ProcessControl'].start() rgoThread['Bugzilla'].start() rgoThread['CheckSick'].start() while True: time.sleep(35) fReady = True return 1 except KeyboardInterrupt: stdMsg('terminating') # Stop all threads oXmlRpc.stop() rgoThread['ProcessControl'].stop() rgoThread['Bugzilla'].stop() rgoThread['CheckSick'].stop() bCount = 100 while bCount > 0: if not oXmlRpc.checkIsRunning() and \ not rgoThread['ProcessControl'].checkIsRunning() and \ not rgoThread['Bugzilla'].checkIsRunning() and \ not rgoThread['CheckSick'].checkIsRunning(): break bCount -= 1 time.sleep(0.1) # Do any cleanup here removePID() stdMsg('stopped') return 0 except SystemExit: return 0 except Exception, e: if optDebug: print 'Exception in user code:' print '-'*60 traceback.print_exc(file=sys.stdout) print '-'*60 else: errMsg('caught exception: %s' % e) stdMsg('aborting') removePID() return 1
except Exception, e: errMsg('error caching user from ldap [%s]' % e) return 'noauth' # Auth against internal DB only else: # Get User oUser = self._dbUserList.getUser(sName=sName) if oUser is None: # User not found return 'noauth' # Auth User if not self._authUserInternal(oUser, sPass): self._libUtil.logMsg('login failed [%s] [noauth]' % sName) stdMsg('login failed user-[%s] reason-[noauth]' % sName) return 'noauth' # Get ACL oAccessEntry = self._dbAccessList.getEntry(oUser.getID()) if oAccessEntry is None and oUser.getType() != 0: # No rights to this server return 'noauth' bAccessValue = self._dbRightList.getValueByName('access') # Make sure this user has "access" rights to this system if oUser.getType( ) != 0 and not bAccessValue in oAccessEntry.getRights(): self._libUtil.logMsg('login failed [%s] [noaccess]' % sName) stdMsg('login failed user-[%s] reason-[noaccess]' % sName)
def cameraFail( self, bSerial, rgbCamera, sStatus='' ): """ Server is notifying us that a camera is failed or restored. """ try: try: bSerial = int( bSerial ) except: return False if not isinstance( rgbCamera, list ): # Old style. This should be removed after dvs-3.0 comes out and we have time to upgrade all systems. #dbgMsg( 'camera failure reported using old style' ) bCamera = rgbCamera try: bCamera = int( bCamera ) except: return False oCamera = self._dbCameraList.getCamera( bSerial, bCamera ) if oCamera is None: if sStatus == 'fail': self._dbCameraList.addCamera( bSerial, bCamera ) stdMsg( 'camera failed serial-[%d] camera-[%d]' % ( bSerial, bCamera ) ) return True if oCamera is not None: if sStatus == 'restore': # Make sure it is not marked as permenantly failed if not oCamera.checkHasSkip(): self._dbCameraList.delCamera( oCamera ) stdMsg( 'camera restored serial-[%d] camera-[%d]' % ( bSerial, bCamera ) ) return True return False else: # New style. We are only receiving a camera array of failed cameras, all others are expected to be restored. #dbgMsg( 'camera failure reported using new style' ) # Delete all cameras that were not sent to us since they have been restored for ixCam in range( 1, 65 ): if ixCam in rgbCamera: continue oCamera = self._dbCameraList.getCamera( bSerial, ixCam ) if oCamera is None: continue if oCamera.checkHasSkip(): continue self._dbCameraList.delCamera( oCamera ) stdMsg( 'camera restored serial-[%d] camera-[%d]' % ( bSerial, ixCam ) ) # Now insert new failed cameras into database for bCamera in rgbCamera: oCamera = self._dbCameraList.getCamera( bSerial, bCamera ) if oCamera is not None: continue self._dbCameraList.addCamera( bSerial, bCamera ) stdMsg( 'camera failed serial-[%d] camera-[%d]' % ( bSerial, bCamera ) ) return True except Exception, e: errMsg( 'error updating camera fail info [%s]' % e ) raise Exception, "System error updating camera fail info."
def newserver( self, bSerial, sIP, sName, sUrl, sVersion='2.5' ): """ Register new Server in our Database. """ try: # Block all access to adding new servers unless they are within our network #rgsIP = sIP.split( '.' ) #if not ( rgsIP[ 0 ] == '192' and rgsIP[ 1 ] == '168' and rgsIP[ 2 ] == '0' ): rgsDividia = [ '24.178.194.110', '75.93.27.4', '47.51.193.54' ] if sIP not in rgsDividia: dbgMsg( 'blocking newserver call from outside Dividia network [%s]' % sIP ) return False bSerial = int( bSerial ) # Block any >= 4000 serial since these are currently used as development systems and shouldn't be in the DB if bSerial >= 4000: dbgMsg( 'blocking newserver call for development serial >= 4000 [%s]' % sIP ) return False oServer = self._dbServerList.getServer( bSerial=bSerial ) if oServer is not None: dbgMsg( 'server [%s] already exists' % bSerial ) return False oServer = self._dbServerList.addServer( bSerial ) oServer.setName( sName ) oServer.setCategories( sUrl ) oServer.setPreferred( sUrl ) oServer.setIP( '127.0.0.1' ) oServer.setRemoteIP( '127.0.0.1' ) oServer.setVersion( sVersion ) self._dbServerList.setServer( oServer ) try: self._oLock.acquire() try: # Add this serial to our Bugzilla installation for Support rgoResult = self._libDBbug.query( 'SELECT id FROM products WHERE name=%s', 'Support' ) if rgoResult is not None: bProduct = int( rgoResult[ 0 ][ 0 ] ) sSerial = '%03d' % bSerial # Loop here to make sure it gets added for ix in range( 0, 5 ): self._libDBbug.query( 'INSERT INTO versions (value, product_id) VALUES (%s, %s)', sSerial, bProduct ) rgoResult = self._libDBbug.query( 'SELECT value FROM versions WHERE product_id=%s AND value=%s', bProduct, sSerial ) if rgoResult is not None and len( rgoResult ) > 0: stdMsg( 'added new serial to Bugzilla [%s]' % sSerial ) break except Exception, e: errMsg( 'error adding serial to Bugzilla' ) errMsg( e ) finally: self._oLock.release() stdMsg( 'new serial added to system serial-[%d] name-[%s] url-[%s] version-[%s]' % ( bSerial, sName, sUrl, sVersion ) ) return True except Exception, e: errMsg( 'error registering new server [%s]' % e ) raise Exception, "System error registering new server."