def disconnectRunningTools(self): """ """ for k1, c1 in ASI.instance().agentsRegistered.items(): ASI.instance().stopClient(client=c1['address']) for k2, c2 in PSI.instance().probesRegistered.items(): PSI.instance().stopClient(client=c2['address']) return True
def disconnectAgent(self, name): """ Disconnect agent """ self.info("Disconnect agent Name=%s" % name) if not name in ASI.instance().agentsRegistered: self.trace("disconnect agent, agent %s not found" % name) return self.context.CODE_NOT_FOUND else: agentProfile = ASI.instance().agentsRegistered[name] ASI.instance().stopClient(client=agentProfile['address']) return self.context.CODE_OK
def delDefaultAgent(self, aName): """ Delete default agent @type aName: @param aName: @return: @rtype: boolean """ ret = self.context.CODE_ERROR try: if self.configsFile is not None: # remove the section in the config file object self.configsFile.remove_section(aName) # 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', ('del', self.getDefaultAgents())) ESI.instance().notifyByUserTypes(body=notif, admin=True, monitor=False, tester=True) runningAgent = ASI.instance().getAgent(aname=aName) if runningAgent is not None: runningAgent['auto-startup'] = False notif2 = ('agents', ('del', ASI.instance().getAgents())) ESI.instance().notifyByUserTypes(body=notif2, admin=True, monitor=False, tester=True) # return OK ret = self.context.CODE_OK except ConfigParser.NoSectionError: self.error("agent not found: %s" % str(aName)) ret = self.context.CODE_NOT_FOUND except Exception as e: self.error("unable to delete default agent: %s" % str(e)) ret = self.context.CODE_FAILED return ret
def getRunning(self, b64=False): """ Returns all registered agent @return: all registered agent @rtype: list """ self.trace("get running agents") ret = ASI.instance().getAgents() return ret
def stopAgent(self, aname): """ Stop the agent gived in argument @type aname: @param aname: @return: @rtype: """ self.trace("stop agent %s" % aname) ret = False try: client = ASI.instance().getAgent(aname=aname) if client is None: self.trace("agent %s not found" % aname) ret = False else: self.trace("agent %s found" % aname) # Stop remote agents or manual: start a agent manually is equal to a remote agent if not client['auto-startup']: ASI.instance().stopClient(client=client['address']) else: # Stop local agent if aname in self.__pids__: try: p = self.__pids__[aname] p_id = p.pid os.kill(p_id, signal.SIGKILL) p.wait() self.__pids__.pop(aname) except Exception as e: self.error("Unable to kill %d: %s" % (p_id, str(e))) else: self.error("agent not found on pids: %s" % aname) ret = True except Exception as e: self.error("unable to stop agent: %s" % 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("Database manager ready") 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()