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,
                   ip,
                   port,
                   type,
                   name,
                   descr,
                   defaultTool,
                   supportProxy=False,
                   proxyIp=None,
                   proxyPort=None,
                   sslSupport=True,
                   isAgent=0,
                   fromCmd=False):
        """
        Initialize the tool

        @param ip:
        @type ip: 

        @param type:
        @type type: 

        @param name:
        @type name: 

        @param descr:
        @type descr: 
        """
        self.running = False
        self.defaultTool = defaultTool
        self.tool = None
        if not Settings.cfgFileIsPresent():
            sys.stdout.write(" (config file doesn't exist)")
            sys.exit(RETURN_CONFIG)

        try:
            Settings.initialize()
            Logger.initialize(
                logPathFile="%s/%s/%s.log" %
                (Settings.getDirExec(), Settings.get('Paths', 'logs'), name))
            if (isAgent, str(type)) in plugins:
                self.info("Initialize %s tool type=%s..." % (type, isAgent))

                if not len(ip): raise Exception("No controller ip specified!")
                if not len(name): raise Exception("No tool name specified!")

                self.tool = plugins[(isAgent, str(type))].initialize(
                    controllerIp=str(ip),
                    toolName=str(name),
                    toolDesc=str(descr),
                    defaultTool=defaultTool,
                    controllerPort=int(port),
                    supportProxy=supportProxy,
                    proxyIp=str(proxyIp),
                    proxyPort=proxyPort,
                    sslSupport=sslSupport)
                self.tool.setFromCmd()

                self.tool.onRegistrationSuccessful = self.onRegistrationSuccessful
                self.tool.onRegistrationFailed = self.onRegistrationFailed
                self.tool.onRegistrationRefused = self.onRegistrationRefused
                self.tool.onToolWarningCalled = self.onToolLogWarningCalled
                self.tool.onToolLogErrorCalled = self.onToolLogErrorCalled
                self.tool.onToolDisconnection = self.onToolDisconnection
                self.tool.onConnectionRefused = self.onConnectionRefused
                self.tool.onConnectionTimeout = self.onConnectionTimeout
                self.tool.onProxyConnectionTimeout = self.onProxyConnectionTimeout
                self.tool.onProxyConnectionRefused = self.onProxyConnectionRefused
                self.tool.onProxyConnectionError = self.onProxyConnectionError
                self.tool.checkPrerequisites()
                self.tool.startCA()
            else:
                self.error('tool type unknown: %s' % type)
                self.finalize(RETURN_TYPE_UNKNOWN)
        except Exception as e:
            self.error("unable to start tool: " + str(e))
            try:
                Settings.finalize()
            except:
                pass
            Logger.finalize()

        else:
            # run in loop
            self.running = True
            self.run()