def run(self): """ On run """ rsp = ESI.instance().interact( body = self.bodyReq, timeout=self.timeout ) _data_ = {'cmd': Messages.CMD_INTERACT } if rsp is None: _data_['rsp'] = None else: _data_['rsp'] = rsp['body'] instance().ok( self.client, self.tid, body = _data_ )
def addResultScript(self, scriptResult, scriptUser, scriptDuration, scriptProject): """ Add script result to db @param scriptResult: COMPLETE, ERROR, KILLED, etc... @type scriptResult: str @param scriptUser: username @type scriptUser: str @param scriptDuration: duration @type scriptDuration: str """ if not Settings.getInt('MySql', 'insert-test-statistics'): return self.trace('add result sc %s' % scriptResult) try: strDate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) # ret, rows = DbManager.instance().querySQL( query= "INSERT INTO `%s` (date, result, user_id, duration, project_id) VALUES ('%s','%s',%s,'%s','%s')" % (self.dbt_scripts, strDate, scriptResult, scriptUser, scriptDuration, scriptProject)) if not ret: raise Exception("failed to add result script in db") except Exception as e: self.error(e) else: if self.notifyUsers: data = ('stats', (None, self.getStats())) ESI.instance().notifyByUserTypes(body=data, admin=True, leader=True, tester=False, developer=False)
def onDisconnection (self, client): """ Reimplemented from ServerAgent @type client: @param client: """ self.trace("on disconnection" ) NetLayerLib.ServerAgent.onDisconnection(self, client) clientRegistered = self.agentsRegistered.items() for k, c in clientRegistered: if c['address'] == client.client_address: ret = self.agentsRegistered.pop(k) publicip = self.agentsPublicIp.pop(c['address']) del publicip self.info( 'Agent unregistered: Name="%s"' % k ) notif = ( 'agents', ( 'del', self.getAgents() ) ) ESI.instance().notifyByUserTypes(body = notif, admin=True, monitor=False, tester=True) del ret break
def zipData(self, dirToday, dirTest, destPathZip, replayId, projectId=0, virtualName = ""): """ Zip data by adapters and notify users @param dirToday: date YYYY-MM-DD @type dirToday: string @param dirTest: @type dirTest: string @param destPathZip: @type destPathZip: string @param replayId: @type replayId: string """ ret = False try: mainDir = "%s/%s/%s" % (self.adpDataPath, projectId, dirToday) testDir = "%s/%s" % (mainDir, dirTest) for x in os.listdir(testDir): # is directory if not os.path.isfile( '%s/%s' % (testDir,x) ): pathTozip = '%s/%s' % (testDir,x) # empty folders are not zipped if len( os.listdir(pathTozip) ) == 0: continue # prepare the file name tp = time.strftime( "%Y-%m-%d_%H-%M-%S", time.localtime(time.time()) ) \ + ".%3.3d" % int((time.time() * 1000) % 1000 ) fileName = "%s_%s_%s_%s" % (self.prefixAdapters, x, tp , replayId) # zip the folder zipped = self.toZip( file=pathTozip, filename="%s/%s.zip" % (destPathZip, fileName), extToInclude = [], # include all files keepTree=False ) if zipped == self.context.CODE_OK: # notify users if Settings.getInt( 'Notifications', 'archives'): size_ = os.path.getsize( "%s/%s.zip" % (destPathZip, fileName) ) notif = {} m = [ { "type": "folder", "name": dirToday, "project": "%s" % projectId, "content": [ { "type": "folder", "name": dirTest, "project": "%s" % projectId, "virtual-name": virtualName, "content": [ {"type": "file", "name": "%s.zip" % fileName, 'size': str(size_), "project": "%s" % projectId } ]} ] } ] notif['archive'] = m data = ( 'archive', ( None, notif) ) ESI.instance().notifyByUserTypes(body = data, admin=True, leader=False, tester=True, developer=False) else: self.error( 'error to zip data adapters' ) ret = False break ret = True except Exception as e: self.error( 'unable to zip data: %s' % str(e) ) return ret
def notifyUpdate(self): """ """ # update context and rns of all connected users data = ( 'context-server', ( 'update', self.context.getInformations() ) ) ESI.instance().notifyAll(body = data)
def onRegistration(self, client, tid, request): """ Called on the registration of a new agents @param client: @type client: @param tid: @type tid: @param request: @type request: """ self.trace("on registration") self.__mutex.acquire() doNotify = False if request['userid'] in self.agentsRegistered: self.info('duplicate agents registration: %s' % request['userid']) NetLayerLib.ServerAgent.failed(self, client, tid) else: if not ('type' in request['body']): self.error('type missing in request: %s' % request['body']) NetLayerLib.ServerAgent.failed(self, client, tid) else: if request['body']['type'] != ClientAgent.TYPE_AGENT_AGENT: self.error('agent type refused: %s' % request['body']['type']) NetLayerLib.ServerAgent.forbidden(self, client, tid) else: tpl = { 'address': client, 'version': request['body']['version'], 'description': request['body']['description']['details'], 'auto-startup': request['body']['description']['default'], 'type': request['body']['name'], 'start-at': request['body']['start-at'], 'publicip': self.agentsPublicIp[client] } if not Settings.getInt('WebServices', 'remote-agents-enabled'): if not tpl['auto-startup']: self.info( 'remote agents registration not authorized') NetLayerLib.ServerAgent.forbidden( self, client, tid) else: self.agentsRegistered[request['userid']] = tpl NetLayerLib.ServerAgent.ok(self, client, tid) self.trace('Local agent registered: Name="%s"' % request['userid']) doNotify = True else: self.agentsRegistered[request['userid']] = tpl NetLayerLib.ServerAgent.ok(self, client, tid) self.info('Remote agent registered: Name="%s"' % request['userid']) doNotify = True if doNotify: # Notify all connected users notif = ('agents', ('add', self.getAgents())) ESI.instance().notifyByUserTypes(body=notif, admin=True, leader=False, tester=True, developer=False) self.__mutex.release()
ret = self.context.CODE_FORBIDDEN else: # add the section in the config file object self.configsFile.add_section(aName) self.configsFile.set( aName, 'enable', 1) self.configsFile.set( aName, 'type', aType) self.configsFile.set( aName, 'description', aDescr) # write date the file f = open( "%s/agents.ini" % Settings.getDirExec() , 'w') self.configsFile.write(f) f.close() # notify all admin and tester notif = ( 'agents-default', ( 'add', self.getDefaultAgents() ) ) ESI.instance().notifyByUserTypes(body = notif, admin=True, leader=False, tester=True, developer=False) # return OK ret = self.context.CODE_OK ======= ======= >>>>>>> upstream1/master # add the section in the config file object self.configsFile.add_section(aName) self.configsFile.set( aName, 'enable', 1) self.configsFile.set( aName, 'type', aType) self.configsFile.set( aName, 'description', aDescr) # write date the file f = open( "%s/agents.ini" % Settings.getDirExec() , 'w') self.configsFile.write(f)
def onRequest(self, client, tid, request): """ Reimplemented from ServerAgent Called on incoming request @param client: @type client: @param tid: @type tid: @param request: @type request: """ self.__mutex__.acquire() try: _body_ = request['body'] if client not in self.testsConnected: self.__mutex__.release() return self.testsConnected[client]['task-id'] = _body_['task-id'] # handle notify and save some statistics on the database if request['cmd'] == Messages.RSQ_NOTIFY: try: if _body_['event'] in [ 'agent-data', 'agent-notify', 'agent-init', 'agent-reset', 'agent-alive', 'agent-ready' ]: if _body_['event'] == 'agent-ready': self.testsConnected[client]['agents'].append( { 'agent-name': _body_['destination-agent'] , 'script-id':_body_['script_id'], 'uuid':_body_['uuid'], 'source-adapter':_body_['source-adapter'] } ) ASI.instance().notifyAgent( client, tid, data=_body_ ) except Exception as e: self.error('unable to handle notify for agent: %s' % e) if _body_['event'] == 'testcase-stopped': self.statsmgr.addResultTestCase( testResult =_body_['result'], fromUser=_body_['user-id'], testDuration=_body_['duration'], testProject=_body_['prj-id'] ) # reset agents self.resetRunningAgent( client=self.testsConnected[client] ) if _body_['event'] == 'testabstract-stopped': self.statsmgr.addResultTestAbstract( taResult =_body_['result'], fromUser=_body_['user-id'], taDuration=_body_['duration'], nbTc=_body_['nb-tc'], prjId=_body_['prj-id'] ) if _body_['event'] == 'testunit-stopped': self.statsmgr.addResultTestUnit( tuResult =_body_['result'], fromUser=_body_['user-id'], tuDuration=_body_['duration'], nbTc=_body_['nb-tc'], prjId=_body_['prj-id'] ) if _body_['event'] == 'testsuite-stopped': self.statsmgr.addResultTestSuite( tsResult =_body_['result'], fromUser=_body_['user-id'], tsDuration=_body_['duration'], nbTc=_body_['nb-tc'], prjId=_body_['prj-id'] ) if _body_['event'] == 'testglobal-stopped': self.statsmgr.addResultTestGlobal( tgResult =_body_['result'], fromUser=_body_['user-id'], tgDuration=_body_['duration'], nbTs=_body_['nb-ts'], nbTu=_body_['nb-tu'], nbTc=_body_['nb-tc'], prjId=_body_['prj-id'] ) if _body_['event'] == 'testplan-stopped': self.statsmgr.addResultTestPlan( tpResult =_body_['result'], fromUser=_body_['user-id'], tpDuration=_body_['duration'], nbTs=_body_['nb-ts'], nbTu=_body_['nb-tu'], nbTc=_body_['nb-tc'], prjId=_body_['prj-id'] ) if _body_['task-id'] in self.tests: if not self.tests[ _body_['task-id'] ]: # check connected time of the associated user and test # if connected-at of the user > connected-at of the test then not necessary to send events userFounded = self.context.getUser( login=_body_['from'] ) if userFounded is not None : if not client in self.testsConnected: self.error( 'unknown test from %s' % str(client) ) else: if userFounded['connected-at'] < self.testsConnected[client]['connected-at']: if _body_['channel-id']: ESI.instance().notify( body = ( 'event', _body_ ), toAddress=_body_['channel-id'] ) else: ESI.instance().notify( body = ( 'event', _body_ ) ) else: self.error( 'test unknown: %s' % _body_['task-id'] ) if _body_['event'] == 'script-stopped': # reset probes self.resetRunningProbe( client=self.testsConnected[client] ) # reset agents self.resetRunningAgent( client=self.testsConnected[client] ) if _body_['task-id'] in self.tests: self.tests.pop( _body_['task-id'] ) else: self.error( 'task-id unknown: %s' % _body_['task-id'] ) if client in self.testsConnected: self.testsConnected.pop( client ) else: self.error( 'test unknown: %s' % str(client) ) # handle requests elif request['cmd'] == Messages.RSQ_CMD: #_body_ = request['body'] self.trace("cmd received: %s" % _body_['cmd']) if 'cmd' in _body_: # handle get probe command if _body_['cmd'] == Messages.CMD_GET_PROBE: self.trace( 'getting probe %s' % _body_['name'] ) _data_ = {'cmd': Messages.CMD_GET_PROBE } _data_['res'] = PSI.instance().findProbe( _body_ ) NetLayerLib.ServerAgent.ok( self, client, tid, body = _data_ ) # handle start probe command elif _body_['cmd'] == Messages.CMD_START_PROBE: self.trace( 'starting probe %s' % _body_['name'] ) _data_ = {'cmd': Messages.CMD_START_PROBE } _data_['res'] = PSI.instance().startProbe( _body_ ) # memorize the probes used on the test tpl = _body_ tpl.update( {'callid': _data_['res']['callid'] } ) self.testsConnected[client]['probes'].append( tpl ) NetLayerLib.ServerAgent.ok( self, client, tid, body = _data_, ) # handle stop probe command elif _body_['cmd'] == Messages.CMD_STOP_PROBE: self.trace( 'stopping probe %s' % _body_['name'] ) _data_ = {'cmd': Messages.CMD_STOP_PROBE } _data_['res'] = PSI.instance().stopProbe( _body_ ) NetLayerLib.ServerAgent.ok( self, client, tid, body = _data_ ) # handle interact command elif _body_['cmd'] == Messages.CMD_INTERACT: self.trace( 'interact called' ) if _body_['task-id'] in self.tests: if not self.tests[ _body_['task-id'] ]: # check connected time of the associated user and test # if connected-at of the user > connected-at of the test then not necessary to send events userFounded = self.context.getUser( login=_body_['from'] ) if userFounded is not None : if not client in self.testsConnected: self.error( 'unknown test from %s' % str(client) ) else: if userFounded['connected-at'] < self.testsConnected[client]['connected-at']: self.__fifoThread.putItem(lambda: self.onInteract(client, tid, bodyReq=_body_, timeout=_body_['timeout'] )) else: self.error( 'test unknown: %s' % _body_['task-id'] ) else: self.error( 'cmd unknown %s' % _body_['cmd']) rsp = {'cmd': _body_['cmd'], 'res': Messages.CMD_ERROR } NetLayerLib.ServerAgent.failed(self, client, tid, body = rsp ) else: self.error( 'cmd is missing') # handle other request else: self.trace('%s received ' % request['cmd']) except Exception as e: self.error( "unable to handle incoming request: %s" % e ) self.__mutex__.release()
def zipDataV2(self, dirToday, dirTest, destPathZip, replayId, projectId=0, virtualName=""): """ Zip data by adapters and notify users in just one zip @param dirToday: date YYYY-MM-DD @type dirToday: string @param dirTest: @type dirTest: string @param destPathZip: @type destPathZip: string @param replayId: @type replayId: string """ self.trace("Starting to zip all adapters logs") ret = False try: mainDir = "%s/%s/%s" % (self.adpDataPath, projectId, dirToday) testDir = "%s/%s" % (mainDir, dirTest) # prepare the file name tp = time.strftime( "%Y-%m-%d_%H-%M-%S", time.localtime(time.time()) ) \ + ".%3.3d" % int((time.time() * 1000) % 1000 ) fileName = "%s_%s_%s" % (self.prefixAdaptersAll, tp, replayId) # zip the folder zipped = self.zipFolder(folderPath=testDir, zipName="%s.zip" % fileName, zipPath=destPathZip) if zipped == self.context.CODE_OK: # notify users if Settings.getInt('Notifications', 'archives'): size_ = os.path.getsize("%s/%s.zip" % (destPathZip, fileName)) notif = {} m = [{ "type": "folder", "name": dirToday, "project": "%s" % projectId, "content": [{ "type": "folder", "name": dirTest, "project": "%s" % projectId, "virtual-name": virtualName, "content": [{ "type": "file", "name": "%s.zip" % fileName, 'size': str(size_), "project": "%s" % projectId }] }] }] notif['archive'] = m data = ('archive', (None, notif)) ESI.instance().notifyByUserTypes(body=data, admin=True, leader=False, tester=True, developer=False) ret = True else: self.error('error to zip data adapters') ret = False except Exception as e: self.error('unable to zip data adapters v2: %s' % str(e)) return ret
def cleanup(self): """ Cleanup the server """ self.info('Cleanup...') self.trace("finalize probes manager") try: ProbesManager.finalize() except Exception: pass self.trace("finalize agent manager") try: AgentsManager.finalize() except Exception: pass self.trace("finalize toolbox manager") try: ToolboxManager.finalize() except Exception: pass self.trace("finalize settings") try: Settings.finalize() except Exception: pass self.trace("finalize context") try: Context.finalize() except Exception: pass self.trace("finalize projects manager") try: ProjectsManager.finalize() except Exception: pass self.trace("finalize users manager") try: UsersManager.finalize() except Exception: pass self.trace("finalize stats manager") try: StatsManager.finalize() except Exception: pass self.trace("finalize task manager") try: TaskManager.finalize() except Exception: pass self.trace("finalize test public manager") try: RepoPublic.finalize() except Exception: pass self.trace("finalize test repo manager") try: RepoTests.finalize() except Exception: pass self.trace("finalize test archives manager") try: RepoArchives.finalize() except Exception: pass self.trace("finalize helper manager") try: HelperManager.finalize() except Exception: pass self.trace("finalize libraries manager") try: RepoLibraries.finalize() except Exception: pass self.trace("finalize adapters manager") try: RepoAdapters.finalize() except Exception: pass self.trace("finalize adapters data storage") try: StorageDataAdapters.finalize() except Exception: pass self.trace("finalize WSU") try: RestServerInterface.instance().stop() RestServerInterface.finalize() except Exception: pass self.trace("finalize ESI") try: EventServerInterface.instance().stopSA() EventServerInterface.finalize() except Exception: pass self.trace("finalize TSI") try: TestServerInterface.instance().stopSA() TestServerInterface.finalize() except Exception: pass self.trace("finalize PSI") try: ProbeServerInterface.instance().stopSA() ProbeServerInterface.finalize() except Exception: pass self.trace("finalize ASI") try: AgentServerInterface.instance().stopSA() AgentServerInterface.finalize() except Exception: pass self.trace("finalize db manager") try: DbManager.finalize() except Exception: pass self.trace("finalize logger, cli") try: CliFunctions.finalize( ) Logger.finalize() except Exception: pass
def initialize (self): """ Starts all modules Exit if the service is alreayd running or if the config file is missing """ starttime = time.time() if self.isrunning(): sys.stdout.write( " (server is already running)" ) sys.exit(1) self.daemonize() try: # Initialize self.info( "Starting up server..." ) self.trace( "** System encoding (in): %s" % sys.stdin.encoding ) self.trace( "** System encoding (out): %s" % sys.stdout.encoding ) self.info("Settings, Logger and CLI ready") DbManager.initialize( ) DbManager.instance().isUp() self.info("DB Managerready") WebServer.isUp() self.info("Web server ready") # Initialize the core Context.initialize() Context.instance().setStartTime() Context.instance().setMysqlVersion() Context.instance().setApacheVersion() Context.instance().setPhpVersion() Context.instance().synchronizeDynamicCfg() if Settings.getInt('Server','use-ifconfig'): Context.instance().listEths() else: Context.instance().listEthsNew() Context.instance().listRoutes() self.info("Context ready") self.deploy() self.info("Symbolic links created") ProjectsManager.initialize(context = Context.instance()) self.info("Projects Manager ready") UsersManager.initialize( context = Context.instance() ) self.info("Users Manager ready") StatsManager.initialize( ) self.info("Stats Manager ready") TaskManager.initialize(statsmgr=StatsManager.instance(), context = Context) self.info("Task Manager ready") # Initialize all repositories RepoTests.initialize( context = Context.instance(), taskmgr = TaskManager.instance() ) self.info("Repo manager for tests ready") RepoArchives.initialize( context = Context.instance(), taskmgr = TaskManager.instance() ) self.info("Repo manager for archives ready") RepoAdapters.initialize( context = Context.instance(), taskmgr = TaskManager.instance() ) StorageDataAdapters.initialize( context = Context.instance() ) self.info("Adapters Manager and Storage Data ready") RepoLibraries.initialize( context = Context.instance(), taskmgr = TaskManager.instance() ) self.info("Libraries adapters manager ready") RepoPublic.initialize() self.info("Repo manager for public area is ready") HelperManager.initialize() self.info("Helper manager ready") ProbesManager.initialize( context = Context.instance() ) self.info("Probes Manager ready") AgentsManager.initialize( context = Context.instance() ) self.info("Agents Manager ready") ToolboxManager.initialize( ) self.info("Toolbox Manager ready") # Initialize all interfaces self.info("Starting ESI on %s:%s" % ( Settings.get('Bind','ip-esi') , Settings.getInt('Bind','port-esi') ) ) EventServerInterface.initialize(listeningAddress = ( Settings.get('Bind','ip-esi') , Settings.getInt('Bind','port-esi') ), sslSupport=Settings.getInt('Client_Channel','channel-ssl'), wsSupport=Settings.getInt('Client_Channel','channel-websocket-support'), context = Context.instance() ) self.info("Starting TSI on %s:%s" % ( Settings.get('Bind','ip-tsi') , Settings.getInt('Bind','port-tsi') ) ) TestServerInterface.initialize(listeningAddress = ( Settings.get('Bind','ip-tsi'), Settings.getInt('Bind','port-tsi') ), statsmgr=StatsManager.instance(), context = Context.instance() ) self.info("Starting RSU on %s:%s" % ( Settings.get('Bind','ip-rsi') , Settings.getInt('Bind','port-rsi') ) ) RestServerInterface.initialize( listeningAddress = ( Settings.get('Bind','ip-rsi'), Settings.getInt('Bind','port-rsi') ) ) self.info("Starting PSI on %s:%s" % ( Settings.get('Bind','ip-psi') , Settings.getInt('Bind','port-psi') ) ) ProbeServerInterface.initialize( listeningAddress = ( Settings.get('Bind','ip-psi'), Settings.getInt('Bind','port-psi') ), sslSupport=Settings.getInt('Probe_Channel','channel-ssl'), wsSupport=Settings.getInt('Probe_Channel','channel-websocket-support'), context = Context.instance() ) self.info("Starting ASI on %s:%s" % ( Settings.get('Bind','ip-asi') , Settings.getInt('Bind','port-asi') ) ) AgentServerInterface.initialize( listeningAddress = ( Settings.get('Bind','ip-asi'), Settings.getInt('Bind','port-asi') ), sslSupport=Settings.getInt('Agent_Channel','channel-ssl'), wsSupport=Settings.getInt('Agent_Channel','channel-websocket-support'), tsi=TestServerInterface.instance(), context = Context.instance() ) # Start on modules RestServerInterface.instance().start() self.info("RSI is listening on tcp://%s:%s" % ( Settings.get('Bind','ip-rsi'), Settings.get('Bind','port-rsi') ) ) EventServerInterface.instance().startSA() self.info("ESI is listening on tcp://%s:%s" % ( Settings.get('Bind','ip-esi'), Settings.get('Bind','port-esi') ) ) TestServerInterface.instance().startSA() self.info("TSI is listening on tcp://%s:%s" % ( Settings.get('Bind','ip-tsi'), Settings.get('Bind','port-tsi') ) ) ProbeServerInterface.instance().startSA() self.info("PSI is listening on tcp://%s:%s" % ( Settings.get('Bind','ip-psi'), Settings.get('Bind','port-psi') ) ) AgentServerInterface.instance().startSA() self.info("ASI is listening on tcp://%s:%s" % ( Settings.get('Bind','ip-asi'), Settings.get('Bind','port-asi') ) ) # Now start the scheduler and reload tasks taskReloaded = TaskManager.instance().loadBackups() if taskReloaded is None: self.info("Reload tasks disabled") elif taskReloaded: self.info("Tasks reloaded") else: self.error("Failed to reload tasks") self.info("Schedule automatic backups...") if Settings.getInt('Backups','tests'): RepoAdapters.instance().scheduleBackup() self.info("Backup tests scheduled") else: self.info("Backup tests disabled") if Settings.getInt('Backups','adapters'): RepoTests.instance().scheduleBackup() self.info("Backup adapters scheduled") else: self.info("Backup adapters disabled") if Settings.getInt('Backups','libraries'): RepoLibraries.instance().scheduleBackup() self.info("Backup libraries scheduled") else: self.info("Backup libraries disabled") if Settings.getInt('Backups','archives'): RepoArchives.instance().scheduleBackup() self.info("Backup archives scheduled") else: self.info("Backup archives disabled") except Exception as e: self.error("Unable to start server: %s" % str(e)) self.cleanup() sys.exit(3) stoptime = time.time() self.info( "%s successfully started (in %s sec.)" % ( Settings.get('Server','name'), int(stoptime - starttime) ) ) self.setrunning() self.run()
def createResultLog(self, testsPath, logPath, logName, logData): """ Create result log """ self.trace("create result log=%s to %s" % (logName, logPath)) try: # write the file f = open("%s/%s/%s" % (testsPath, logPath, logName), 'wb') f.write(base64.b64decode(logData)) f.close() # notify all users size_ = os.path.getsize("%s/%s/%s" % (testsPath, logPath, logName)) # extract project id if logPath.startswith('/'): logPath = logPath[1:] tmp = logPath.split('/', 1) projectId = tmp[0] tmp = tmp[1].split('/', 1) mainPathTozip = tmp[0] subPathTozip = tmp[1] if Settings.getInt('Notifications', 'archives'): m = [{ "type": "folder", "name": mainPathTozip, "project": "%s" % projectId, "content": [{ "type": "folder", "name": subPathTozip, "project": "%s" % projectId, "content": [{ "project": "%s" % projectId, "type": "file", "name": logName, 'size': str(size_) }] }] }] notif = {} notif['archive'] = m notif['stats-repo-archives'] = { 'nb-zip': 1, 'nb-trx': 0, 'nb-tot': 1, 'mb-used': self.getSizeRepoV2(folder=self.testsPath), 'mb-free': self.freeSpace(p=self.testsPath) } data = ('archive', (None, notif)) ESI.instance().notifyByUserAndProject(body=data, admin=True, leader=False, tester=True, developer=False, projectId="%s" % projectId) except Exception as e: self.error("unable to create result log: %s" % e) return False return True
def createZip(self, trPath, projectId=1): """ Create a zip @type mainPathTozip: @param mainPathTozip: @type subPathTozip: @param subPathTozip: @return: response code @rtype: int """ ret = self.context.CODE_ERROR mainPathTozip, subPathTozip = trPath.split("/", 1) try: timeArch, milliArch, testName, testUser = subPathTozip.split(".") fulltestNameDecoded = base64.b64decode(testName) logIndex = self.getLastLogIndex(pathZip=trPath, projectId=projectId) tmp = fulltestNameDecoded.rsplit('/', 1) if len(tmp) == 2: testNameDecoded = tmp[1] else: testNameDecoded = tmp[0] fileName = 'logs%s_%s_%s_0' % (str(logIndex), testNameDecoded, self.getTimestamp()) self.trace("zip %s to %s.zip" % (trPath, fileName)) zipped = self.toZip(file="%s/%s/%s" % (self.testsPath, projectId, trPath), filename="%s/%s/%s/%s.zip" % (self.testsPath, projectId, trPath, fileName), fileToExclude=['%s.zip' % fileName], keepTree=False) ret = zipped if zipped == self.context.CODE_OK: self.info("zip successfull: %s" % fileName) if Settings.getInt('Notifications', 'archives'): # now notify all connected users size_ = os.path.getsize( "%s/%s/%s/%s.zip" % (self.testsPath, projectId, trPath, fileName)) m = [{ "type": "folder", "name": mainPathTozip, "project": "%s" % projectId, "content": [{ "type": "folder", "name": subPathTozip, "project": "%s" % projectId, "content": [{ "project": "%s" % projectId, "type": "file", "name": '%s.zip' % fileName, 'size': str(size_) }] }] }] notif = {} notif['archive'] = m notif['stats-repo-archives'] = { 'nb-zip': 1, 'nb-trx': 0, 'nb-tot': 1, 'mb-used': self.getSizeRepoV2(folder=self.testsPath), 'mb-free': self.freeSpace(p=self.testsPath) } data = ('archive', (None, notif)) ESI.instance().notifyByUserTypes(body=data, admin=True, leader=False, tester=True, developer=False) else: self.error("zip %s failed" % trPath) except Exception as e: raise Exception("[createZip] %s" % str(e)) return ret