mirror of
https://github.com/GNS3/gns3-server.git
synced 2025-01-30 21:03:49 +02:00
Cleanup code for getting the default project directory
This commit is contained in:
parent
a0344576ba
commit
9ccb55206d
@ -29,7 +29,7 @@ from .port_manager import PortManager
|
|||||||
from .notification_manager import NotificationManager
|
from .notification_manager import NotificationManager
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..utils.asyncio import wait_run_in_executor
|
from ..utils.asyncio import wait_run_in_executor
|
||||||
from ..utils.path import check_path_allowed
|
from ..utils.path import check_path_allowed, get_default_project_directory
|
||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
@ -63,7 +63,7 @@ class Project:
|
|||||||
self._used_udp_ports = set()
|
self._used_udp_ports = set()
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
location = self._config().get("project_directory", self._get_default_project_directory())
|
location = get_default_project_directory()
|
||||||
path = os.path.join(location, self._id)
|
path = os.path.join(location, self._id)
|
||||||
try:
|
try:
|
||||||
os.makedirs(path, exist_ok=True)
|
os.makedirs(path, exist_ok=True)
|
||||||
@ -95,22 +95,6 @@ class Project:
|
|||||||
|
|
||||||
return self._config().getboolean("local", False)
|
return self._config().getboolean("local", False)
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_default_project_directory(cls):
|
|
||||||
"""
|
|
||||||
Return the default location for the project directory
|
|
||||||
depending of the operating system
|
|
||||||
"""
|
|
||||||
|
|
||||||
server_config = Config.instance().get_section_config("Server")
|
|
||||||
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
|
|
||||||
path = os.path.normpath(path)
|
|
||||||
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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
|
|
||||||
@ -418,8 +402,7 @@ class Project:
|
|||||||
At startup drop old temporary project. After a crash for example
|
At startup drop old temporary project. After a crash for example
|
||||||
"""
|
"""
|
||||||
|
|
||||||
config = Config.instance().get_section_config("Server")
|
directory = get_default_project_directory()
|
||||||
directory = config.get("project_directory", cls._get_default_project_directory())
|
|
||||||
if os.path.exists(directory):
|
if os.path.exists(directory):
|
||||||
for project in os.listdir(directory):
|
for project in os.listdir(directory):
|
||||||
path = os.path.join(directory, project)
|
path = os.path.join(directory, project)
|
||||||
|
@ -25,7 +25,7 @@ from .vm import VM
|
|||||||
from .udp_link import UDPLink
|
from .udp_link import UDPLink
|
||||||
from ..notification_queue import NotificationQueue
|
from ..notification_queue import NotificationQueue
|
||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..utils.path import check_path_allowed
|
from ..utils.path import check_path_allowed, get_default_project_directory
|
||||||
|
|
||||||
|
|
||||||
class Project:
|
class Project:
|
||||||
@ -50,8 +50,7 @@ class Project:
|
|||||||
self._id = project_id
|
self._id = project_id
|
||||||
|
|
||||||
if path is None:
|
if path is None:
|
||||||
location = self._config().get("project_directory", self._get_default_project_directory())
|
path = os.path.join(get_default_project_directory(), self._id)
|
||||||
path = os.path.join(location, self._id)
|
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
self._temporary = temporary
|
self._temporary = temporary
|
||||||
@ -205,22 +204,6 @@ class Project:
|
|||||||
for listener in self._listeners:
|
for listener in self._listeners:
|
||||||
listener.put_nowait((action, event, kwargs))
|
listener.put_nowait((action, event, kwargs))
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _get_default_project_directory(cls):
|
|
||||||
"""
|
|
||||||
Return the default location for the project directory
|
|
||||||
depending of the operating system
|
|
||||||
"""
|
|
||||||
|
|
||||||
server_config = Config.instance().get_section_config("Server")
|
|
||||||
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
|
|
||||||
path = os.path.normpath(path)
|
|
||||||
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
|
|
||||||
|
|
||||||
def __json__(self):
|
def __json__(self):
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -21,6 +21,22 @@ import aiohttp
|
|||||||
from ..config import Config
|
from ..config import Config
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_project_directory():
|
||||||
|
"""
|
||||||
|
Return the default location for the project directory
|
||||||
|
depending of the operating system
|
||||||
|
"""
|
||||||
|
|
||||||
|
server_config = Config.instance().get_section_config("Server")
|
||||||
|
path = os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))
|
||||||
|
path = os.path.normpath(path)
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def check_path_allowed(path):
|
def check_path_allowed(path):
|
||||||
"""
|
"""
|
||||||
If the server is non local raise an error if
|
If the server is non local raise an error if
|
||||||
@ -30,10 +46,10 @@ def check_path_allowed(path):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
config = Config.instance().get_section_config("Server")
|
config = Config.instance().get_section_config("Server")
|
||||||
project_directory = config.get("project_directory")
|
|
||||||
|
|
||||||
|
project_directory = get_default_project_directory()
|
||||||
if len(os.path.commonprefix([project_directory, path])) == len(project_directory):
|
if len(os.path.commonprefix([project_directory, path])) == len(project_directory):
|
||||||
return
|
return
|
||||||
|
|
||||||
if config.getboolean("local") is False:
|
if "local" in config and config.getboolean("local") is False:
|
||||||
raise aiohttp.web.HTTPForbidden(text="The path is not allowed")
|
raise aiohttp.web.HTTPForbidden(text="The path is not allowed")
|
||||||
|
@ -69,10 +69,10 @@ def test_clean_tmp_directory(async_run):
|
|||||||
|
|
||||||
def test_path(tmpdir):
|
def test_path(tmpdir):
|
||||||
|
|
||||||
directory = Config.instance().get_section_config("Server").get("project_directory")
|
directory = Config.instance().get_section_config("Server").get("projects_path")
|
||||||
|
|
||||||
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
||||||
with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory):
|
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
|
||||||
p = Project(project_id=str(uuid4()))
|
p = Project(project_id=str(uuid4()))
|
||||||
assert p.path == os.path.join(directory, p.id)
|
assert p.path == os.path.join(directory, p.id)
|
||||||
assert os.path.exists(os.path.join(directory, p.id))
|
assert os.path.exists(os.path.join(directory, p.id))
|
||||||
@ -124,7 +124,7 @@ def test_json(tmpdir):
|
|||||||
|
|
||||||
|
|
||||||
def test_vm_working_directory(tmpdir, vm):
|
def test_vm_working_directory(tmpdir, vm):
|
||||||
directory = Config.instance().get_section_config("Server").get("project_directory")
|
directory = Config.instance().get_section_config("Server").get("projects_path")
|
||||||
|
|
||||||
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
with patch("gns3server.compute.project.Project.is_local", return_value=True):
|
||||||
p = Project(project_id=str(uuid4()))
|
p = Project(project_id=str(uuid4()))
|
||||||
@ -211,15 +211,6 @@ def test_project_close_temporary_project(loop, manager):
|
|||||||
assert os.path.exists(directory) is False
|
assert os.path.exists(directory) is False
|
||||||
|
|
||||||
|
|
||||||
def test_get_default_project_directory(monkeypatch):
|
|
||||||
|
|
||||||
monkeypatch.undo()
|
|
||||||
project = Project(project_id=str(uuid4()))
|
|
||||||
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
|
|
||||||
assert project._get_default_project_directory() == path
|
|
||||||
assert os.path.exists(path)
|
|
||||||
|
|
||||||
|
|
||||||
def test_clean_project_directory(tmpdir):
|
def test_clean_project_directory(tmpdir):
|
||||||
|
|
||||||
# A non anonymous project with uuid.
|
# A non anonymous project with uuid.
|
||||||
@ -237,7 +228,7 @@ def test_clean_project_directory(tmpdir):
|
|||||||
with open(str(tmp), 'w+') as f:
|
with open(str(tmp), 'w+') as f:
|
||||||
f.write("1")
|
f.write("1")
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
|
||||||
Project.clean_project_directory()
|
Project.clean_project_directory()
|
||||||
|
|
||||||
assert os.path.exists(str(project1))
|
assert os.path.exists(str(project1))
|
||||||
@ -247,7 +238,7 @@ def test_clean_project_directory(tmpdir):
|
|||||||
|
|
||||||
def test_list_files(tmpdir, loop):
|
def test_list_files(tmpdir, loop):
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
|
||||||
project = Project(project_id=str(uuid4()))
|
project = Project(project_id=str(uuid4()))
|
||||||
path = project.path
|
path = project.path
|
||||||
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
os.makedirs(os.path.join(path, "vm-1", "dynamips"))
|
||||||
|
@ -203,7 +203,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config):
|
|||||||
|
|
||||||
port_manager._instance = port_manager
|
port_manager._instance = port_manager
|
||||||
os.makedirs(os.path.join(tmppath, 'projects'))
|
os.makedirs(os.path.join(tmppath, 'projects'))
|
||||||
config.set("Server", "project_directory", os.path.join(tmppath, 'projects'))
|
config.set("Server", "projects_path", os.path.join(tmppath, 'projects'))
|
||||||
config.set("Server", "images_path", os.path.join(tmppath, 'images'))
|
config.set("Server", "images_path", os.path.join(tmppath, 'images'))
|
||||||
config.set("Server", "auth", False)
|
config.set("Server", "auth", False)
|
||||||
config.set("Server", "controller", True)
|
config.set("Server", "controller", True)
|
||||||
@ -216,7 +216,7 @@ def run_around_tests(monkeypatch, port_manager, controller, config):
|
|||||||
# Force turn off KVM because it's not available on CI
|
# Force turn off KVM because it's not available on CI
|
||||||
config.set("Qemu", "enable_kvm", False)
|
config.set("Qemu", "enable_kvm", False)
|
||||||
|
|
||||||
monkeypatch.setattr("gns3server.compute.project.Project._get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
|
monkeypatch.setattr("gns3server.utils.path.get_default_project_directory", lambda *args: os.path.join(tmppath, 'projects'))
|
||||||
|
|
||||||
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
|
# Force sys.platform to the original value. Because it seem not be restore correctly at each tests
|
||||||
sys.platform = sys.original_platform
|
sys.platform = sys.original_platform
|
||||||
|
@ -43,9 +43,9 @@ def test_json(tmpdir):
|
|||||||
|
|
||||||
def test_path(tmpdir):
|
def test_path(tmpdir):
|
||||||
|
|
||||||
directory = Config.instance().get_section_config("Server").get("project_directory")
|
directory = Config.instance().get_section_config("Server").get("projects_path")
|
||||||
|
|
||||||
with patch("gns3server.compute.project.Project._get_default_project_directory", return_value=directory):
|
with patch("gns3server.utils.path.get_default_project_directory", return_value=directory):
|
||||||
p = Project(project_id=str(uuid4()))
|
p = Project(project_id=str(uuid4()))
|
||||||
assert p.path == os.path.join(directory, p.id)
|
assert p.path == os.path.join(directory, p.id)
|
||||||
assert os.path.exists(os.path.join(directory, p.id))
|
assert os.path.exists(os.path.join(directory, p.id))
|
||||||
|
@ -201,7 +201,7 @@ def test_close_project_invalid_uuid(http_compute):
|
|||||||
|
|
||||||
def test_get_file(http_compute, tmpdir):
|
def test_get_file(http_compute, tmpdir):
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
|
||||||
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
||||||
|
|
||||||
with open(os.path.join(project.path, "hello"), "w+") as f:
|
with open(os.path.join(project.path, "hello"), "w+") as f:
|
||||||
@ -220,7 +220,7 @@ def test_get_file(http_compute, tmpdir):
|
|||||||
|
|
||||||
def test_stream_file(http_compute, tmpdir):
|
def test_stream_file(http_compute, tmpdir):
|
||||||
|
|
||||||
with patch("gns3server.config.Config.get_section_config", return_value={"project_directory": str(tmpdir)}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"projects_path": str(tmpdir)}):
|
||||||
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
project = ProjectManager.instance().create_project(project_id="01010203-0405-0607-0809-0a0b0c0d0e0b")
|
||||||
|
|
||||||
with open(os.path.join(project.path, "hello"), "w+") as f:
|
with open(os.path.join(project.path, "hello"), "w+") as f:
|
||||||
|
@ -15,18 +15,28 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
from gns3server.utils.path import check_path_allowed
|
from gns3server.utils.path import check_path_allowed, get_default_project_directory
|
||||||
|
|
||||||
|
|
||||||
def test_check_path_allowed(config, tmpdir):
|
def test_check_path_allowed(config, tmpdir):
|
||||||
config.set("Server", "local", False)
|
config.set("Server", "local", False)
|
||||||
config.set("Server", "project_directory", str(tmpdir))
|
config.set("Server", "projects_path", str(tmpdir))
|
||||||
with pytest.raises(aiohttp.web.HTTPForbidden):
|
with pytest.raises(aiohttp.web.HTTPForbidden):
|
||||||
check_path_allowed("/private")
|
check_path_allowed("/private")
|
||||||
|
|
||||||
config.set("Server", "local", True)
|
config.set("Server", "local", True)
|
||||||
check_path_allowed(str(tmpdir / "hello" / "world"))
|
check_path_allowed(str(tmpdir / "hello" / "world"))
|
||||||
check_path_allowed("/private")
|
check_path_allowed("/private")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_default_project_directory(config):
|
||||||
|
|
||||||
|
config.clear()
|
||||||
|
|
||||||
|
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
|
||||||
|
assert get_default_project_directory() == path
|
||||||
|
assert os.path.exists(path)
|
||||||
|
Loading…
Reference in New Issue
Block a user