2015-01-19 15:23:41 +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/>.
|
|
|
|
|
2015-01-21 02:02:22 +00:00
|
|
|
import aiohttp
|
2015-01-19 15:23:41 +00:00
|
|
|
import os
|
2015-01-23 10:28:58 +00:00
|
|
|
import shutil
|
2015-01-26 11:10:30 +00:00
|
|
|
import asyncio
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
from uuid import UUID, uuid4
|
2015-02-24 02:59:19 +00:00
|
|
|
from .port_manager import PortManager
|
2015-01-26 11:10:30 +00:00
|
|
|
from ..config import Config
|
|
|
|
from ..utils.asyncio import wait_run_in_executor
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-01-23 17:37:29 +00:00
|
|
|
import logging
|
|
|
|
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
|
|
|
"""
|
|
|
|
A project contains a list of VM.
|
|
|
|
In theory VM are isolated project/project.
|
|
|
|
|
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)
|
|
|
|
:param location: parent path of the project. (None should create a tmp directory)
|
|
|
|
:param temporary: boolean to tell if the project is a temporary project (destroy when closed)
|
2015-01-19 15:23:41 +00:00
|
|
|
"""
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
def __init__(self, name=None, project_id=None, path=None, location=None, temporary=False):
|
2015-01-19 21:43:35 +00:00
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
self._name = name
|
2015-02-04 20:48:29 +00:00
|
|
|
if project_id is None:
|
|
|
|
self._id = str(uuid4())
|
2015-01-19 15:23:41 +00:00
|
|
|
else:
|
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-04 20:48:29 +00:00
|
|
|
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(project_id))
|
|
|
|
self._id = project_id
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-02-04 20:17:00 +00:00
|
|
|
self._location = None
|
2015-01-19 15:23:41 +00:00
|
|
|
if location is None:
|
2015-02-04 20:17:00 +00:00
|
|
|
self._location = self._config().get("project_directory", self._get_default_project_directory())
|
2015-01-23 15:57:41 +00:00
|
|
|
else:
|
2015-02-04 20:17:00 +00:00
|
|
|
self.location = location
|
2015-01-19 15:23:41 +00:00
|
|
|
|
2015-01-23 13:07:10 +00:00
|
|
|
self._vms = set()
|
2015-01-23 10:28:58 +00:00
|
|
|
self._vms_to_destroy = set()
|
2015-02-05 16:52:37 +00:00
|
|
|
self.temporary = temporary
|
2015-03-21 23:19:12 +00:00
|
|
|
self._used_tcp_ports = set()
|
|
|
|
self._used_udp_ports = set()
|
2015-02-05 16:52:37 +00:00
|
|
|
|
|
|
|
if path is None:
|
|
|
|
path = os.path.join(self._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:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
|
2015-02-05 16:52:37 +00:00
|
|
|
self.path = path
|
|
|
|
|
2015-02-25 23:05:57 +00:00
|
|
|
log.info("Project {id} with path '{path}' created".format(path=self._path, id=self._id))
|
2015-01-20 13:31:47 +00:00
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
def __json__(self):
|
|
|
|
|
|
|
|
return {
|
2015-03-09 01:13:01 +00:00
|
|
|
"name": self._name,
|
2015-02-16 05:13:24 +00:00
|
|
|
"project_id": self._id,
|
|
|
|
"location": self._location,
|
|
|
|
"temporary": self._temporary,
|
|
|
|
"path": self._path,
|
|
|
|
}
|
|
|
|
|
2015-02-04 20:17:00 +00:00
|
|
|
def _config(self):
|
|
|
|
|
|
|
|
return Config.instance().get_section_config("Server")
|
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
def is_local(self):
|
|
|
|
|
2015-03-09 18:45:02 +00:00
|
|
|
return self._config().getboolean("local", False)
|
2015-03-09 01:13:01 +00:00
|
|
|
|
2015-02-04 16:18:53 +00:00
|
|
|
@classmethod
|
|
|
|
def _get_default_project_directory(cls):
|
2015-01-23 16:39:17 +00:00
|
|
|
"""
|
|
|
|
Return the default location for the project directory
|
|
|
|
depending of the operating system
|
|
|
|
"""
|
|
|
|
|
2015-02-24 00:08:34 +00:00
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
|
2015-04-27 21:12:13 +00:00
|
|
|
path = os.path.normpath(path)
|
2015-01-23 16:39:17 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(path, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create project directory: {}".format(e))
|
|
|
|
return path
|
|
|
|
|
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 location(self):
|
|
|
|
|
|
|
|
return self._location
|
|
|
|
|
2015-02-04 20:17:00 +00:00
|
|
|
@location.setter
|
|
|
|
def location(self, location):
|
|
|
|
|
2015-03-09 01:13:01 +00:00
|
|
|
if location != self._location and self.is_local() is False:
|
2015-02-08 21:44:56 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory location")
|
2015-02-04 20:17:00 +00:00
|
|
|
|
|
|
|
self._location = location
|
|
|
|
|
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"):
|
2015-03-09 01:13:01 +00:00
|
|
|
if path != self._path and self.is_local() is False:
|
2015-02-08 21:44:56 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden(text="You are not allowed to modify the project directory location")
|
2015-02-04 20:17:00 +00:00
|
|
|
|
2015-05-04 12:04:57 +00:00
|
|
|
old_path = None
|
|
|
|
if hasattr(self, "_path"):
|
|
|
|
old_path = self._path
|
|
|
|
|
2015-02-04 20:17:00 +00:00
|
|
|
self._path = path
|
2015-02-05 10:39:32 +00:00
|
|
|
self._update_temporary_file()
|
2015-02-04 20:17:00 +00:00
|
|
|
|
2015-05-04 12:04:57 +00:00
|
|
|
# The order of operation is important because we want to avoid losing
|
|
|
|
# data
|
|
|
|
if old_path:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(old_path)
|
|
|
|
except OSError as e:
|
2015-05-06 08:40:51 +00:00
|
|
|
raise aiohttp.web.HTTPConflict(text="Can't remove temporary directory {}: {}".format(old_path, e))
|
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):
|
|
|
|
|
|
|
|
self._name = name
|
|
|
|
|
2015-01-23 13:07:10 +00:00
|
|
|
@property
|
|
|
|
def vms(self):
|
|
|
|
|
|
|
|
return self._vms
|
|
|
|
|
2015-01-23 15:13:58 +00:00
|
|
|
@property
|
|
|
|
def temporary(self):
|
|
|
|
|
|
|
|
return self._temporary
|
|
|
|
|
|
|
|
@temporary.setter
|
|
|
|
def temporary(self, temporary):
|
|
|
|
|
2015-02-04 16:18:53 +00:00
|
|
|
if hasattr(self, 'temporary') and temporary == self._temporary:
|
|
|
|
return
|
|
|
|
|
2015-01-23 15:13:58 +00:00
|
|
|
self._temporary = temporary
|
2015-02-05 10:39:32 +00:00
|
|
|
self._update_temporary_file()
|
|
|
|
|
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-05 10:39:32 +00:00
|
|
|
def _update_temporary_file(self):
|
|
|
|
"""
|
|
|
|
Update the .gns3_temporary file in order to reflect current
|
|
|
|
project status.
|
|
|
|
"""
|
2015-01-23 15:13:58 +00:00
|
|
|
|
2015-02-05 16:52:37 +00:00
|
|
|
if not hasattr(self, "_path"):
|
|
|
|
return
|
|
|
|
|
2015-02-04 16:18:53 +00:00
|
|
|
if self._temporary:
|
|
|
|
try:
|
|
|
|
with open(os.path.join(self._path, ".gns3_temporary"), 'w+') as f:
|
|
|
|
f.write("1")
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create temporary project: {}".format(e))
|
|
|
|
else:
|
|
|
|
if os.path.exists(os.path.join(self._path, ".gns3_temporary")):
|
2015-05-05 08:51:51 +00:00
|
|
|
try:
|
|
|
|
os.remove(os.path.join(self._path, ".gns3_temporary"))
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not mark project as no longer temporary: {}".format(e))
|
2015-02-04 16:18:53 +00:00
|
|
|
|
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
|
2015-02-16 05:13:24 +00:00
|
|
|
If the directory doesn't exist, the directory is created.
|
|
|
|
|
|
|
|
: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)
|
2015-02-16 05:13:24 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(workdir, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create module working directory: {}".format(e))
|
|
|
|
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)
|
|
|
|
|
2015-01-23 10:28:58 +00:00
|
|
|
def vm_working_directory(self, vm):
|
2015-01-20 13:31:47 +00:00
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Returns a working directory for a specific VM.
|
2015-01-20 13:31:47 +00:00
|
|
|
If the directory doesn't exist, the directory is created.
|
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
:param vm: VM instance
|
2015-04-08 17:17:34 +00:00
|
|
|
|
2015-02-16 05:13:24 +00:00
|
|
|
:returns: VM working directory
|
2015-01-20 13:31:47 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-19 01:40:01 +00:00
|
|
|
workdir = os.path.join(self._path, "project-files", vm.manager.module_name.lower(), vm.id)
|
2015-01-21 02:02:22 +00:00
|
|
|
try:
|
2015-01-22 02:28:52 +00:00
|
|
|
os.makedirs(workdir, exist_ok=True)
|
2015-01-21 02:02:22 +00:00
|
|
|
except OSError as e:
|
2015-01-24 01:33:49 +00:00
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create the VM working directory: {}".format(e))
|
|
|
|
return workdir
|
|
|
|
|
|
|
|
def capture_working_directory(self):
|
|
|
|
"""
|
2015-04-08 17:17:34 +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
|
|
|
|
"""
|
|
|
|
|
2015-02-19 01:40:01 +00:00
|
|
|
workdir = os.path.join(self._path, "project-files", "captures")
|
2015-01-24 01:33:49 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(workdir, exist_ok=True)
|
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not create the capture working directory: {}".format(e))
|
2015-01-22 02:28:52 +00:00
|
|
|
return workdir
|
2015-01-20 13:31:47 +00:00
|
|
|
|
2015-01-23 10:28:58 +00:00
|
|
|
def mark_vm_for_destruction(self, vm):
|
|
|
|
"""
|
|
|
|
:param vm: An instance of VM
|
|
|
|
"""
|
|
|
|
|
2015-01-23 13:34:50 +00:00
|
|
|
self.remove_vm(vm)
|
2015-01-23 10:28:58 +00:00
|
|
|
self._vms_to_destroy.add(vm)
|
|
|
|
|
2015-01-23 13:07:10 +00:00
|
|
|
def add_vm(self, vm):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Adds a VM to the project.
|
2015-01-23 13:34:50 +00:00
|
|
|
In theory this should be called by the VM manager.
|
2015-01-23 13:07:10 +00:00
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
:param vm: VM instance
|
2015-01-23 13:07:10 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
self._vms.add(vm)
|
|
|
|
|
2015-01-23 13:34:50 +00:00
|
|
|
def remove_vm(self, vm):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Removes a VM from the project.
|
2015-01-23 13:34:50 +00:00
|
|
|
In theory this should be called by the VM manager.
|
|
|
|
|
2015-02-15 19:18:12 +00:00
|
|
|
:param vm: VM instance
|
2015-01-23 13:34:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if vm in self._vms:
|
|
|
|
self._vms.remove(vm)
|
|
|
|
|
2015-01-26 11:10:30 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-23 10:48:20 +00:00
|
|
|
def close(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Closes the project, but keep information on disk
|
|
|
|
"""
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
for module in self.modules():
|
|
|
|
yield from module.instance().project_closing(self)
|
2015-01-26 11:10:30 +00:00
|
|
|
yield from self._close_and_clean(self._temporary)
|
2015-03-02 16:17:28 +00:00
|
|
|
for module in self.modules():
|
|
|
|
yield from module.instance().project_closed(self)
|
2015-01-23 15:02:26 +00:00
|
|
|
|
2015-01-26 11:10:30 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-23 15:02:26 +00:00
|
|
|
def _close_and_clean(self, cleanup):
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
:param cleanup: If True drop the project directory
|
|
|
|
"""
|
|
|
|
|
2015-02-05 21:24:06 +00:00
|
|
|
tasks = []
|
2015-01-23 13:07:10 +00:00
|
|
|
for vm in self._vms:
|
2015-03-06 14:48:16 +00:00
|
|
|
tasks.append(asyncio.async(vm.manager.close_vm(vm.id)))
|
2015-02-05 21:24:06 +00:00
|
|
|
|
|
|
|
if tasks:
|
|
|
|
done, _ = yield from asyncio.wait(tasks)
|
|
|
|
for future in done:
|
|
|
|
try:
|
|
|
|
future.result()
|
|
|
|
except Exception as e:
|
2015-02-15 19:18:12 +00:00
|
|
|
log.error("Could not close VM or device {}".format(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):
|
2015-01-26 12:54:44 +00:00
|
|
|
try:
|
|
|
|
yield from wait_run_in_executor(shutil.rmtree, self.path)
|
2015-02-25 23:05:57 +00:00
|
|
|
log.info("Project {id} with path '{path}' deleted".format(path=self._path, id=self._id))
|
2015-01-26 12:54:44 +00:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not delete the project directory: {}".format(e))
|
2015-02-25 23:05:57 +00:00
|
|
|
else:
|
|
|
|
log.info("Project {id} with path '{path}' closed".format(path=self._path, id=self._id))
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-03-21 23:19:12 +00:00
|
|
|
if self._used_tcp_ports:
|
|
|
|
log.warning("Project {} has TCP ports still in use: {}".format(self.id, self._used_tcp_ports))
|
|
|
|
if self._used_udp_ports:
|
|
|
|
log.warning("Project {} has UDP ports still in use: {}".format(self.id, self._used_udp_ports))
|
2015-02-24 02:59:19 +00:00
|
|
|
|
2015-03-21 23:19:12 +00:00
|
|
|
# clean the remaining ports that have not been cleaned by their respective VM or device.
|
|
|
|
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
|
|
|
|
2015-01-26 11:10:30 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-23 10:28:58 +00:00
|
|
|
def commit(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Writes project changes on disk
|
|
|
|
"""
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-01-23 10:28:58 +00:00
|
|
|
while self._vms_to_destroy:
|
|
|
|
vm = self._vms_to_destroy.pop()
|
2015-02-16 05:13:24 +00:00
|
|
|
yield from vm.delete()
|
2015-01-23 13:34:50 +00:00
|
|
|
self.remove_vm(vm)
|
2015-03-21 20:52:17 +00:00
|
|
|
for module in self.modules():
|
|
|
|
yield from module.instance().project_committed(self)
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-01-26 11:10:30 +00:00
|
|
|
@asyncio.coroutine
|
2015-01-23 10:48:20 +00:00
|
|
|
def delete(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Removes project from disk
|
|
|
|
"""
|
2015-01-23 10:48:20 +00:00
|
|
|
|
2015-03-02 16:17:28 +00:00
|
|
|
for module in self.modules():
|
|
|
|
yield from module.instance().project_closing(self)
|
2015-01-26 11:10:30 +00:00
|
|
|
yield from self._close_and_clean(True)
|
2015-03-02 16:17:28 +00:00
|
|
|
for module in self.modules():
|
|
|
|
yield from module.instance().project_closed(self)
|
2015-02-04 16:18:53 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def clean_project_directory(cls):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
At startup drop old temporary project. After a crash for example
|
|
|
|
"""
|
2015-02-04 16:18:53 +00:00
|
|
|
|
|
|
|
config = Config.instance().get_section_config("Server")
|
|
|
|
directory = config.get("project_directory", cls._get_default_project_directory())
|
|
|
|
if os.path.exists(directory):
|
|
|
|
for project in os.listdir(directory):
|
|
|
|
path = os.path.join(directory, project)
|
|
|
|
if os.path.exists(os.path.join(path, ".gns3_temporary")):
|
|
|
|
log.warning("Purge old temporary project {}".format(project))
|
|
|
|
shutil.rmtree(path)
|
2015-03-02 16:17:28 +00:00
|
|
|
|
|
|
|
def modules(self):
|
2015-04-08 17:17:34 +00:00
|
|
|
"""
|
|
|
|
Returns all loaded VM modules.
|
|
|
|
"""
|
2015-03-02 16:17:28 +00:00
|
|
|
|
|
|
|
# We import it at the last time to avoid circular dependencies
|
|
|
|
from ..modules import MODULES
|
|
|
|
return MODULES
|