2015-01-19 15:23:41 +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/>.
|
|
|
|
|
|
|
|
import os
|
2015-01-23 10:28:58 +00:00
|
|
|
import shutil
|
2015-01-26 11:10:30 +00:00
|
|
|
import asyncio
|
2015-05-14 10:03:17 +00:00
|
|
|
import hashlib
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2016-11-07 10:16:51 +00:00
|
|
|
from uuid import UUID, uuid4
|
2018-05-09 13:29:35 +00:00
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
from gns3server.compute.compute_error import ComputeError, ComputeNotFoundError, ComputeForbiddenError
|
2015-02-24 02:59:19 +00:00
|
|
|
from .port_manager import PortManager
|
2016-03-17 14:15:30 +00:00
|
|
|
from .notification_manager import NotificationManager
|
2015-01-26 11:10:30 +00:00
|
|
|
from ..config import Config
|
2018-10-15 10:05:49 +00:00
|
|
|
from ..utils.asyncio import wait_run_in_executor
|
2016-05-11 16:42:55 +00:00
|
|
|
from ..utils.path import check_path_allowed, get_default_project_directory
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-01-23 17:37:29 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2015-01-23 17:37:29 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-01-19 15:23:41 +00:00
|
|
|
class Project:
|
2015-01-31 21:34:49 +00:00
|
|
|
|
2015-01-19 15:23:41 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
A project contains a list of nodes.
|
|
|
|
In theory nodes are isolated project/project.
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-04-08 17:17:34 +00:00
|
|
|
:param project_id: force project identifier (None by default auto generate an UUID)
|
|
|
|
:param path: path of the project. (None use the standard directory)
|
2015-01-19 15:23:41 +00:00
|
|
|
"""
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2018-05-04 12:34:44 +00:00
|
|
|
def __init__(self, name=None, project_id=None, path=None, variables=None):
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
self._name = name
|
2016-11-07 10:16:51 +00:00
|
|
|
if project_id:
|
|
|
|
try:
|
|
|
|
UUID(project_id, version=4)
|
|
|
|
except ValueError:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"{project_id} is not a valid UUID")
|
2016-11-07 10:16:51 +00:00
|
|
|
else:
|
|
|
|
project_id = str(uuid4())
|
2016-03-10 09:32:07 +00:00
|
|
|
self._id = project_id
|
2017-01-17 18:37:38 +00:00
|
|
|
self._deleted = False
|
2016-05-11 17:35:36 +00:00
|
|
|
self._nodes = set()
|
2015-03-21 23:19:12 +00:00
|
|
|
self._used_tcp_ports = set()
|
|
|
|
self._used_udp_ports = set()
|
2018-05-04 12:34:44 +00:00
|
|
|
self._variables = variables
|
2015-02-05 16:52:37 +00:00
|
|
|
|
|
|
|
if path is None:
|
2016-05-11 16:42:55 +00:00
|
|
|
location = get_default_project_directory()
|
2016-03-10 09:32:07 +00:00
|
|
|
path = os.path.join(location, self._id)
|
2015-01-21 02:02:22 +00:00
|
|
|
try:
|
2015-02-05 16:52:37 +00:00
|
|
|
os.makedirs(path, exist_ok=True)
|
2015-01-21 02:02:22 +00:00
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not create project directory: {e}")
|
2015-02-05 16:52:37 +00:00
|
|
|
self.path = path
|
|
|
|
|
2016-04-21 15:27:49 +00:00
|
|
|
try:
|
|
|
|
if os.path.exists(self.tmp_working_directory()):
|
|
|
|
shutil.rmtree(self.tmp_working_directory())
|
2017-03-13 15:55:35 +00:00
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not clean project directory: {e}")
|
2016-04-21 15:27:49 +00:00
|
|
|
|
2021-04-13 09:07:58 +00:00
|
|
|
log.info(f"Project {self._id} with path '{self._path}' created")
|
2015-01-20 13:31:47 +00:00
|
|
|
|
2021-04-17 14:04:28 +00:00
|
|
|
def asdict(self):
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2021-04-17 09:06:32 +00:00
|
|
|
return {
|
|
|
|
"name": self._name,
|
|
|
|
"project_id": self._id,
|
|
|
|
"variables": self._variables
|
|
|
|
}
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2015-01-19 21:43:35 +00:00
|
|
|
@property
|
2015-02-04 20:48:29 +00:00
|
|
|
def id(self):
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
return self._id
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
|
|
|
|
return self._path
|
|
|
|
|
2015-02-04 20:17:00 +00:00
|
|
|
@path.setter
|
|
|
|
def path(self, path):
|
|
|
|
|
2015-02-05 16:52:37 +00:00
|
|
|
if hasattr(self, "_path"):
|
2022-03-30 10:38:34 +00:00
|
|
|
if path != self._path:
|
2020-10-02 06:37:50 +00:00
|
|
|
raise ComputeForbiddenError("Changing the project directory path is not allowed")
|
2016-02-04 10:46:05 +00:00
|
|
|
|
2022-03-30 10:38:34 +00:00
|
|
|
check_path_allowed(path)
|
2015-02-04 20:17:00 +00:00
|
|
|
self._path = path
|
2015-05-04 12:04:57 +00:00
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, name):
|
|
|
|
|
2016-04-05 16:32:48 +00:00
|
|
|
if "/" in name or "\\" in name:
|
2020-10-02 06:37:50 +00:00
|
|
|
raise ComputeForbiddenError("Project names cannot contain path separators")
|
2015-03-09 01:13:01 +00:00
|
|
|
self._name = name
|
|
|
|
|
2015-01-23 13:07:10 +00:00
|
|
|
@property
|
2016-05-11 17:35:36 +00:00
|
|
|
def nodes(self):
|
2015-01-23 13:07:10 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
return self._nodes
|
2015-01-23 13:07:10 +00:00
|
|
|
|
2018-05-04 12:34:44 +00:00
|
|
|
@property
|
|
|
|
def variables(self):
|
|
|
|
return self._variables
|
|
|
|
|
|
|
|
@variables.setter
|
|
|
|
def variables(self, variables):
|
|
|
|
self._variables = variables
|
|
|
|
|
2015-03-21 23:19:12 +00:00
|
|
|
def record_tcp_port(self, port):
|
|
|
|
"""
|
|
|
|
Associate a reserved TCP port number with this project.
|
|
|
|
|
|
|
|
:param port: TCP port number
|
|
|
|
"""
|
|
|
|
|
|
|
|
if port not in self._used_tcp_ports:
|
|
|
|
self._used_tcp_ports.add(port)
|
|
|
|
|
|
|
|
def record_udp_port(self, port):
|
|
|
|
"""
|
|
|
|
Associate a reserved UDP port number with this project.
|
|
|
|
|
|
|
|
:param port: UDP port number
|
|
|
|
"""
|
|
|
|
|
|
|
|
if port not in self._used_udp_ports:
|
|
|
|
self._used_udp_ports.add(port)
|
|
|
|
|
|
|
|
def remove_tcp_port(self, port):
|
|
|
|
"""
|
|
|
|
Removes an associated TCP port number from this project.
|
|
|
|
|
|
|
|
:param port: TCP port number
|
|
|
|
"""
|
|
|
|
|
|
|
|
if port in self._used_tcp_ports:
|
|
|
|
self._used_tcp_ports.remove(port)
|
|
|
|
|
|
|
|
def remove_udp_port(self, port):
|
|
|
|
"""
|
|
|
|
Removes an associated UDP port number from this project.
|
|
|
|
|
|
|
|
:param port: UDP port number
|
|
|
|
"""
|
|
|
|
|
|
|
|
if port in self._used_udp_ports:
|
|
|
|
self._used_udp_ports.remove(port)
|
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
def module_working_directory(self, module_name):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Returns a working directory for the module
|
2016-05-14 00:48:10 +00:00
|
|
|
The directory is created if the directory doesn't exist.
|
2015-02-16 05:13:24 +00:00
|
|
|
|
|
|
|
:param module_name: name for the module
|
|
|
|
:returns: working directory
|
|
|
|
"""
|
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
workdir = self.module_working_path(module_name)
|
2017-01-17 18:37:38 +00:00
|
|
|
if not self._deleted:
|
|
|
|
try:
|
|
|
|
os.makedirs(workdir, exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not create module working directory: {e}")
|
2015-02-16 05:13:24 +00:00
|
|
|
return workdir
|
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
def module_working_path(self, module_name):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Returns the working directory for the module. If you want
|
2015-03-02 16:17:28 +00:00
|
|
|
to be sure to have the directory on disk take a look on:
|
|
|
|
module_working_directory
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
return os.path.join(self._path, "project-files", module_name)
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
def node_working_directory(self, node):
|
2015-01-20 13:31:47 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns a working directory for a specific node.
|
2015-01-20 13:31:47 +00:00
|
|
|
If the directory doesn't exist, the directory is created.
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param node: Node instance
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:returns: Node working directory
|
2015-01-20 13:31:47 +00:00
|
|
|
"""
|
|
|
|
|
2017-10-02 08:41:57 +00:00
|
|
|
workdir = self.node_working_path(node)
|
2017-01-17 18:37:38 +00:00
|
|
|
if not self._deleted:
|
|
|
|
try:
|
|
|
|
os.makedirs(workdir, exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not create the node working directory: {e}")
|
2015-01-24 01:33:49 +00:00
|
|
|
return workdir
|
|
|
|
|
2017-10-02 08:41:57 +00:00
|
|
|
def node_working_path(self, node):
|
|
|
|
"""
|
|
|
|
Returns a node working path for node. It doesn't create structure if not present on system.
|
|
|
|
:param node: Node instance
|
|
|
|
:return: Node working path
|
|
|
|
"""
|
|
|
|
return os.path.join(self._path, "project-files", node.manager.module_name.lower(), node.id)
|
|
|
|
|
2016-04-21 15:27:49 +00:00
|
|
|
def tmp_working_directory(self):
|
|
|
|
"""
|
|
|
|
A temporary directory. Will be clean at project open and close
|
|
|
|
"""
|
2016-04-22 14:22:03 +00:00
|
|
|
return os.path.join(self._path, "tmp")
|
2016-04-21 15:27:49 +00:00
|
|
|
|
2015-01-24 01:33:49 +00:00
|
|
|
def capture_working_directory(self):
|
|
|
|
"""
|
2018-10-27 07:47:17 +00:00
|
|
|
Returns a working directory where to store packet capture files.
|
2015-01-24 01:33:49 +00:00
|
|
|
|
|
|
|
:returns: path to the directory
|
|
|
|
"""
|
|
|
|
|
2018-10-27 07:47:17 +00:00
|
|
|
workdir = os.path.join(self._path, "project-files", "captures")
|
2017-01-17 18:37:38 +00:00
|
|
|
if not self._deleted:
|
|
|
|
try:
|
|
|
|
os.makedirs(workdir, exist_ok=True)
|
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not create the capture working directory: {e}")
|
2015-01-22 02:28:52 +00:00
|
|
|
return workdir
|
2015-01-20 13:31:47 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
def add_node(self, node):
|
2015-01-23 13:07:10 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Adds a node to the project.
|
|
|
|
In theory this should be called by the node manager.
|
2015-01-23 13:07:10 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param node: Node instance
|
2015-01-23 13:07:10 +00:00
|
|
|
"""
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
self._nodes.add(node)
|
2015-01-23 13:07:10 +00:00
|
|
|
|
2016-06-23 01:40:46 +00:00
|
|
|
def get_node(self, node_id):
|
|
|
|
"""
|
|
|
|
Returns a Node instance.
|
|
|
|
|
|
|
|
:param node_id: Node identifier
|
|
|
|
|
|
|
|
:returns: Node instance
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
UUID(node_id, version=4)
|
|
|
|
except ValueError:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Node ID {node_id} is not a valid UUID")
|
2016-06-23 01:40:46 +00:00
|
|
|
|
|
|
|
for node in self._nodes:
|
|
|
|
if node.id == node_id:
|
|
|
|
return node
|
|
|
|
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeNotFoundError(f"Node ID {node_id} doesn't exist")
|
2016-06-23 01:40:46 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def remove_node(self, node):
|
2015-01-23 13:34:50 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Removes a node from the project.
|
|
|
|
In theory this should be called by the node manager.
|
2015-01-23 13:34:50 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param node: Node instance
|
2015-01-23 13:34:50 +00:00
|
|
|
"""
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
if node in self._nodes:
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.delete()
|
2016-05-11 17:35:36 +00:00
|
|
|
self._nodes.remove(node)
|
2015-01-23 13:34:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def update(self, variables=None, **kwargs):
|
2018-05-09 13:29:35 +00:00
|
|
|
original_variables = self.variables
|
|
|
|
self.variables = variables
|
|
|
|
|
|
|
|
# we need to update docker nodes when variables changes
|
|
|
|
if original_variables != variables:
|
|
|
|
for node in self.nodes:
|
2021-04-13 09:16:50 +00:00
|
|
|
if hasattr(node, "update"):
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.update()
|
2018-05-09 13:29:35 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def close(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
Closes the project, but keep project data on disk
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2021-04-13 09:07:58 +00:00
|
|
|
project_nodes_id = {n.id for n in self.nodes}
|
2016-08-16 17:41:59 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
for module in self.compute():
|
2021-04-13 09:07:58 +00:00
|
|
|
module_nodes_id = {n.id for n in module.instance().nodes}
|
2016-08-16 17:41:59 +00:00
|
|
|
# We close the project only for the modules using it
|
|
|
|
if len(module_nodes_id & project_nodes_id):
|
2018-10-15 10:05:49 +00:00
|
|
|
await module.instance().project_closing(self)
|
2016-08-16 17:41:59 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._close_and_clean(False)
|
2016-08-16 17:41:59 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
for module in self.compute():
|
2021-04-13 09:07:58 +00:00
|
|
|
module_nodes_id = {n.id for n in module.instance().nodes}
|
2016-08-16 17:41:59 +00:00
|
|
|
# We close the project only for the modules using it
|
|
|
|
if len(module_nodes_id & project_nodes_id):
|
2018-10-15 10:05:49 +00:00
|
|
|
await module.instance().project_closed(self)
|
2015-01-23 15:02:26 +00:00
|
|
|
|
2016-04-21 15:27:49 +00:00
|
|
|
try:
|
|
|
|
if os.path.exists(self.tmp_working_directory()):
|
|
|
|
shutil.rmtree(self.tmp_working_directory())
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _close_and_clean(self, cleanup):
|
2015-01-23 15:02:26 +00:00
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Closes the project, and cleanup the disk if cleanup is True
|
2015-01-23 15:02:26 +00:00
|
|
|
|
2016-05-14 00:48:10 +00:00
|
|
|
:param cleanup: Whether to delete the project directory
|
2015-01-23 15:02:26 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-05 21:24:06 +00:00
|
|
|
tasks = []
|
2016-05-11 17:35:36 +00:00
|
|
|
for node in self._nodes:
|
2018-10-15 10:05:49 +00:00
|
|
|
tasks.append(asyncio.ensure_future(node.manager.close_node(node.id)))
|
2015-02-05 21:24:06 +00:00
|
|
|
|
|
|
|
if tasks:
|
2018-10-15 10:05:49 +00:00
|
|
|
done, _ = await asyncio.wait(tasks)
|
2015-02-05 21:24:06 +00:00
|
|
|
for future in done:
|
|
|
|
try:
|
|
|
|
future.result()
|
2015-07-25 22:46:23 +00:00
|
|
|
except (Exception, GeneratorExit) as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
log.error(f"Could not close node: {e}", exc_info=1)
|
2015-02-05 21:24:06 +00:00
|
|
|
|
2015-01-23 15:02:26 +00:00
|
|
|
if cleanup and os.path.exists(self.path):
|
2017-01-17 18:37:38 +00:00
|
|
|
self._deleted = True
|
2015-01-26 12:54:44 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await wait_run_in_executor(shutil.rmtree, self.path)
|
2021-04-13 09:07:58 +00:00
|
|
|
log.info(f"Project {self._id} with path '{self._path}' deleted")
|
2015-01-26 12:54:44 +00:00
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not delete the project directory: {e}")
|
2015-02-25 23:05:57 +00:00
|
|
|
else:
|
2021-04-13 09:07:58 +00:00
|
|
|
log.info(f"Project {self._id} with path '{self._path}' closed")
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-03-21 23:19:12 +00:00
|
|
|
if self._used_tcp_ports:
|
2021-04-13 09:07:58 +00:00
|
|
|
log.warning(f"Project {self.id} has TCP ports still in use: {self._used_tcp_ports}")
|
2015-03-21 23:19:12 +00:00
|
|
|
if self._used_udp_ports:
|
2021-04-13 09:07:58 +00:00
|
|
|
log.warning(f"Project {self.id} has UDP ports still in use: {self._used_udp_ports}")
|
2015-02-24 02:59:19 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
# clean the remaining ports that have not been cleaned by their respective node.
|
2015-03-21 23:19:12 +00:00
|
|
|
port_manager = PortManager.instance()
|
|
|
|
for port in self._used_tcp_ports.copy():
|
|
|
|
port_manager.release_tcp_port(port, self)
|
|
|
|
for port in self._used_udp_ports.copy():
|
2015-04-15 13:58:31 +00:00
|
|
|
port_manager.release_udp_port(port, self)
|
2015-02-24 02:59:19 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def delete(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Removes project from disk
|
|
|
|
"""
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
for module in self.compute():
|
2018-10-15 10:05:49 +00:00
|
|
|
await module.instance().project_closing(self)
|
|
|
|
await self._close_and_clean(True)
|
2016-04-15 15:57:06 +00:00
|
|
|
for module in self.compute():
|
2018-10-15 10:05:49 +00:00
|
|
|
await module.instance().project_closed(self)
|
2015-02-04 16:18:53 +00:00
|
|
|
|
2016-04-15 15:57:06 +00:00
|
|
|
def compute(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns all loaded modules from compute.
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
2015-03-02 16:17:28 +00:00
|
|
|
|
|
|
|
# We import it at the last time to avoid circular dependencies
|
2016-04-15 15:57:06 +00:00
|
|
|
from ..compute import MODULES
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
return MODULES
|
2015-03-04 15:01:56 +00:00
|
|
|
|
|
|
|
def emit(self, action, event):
|
|
|
|
"""
|
2015-05-13 19:53:42 +00:00
|
|
|
Send an event to all the client listening for notifications
|
2015-03-04 15:01:56 +00:00
|
|
|
|
2015-05-13 19:53:42 +00:00
|
|
|
:param action: Action name
|
|
|
|
:param event: Event to send
|
2015-03-04 15:01:56 +00:00
|
|
|
"""
|
2016-03-17 14:15:30 +00:00
|
|
|
NotificationManager.instance().emit(action, event, project_id=self.id)
|
2015-12-22 14:19:38 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def list_files(self):
|
2015-05-14 10:03:17 +00:00
|
|
|
"""
|
2016-05-14 00:48:10 +00:00
|
|
|
:returns: Array of files in project without temporary files. The files are dictionary {"path": "test.bin", "md5sum": "aaaaa"}
|
2015-05-14 10:03:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
files = []
|
2019-03-18 11:05:40 +00:00
|
|
|
for dirpath, dirnames, filenames in os.walk(self.path, followlinks=False):
|
2015-05-14 10:03:17 +00:00
|
|
|
for filename in filenames:
|
|
|
|
if not filename.endswith(".ghost"):
|
|
|
|
path = os.path.relpath(dirpath, self.path)
|
|
|
|
path = os.path.join(path, filename)
|
|
|
|
path = os.path.normpath(path)
|
|
|
|
file_info = {"path": path}
|
|
|
|
|
|
|
|
try:
|
2021-04-13 09:16:50 +00:00
|
|
|
file_info["md5sum"] = await wait_run_in_executor(
|
|
|
|
self._hash_file, os.path.join(dirpath, filename)
|
|
|
|
)
|
2015-05-14 10:03:17 +00:00
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
files.append(file_info)
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
def _hash_file(self, path):
|
|
|
|
"""
|
|
|
|
Compute and md5 hash for file
|
|
|
|
|
|
|
|
:returns: hexadecimal md5
|
|
|
|
"""
|
|
|
|
|
|
|
|
m = hashlib.md5()
|
|
|
|
with open(path, "rb") as f:
|
|
|
|
while True:
|
|
|
|
buf = f.read(128)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
m.update(buf)
|
|
|
|
return m.hexdigest()
|