2016-03-10 20:51:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 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-06-11 23:31:30 +00:00
|
|
|
import aiohttp
|
2020-01-31 09:31:27 +00:00
|
|
|
import asyncio
|
2023-08-02 05:28:32 +00:00
|
|
|
import ipaddress
|
2016-06-11 23:31:30 +00:00
|
|
|
|
2016-05-14 00:00:07 +00:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
2017-04-18 16:04:00 +00:00
|
|
|
from gns3server.utils import force_unix_path
|
2016-05-14 00:00:07 +00:00
|
|
|
|
|
|
|
from gns3server.schemas.node import (
|
|
|
|
NODE_OBJECT_SCHEMA,
|
2016-11-14 12:59:22 +00:00
|
|
|
NODE_UPDATE_SCHEMA,
|
2017-07-20 15:29:42 +00:00
|
|
|
NODE_CREATE_SCHEMA,
|
|
|
|
NODE_DUPLICATE_SCHEMA
|
2016-05-14 00:00:07 +00:00
|
|
|
)
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2023-02-18 07:32:57 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-03-10 20:51:29 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
class NodeHandler:
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
API entry point for node
|
2016-03-10 20:51:29 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 17:35:36 +00:00
|
|
|
r"/projects/{project_id}/nodes",
|
2016-03-10 20:51:29 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID"
|
2016-03-10 20:51:29 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Instance created",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-05-11 17:35:36 +00:00
|
|
|
description="Create a new node instance",
|
2016-11-14 12:59:22 +00:00
|
|
|
input=NODE_CREATE_SCHEMA,
|
2016-05-11 17:35:36 +00:00
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def create(request, response):
|
2016-03-10 20:51:29 +00:00
|
|
|
|
|
|
|
controller = Controller.instance()
|
2016-05-11 21:19:00 +00:00
|
|
|
compute = controller.get_compute(request.json.pop("compute_id"))
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await controller.get_loaded_project(request.match_info["project_id"])
|
|
|
|
node = await project.add_node(compute, request.json.pop("name"), request.json.pop("node_id", None), **request.json)
|
2016-03-10 20:51:29 +00:00
|
|
|
response.set_status(201)
|
2016-05-11 17:35:36 +00:00
|
|
|
response.json(node)
|
2016-04-12 16:02:36 +00:00
|
|
|
|
2018-10-23 09:09:38 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "List of nodes returned",
|
|
|
|
},
|
|
|
|
description="List nodes of a project")
|
|
|
|
async def list_nodes(request, response):
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
response.json([v for v in project.nodes.values()])
|
2018-11-06 12:37:03 +00:00
|
|
|
|
2016-05-14 01:26:50 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/start",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully started",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Start all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def start_all(request, response):
|
2016-05-14 01:26:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.start_all()
|
2016-05-14 01:26:50 +00:00
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/stop",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully stopped",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Stop all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop_all(request, response):
|
2016-05-14 01:26:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.stop_all()
|
2016-05-14 01:26:50 +00:00
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/suspend",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully suspended",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Suspend all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def suspend_all(request, response):
|
2016-05-14 01:26:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.suspend_all()
|
2016-05-14 01:26:50 +00:00
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/reload",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Reload all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def reload_all(request, response):
|
2016-05-14 01:26:50 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.stop_all()
|
|
|
|
await project.start_all()
|
2016-05-14 01:26:50 +00:00
|
|
|
response.set_status(204)
|
|
|
|
|
2019-03-11 15:16:10 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}",
|
|
|
|
status_codes={
|
|
|
|
200: "Node found",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Node doesn't exist"
|
|
|
|
},
|
|
|
|
description="Get a node",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
def get_node(request, response):
|
|
|
|
project = Controller.instance().get_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
response.set_status(200)
|
|
|
|
response.json(node)
|
|
|
|
|
|
|
|
@Route.put(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}",
|
|
|
|
status_codes={
|
|
|
|
200: "Instance updated",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Update a node instance",
|
|
|
|
input=NODE_UPDATE_SCHEMA,
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
async def update(request, response):
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
|
|
|
|
# Ignore these because we only use them when creating a node
|
|
|
|
request.json.pop("node_id", None)
|
|
|
|
request.json.pop("node_type", None)
|
|
|
|
request.json.pop("compute_id", None)
|
|
|
|
|
|
|
|
await node.update(**request.json)
|
|
|
|
response.set_status(200)
|
|
|
|
response.json(node)
|
|
|
|
|
|
|
|
@Route.delete(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance deleted",
|
|
|
|
400: "Invalid request",
|
2019-04-10 08:43:51 +00:00
|
|
|
404: "Instance doesn't exist",
|
|
|
|
409: "Cannot delete locked node"
|
2019-03-11 15:16:10 +00:00
|
|
|
},
|
|
|
|
description="Delete a node instance")
|
|
|
|
async def delete(request, response):
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.delete_node(request.match_info["node_id"])
|
|
|
|
response.set_status(204)
|
|
|
|
|
2017-07-20 15:29:42 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/duplicate",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Instance duplicated",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Duplicate a node instance",
|
|
|
|
input=NODE_DUPLICATE_SCHEMA,
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def duplicate(request, response):
|
2017-07-20 15:29:42 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2017-07-20 15:29:42 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
new_node = await project.duplicate_node(
|
2017-07-20 15:29:42 +00:00
|
|
|
node,
|
|
|
|
request.json["x"],
|
|
|
|
request.json["y"],
|
|
|
|
request.json.get("z", 0))
|
|
|
|
response.json(new_node)
|
|
|
|
response.set_status(201)
|
|
|
|
|
2016-04-12 16:02:36 +00:00
|
|
|
@Route.post(
|
2016-05-11 17:35:36 +00:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/start",
|
2016-04-12 16:02:36 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2018-06-14 08:39:31 +00:00
|
|
|
200: "Instance started",
|
2016-05-14 00:00:07 +00:00
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
2016-05-11 17:35:36 +00:00
|
|
|
description="Start a node instance",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def start(request, response):
|
2016-04-12 16:02:36 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.start(data=request.json)
|
2016-08-22 09:26:51 +00:00
|
|
|
response.json(node)
|
2018-06-14 08:39:31 +00:00
|
|
|
response.set_status(200)
|
2016-04-12 16:02:36 +00:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 17:35:36 +00:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/stop",
|
2016-04-12 16:02:36 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2018-06-14 08:39:31 +00:00
|
|
|
200: "Instance stopped",
|
2016-05-14 00:00:07 +00:00
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Stop a node instance",
|
2016-05-11 17:35:36 +00:00
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop(request, response):
|
2016-04-12 16:02:36 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.stop()
|
2016-08-22 09:26:51 +00:00
|
|
|
response.json(node)
|
2018-06-14 08:39:31 +00:00
|
|
|
response.set_status(200)
|
2016-04-12 16:02:36 +00:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 17:35:36 +00:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/suspend",
|
2016-04-12 16:02:36 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2018-06-14 08:39:31 +00:00
|
|
|
200: "Instance suspended",
|
2016-05-14 00:00:07 +00:00
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-12 16:02:36 +00:00
|
|
|
},
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Suspend a node instance",
|
2016-05-11 17:35:36 +00:00
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def suspend(request, response):
|
2016-04-12 16:02:36 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.suspend()
|
2016-08-22 09:26:51 +00:00
|
|
|
response.json(node)
|
2018-06-14 08:39:31 +00:00
|
|
|
response.set_status(200)
|
2016-04-18 14:57:02 +00:00
|
|
|
|
|
|
|
@Route.post(
|
2016-05-11 17:35:36 +00:00
|
|
|
r"/projects/{project_id}/nodes/{node_id}/reload",
|
2016-04-18 14:57:02 +00:00
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
2016-04-18 14:57:02 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2018-06-14 08:39:31 +00:00
|
|
|
200: "Instance reloaded",
|
2016-05-14 00:00:07 +00:00
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
2016-04-18 14:57:02 +00:00
|
|
|
},
|
2016-05-11 17:35:36 +00:00
|
|
|
description="Reload a node instance",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def reload(request, response):
|
2016-04-18 14:57:02 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2019-01-24 08:51:14 +00:00
|
|
|
await node.reload()
|
2016-08-22 09:26:51 +00:00
|
|
|
response.json(node)
|
2018-06-14 08:39:31 +00:00
|
|
|
response.set_status(200)
|
2016-04-18 15:36:38 +00:00
|
|
|
|
2018-06-06 13:46:44 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/links",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "Links returned",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Return all the links connected to this node")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def links(request, response):
|
2018-06-06 13:46:44 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2018-06-06 13:46:44 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
links = []
|
|
|
|
for link in node.links:
|
|
|
|
links.append(link.__json__())
|
|
|
|
response.json(links)
|
|
|
|
response.set_status(200)
|
|
|
|
|
2016-05-19 14:21:35 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/dynamips/auto_idlepc",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Compute the IDLE PC for a Dynamips node")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def auto_idlepc(request, response):
|
2016-05-19 14:21:35 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-19 14:21:35 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
idle = await node.dynamips_auto_idlepc()
|
2016-05-19 14:21:35 +00:00
|
|
|
response.json(idle)
|
|
|
|
response.set_status(200)
|
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/dynamips/idlepc_proposals",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Compute a list of potential idle PC for a node")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def idlepc_proposals(request, response):
|
2016-05-19 14:21:35 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-19 14:21:35 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
idle = await node.dynamips_idlepc_proposals()
|
2016-05-19 14:21:35 +00:00
|
|
|
response.json(idle)
|
|
|
|
response.set_status(200)
|
2016-07-27 16:31:02 +00:00
|
|
|
|
2018-03-26 11:05:49 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/resize_disk",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Disk image resized",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Reload a node instance")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def resize_disk(request, response):
|
2018-03-26 11:05:49 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2018-03-26 11:05:49 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await node.post("/resize_disk", request.json)
|
2018-03-26 11:05:49 +00:00
|
|
|
response.set_status(201)
|
|
|
|
|
2016-07-27 16:31:02 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/files/{path:.+}",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Get a file in the node directory")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def get_file(request, response):
|
2016-07-27 16:31:02 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-07-27 16:31:02 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
path = request.match_info["path"]
|
2017-04-18 16:04:00 +00:00
|
|
|
path = force_unix_path(path)
|
2016-07-27 16:31:02 +00:00
|
|
|
|
|
|
|
# Raise error if user try to escape
|
2021-05-15 08:05:32 +00:00
|
|
|
if path[0] == "." or "/../" in path:
|
2017-11-23 15:28:10 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden()
|
2016-07-27 16:31:02 +00:00
|
|
|
|
|
|
|
node_type = node.node_type
|
2017-02-03 13:44:32 +00:00
|
|
|
path = "/project-files/{}/{}/{}".format(node_type, node.id, path)
|
2018-10-15 10:05:49 +00:00
|
|
|
res = await node.compute.http_query("GET", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), timeout=None, raw=True)
|
2021-05-15 08:05:32 +00:00
|
|
|
response.set_status(res.status)
|
|
|
|
if res.status == 200:
|
|
|
|
response.content_type = "application/octet-stream"
|
|
|
|
response.enable_chunked_encoding()
|
|
|
|
await response.prepare(request)
|
|
|
|
await response.write(res.body)
|
2016-07-27 16:31:02 +00:00
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/files/{path:.+}",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Instance reloaded",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
raw=True,
|
|
|
|
description="Write a file in the node directory")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def post_file(request, response):
|
2016-07-27 16:31:02 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-07-27 16:31:02 +00:00
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
path = request.match_info["path"]
|
2017-10-03 16:13:19 +00:00
|
|
|
path = force_unix_path(path)
|
2016-07-27 16:31:02 +00:00
|
|
|
|
|
|
|
# Raise error if user try to escape
|
2021-05-15 08:05:32 +00:00
|
|
|
if path[0] == "." or "/../" in path:
|
2017-11-23 15:28:10 +00:00
|
|
|
raise aiohttp.web.HTTPForbidden()
|
2016-07-27 16:31:02 +00:00
|
|
|
|
|
|
|
node_type = node.node_type
|
2017-02-03 13:44:32 +00:00
|
|
|
path = "/project-files/{}/{}/{}".format(node_type, node.id, path)
|
2018-10-15 10:05:49 +00:00
|
|
|
data = await request.content.read() #FIXME: are we handling timeout or large files correctly?
|
2021-05-15 08:05:32 +00:00
|
|
|
res = await node.compute.http_query("POST", "/projects/{project_id}/files{path}".format(project_id=project.id, path=path), data=data, timeout=None, raw=True)
|
|
|
|
response.set_status(res.status)
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/console/ws",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
description="Connect to WebSocket console",
|
|
|
|
status_codes={
|
|
|
|
200: "File returned",
|
|
|
|
403: "Permission denied",
|
|
|
|
404: "The file doesn't exist"
|
|
|
|
})
|
|
|
|
async def ws_console(request, response):
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
compute = node.compute
|
|
|
|
ws = aiohttp.web.WebSocketResponse()
|
|
|
|
await ws.prepare(request)
|
|
|
|
request.app['websockets'].add(ws)
|
|
|
|
|
2023-08-02 05:28:32 +00:00
|
|
|
compute_host = compute.host
|
|
|
|
try:
|
|
|
|
# handle IPv6 address
|
|
|
|
ip = ipaddress.ip_address(compute_host)
|
|
|
|
if isinstance(ip, ipaddress.IPv6Address):
|
|
|
|
compute_host = '[' + compute_host + ']'
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
ws_console_compute_url = "ws://{compute_host}:{compute_port}/v2/compute/projects/{project_id}/{node_type}/nodes/{node_id}/console/ws".format(compute_host=compute_host,
|
2020-01-31 09:31:27 +00:00
|
|
|
compute_port=compute.port,
|
|
|
|
project_id=project.id,
|
|
|
|
node_type=node.node_type,
|
|
|
|
node_id=node.id)
|
|
|
|
|
|
|
|
async def ws_forward(ws_client):
|
|
|
|
async for msg in ws:
|
|
|
|
if msg.type == aiohttp.WSMsgType.TEXT:
|
|
|
|
await ws_client.send_str(msg.data)
|
|
|
|
elif msg.type == aiohttp.WSMsgType.BINARY:
|
|
|
|
await ws_client.send_bytes(msg.data)
|
|
|
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(limit=None, force_close=True)) as session:
|
|
|
|
async with session.ws_connect(ws_console_compute_url) as ws_client:
|
|
|
|
asyncio.ensure_future(ws_forward(ws_client))
|
|
|
|
async for msg in ws_client:
|
|
|
|
if msg.type == aiohttp.WSMsgType.TEXT:
|
|
|
|
await ws.send_str(msg.data)
|
|
|
|
elif msg.type == aiohttp.WSMsgType.BINARY:
|
|
|
|
await ws.send_bytes(msg.data)
|
|
|
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
|
|
|
break
|
2023-02-18 07:32:57 +00:00
|
|
|
except ConnectionResetError:
|
|
|
|
log.info("Websocket console connection with compute disconnected")
|
2020-01-31 09:31:27 +00:00
|
|
|
finally:
|
|
|
|
if not ws.closed:
|
|
|
|
await ws.close()
|
|
|
|
request.app['websockets'].discard(ws)
|
|
|
|
|
|
|
|
return ws
|
2020-07-26 08:57:18 +00:00
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/console/reset",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "All nodes successfully reset consoles",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Reset console for all nodes belonging to the project",
|
|
|
|
output=NODE_OBJECT_SCHEMA)
|
|
|
|
async def reset_console_all(request, response):
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.reset_console_all()
|
|
|
|
response.set_status(204)
|
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/nodes/{node_id}/console/reset",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"node_id": "Node UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
204: "Console reset",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Instance doesn't exist"
|
|
|
|
},
|
|
|
|
description="Reload a node instance")
|
|
|
|
async def console_reset(request, response):
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
node = project.get_node(request.match_info["node_id"])
|
|
|
|
await node.post("/console/reset", request.json)
|
|
|
|
response.set_status(204)
|