1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-19 06:48:10 +00:00
gns3-server/gns3server/compute/project_manager.py

123 lines
3.6 KiB
Python
Raw Normal View History

2015-01-19 16:07:32 +00:00
#
# 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/>.
2020-10-02 06:37:50 +00:00
import psutil
import platform
2015-01-19 16:07:32 +00:00
from .project import Project
from uuid import UUID
2015-01-19 16:07:32 +00:00
2020-10-02 06:37:50 +00:00
from gns3server.compute.compute_error import ComputeError, ComputeNotFoundError
import logging
2021-04-13 09:16:50 +00:00
log = logging.getLogger(__name__)
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-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
@property
def projects(self):
"""
Returns all projects.
:returns: Project instances
"""
return self._projects.values()
def get_project(self, project_id):
2015-01-19 16:07:32 +00:00
"""
Returns a Project instance.
:param project_id: Project identifier
2015-01-19 16:07:32 +00:00
:returns: Project instance
"""
try:
UUID(project_id, version=4)
except ValueError:
2021-04-13 09:07:58 +00:00
raise ComputeError(f"Project ID {project_id} is not a valid UUID")
2015-01-20 11:46:15 +00:00
if project_id not in self._projects:
2021-04-13 09:07:58 +00:00
raise ComputeNotFoundError(f"Project ID {project_id} doesn't exist")
return self._projects[project_id]
2015-01-19 16:07:32 +00:00
def _check_available_disk_space(self, project):
"""
Sends a warning notification if disk space is getting low.
:param project: project instance
"""
try:
used_disk_space = psutil.disk_usage(project.path).percent
except FileNotFoundError:
2021-04-13 09:07:58 +00:00
log.warning(f"Could not find '{project.path}' when checking for used disk space")
return
# send a warning if used disk space is >= 90%
if used_disk_space >= 90:
2021-04-13 09:07:58 +00:00
message = 'Only {:.2f}% or less of free disk space detected in "{}" on "{}"'.format(
2021-04-13 09:16:50 +00:00
100 - used_disk_space, project.path, platform.node()
2021-04-13 09:07:58 +00:00
)
log.warning(message)
project.emit("log.warning", {"message": message})
def create_project(self, name=None, project_id=None, path=None, variables=None):
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
"""
if project_id is not None and project_id in self._projects:
return self._projects[project_id]
2021-04-13 09:16:50 +00:00
project = Project(name=name, project_id=project_id, path=path, variables=variables)
self._check_available_disk_space(project)
self._projects[project.id] = project
2015-01-19 16:07:32 +00:00
return project
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:
2021-04-13 09:07:58 +00:00
raise ComputeNotFoundError(f"Project ID {project_id} doesn't exist")
del self._projects[project_id]