2015-01-19 16:07:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
from .project import Project
|
2015-01-21 02:02:22 +00:00
|
|
|
from uuid import UUID
|
2015-01-19 16:07:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProjectManager:
|
2015-01-20 12:24:00 +00:00
|
|
|
|
2015-01-19 16:07:32 +00:00
|
|
|
"""
|
2015-01-19 21:43:35 +00:00
|
|
|
This singleton keeps track of available projects.
|
2015-01-19 16:07:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-01-22 00:41:35 +00:00
|
|
|
|
2015-01-19 16:07:32 +00:00
|
|
|
self._projects = {}
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def instance(cls):
|
|
|
|
"""
|
2015-01-19 21:43:35 +00:00
|
|
|
Singleton to return only one instance of ProjectManager.
|
2015-01-19 16:07:32 +00:00
|
|
|
|
2015-01-19 21:43:35 +00:00
|
|
|
:returns: instance of ProjectManager
|
2015-01-19 16:07:32 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not hasattr(cls, "_instance"):
|
|
|
|
cls._instance = cls()
|
|
|
|
return cls._instance
|
|
|
|
|
2015-03-14 00:57:27 +00:00
|
|
|
@property
|
|
|
|
def projects(self):
|
|
|
|
"""
|
|
|
|
Returns all projects.
|
|
|
|
|
|
|
|
:returns: Project instances
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._projects.values()
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
def get_project(self, project_id):
|
2015-01-19 16:07:32 +00:00
|
|
|
"""
|
|
|
|
Returns a Project instance.
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
:param project_id: Project identifier
|
2015-01-19 16:07:32 +00:00
|
|
|
|
|
|
|
:returns: Project instance
|
|
|
|
"""
|
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
try:
|
2015-02-04 20:48:29 +00:00
|
|
|
UUID(project_id, version=4)
|
2015-01-21 02:02:22 +00:00
|
|
|
except ValueError:
|
2015-02-05 00:13:35 +00:00
|
|
|
raise aiohttp.web.HTTPBadRequest(text="Project ID {} is not a valid UUID".format(project_id))
|
2015-01-20 11:46:15 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
if project_id not in self._projects:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't exist".format(project_id))
|
|
|
|
return self._projects[project_id]
|
2015-01-19 16:07:32 +00:00
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
def create_project(self, name=None, project_id=None, path=None, temporary=False):
|
2015-01-19 16:07:32 +00:00
|
|
|
"""
|
|
|
|
Create a project and keep a references to it in project manager.
|
|
|
|
|
|
|
|
See documentation of Project for arguments
|
|
|
|
"""
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-02-25 23:05:57 +00:00
|
|
|
if project_id is not None and project_id in self._projects:
|
2015-02-26 00:19:12 +00:00
|
|
|
return self._projects[project_id]
|
2015-03-09 01:13:01 +00:00
|
|
|
project = Project(name=name, project_id=project_id, path=path, temporary=temporary)
|
2015-02-04 20:48:29 +00:00
|
|
|
self._projects[project.id] = project
|
2015-01-19 16:07:32 +00:00
|
|
|
return project
|
2015-02-25 23:05:57 +00:00
|
|
|
|
|
|
|
def remove_project(self, project_id):
|
|
|
|
"""
|
|
|
|
Removes a Project instance from the list of projects in use.
|
|
|
|
|
|
|
|
:param project_id: Project identifier
|
|
|
|
"""
|
|
|
|
|
|
|
|
if project_id not in self._projects:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't exist".format(project_id))
|
|
|
|
del self._projects[project_id]
|
2015-07-22 04:58:28 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
def check_hardware_virtualization(self, source_node):
|
2015-07-22 04:58:28 +00:00
|
|
|
"""
|
|
|
|
Checks if hardware virtualization can be used.
|
|
|
|
|
|
|
|
:returns: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
for project in self._projects.values():
|
2016-05-11 17:35:36 +00:00
|
|
|
for node in project.nodes:
|
|
|
|
if node == source_node:
|
2015-07-22 04:58:28 +00:00
|
|
|
continue
|
2016-05-11 17:35:36 +00:00
|
|
|
if node.hw_virtualization and node.__class__.__name__ != source_node.__class__.__name__:
|
2015-07-27 01:21:30 +00:00
|
|
|
return False
|
2015-07-22 04:58:28 +00:00
|
|
|
return True
|