1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-11 09:38:08 +00:00

Upload images. Upgrade to aiohttp 0.15.3

This commit is contained in:
Julien Duponchelle 2015-04-24 10:15:23 +02:00
parent 11ee72ddf3
commit a1b9b442f4
10 changed files with 123 additions and 7 deletions

View File

@ -436,3 +436,16 @@ class DynamipsVMHandler:
vms = yield from dynamips_manager.list_images() vms = yield from dynamips_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/dynamips/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload Dynamips image.")
def upload_vm(request, response):
dynamips_manager = Dynamips.instance()
yield from dynamips_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -330,3 +330,16 @@ class IOUHandler:
vms = yield from iou_manager.list_images() vms = yield from iou_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/iou/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload IOU image.")
def upload_vm(request, response):
iou_manager = IOU.instance()
yield from iou_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -305,3 +305,16 @@ class QEMUHandler:
vms = yield from qemu_manager.list_images() vms = yield from qemu_manager.list_images()
response.set_status(200) response.set_status(200)
response.json(vms) response.json(vms)
@Route.post(
r"/qemu/vms/{filename}",
status_codes={
204: "Image uploaded",
},
raw=True,
description="Upload Qemu image.")
def upload_vm(request, response):
qemu_manager = Qemu.instance()
yield from qemu_manager.write_image(request.match_info["filename"], request.content)
response.set_status(204)

View File

@ -432,3 +432,20 @@ class BaseManager:
""" """
raise NotImplementedError raise NotImplementedError
@asyncio.coroutine
def write_image(self, filename, stream):
directory = self.get_images_directory()
path = os.path.join(directory, os.path.basename(filename))
log.info("Writting image file %s", path)
try:
os.makedirs(directory, exist_ok=True)
with open(path, 'wb+') as f:
while True:
packet = yield from stream.read(512)
if not packet:
break
f.write(packet)
os.chmod(path, stat.S_IWRITE | stat.S_IREAD | stat.S_IEXEC)
except OSError as e:
raise aiohttp.web.HTTPConflict(text="Could not write image: {} to {}".format(filename, e))

View File

@ -89,6 +89,7 @@ class Route(object):
output_schema = kw.get("output", {}) output_schema = kw.get("output", {})
input_schema = kw.get("input", {}) input_schema = kw.get("input", {})
api_version = kw.get("api_version", 1) api_version = kw.get("api_version", 1)
raw = kw.get("raw", False)
# If it's a JSON api endpoint just register the endpoint an do nothing # If it's a JSON api endpoint just register the endpoint an do nothing
if api_version is None: if api_version is None:
@ -119,8 +120,9 @@ class Route(object):
# This block is executed at each method call # This block is executed at each method call
# Non API call # Non API call
if api_version is None: if api_version is None or raw is True:
response = Response(request=request, route=route, output_schema=output_schema) response = Response(request=request, route=route, output_schema=output_schema)
yield from func(request, response) yield from func(request, response)
return response return response

View File

@ -1,6 +1,6 @@
netifaces==0.10.4 netifaces==0.10.4
jsonschema==2.4.0 jsonschema==2.4.0
python-dateutil==2.3 python-dateutil==2.3
aiohttp==0.14.4 aiohttp==0.15.3
Jinja2==2.7.3 Jinja2==2.7.3
raven==5.2.0 raven==5.2.0

View File

@ -34,12 +34,12 @@ class PyTest(TestCommand):
sys.exit(errcode) sys.exit(errcode)
dependencies = ["aiohttp>=0.14.4", dependencies = ["aiohttp>=0.15.3",
"jsonschema>=2.4.0", "jsonschema>=2.4.0",
"Jinja2>=2.7.3", "Jinja2>=2.7.3",
"raven>=5.2.0"] "raven>=5.2.0"]
#if not sys.platform.startswith("win"): # if not sys.platform.startswith("win"):
# dependencies.append("netifaces==0.10.4") # dependencies.append("netifaces==0.10.4")
if sys.version_info == (3, 3): if sys.version_info == (3, 3):

View File

@ -146,3 +146,22 @@ def test_vms(server, tmpdir, fake_dynamips):
response = server.get("/dynamips/vms") response = server.get("/dynamips/vms")
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "7200.bin"}] assert response.json == [{"filename": "7200.bin"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = server.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir),):
response = server.post("/dynamips/vms/test2", body="TEST", raw=True)
assert response.status == 409

View File

@ -20,6 +20,7 @@ import os
import stat import stat
import sys import sys
import uuid import uuid
import aiohttp
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock, PropertyMock from unittest.mock import patch, MagicMock, PropertyMock
@ -311,9 +312,28 @@ def test_get_initial_config_with_config_file(server, project, vm):
assert response.json["content"] == "TEST" assert response.json["content"] == "TEST"
def test_vms(server, vm, tmpdir, fake_iou_bin): def test_vms(server, tmpdir, fake_iou_bin):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir), example=True): with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir)):
response = server.get("/iou/vms") response = server.get("/iou/vms", example=True)
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "iou.bin"}] assert response.json == [{"filename": "iou.bin"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir),):
response = server.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.IOU.get_images_directory", return_value=str(tmpdir),):
response = server.post("/iou/vms/test2", body="TEST", raw=True)
assert response.status == 409

View File

@ -200,3 +200,22 @@ def test_vms(server, tmpdir, fake_qemu_vm):
response = server.get("/qemu/vms") response = server.get("/qemu/vms")
assert response.status == 200 assert response.status == 200
assert response.json == [{"filename": "linux.img"}] assert response.json == [{"filename": "linux.img"}]
def test_upload_vm(server, tmpdir):
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir),):
response = server.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 204
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
def test_upload_vm_permission_denied(server, tmpdir):
with open(str(tmpdir / "test2"), "w+") as f:
f.write("")
os.chmod(str(tmpdir / "test2"), 0)
with patch("gns3server.modules.Qemu.get_images_directory", return_value=str(tmpdir),):
response = server.post("/qemu/vms/test2", body="TEST", raw=True)
assert response.status == 409