2016-03-10 09:32:07 +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/>.
|
|
|
|
|
2016-07-20 19:52:12 +00:00
|
|
|
import os
|
2016-03-10 09:32:07 +00:00
|
|
|
import aiohttp
|
|
|
|
import asyncio
|
2016-07-21 12:48:13 +00:00
|
|
|
import tempfile
|
2019-02-26 08:55:07 +00:00
|
|
|
import zipfile
|
2019-03-06 16:00:01 +00:00
|
|
|
import aiofiles
|
2019-02-26 08:55:07 +00:00
|
|
|
import time
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-05-14 00:00:07 +00:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
2016-07-21 12:48:13 +00:00
|
|
|
from gns3server.controller.import_project import import_project
|
2016-07-21 16:15:35 +00:00
|
|
|
from gns3server.controller.export_project import export_project
|
2019-02-26 08:55:07 +00:00
|
|
|
from gns3server.utils.asyncio import aiozipstream
|
2021-05-15 08:05:32 +00:00
|
|
|
from gns3server.utils.path import is_safe_path
|
2016-06-15 14:43:03 +00:00
|
|
|
from gns3server.config import Config
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
|
2016-05-14 00:00:07 +00:00
|
|
|
from gns3server.schemas.project import (
|
|
|
|
PROJECT_OBJECT_SCHEMA,
|
2016-08-15 14:44:09 +00:00
|
|
|
PROJECT_UPDATE_SCHEMA,
|
2016-06-15 14:43:03 +00:00
|
|
|
PROJECT_LOAD_SCHEMA,
|
2020-05-27 02:44:47 +00:00
|
|
|
PROJECT_CREATE_SCHEMA,
|
|
|
|
PROJECT_DUPLICATE_SCHEMA
|
2016-05-14 00:00:07 +00:00
|
|
|
)
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def process_websocket(ws):
|
2016-08-23 19:29:46 +00:00
|
|
|
"""
|
|
|
|
Process ping / pong and close message
|
|
|
|
"""
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await ws.receive()
|
2017-05-16 17:28:47 +00:00
|
|
|
except aiohttp.WSServerHandshakeError:
|
2016-08-23 19:29:46 +00:00
|
|
|
pass
|
|
|
|
|
2019-03-06 16:00:01 +00:00
|
|
|
CHUNK_SIZE = 1024 * 8 # 8KB
|
|
|
|
|
2016-08-23 19:29:46 +00:00
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
class ProjectHandler:
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects",
|
|
|
|
description="Create a new project on the server",
|
|
|
|
status_codes={
|
|
|
|
201: "Project created",
|
|
|
|
409: "Project already created"
|
|
|
|
},
|
|
|
|
output=PROJECT_OBJECT_SCHEMA,
|
|
|
|
input=PROJECT_CREATE_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def create_project(request, response):
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.add_project(**request.json)
|
2016-03-10 09:32:07 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
|
|
|
|
2016-04-20 12:38:14 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects",
|
|
|
|
description="List projects",
|
|
|
|
status_codes={
|
|
|
|
200: "List of projects",
|
|
|
|
})
|
|
|
|
def list_projects(request, response):
|
|
|
|
controller = Controller.instance()
|
2016-05-14 00:00:07 +00:00
|
|
|
response.json([p for p in controller.projects.values()])
|
2016-04-20 12:38:14 +00:00
|
|
|
|
2016-03-17 16:32:37 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}",
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Get a project",
|
2016-03-17 16:32:37 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 16:32:37 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-05-14 00:00:07 +00:00
|
|
|
200: "Project information returned",
|
2016-03-17 16:32:37 +00:00
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def get(request, response):
|
|
|
|
controller = Controller.instance()
|
2016-05-11 17:35:36 +00:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 16:32:37 +00:00
|
|
|
response.json(project)
|
|
|
|
|
2016-08-15 14:44:09 +00:00
|
|
|
@Route.put(
|
|
|
|
r"/projects/{project_id}",
|
|
|
|
status_codes={
|
|
|
|
200: "Node updated",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Update a project instance",
|
|
|
|
input=PROJECT_UPDATE_SCHEMA,
|
|
|
|
output=PROJECT_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def update(request, response):
|
2016-08-15 14:44:09 +00:00
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
|
|
|
|
# Ignore these because we only use them when creating a project
|
|
|
|
request.json.pop("project_id", None)
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
await project.update(**request.json)
|
2016-08-15 14:44:09 +00:00
|
|
|
response.set_status(200)
|
|
|
|
response.json(project)
|
|
|
|
|
2018-11-08 10:47:09 +00:00
|
|
|
@Route.delete(
|
|
|
|
r"/projects/{project_id}",
|
|
|
|
description="Delete a project from disk",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Changes have been written on disk",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
async def delete(request, response):
|
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
|
|
|
await project.delete()
|
|
|
|
controller.remove_project(project)
|
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/stats",
|
|
|
|
description="Get a project statistics",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "Project statistics returned",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
|
|
|
def get(request, response):
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
|
|
|
response.json(project.stats())
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/close",
|
|
|
|
description="Close a project",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
2016-03-10 09:32:07 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "The project has been closed",
|
|
|
|
404: "The project doesn't exist"
|
2016-06-15 14:43:03 +00:00
|
|
|
},
|
|
|
|
output=PROJECT_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def close(request, response):
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 17:35:36 +00:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await project.close()
|
2019-11-11 07:32:57 +00:00
|
|
|
response.set_status(204)
|
2016-06-15 13:12:38 +00:00
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/open",
|
|
|
|
description="Open a project",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "The project has been opened",
|
|
|
|
404: "The project doesn't exist"
|
2016-06-15 14:43:03 +00:00
|
|
|
},
|
|
|
|
output=PROJECT_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def open(request, response):
|
2016-06-15 13:12:38 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await project.open()
|
2016-06-15 13:12:38 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
2016-03-10 09:32:07 +00:00
|
|
|
|
2016-06-15 14:43:03 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/load",
|
|
|
|
description="Open a project (only local server)",
|
|
|
|
parameters={
|
|
|
|
"path": ".gns3 path",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "The project has been opened",
|
|
|
|
403: "The server is not the local server"
|
|
|
|
},
|
|
|
|
input=PROJECT_LOAD_SCHEMA,
|
|
|
|
output=PROJECT_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def load(request, response):
|
2016-06-15 14:43:03 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
config = Config.instance()
|
2019-10-30 08:25:06 +00:00
|
|
|
dot_gns3_file = request.json.get("path")
|
2016-06-15 14:43:03 +00:00
|
|
|
if config.get_section_config("Server").getboolean("local", False) is False:
|
2019-10-30 08:25:06 +00:00
|
|
|
log.error("Cannot load '{}' because the server has not been started with the '--local' parameter".format(dot_gns3_file))
|
2016-06-15 14:43:03 +00:00
|
|
|
response.set_status(403)
|
|
|
|
return
|
2019-10-30 08:25:06 +00:00
|
|
|
project = await controller.load_project(dot_gns3_file,)
|
2016-06-15 14:43:03 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(project)
|
|
|
|
|
2016-03-17 16:32:37 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/notifications",
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Receive notifications about projects",
|
2016-03-17 16:32:37 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 16:32:37 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "End of stream",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def notification(request, response):
|
2016-03-17 16:32:37 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2019-02-23 16:20:11 +00:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 16:32:37 +00:00
|
|
|
response.content_type = "application/json"
|
|
|
|
response.set_status(200)
|
|
|
|
response.enable_chunked_encoding()
|
2018-10-15 10:05:49 +00:00
|
|
|
await response.prepare(request)
|
2019-02-23 16:20:11 +00:00
|
|
|
log.info("New client has connected to the notification stream for project ID '{}' (HTTP long-polling method)".format(project.id))
|
2019-02-23 14:08:52 +00:00
|
|
|
|
|
|
|
try:
|
2019-02-23 16:20:11 +00:00
|
|
|
with controller.notification.project_queue(project.id) as queue:
|
2019-02-23 14:08:52 +00:00
|
|
|
while True:
|
|
|
|
msg = await queue.get_json(5)
|
|
|
|
await response.write(("{}\n".format(msg)).encode("utf-8"))
|
|
|
|
finally:
|
2019-02-23 16:20:11 +00:00
|
|
|
log.info("Client has disconnected from notification for project ID '{}' (HTTP long-polling method)".format(project.id))
|
|
|
|
if project.auto_close:
|
|
|
|
# To avoid trouble with client connecting disconnecting we sleep few seconds before checking
|
|
|
|
# if someone else is not connected
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
if not controller.notification.project_has_listeners(project.id):
|
|
|
|
log.info("Project '{}' is automatically closing due to no client listening".format(project.id))
|
|
|
|
await project.close()
|
|
|
|
|
2016-08-16 15:35:43 +00:00
|
|
|
|
2016-03-17 16:32:37 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/notifications/ws",
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Receive notifications about projects from a Websocket",
|
2016-03-17 16:32:37 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
2016-03-17 16:32:37 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "End of stream",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def notification_ws(request, response):
|
2016-03-17 16:32:37 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2019-02-23 16:20:11 +00:00
|
|
|
project = controller.get_project(request.match_info["project_id"])
|
2016-03-17 16:32:37 +00:00
|
|
|
ws = aiohttp.web.WebSocketResponse()
|
2018-10-15 10:05:49 +00:00
|
|
|
await ws.prepare(request)
|
2016-03-17 16:32:37 +00:00
|
|
|
|
2018-10-16 08:56:06 +00:00
|
|
|
request.app['websockets'].add(ws)
|
2018-10-15 10:05:49 +00:00
|
|
|
asyncio.ensure_future(process_websocket(ws))
|
2019-02-23 16:20:11 +00:00
|
|
|
log.info("New client has connected to the notification stream for project ID '{}' (WebSocket method)".format(project.id))
|
2019-01-12 09:02:36 +00:00
|
|
|
try:
|
2019-02-23 16:20:11 +00:00
|
|
|
with controller.notification.project_queue(project.id) as queue:
|
2018-12-03 11:14:22 +00:00
|
|
|
while True:
|
2018-10-15 10:05:49 +00:00
|
|
|
notification = await queue.get_json(5)
|
2018-10-16 08:56:06 +00:00
|
|
|
if ws.closed:
|
|
|
|
break
|
|
|
|
await ws.send_str(notification)
|
2019-01-12 09:02:36 +00:00
|
|
|
finally:
|
2019-02-23 16:20:11 +00:00
|
|
|
log.info("Client has disconnected from notification stream for project ID '{}' (WebSocket method)".format(project.id))
|
2019-01-12 09:02:36 +00:00
|
|
|
if not ws.closed:
|
|
|
|
await ws.close()
|
|
|
|
request.app['websockets'].discard(ws)
|
2019-02-23 16:20:11 +00:00
|
|
|
if project.auto_close:
|
|
|
|
# To avoid trouble with client connecting disconnecting we sleep few seconds before checking
|
|
|
|
# if someone else is not connected
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
if not controller.notification.project_has_listeners(project.id):
|
|
|
|
log.info("Project '{}' is automatically closing due to no client listening".format(project.id))
|
|
|
|
await project.close()
|
2016-08-16 15:35:43 +00:00
|
|
|
|
2016-03-17 16:32:37 +00:00
|
|
|
return ws
|
2016-07-20 12:50:15 +00:00
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/export",
|
|
|
|
description="Export a project as a portable archive",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
raw=True,
|
|
|
|
status_codes={
|
|
|
|
200: "File returned",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def export_project(request, response):
|
2016-07-20 12:50:15 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.get_loaded_project(request.match_info["project_id"])
|
2019-05-22 07:35:37 +00:00
|
|
|
if request.query.get("include_snapshots", "no").lower() == "yes":
|
|
|
|
include_snapshots = True
|
|
|
|
else:
|
|
|
|
include_snapshots = False
|
2019-03-07 10:05:32 +00:00
|
|
|
if request.query.get("include_images", "no").lower() == "yes":
|
|
|
|
include_images = True
|
|
|
|
else:
|
|
|
|
include_images = False
|
2020-05-27 02:44:47 +00:00
|
|
|
if request.query.get("reset_mac_addresses", "no").lower() == "yes":
|
|
|
|
reset_mac_addresses = True
|
|
|
|
else:
|
|
|
|
reset_mac_addresses = False
|
2024-07-06 15:08:16 +00:00
|
|
|
if request.query.get("keep_compute_ids", "no").lower() == "yes":
|
|
|
|
keep_compute_ids = True
|
|
|
|
else:
|
|
|
|
keep_compute_ids = False
|
2020-05-27 02:44:47 +00:00
|
|
|
|
2019-03-07 10:05:32 +00:00
|
|
|
compression_query = request.query.get("compression", "zip").lower()
|
|
|
|
if compression_query == "zip":
|
|
|
|
compression = zipfile.ZIP_DEFLATED
|
|
|
|
elif compression_query == "none":
|
|
|
|
compression = zipfile.ZIP_STORED
|
|
|
|
elif compression_query == "bzip2":
|
|
|
|
compression = zipfile.ZIP_BZIP2
|
|
|
|
elif compression_query == "lzma":
|
|
|
|
compression = zipfile.ZIP_LZMA
|
2016-07-20 12:50:15 +00:00
|
|
|
|
2016-11-28 11:53:50 +00:00
|
|
|
try:
|
2019-02-26 08:55:07 +00:00
|
|
|
begin = time.time()
|
2020-07-17 05:39:43 +00:00
|
|
|
# use the parent directory as a temporary working dir
|
|
|
|
working_dir = os.path.abspath(os.path.join(project.path, os.pardir))
|
|
|
|
with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
|
2019-03-07 10:05:32 +00:00
|
|
|
with aiozipstream.ZipFile(compression=compression) as zstream:
|
2024-07-06 15:08:16 +00:00
|
|
|
await export_project(
|
|
|
|
zstream,
|
|
|
|
project,
|
|
|
|
tmpdir,
|
|
|
|
include_snapshots=include_snapshots,
|
|
|
|
include_images=include_images,
|
|
|
|
reset_mac_addresses=reset_mac_addresses,
|
|
|
|
keep_compute_ids=keep_compute_ids
|
|
|
|
)
|
|
|
|
|
|
|
|
# We need to do that now because export could fail and raise an HTTP error
|
2019-02-26 08:55:07 +00:00
|
|
|
# that why response start need to be the later possible
|
2019-02-28 10:25:05 +00:00
|
|
|
response.content_type = 'application/gns3project'
|
2019-02-26 08:55:07 +00:00
|
|
|
response.headers['CONTENT-DISPOSITION'] = 'attachment; filename="{}.gns3project"'.format(project.name)
|
|
|
|
response.enable_chunked_encoding()
|
|
|
|
await response.prepare(request)
|
2016-11-28 11:53:50 +00:00
|
|
|
|
2019-02-26 08:55:07 +00:00
|
|
|
async for chunk in zstream:
|
|
|
|
await response.write(chunk)
|
2016-11-28 11:53:50 +00:00
|
|
|
|
2019-03-06 16:00:01 +00:00
|
|
|
log.info("Project '{}' exported in {:.4f} seconds".format(project.name, time.time() - begin))
|
|
|
|
|
2024-07-06 15:08:16 +00:00
|
|
|
# Will be raised if you have no space left or permission issue on your temporary directory
|
2017-01-09 09:20:14 +00:00
|
|
|
# RuntimeError: something was wrong during the zip process
|
2018-08-25 11:10:40 +00:00
|
|
|
except (ValueError, OSError, RuntimeError) as e:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="Cannot export project: {}".format(str(e)))
|
2016-07-20 19:52:12 +00:00
|
|
|
|
2016-07-21 12:48:13 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/import",
|
|
|
|
description="Import a project from a portable archive",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
raw=True,
|
|
|
|
output=PROJECT_OBJECT_SCHEMA,
|
|
|
|
status_codes={
|
|
|
|
200: "Project imported",
|
|
|
|
403: "Forbidden to import project"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def import_project(request, response):
|
2016-07-21 12:48:13 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
|
|
|
|
2016-07-25 16:58:34 +00:00
|
|
|
if request.get("path"):
|
|
|
|
config = Config.instance()
|
|
|
|
if config.get_section_config("Server").getboolean("local", False) is False:
|
|
|
|
response.set_status(403)
|
|
|
|
return
|
|
|
|
path = request.json.get("path")
|
|
|
|
name = request.json.get("name")
|
|
|
|
|
2016-07-21 12:48:13 +00:00
|
|
|
# We write the content to a temporary location and after we extract it all.
|
|
|
|
# It could be more optimal to stream this but it is not implemented in Python.
|
|
|
|
try:
|
2019-03-06 16:00:01 +00:00
|
|
|
begin = time.time()
|
2020-07-17 06:04:00 +00:00
|
|
|
# use the parent directory or projects dir as a temporary working dir
|
|
|
|
if path:
|
|
|
|
working_dir = os.path.abspath(os.path.join(path, os.pardir))
|
|
|
|
else:
|
|
|
|
working_dir = controller.projects_directory()
|
2020-07-17 05:39:43 +00:00
|
|
|
with tempfile.TemporaryDirectory(dir=working_dir) as tmpdir:
|
2019-03-06 16:00:01 +00:00
|
|
|
temp_project_path = os.path.join(tmpdir, "project.zip")
|
|
|
|
async with aiofiles.open(temp_project_path, 'wb') as f:
|
2018-08-25 08:50:08 +00:00
|
|
|
while True:
|
2019-03-06 16:00:01 +00:00
|
|
|
chunk = await request.content.read(CHUNK_SIZE)
|
2018-08-25 08:50:08 +00:00
|
|
|
if not chunk:
|
|
|
|
break
|
2019-03-06 16:00:01 +00:00
|
|
|
await f.write(chunk)
|
|
|
|
|
|
|
|
with open(temp_project_path, "rb") as f:
|
|
|
|
project = await import_project(controller, request.match_info["project_id"], f, location=path, name=name)
|
|
|
|
|
|
|
|
log.info("Project '{}' imported in {:.4f} seconds".format(project.name, time.time() - begin))
|
2016-07-21 12:48:13 +00:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPInternalServerError(text="Could not import the project: {}".format(e))
|
|
|
|
response.json(project)
|
|
|
|
response.set_status(201)
|
|
|
|
|
2016-07-25 12:47:37 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/duplicate",
|
|
|
|
description="Duplicate a project",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
2020-05-27 02:44:47 +00:00
|
|
|
input=PROJECT_DUPLICATE_SCHEMA,
|
2016-07-25 12:47:37 +00:00
|
|
|
output=PROJECT_OBJECT_SCHEMA,
|
|
|
|
status_codes={
|
|
|
|
201: "Project duplicate",
|
|
|
|
403: "The server is not the local server",
|
|
|
|
404: "The project doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def duplicate(request, response):
|
2016-07-25 12:47:37 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.get_loaded_project(request.match_info["project_id"])
|
2016-07-25 12:47:37 +00:00
|
|
|
|
|
|
|
if request.json.get("path"):
|
|
|
|
config = Config.instance()
|
|
|
|
if config.get_section_config("Server").getboolean("local", False) is False:
|
|
|
|
response.set_status(403)
|
|
|
|
return
|
|
|
|
location = request.json.get("path")
|
|
|
|
else:
|
|
|
|
location = None
|
|
|
|
|
2020-05-27 02:44:47 +00:00
|
|
|
reset_mac_addresses = request.json.get("reset_mac_addresses", False)
|
|
|
|
|
|
|
|
new_project = await project.duplicate(name=request.json.get("name"), location=location, reset_mac_addresses=reset_mac_addresses)
|
2016-07-25 12:47:37 +00:00
|
|
|
|
|
|
|
response.json(new_project)
|
|
|
|
response.set_status(201)
|
|
|
|
|
2016-07-20 19:52:12 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/files/{path:.+}",
|
|
|
|
description="Get a file from a project. Beware you have warranty to be able to access only to file global to the project (for example README.txt)",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "File returned",
|
|
|
|
403: "Permission denied",
|
|
|
|
404: "The file doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def get_file(request, response):
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.get_loaded_project(request.match_info["project_id"])
|
2021-05-15 08:05:32 +00:00
|
|
|
path = os.path.normpath(request.match_info["path"])
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
# Raise error if user try to escape
|
2021-05-15 08:05:32 +00:00
|
|
|
if not is_safe_path(path, project.path):
|
2017-11-23 15:28:10 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden()
|
2016-07-20 19:52:12 +00:00
|
|
|
path = os.path.join(project.path, path)
|
2019-03-07 10:05:32 +00:00
|
|
|
await response.stream_file(path)
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/files/{path:.+}",
|
|
|
|
description="Write a file to a project",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
},
|
|
|
|
raw=True,
|
|
|
|
status_codes={
|
|
|
|
200: "File returned",
|
|
|
|
403: "Permission denied",
|
|
|
|
404: "The path doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def write_file(request, response):
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.get_loaded_project(request.match_info["project_id"])
|
2021-05-15 08:05:32 +00:00
|
|
|
path = os.path.normpath(request.match_info["path"])
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
# Raise error if user try to escape
|
2021-05-15 08:05:32 +00:00
|
|
|
if not is_safe_path(path, project.path):
|
2017-11-23 15:28:10 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden()
|
2016-07-20 19:52:12 +00:00
|
|
|
path = os.path.join(project.path, path)
|
2021-05-15 08:05:32 +00:00
|
|
|
response.set_status(201)
|
2016-07-20 19:52:12 +00:00
|
|
|
|
|
|
|
try:
|
2019-03-07 10:05:32 +00:00
|
|
|
async with aiofiles.open(path, 'wb+') as f:
|
2016-07-20 19:52:12 +00:00
|
|
|
while True:
|
2018-04-28 10:42:02 +00:00
|
|
|
try:
|
2019-03-06 16:00:01 +00:00
|
|
|
chunk = await request.content.read(CHUNK_SIZE)
|
2018-04-28 10:42:02 +00:00
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise aiohttp.web.HTTPRequestTimeout(text="Timeout when writing to file '{}'".format(path))
|
|
|
|
if not chunk:
|
2016-07-20 19:52:12 +00:00
|
|
|
break
|
2019-03-07 10:05:32 +00:00
|
|
|
await f.write(chunk)
|
2016-07-20 19:52:12 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
raise aiohttp.web.HTTPNotFound()
|
|
|
|
except PermissionError:
|
|
|
|
raise aiohttp.web.HTTPForbidden()
|
2016-12-13 08:25:44 +00:00
|
|
|
except OSError as e:
|
|
|
|
raise aiohttp.web.HTTPConflict(text=str(e))
|