def reconnectCouchManager(self): retval = True if not self.couchdbmanager.reconnect(): retval = False return retval WorkspacePersister.reExecutePendingActions() return retval
def setUp(self): self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController)) self.temp_workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(self.temp_workspace) WorkspacePersister.stopThreads()
def setUp(self): self.wm = WorkspaceManager(self.model_controller, mock(plcore.PluginController)) self.workspace = self.wm.createWorkspace( new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self.wm.setActiveWorkspace(self.workspace) WorkspacePersister.stopThreads()
def _test_remove_active_workspace_fs(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") self.wm.removeWorkspace(workspace.name) self.assertNotIn(host1, self.model_controller.getAllHosts(), 'Host not removed while removing active workspace')
def setUpClass(cls): cls.model_controller = controller.ModelController(mock()) api.setUpAPIs(cls.model_controller) cls.wm = WorkspaceManager(cls.model_controller, mock(plcore.PluginController)) cls.temp_workspace = cls.wm.createWorkspace( test_utils.new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) cls.wm.setActiveWorkspace(cls.temp_workspace) WorkspacePersister.stopThreads()
def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = CouchdbManager(uri=CONF.getCouchURI()) self._workspaces = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister()
def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = PersistenceManagerFactory().getInstance() self.fsmanager = FSManager() self._workspaces = {} self._workspaces_types = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister()
def _test_remove_another_workspace(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() create_host(self, "coquito") self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.wm.removeWorkspace(workspace.name) self.assertNotIn(workspace.name, self.wm.getWorkspacesNames(), "Workspace not removed") self.assertIn(workspace2.name, self.wm.getWorkspacesNames(), "Workspace removed while removing another workspace")
def wrapper(self, *args, **kwargs): try: if self._lostConnection: WorkspacePersister.addPendingAction(self, func, args, kwargs) return func(self, *args, **kwargs) except restkit.errors.RequestError as req_error: self.lostConnectionResolv() WorkspacePersister.stopThreads() WorkspacePersister.addPendingAction(self, func, args, kwargs) WorkspacePersister.notifyPersisterConnectionLost() model.api.devlog("Operation [%s] timeout" % func.__name__) return func(self, *args, **kwargs)
def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = PersistenceManagerFactory().getInstance() self._workspaces = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister()
def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = CouchdbManager(uri = CONF.getCouchURI()) self._workspaces = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister()
def _test_switch_workspace_with_objects(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") interface1 = create_interface(self, host1, iname="pepito") service1 = create_service(self, host1, interface1) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.assertNotIn( host1, self.model_controller.getAllHosts(), "Host in controller, should be removed when \ switching workspaces") self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!")
def _test_switch_workspace_with_objects(self): workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host1 = create_host(self, "coquito") interface1 = create_interface(self, host1, iname="pepito") service1 = create_service(self, host1, interface1) self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!") workspace2 = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnCouch) self._couchdb_workspaces.append(workspace2.name) self.wm.setActiveWorkspace(workspace2) WorkspacePersister.stopThreads() self.assertNotIn(host1, self.model_controller.getAllHosts(), "Host in controller, should be removed when \ switching workspaces") self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() self.assertIn(host1, self.model_controller.getAllHosts(), "Host not in controller") self.assertIn(interface1, host1.getAllInterfaces(), "Interface not in host!") self.assertIn(service1, interface1.getAllServices(), "Service not in Interface!")
def wrapper(self, *args, **kwargs): func(self, *args, **kwargs) WorkspacePersister.save(self)
class WorkspaceManager(object): """ This handles all workspaces. It checks for existing workspaces inside the persistence directory. It is in charge of starting the WorkspacesAutoSaver to persist each workspace. This class stores information in $HOME/.faraday/config/workspacemanager.xml file to keep track of created workspaces to be able to load them """ def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = CouchdbManager(uri = CONF.getCouchURI()) self._workspaces = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister() def couchAvailable(self, isit): self._couchAvailable = isit def reconnect(self): if not self.reconnectCouchManager(): self._model_controller._notifyWorkspaceConnectionLost() def getCouchManager(self): return self.couchdbmanager def setCouchManager(self, cm): self.couchdbmanager = cm @staticmethod def getAvailableWorkspaceTypes(): av = [w.__name__ for w in Workspace.__subclasses__() if w.isAvailable() ] model.api.devlog("Available wortkspaces: %s" ", ".join(av)) return av def reconnectCouchManager(self): retval = True if not self.couchdbmanager.reconnect(): retval = False return retval WorkspacePersister.reExecutePendingActions() return retval def startAutoLoader(self): pass def stopAutoLoader(self): pass def startReportManager(self): self.report_manager.start() def stopReportManager(self): self.report_manager.stop() self.report_manager.join() def getActiveWorkspace(self): return self.active_workspace def saveWorkspaces(self): pass def addWorkspace(self, workspace): self._workspaces[workspace.name] = workspace def createVisualizations(self): stat = False url = "" if self.couchdbmanager.isAvailable(): stat = True url = self.couchdbmanager.pushReports() else: self._notifyNoVisualizationAvailable() return stat, url def _notifyNoVisualizationAvailable(self): guiapi.postCustomEvent((ShowPopupCustomEvent("No visualizations available, please install and configure CouchDB"))) def createWorkspace(self, name, description="", workspaceClass = WorkspaceOnFS, shared=CONF.getAutoShareWorkspace(), customer="", sdate=None, fdate=None): if name not in self._workspaces: w = workspaceClass(name, self, shared) w.description = description w.customer = customer if sdate is not None: w.start_date = sdate if fdate is not None: w.finish_date = fdate self.addWorkspace(w) else: w = self._workspaces[name] return w def removeWorkspace(self, name): dm = self.getWorkspace(name).getDataManager() dm.removeWorkspace(name) datapath = CONF.getDataPath() todelete = [i for i in os.listdir(datapath) if name in i ] for i in todelete: os.remove(os.path.join(datapath, i)) shutil.rmtree(self.getWorkspace(name).getReportPath()) del self._workspaces[name] if self.getWorkspace(name) == self.getActiveWorkspace() and self.getWorkspacesCount() > 0: self.setActiveWorkspace(self.getWorkspace(self._workspaces.keys()[0])) def getWorkspace(self, name): return self._workspaces.get(name) def openWorkspace(self, name): if name in self._workspaces: w = self._workspaces[name] self.setActiveWorkspace(w) return w raise Exception("Error on OpenWorkspace for %s " % name) def getWorkspaces(self): """ Simply returns a list of all existing workspaces (including the active one) """ self.loadWorkspaces() return [w for w in self._workspaces.itervalues()] def getWorkspacesCount(self): return len(self._workspaces) def getWorkspacesNames(self): return self._workspaces.keys() def loadWorkspaces(self): self._workspaces.clear() for name in os.listdir(CONF.getPersistencePath()): if name not in self._workspaces: if os.path.isdir(os.path.join(CONF.getPersistencePath(),name)) and name not in self._excluded_directories: w = self.createWorkspace(name, workspaceClass = WorkspaceOnFS) for name in self.couchdbmanager.getWorkspacesNames(): if name not in self._workspaces and not name == "reports": self.createWorkspace(name, workspaceClass = WorkspaceOnCouch) def setActiveWorkspace(self, workspace): try: self.stopAutoLoader() except : pass if self.active_workspace is not None: self.active_workspace.setModelController(None) CONF.setLastWorkspace(workspace.name) CONF.saveConfig() self.active_workspace = workspace self.active_workspace.setModelController(self._model_controller) self._model_controller.setWorkspace(self.active_workspace) self.workspace_persister.setPersister(self.active_workspace, self.active_workspace._dmanager) self.report_manager.path = workspace.report_path if isinstance(self.active_workspace, WorkspaceOnCouch): self.startAutoLoader() def syncWorkspaces(self): """ Synchronize persistence directory using the DataManager. We first make sure that all shared workspaces were added to the repo """ pass
def test_load_workspace_on_fs(self): """ This test case creates a host within the Model Controller context adds an interface to it then adds a VulnWeb""" """ We are going to test this structure: host -> interface1 -> service1 -> vuln_web -> vuln -> note -> service2 -> vuln -> vuln -> vuln -> note -> note -> interface2 -> service3 -> note -> credential -> vuln -> vuln """ workspace = self.wm.createWorkspace(new_random_workspace_name(), workspaceClass=WorkspaceOnFS) #self._couchdb_workspaces.append(workspace.name) self.wm.setActiveWorkspace(workspace) WorkspacePersister.stopThreads() host = create_host(self) interface = create_interface(self, host, ip="127.0.0.1") interface2 = create_interface(self, host, ip="127.0.0.2") service = create_service(self, host, interface, ports=1) service2 = create_service(self, host, interface, ports=2) service3 = create_service(self, host, interface2, ports=3) vulnweb = ModelObjectVulnWeb(name='VulnWebTest', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vulnweb) vuln = ModelObjectVuln(name='VulnTest', desc='TestDescription', severity='high') vuln2 = ModelObjectVuln(name='VulnTest2', desc='TestDescription', severity='high') vuln3 = ModelObjectVuln(name='VulnTest3', desc='TestDescription', severity='high') vuln4 = ModelObjectVuln(name='VulnTest4', desc='TestDescription', severity='high') vuln5 = ModelObjectVuln(name='VulnTest5', desc='TestDescription', severity='high') vuln6 = ModelObjectVuln(name='VulnTest6', desc='TestDescription', severity='high') self.model_controller.addVulnToServiceSYNC(host.getID(), service.getID(), vuln) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln2) self.model_controller.addVulnToServiceSYNC(host.getID(), service2.getID(), vuln3) self.model_controller.addVulnToHostSYNC(host.getID(), vuln4) self.model_controller.addVulnToServiceSYNC(host.getID(), service3.getID(), vuln5) self.model_controller.addVulnToInterfaceSYNC(host.getID(), interface2.getID(), vuln6) note = ModelObjectNote(name='NoteTest', text='TestDescription') note2 = ModelObjectNote(name='NoteTest2', text='TestDescription') note3 = ModelObjectNote(name='NoteTest3', text='TestDescription') note4 = ModelObjectNote(name='NoteTest4', text='TestDescription') self.model_controller.addNoteToServiceSYNC(host.getID(), service.getID(), note) self.model_controller.addNoteToHostSYNC(host.getID(), note2) self.model_controller.addNoteToHostSYNC(host.getID(), note3) self.model_controller.addNoteToServiceSYNC(host.getID(), service3.getID(), note4) cred = ModelObjectCred(username='******', password='******') self.model_controller.addCredToServiceSYNC(host.getID(), service3.getID(), cred) # First, we test if the structure was correctly created # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created") # So, now we reload the worskpace and check everything again print workspace.name workspace.load() # one host with two interfaces, one vuln and two notes self.assertEquals(len(self.model_controller.getAllHosts()), 1, "Host not created") added_host = self.model_controller.getHost(host.getID()) self.assertEquals(len(added_host.getAllInterfaces()), 2, "Interfaces not added to Host") self.assertEquals(len(added_host.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_host.getNotes()), 2, "Notes not created") # one interface with two services, and another one # with a service and a vuln added_interface1 = added_host.getInterface(interface.getID()) added_interface2 = added_host.getInterface(interface2.getID()) self.assertEquals(len(added_interface1.getAllServices()), 2, "Services not created") self.assertEquals(len(added_interface2.getAllServices()), 1, "Service not created") self.assertEquals(len(added_interface2.getVulns()), 1, "Vulns not created") # one service with a note, a vuln and a vuln web added_service1 = added_interface1.getService(service.getID()) self.assertEquals(len(added_service1.getNotes()), 1, "Note not created") self.assertEquals(len(added_service1.getVulns()), 2, "Vulns not created") added_vuln_web = added_service1.getVuln(vulnweb.getID()) self.assertEquals(added_vuln_web.class_signature, "VulnerabilityWeb", "Not a vuln web") # one service with two vulns added_service2 = added_interface1.getService(service2.getID()) self.assertEquals(len(added_service2.getVulns()), 2, "Services not created") # one service with a note, a vuln and a credential added_service3 = added_interface2.getService(service3.getID()) self.assertEquals(len(added_service3.getVulns()), 1, "Vuln not created") self.assertEquals(len(added_service3.getNotes()), 1, "Note not created") self.assertEquals(len(added_service3.getCreds()), 1, "Cred not created")
class WorkspaceManager(object): """ This handles all workspaces. It checks for existing workspaces inside the persistence directory. It is in charge of starting the WorkspacesAutoSaver to persist each workspace. This class stores information in $HOME/.faraday/config/workspacemanager.xml file to keep track of created workspaces to be able to load them """ def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = PersistenceManagerFactory().getInstance() self.fsmanager = FSManager() self._workspaces = {} self._workspaces_types = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister() def couchAvailable(self, isit): self._couchAvailable = isit def _notifyWorkspaceNoConnection(self): notifier.showPopup( "Couchdb Connection lost. Defaulting to memory. Fix network and try again in 5 minutes." ) def reconnect(self): if not self.reconnectCouchManager(): self._notifyWorkspaceNoConnection() def getCouchManager(self): return self.couchdbmanager def setCouchManager(self, cm): self.couchdbmanager = cm @staticmethod def getAvailableWorkspaceTypes(): av = [ w.__name__ for w in Workspace.__subclasses__() if w.isAvailable() ] model.api.devlog("Available wortkspaces: %s" ", ".join(av)) return av def reconnectCouchManager(self): retval = True if not self.couchdbmanager.reconnect(): retval = False return retval WorkspacePersister.reExecutePendingActions() return retval def startAutoLoader(self): pass def stopAutoLoader(self): pass def startReportManager(self): self.report_manager.start() def stopReportManager(self): self.report_manager.stop() self.report_manager.join() def getActiveWorkspace(self): return self.active_workspace def saveWorkspaces(self): pass def addWorkspace(self, workspace): self._workspaces[workspace.name] = workspace def createVisualizations(self): stat = False url = "" if self.couchdbmanager.isAvailable(): stat = True url = self.couchdbmanager.pushReports() else: self._notifyNoVisualizationAvailable() return stat, url def _notifyNoVisualizationAvailable(self): notifier.showPopup( "No visualizations available, please install and configure CouchDB" ) def createWorkspace(self, name, description="", workspaceClass=None, shared=CONF.getAutoShareWorkspace(), customer="", sdate=None, fdate=None): model.api.devlog("Creating Workspace") if self.getWorkspaceType(name) in globals(): workspaceClass = globals()[self.getWorkspaceType(name)] elif not workspaceClass: # Defaulting =( model.api.devlog("Defaulting to WorkspaceOnFS") workspaceClass = WorkspaceOnFS w = workspaceClass(name, self, shared) # Register the created workspace type: self._workspaces_types[name] = workspaceClass.__name__ w.description = description w.customer = customer if sdate is not None: w.start_date = sdate if fdate is not None: w.finish_date = fdate self.addWorkspace(w) return w def removeWorkspace(self, name): work = self.getWorkspace(name) if not work: return dm = work.getDataManager() dm.removeWorkspace(name) datapath = CONF.getDataPath() todelete = [i for i in os.listdir(datapath) if name in i] for i in todelete: os.remove(os.path.join(datapath, i)) shutil.rmtree(self.getWorkspace(name).getReportPath()) del self._workspaces[name] if self.getWorkspace(name) == self.getActiveWorkspace( ) and self.getWorkspacesCount() > 0: self.setActiveWorkspace( self.getWorkspace(self._workspaces.keys()[0])) def getWorkspace(self, name): ''' May return None ''' if not self._workspaces.get(name): # Retrieve the workspace self.loadWorkspace(name) return self._workspaces.get(name) def loadWorkspace(self, name): workspaceClass = None workspace = None if name in self.fsmanager.getWorkspacesNames(): workspace = self.createWorkspace(name, workspaceClass=WorkspaceOnFS) elif name in self.couchdbmanager.getWorkspacesNames(): workspace = self.createWorkspace(name, workspaceClass=WorkspaceOnCouch) return workspace def openWorkspace(self, name): w = self.getWorkspace(name) self.setActiveWorkspace(w) return w def getWorkspaces(self): """ Simply returns a list of all existing workspaces (including the active one) """ self.loadWorkspaces() return [w for w in self._workspaces.itervalues()] def getWorkspacesCount(self): return len(self._workspaces) def getWorkspacesNames(self): return self._workspaces.keys() def loadWorkspaces(self): self._workspaces_types = {} fsworkspaces = { name: None for name in self.fsmanager.getWorkspacesNames() } self._workspaces.update(fsworkspaces) couchworkspaces = { name: None for name in self.couchdbmanager.getWorkspacesNames() if not name == 'reports' } self._workspaces.update(couchworkspaces) self._workspaces_types.update( {name: WorkspaceOnFS.__name__ for name in fsworkspaces}) self._workspaces_types.update( {name: WorkspaceOnCouch.__name__ for name in couchworkspaces}) def getWorkspaceType(self, name): return self._workspaces_types.get(name, 'undefined') def setActiveWorkspace(self, workspace): try: self.stopAutoLoader() except: pass if self.active_workspace is not None: self.active_workspace.setModelController(None) CONF.setLastWorkspace(workspace.name) CONF.saveConfig() self.active_workspace = workspace self.active_workspace.setModelController(self._model_controller) self._model_controller.setWorkspace(self.active_workspace) self.workspace_persister.setPersister(self.active_workspace, self.active_workspace._dmanager) self.report_manager.path = workspace.report_path if isinstance(self.active_workspace, WorkspaceOnCouch): self.startAutoLoader() def isActive(self, name): return self.active_workspace.name == name def syncWorkspaces(self): """ Synchronize persistence directory using the DataManager. We first make sure that all shared workspaces were added to the repo """ pass
def tearDownClass(cls): WorkspacePersister.stopThreads() cls.wm.removeWorkspace(cls.temp_workspace.name)
def tearDownClass(cls): WorkspacePersister.stopThreads() cls.cleanCouchDatabases()
class WorkspaceManager(object): """ This handles all workspaces. It checks for existing workspaces inside the persistence directory. It is in charge of starting the WorkspacesAutoSaver to persist each workspace. This class stores information in $HOME/.faraday/config/workspacemanager.xml file to keep track of created workspaces to be able to load them """ def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = CouchdbManager(uri=CONF.getCouchURI()) self._workspaces = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister() def couchAvailable(self, isit): self._couchAvailable = isit def reconnect(self): if not self.reconnectCouchManager(): self._model_controller._notifyWorkspaceConnectionLost() def getCouchManager(self): return self.couchdbmanager def setCouchManager(self, cm): self.couchdbmanager = cm @staticmethod def getAvailableWorkspaceTypes(): av = [ w.__name__ for w in Workspace.__subclasses__() if w.isAvailable() ] model.api.devlog("Available wortkspaces: %s" ", ".join(av)) return av def reconnectCouchManager(self): retval = True if not self.couchdbmanager.reconnect(): retval = False return retval WorkspacePersister.reExecutePendingActions() return retval def startAutoLoader(self): pass def stopAutoLoader(self): pass def startReportManager(self): self.report_manager.start() def stopReportManager(self): self.report_manager.stop() self.report_manager.join() def getActiveWorkspace(self): return self.active_workspace def saveWorkspaces(self): pass def addWorkspace(self, workspace): self._workspaces[workspace.name] = workspace def createVisualizations(self): stat = False url = "" if self.couchdbmanager.isAvailable(): stat = True url = self.couchdbmanager.pushReports() else: self._notifyNoVisualizationAvailable() return stat, url def _notifyNoVisualizationAvailable(self): guiapi.postCustomEvent((ShowPopupCustomEvent( "No visualizations available, please install and configure CouchDB" ))) def createWorkspace(self, name, description="", workspaceClass=WorkspaceOnFS, shared=CONF.getAutoShareWorkspace(), customer="", sdate=None, fdate=None): if name not in self._workspaces: w = workspaceClass(name, self, shared) w.description = description w.customer = customer if sdate is not None: w.start_date = sdate if fdate is not None: w.finish_date = fdate self.addWorkspace(w) else: w = self._workspaces[name] return w def removeWorkspace(self, name): dm = self.getWorkspace(name).getDataManager() dm.removeWorkspace(name) datapath = CONF.getDataPath() todelete = [i for i in os.listdir(datapath) if name in i] for i in todelete: os.remove(os.path.join(datapath, i)) shutil.rmtree(self.getWorkspace(name).getReportPath()) del self._workspaces[name] if self.getWorkspace(name) == self.getActiveWorkspace( ) and self.getWorkspacesCount() > 0: self.setActiveWorkspace( self.getWorkspace(self._workspaces.keys()[0])) def getWorkspace(self, name): return self._workspaces.get(name) def openWorkspace(self, name): if name in self._workspaces: w = self._workspaces[name] self.setActiveWorkspace(w) return w raise Exception("Error on OpenWorkspace for %s " % name) def getWorkspaces(self): """ Simply returns a list of all existing workspaces (including the active one) """ self.loadWorkspaces() return [w for w in self._workspaces.itervalues()] def getWorkspacesCount(self): return len(self._workspaces) def getWorkspacesNames(self): return self._workspaces.keys() def loadWorkspaces(self): self._workspaces.clear() for name in os.listdir(CONF.getPersistencePath()): if name not in self._workspaces: if os.path.isdir(os.path.join( CONF.getPersistencePath(), name)) and name not in self._excluded_directories: w = self.createWorkspace(name, workspaceClass=WorkspaceOnFS) for name in self.couchdbmanager.getWorkspacesNames(): if name not in self._workspaces and not name == "reports": self.createWorkspace(name, workspaceClass=WorkspaceOnCouch) def setActiveWorkspace(self, workspace): try: self.stopAutoLoader() except: pass if self.active_workspace is not None: self.active_workspace.setModelController(None) CONF.setLastWorkspace(workspace.name) CONF.saveConfig() self.active_workspace = workspace self.active_workspace.setModelController(self._model_controller) self._model_controller.setWorkspace(self.active_workspace) self.workspace_persister.setPersister(self.active_workspace, self.active_workspace._dmanager) self.report_manager.path = workspace.report_path if isinstance(self.active_workspace, WorkspaceOnCouch): self.startAutoLoader() def syncWorkspaces(self): """ Synchronize persistence directory using the DataManager. We first make sure that all shared workspaces were added to the repo """ pass
def wrapper(self, *args, **kwargs): func(self, *args, **kwargs) WorkspacePersister.delete(self)
class WorkspaceManager(object): """ This handles all workspaces. It checks for existing workspaces inside the persistence directory. It is in charge of starting the WorkspacesAutoSaver to persist each workspace. This class stores information in $HOME/.faraday/config/workspacemanager.xml file to keep track of created workspaces to be able to load them """ def __init__(self, model_controller, plugin_controller): self.active_workspace = None self._couchAvailable = False self.report_manager = ReportManager(10, plugin_controller) self.couchdbmanager = PersistenceManagerFactory().getInstance() self.fsmanager = FSManager() self._workspaces = {} self._workspaces_types = {} self._model_controller = model_controller self._excluded_directories = [".svn"] self.workspace_persister = WorkspacePersister() def couchAvailable(self, isit): self._couchAvailable = isit def _notifyWorkspaceNoConnection(self): notifier.showPopup("Couchdb Connection lost. Defaulting to memory. Fix network and try again in 5 minutes.") def reconnect(self): if not self.reconnectCouchManager(): self._notifyWorkspaceNoConnection() def getCouchManager(self): return self.couchdbmanager def setCouchManager(self, cm): self.couchdbmanager = cm @staticmethod def getAvailableWorkspaceTypes(): av = [w.__name__ for w in Workspace.__subclasses__() if w.isAvailable() ] model.api.devlog("Available wortkspaces: %s" ", ".join(av)) return av def reconnectCouchManager(self): retval = True if not self.couchdbmanager.reconnect(): retval = False return retval WorkspacePersister.reExecutePendingActions() return retval def startAutoLoader(self): pass def stopAutoLoader(self): pass def startReportManager(self): self.report_manager.start() def stopReportManager(self): self.report_manager.stop() self.report_manager.join() def getActiveWorkspace(self): return self.active_workspace def saveWorkspaces(self): pass def addWorkspace(self, workspace): self._workspaces[workspace.name] = workspace def createVisualizations(self): stat = False url = "" if self.couchdbmanager.isAvailable(): stat = True url = self.couchdbmanager.pushReports() else: self._notifyNoVisualizationAvailable() return stat, url def _notifyNoVisualizationAvailable(self): notifier.showPopup("No visualizations available, please install and configure CouchDB") def createWorkspace(self, name, description="", workspaceClass = None, shared=CONF.getAutoShareWorkspace(), customer="", sdate=None, fdate=None): model.api.devlog("Creating Workspace") if self.getWorkspaceType(name) in globals(): workspaceClass = globals()[self.getWorkspaceType(name)] elif not workspaceClass: # Defaulting =( model.api.devlog("Defaulting to WorkspaceOnFS") workspaceClass = WorkspaceOnFS w = workspaceClass(name, self, shared) # Register the created workspace type: self._workspaces_types[name] = workspaceClass.__name__ w.description = description w.customer = customer if sdate is not None: w.start_date = sdate if fdate is not None: w.finish_date = fdate self.addWorkspace(w) return w def removeWorkspace(self, name): work = self.getWorkspace(name) if not work: return dm = work.getDataManager() dm.removeWorkspace(name) datapath = CONF.getDataPath() todelete = [i for i in os.listdir(datapath) if name in i ] for i in todelete: os.remove(os.path.join(datapath, i)) shutil.rmtree(self.getWorkspace(name).getReportPath()) del self._workspaces[name] if self.getWorkspace(name) == self.getActiveWorkspace() and self.getWorkspacesCount() > 0: self.setActiveWorkspace(self.getWorkspace(self._workspaces.keys()[0])) def getWorkspace(self, name): ''' May return None ''' if not self._workspaces.get(name): # Retrieve the workspace self.loadWorkspace(name) return self._workspaces.get(name) def loadWorkspace(self, name): workspaceClass = None workspace = None if name in self.fsmanager.getWorkspacesNames(): workspace = self.createWorkspace(name, workspaceClass = WorkspaceOnFS) elif name in self.couchdbmanager.getWorkspacesNames(): workspace = self.createWorkspace(name, workspaceClass = WorkspaceOnCouch) return workspace def openWorkspace(self, name): w = self.getWorkspace(name) self.setActiveWorkspace(w) return w def getWorkspaces(self): """ Simply returns a list of all existing workspaces (including the active one) """ self.loadWorkspaces() return [w for w in self._workspaces.itervalues()] def getWorkspacesCount(self): return len(self._workspaces) def getWorkspacesNames(self): return self._workspaces.keys() def loadWorkspaces(self): self._workspaces_types = {} fsworkspaces = {name: None for name in self.fsmanager.getWorkspacesNames()} self._workspaces.update(fsworkspaces) couchworkspaces = {name: None for name in self.couchdbmanager .getWorkspacesNames() if not name == 'reports'} self._workspaces.update(couchworkspaces) self._workspaces_types.update({name: WorkspaceOnFS.__name__ for name in fsworkspaces}) self._workspaces_types.update({name: WorkspaceOnCouch.__name__ for name in couchworkspaces}) def getWorkspaceType(self, name): return self._workspaces_types.get(name, 'undefined') def setActiveWorkspace(self, workspace): try: self.stopAutoLoader() except : pass if self.active_workspace is not None: self.active_workspace.setModelController(None) CONF.setLastWorkspace(workspace.name) CONF.saveConfig() self.active_workspace = workspace self.active_workspace.setModelController(self._model_controller) self._model_controller.setWorkspace(self.active_workspace) self.workspace_persister.setPersister(self.active_workspace, self.active_workspace._dmanager) self.report_manager.path = workspace.report_path if isinstance(self.active_workspace, WorkspaceOnCouch): self.startAutoLoader() def isActive(self, name): return self.active_workspace.name == name def syncWorkspaces(self): """ Synchronize persistence directory using the DataManager. We first make sure that all shared workspaces were added to the repo """ pass