2015-01-14 00:05:26 +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-02-02 22:36:13 +00:00
|
|
|
|
import sys
|
2015-01-14 00:05:26 +00:00
|
|
|
|
import json
|
|
|
|
|
import jsonschema
|
|
|
|
|
import asyncio
|
|
|
|
|
import aiohttp
|
2015-01-16 16:09:45 +00:00
|
|
|
|
import logging
|
2015-02-02 22:36:13 +00:00
|
|
|
|
import traceback
|
2015-01-16 16:09:45 +00:00
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 00:05:26 +00:00
|
|
|
|
|
2015-01-15 23:50:36 +00:00
|
|
|
|
from ..modules.vm_error import VMError
|
2015-01-14 00:05:26 +00:00
|
|
|
|
from .response import Response
|
2015-02-24 16:40:01 +00:00
|
|
|
|
from ..crash_report import CrashReport
|
2015-03-13 20:43:39 +00:00
|
|
|
|
from ..config import Config
|
2015-01-14 00:05:26 +00:00
|
|
|
|
|
2015-02-12 21:28:12 +00:00
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def parse_request(request, input_schema):
|
|
|
|
|
"""Parse body of request and raise HTTP errors in case of problems"""
|
|
|
|
|
content_length = request.content_length
|
|
|
|
|
if content_length is not None and content_length > 0:
|
|
|
|
|
body = yield from request.read()
|
|
|
|
|
try:
|
|
|
|
|
request.json = json.loads(body.decode('utf-8'))
|
|
|
|
|
except ValueError as e:
|
|
|
|
|
raise aiohttp.web.HTTPBadRequest(text="Invalid JSON {}".format(e))
|
2015-02-24 16:40:01 +00:00
|
|
|
|
else:
|
|
|
|
|
request.json = {}
|
2015-01-14 00:05:26 +00:00
|
|
|
|
try:
|
|
|
|
|
jsonschema.validate(request.json, input_schema)
|
|
|
|
|
except jsonschema.ValidationError as e:
|
2015-02-12 20:39:24 +00:00
|
|
|
|
log.error("Invalid input query. JSON schema error: {}".format(e.message))
|
2015-02-12 21:28:12 +00:00
|
|
|
|
raise aiohttp.web.HTTPBadRequest(text="Invalid JSON: {} in schema: {}".format(
|
|
|
|
|
e.message,
|
2015-01-14 00:05:26 +00:00
|
|
|
|
json.dumps(e.schema)))
|
|
|
|
|
return request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Route(object):
|
2015-01-20 12:24:00 +00:00
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
|
""" Decorator adding:
|
|
|
|
|
* json schema verification
|
|
|
|
|
* routing inside handlers
|
|
|
|
|
* documentation information about endpoints
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
_routes = []
|
|
|
|
|
_documentation = {}
|
|
|
|
|
|
2015-02-25 04:02:37 +00:00
|
|
|
|
_vm_locks = {}
|
2015-02-24 20:53:38 +00:00
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def get(cls, path, *args, **kw):
|
|
|
|
|
return cls._route('GET', path, *args, **kw)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def post(cls, path, *args, **kw):
|
|
|
|
|
return cls._route('POST', path, *args, **kw)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def put(cls, path, *args, **kw):
|
|
|
|
|
return cls._route('PUT', path, *args, **kw)
|
|
|
|
|
|
2015-01-16 20:39:58 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, path, *args, **kw):
|
|
|
|
|
return cls._route('DELETE', path, *args, **kw)
|
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def _route(cls, method, path, *args, **kw):
|
|
|
|
|
# This block is executed only the first time
|
|
|
|
|
output_schema = kw.get("output", {})
|
|
|
|
|
input_schema = kw.get("input", {})
|
2015-02-23 10:27:07 +00:00
|
|
|
|
api_version = kw.get("api_version", 1)
|
2015-02-23 16:21:39 +00:00
|
|
|
|
|
|
|
|
|
# If it's a JSON api endpoint just register the endpoint an do nothing
|
2015-02-23 10:27:07 +00:00
|
|
|
|
if api_version is None:
|
|
|
|
|
cls._path = path
|
|
|
|
|
else:
|
|
|
|
|
cls._path = "/v{version}{path}".format(path=path, version=api_version)
|
2015-01-14 00:05:26 +00:00
|
|
|
|
|
|
|
|
|
def register(func):
|
|
|
|
|
route = cls._path
|
|
|
|
|
|
2015-02-24 10:38:57 +00:00
|
|
|
|
handler = func.__module__.replace("_handler", "").replace("gns3server.handlers.api.", "")
|
2015-02-17 09:37:09 +00:00
|
|
|
|
cls._documentation.setdefault(handler, {})
|
2015-02-24 10:38:57 +00:00
|
|
|
|
cls._documentation[handler].setdefault(route, {"api_version": api_version,
|
|
|
|
|
"methods": []})
|
2015-02-17 09:37:09 +00:00
|
|
|
|
|
|
|
|
|
cls._documentation[handler][route]["methods"].append({
|
2015-01-14 00:05:26 +00:00
|
|
|
|
"method": method,
|
|
|
|
|
"status_codes": kw.get("status_codes", {200: "OK"}),
|
|
|
|
|
"parameters": kw.get("parameters", {}),
|
|
|
|
|
"output_schema": output_schema,
|
|
|
|
|
"input_schema": input_schema,
|
2015-02-17 09:37:09 +00:00
|
|
|
|
"description": kw.get("description", ""),
|
2015-01-14 00:05:26 +00:00
|
|
|
|
})
|
|
|
|
|
func = asyncio.coroutine(func)
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def control_schema(request):
|
|
|
|
|
# This block is executed at each method call
|
2015-02-23 16:21:39 +00:00
|
|
|
|
|
|
|
|
|
# Non API call
|
|
|
|
|
if api_version is None:
|
|
|
|
|
response = Response(route=route, output_schema=output_schema)
|
|
|
|
|
yield from func(request, response)
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
# API call
|
2015-01-14 00:05:26 +00:00
|
|
|
|
try:
|
|
|
|
|
request = yield from parse_request(request, input_schema)
|
2015-03-13 20:43:39 +00:00
|
|
|
|
server_config = Config.instance().get_section_config("Server")
|
|
|
|
|
record_file = server_config.get("record")
|
|
|
|
|
if record_file:
|
|
|
|
|
try:
|
|
|
|
|
with open(record_file, "a") as f:
|
|
|
|
|
f.write("curl -X {} 'http://{}{}' -d '{}'".format(request.method, request.host, request.path_qs, json.dumps(request.json)))
|
|
|
|
|
f.write("\n")
|
|
|
|
|
except OSError as e:
|
|
|
|
|
log.warn("Could not write to the record file {}: {}".format(record_file, e))
|
2015-01-14 00:05:26 +00:00
|
|
|
|
response = Response(route=route, output_schema=output_schema)
|
|
|
|
|
yield from func(request, response)
|
|
|
|
|
except aiohttp.web.HTTPException as e:
|
|
|
|
|
response = Response(route=route)
|
|
|
|
|
response.set_status(e.status)
|
|
|
|
|
response.json({"message": e.text, "status": e.status})
|
2015-01-15 23:50:36 +00:00
|
|
|
|
except VMError as e:
|
2015-02-02 22:36:13 +00:00
|
|
|
|
log.error("VM error detected: {type}".format(type=type(e)), exc_info=1)
|
2015-01-14 17:52:02 +00:00
|
|
|
|
response = Response(route=route)
|
2015-02-19 00:48:02 +00:00
|
|
|
|
response.set_status(409)
|
|
|
|
|
response.json({"message": str(e), "status": 409})
|
2015-02-25 10:42:02 +00:00
|
|
|
|
except asyncio.futures.CancelledError as e:
|
|
|
|
|
log.error("Request canceled")
|
|
|
|
|
response = Response(route=route)
|
|
|
|
|
response.set_status(408)
|
|
|
|
|
response.json({"message": "Request canceled", "status": 408})
|
2015-02-02 22:36:13 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
log.error("Uncaught exception detected: {type}".format(type=type(e)), exc_info=1)
|
|
|
|
|
response = Response(route=route)
|
|
|
|
|
response.set_status(500)
|
2015-02-24 16:40:01 +00:00
|
|
|
|
CrashReport.instance().capture_exception(request)
|
2015-02-02 22:36:13 +00:00
|
|
|
|
exc_type, exc_value, exc_tb = sys.exc_info()
|
|
|
|
|
lines = traceback.format_exception(exc_type, exc_value, exc_tb)
|
2015-02-23 10:27:07 +00:00
|
|
|
|
if api_version is not None:
|
|
|
|
|
tb = "".join(lines)
|
|
|
|
|
response.json({"message": tb, "status": 500})
|
|
|
|
|
else:
|
|
|
|
|
tb = "\n".join(lines)
|
|
|
|
|
response.html("<h1>Internal error</h1><pre>{}</pre>".format(tb))
|
|
|
|
|
|
2015-01-14 00:05:26 +00:00
|
|
|
|
return response
|
|
|
|
|
|
2015-02-24 20:53:38 +00:00
|
|
|
|
@asyncio.coroutine
|
|
|
|
|
def vm_concurrency(request):
|
|
|
|
|
"""
|
|
|
|
|
To avoid strange effect we prevent concurrency
|
|
|
|
|
between the same instance of the vm
|
|
|
|
|
"""
|
|
|
|
|
|
2015-02-24 22:26:03 +00:00
|
|
|
|
if "vm_id" in request.match_info or "device_id" in request.match_info:
|
|
|
|
|
vm_id = request.match_info.get("vm_id")
|
|
|
|
|
if vm_id is None:
|
|
|
|
|
vm_id = request.match_info["device_id"]
|
2015-02-25 10:19:16 +00:00
|
|
|
|
cls._vm_locks.setdefault(vm_id, {"lock": asyncio.Lock(), "concurrency": 0})
|
|
|
|
|
cls._vm_locks[vm_id]["concurrency"] += 1
|
|
|
|
|
|
|
|
|
|
with (yield from cls._vm_locks[vm_id]["lock"]):
|
2015-02-24 20:53:38 +00:00
|
|
|
|
response = yield from control_schema(request)
|
2015-02-25 10:19:16 +00:00
|
|
|
|
cls._vm_locks[vm_id]["concurrency"] -= 1
|
|
|
|
|
|
|
|
|
|
# No more waiting requests, garbage collect the lock
|
|
|
|
|
if cls._vm_locks[vm_id]["concurrency"] <= 0:
|
|
|
|
|
del cls._vm_locks[vm_id]
|
2015-02-24 22:26:03 +00:00
|
|
|
|
else:
|
|
|
|
|
response = yield from control_schema(request)
|
2015-02-24 20:53:38 +00:00
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
cls._routes.append((method, cls._path, vm_concurrency))
|
2015-01-14 00:05:26 +00:00
|
|
|
|
|
2015-02-24 20:53:38 +00:00
|
|
|
|
return vm_concurrency
|
2015-01-14 00:05:26 +00:00
|
|
|
|
return register
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_routes(cls):
|
|
|
|
|
return cls._routes
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_documentation(cls):
|
|
|
|
|
return cls._documentation
|