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

Drop /upload

Fix #552
This commit is contained in:
Julien Duponchelle 2016-06-17 17:25:13 +02:00
parent 49eb7d8ce7
commit 4783691c87
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
6 changed files with 1 additions and 449 deletions

View File

@ -15,9 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from gns3server.handlers.upload_handler import UploadHandler
from gns3server.handlers.index_handler import IndexHandler from gns3server.handlers.index_handler import IndexHandler
from gns3server.handlers.api.controller import * from gns3server.handlers.api.controller import *
from gns3server.handlers.api.compute import * from gns3server.handlers.api.compute import *

View File

@ -1,160 +0,0 @@
# -*- 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/>.
import os
import aiohttp
import stat
import io
import tarfile
import asyncio
from gns3server.config import Config
from gns3server.web.route import Route
from gns3server.utils.images import remove_checksum, md5sum
class UploadHandler:
@Route.get(
r"/upload",
description="List binary images",
api_version=None
)
def index(request, response):
uploaded_files = []
try:
for root, _, files in os.walk(UploadHandler.image_directory()):
for filename in files:
if not filename.startswith(".") and not filename.endswith(".md5sum"):
image_file = os.path.join(root, filename)
uploaded_files.append(image_file)
except OSError:
pass
iourc_path = os.path.join(os.path.expanduser("~/"), ".iourc")
if os.path.exists(iourc_path):
uploaded_files.append(iourc_path)
response.template("upload.html", files=uploaded_files)
@Route.post(
r"/upload",
description="Upload binary images",
api_version=None
)
def upload(request, response):
data = yield from request.post()
if not data["file"]:
response.redirect("/upload")
return
if data["type"] not in ["IOU", "IOURC", "QEMU", "IOS", "IMAGES", "PROJECTS"]:
raise aiohttp.web.HTTPForbidden(text="You are not authorized to upload this kind of image {}".format(data["type"]))
try:
if data["type"] == "IMAGES":
UploadHandler._restore_directory(data["file"], UploadHandler.image_directory())
elif data["type"] == "PROJECTS":
UploadHandler._restore_directory(data["file"], UploadHandler.project_directory())
else:
if data["type"] == "IOURC":
destination_dir = os.path.expanduser("~/")
destination_path = os.path.join(destination_dir, ".iourc")
else:
destination_dir = os.path.join(UploadHandler.image_directory(), data["type"])
destination_path = os.path.join(destination_dir, data["file"].filename)
os.makedirs(destination_dir, exist_ok=True)
remove_checksum(destination_path)
with open(destination_path, "wb+") as f:
while True:
chunk = data["file"].file.read(512)
if not chunk:
break
f.write(chunk)
md5sum(destination_path)
st = os.stat(destination_path)
os.chmod(destination_path, st.st_mode | stat.S_IXUSR)
except OSError as e:
response.html("Could not upload file: {}".format(e))
response.set_status(200)
return
response.redirect("/upload")
@Route.get(
r"/backup/images.tar",
description="Backup binary images",
api_version=None
)
def backup_images(request, response):
yield from UploadHandler._backup_directory(request, response, UploadHandler.image_directory())
@Route.get(
r"/backup/projects.tar",
description="Backup GNS3 projects",
api_version=None
)
def backup_projects(request, response):
yield from UploadHandler._backup_directory(request, response, UploadHandler.project_directory())
@staticmethod
def _restore_directory(file, directory):
"""
Extract from HTTP stream the content of a tar
"""
destination_path = os.path.join(directory, "archive.tar")
os.makedirs(directory, exist_ok=True)
with open(destination_path, "wb+") as f:
chunk = file.file.read()
f.write(chunk)
t = tarfile.open(destination_path)
t.extractall(directory)
t.close()
os.remove(destination_path)
@staticmethod
@asyncio.coroutine
def _backup_directory(request, response, directory):
"""
Return a tar archive from a directory
"""
response.content_type = 'application/x-gtar'
response.set_status(200)
response.enable_chunked_encoding()
# Very important: do not send a content length otherwise QT close the connection but curl can consume the Feed
response.content_length = None
response.start(request)
buffer = io.BytesIO()
with tarfile.open('arch.tar', 'w', fileobj=buffer) as tar:
for root, dirs, files in os.walk(directory):
for file in files:
path = os.path.join(root, file)
tar.add(os.path.join(root, file), arcname=os.path.relpath(path, directory))
response.write(buffer.getvalue())
yield from response.drain()
buffer.truncate(0)
buffer.seek(0)
yield from response.write_eof()
@staticmethod
def image_directory():
server_config = Config.instance().get_section_config("Server")
return os.path.expanduser(server_config.get("images_path", "~/GNS3/images"))
@staticmethod
def project_directory():
server_config = Config.instance().get_section_config("Server")
return os.path.expanduser(server_config.get("projects_path", "~/GNS3/projects"))

View File

@ -6,6 +6,6 @@
<ul> <ul>
<li><a href="https://gns3.com">Website</a></li> <li><a href="https://gns3.com">Website</a></li>
<li><a href="http://api.gns3.net">API documentation</a></li> <li><a href="http://api.gns3.net">API documentation</a></li>
<li><a href="/upload">Upload images & backup</a></li>
</ul> </ul>
<p>If you are looking for uploading the IOU. You can since 1.4 upload them directly from the client see: <a href="https://gns3.com/support/docs/how-to-configure-non-native-io-3">this documenation</a>.</p>
{% endblock %} {% endblock %}

View File

@ -8,12 +8,6 @@
<div> <div>
<a href="/">Home</a> <a href="/">Home</a>
| |
<a href="/upload">Upload</a>
|
<a href="/backup/images.tar">Backup images</a>
|
<a href="/backup/projects.tar">Backup projects</a>
|
<a href="/controller">Controller status</a> <a href="/controller">Controller status</a>
| |
<a href="/compute">Compute status</a> <a href="/compute">Compute status</a>

View File

@ -1,45 +0,0 @@
{% extends "layout.html" %}
{% block head %}
<script>
function onSubmit() {
if (document.getElementById("uploadInput").files == undefined) {
//OLD browser
return true;
}
max_size = 200;
var file = document.getElementById("uploadInput").files[0];
var size = Math.round(file.size / 1000000);
if (size > max_size) {
alert("The file is too big (" + size + " MB). The max upload size is " + max_size + " MB. Please Upload your file with the GNS3 GUI");
return false;
}
return true;
}
</script>
{% endblock %}
{% block body %}
<h1>Select & Upload an image for GNS3</h1>
<form enctype="multipart/form-data" action="/upload" method="post" onSubmit="return onSubmit()">
File path: <input type="file" name="file" id="uploadInput" /><br>
File type: <select name="type" />
<option value="IOU">IOU</option>
<option value="IOURC">IOU licence (iourc)</option>
<option value="IOS">IOS</option>
<option value="QEMU">Qemu</option>
<option value="IMAGES">GNS3 images backup (.tar)</option>
<option value="PROJECTS">GNS3 projects backup (.tar)</option>
</select>
<br />
<br />
<input type="submit" value="Upload" />
</form>
{%if files%}
<h2>Files on {{gns3_host}}</h2>
{%for file in files%}
<p>{{file}}</a></p>
{%endfor%}
{%endif%}
{% endblock %}

View File

@ -1,235 +0,0 @@
# -*- 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/>.
import aiohttp
import asyncio
import pytest
import os
import tarfile
from unittest.mock import patch
from gns3server.config import Config
@pytest.yield_fixture(autouse=True)
def restore_working_dir():
directory = os.getcwd()
yield
os.chdir(directory)
def test_index_upload(http_root, tmpdir):
Config.instance().set("Server", "images_path", str(tmpdir))
open(str(tmpdir / "alpha"), "w+").close()
open(str(tmpdir / "alpha.md5sum"), "w+").close()
open(str(tmpdir / ".beta"), "w+").close()
response = http_root.get('/upload')
assert response.status == 200
html = response.html
assert "GNS3 Server" in html
assert "Select & Upload" in html
assert "alpha" in html
assert ".beta" not in html
assert "alpha.md5sum" not in html
def test_upload(http_root, tmpdir):
content = ''.join(['a' for _ in range(0, 1025)])
with open(str(tmpdir / "test"), "w+") as f:
f.write(content)
body = aiohttp.FormData()
body.add_field("type", "QEMU")
body.add_field("file", open(str(tmpdir / "test"), "rb"), content_type="application/iou", filename="test2")
Config.instance().set("Server", "images_path", str(tmpdir))
response = http_root.post('/upload', body=body, raw=True)
assert "test2" in response.body.decode("utf-8")
with open(str(tmpdir / "QEMU" / "test2")) as f:
assert f.read() == content
with open(str(tmpdir / "QEMU" / "test2.md5sum")) as f:
checksum = f.read()
assert checksum == "ae187e1febee2a150b64849c32d566ca"
def test_upload_previous_checksum(http_root, tmpdir):
content = ''.join(['a' for _ in range(0, 1025)])
with open(str(tmpdir / "test"), "w+") as f:
f.write(content)
body = aiohttp.FormData()
body.add_field("type", "QEMU")
body.add_field("file", open(str(tmpdir / "test"), "rb"), content_type="application/iou", filename="test2")
Config.instance().set("Server", "images_path", str(tmpdir))
os.makedirs(str(tmpdir / "QEMU"))
with open(str(tmpdir / "QEMU" / "test2.md5sum"), 'w+') as f:
f.write("FAKE checksum")
response = http_root.post('/upload', body=body, raw=True)
assert "test2" in response.body.decode("utf-8")
with open(str(tmpdir / "QEMU" / "test2")) as f:
assert f.read() == content
with open(str(tmpdir / "QEMU" / "test2.md5sum")) as f:
checksum = f.read()
assert checksum == "ae187e1febee2a150b64849c32d566ca"
def test_upload_images_backup(http_root, tmpdir):
Config.instance().set("Server", "images_path", str(tmpdir / 'images'))
os.makedirs(str(tmpdir / 'images' / 'IOU'))
# An old IOU image that we need to replace
with open(str(tmpdir / 'images' / 'IOU' / 'b.img'), 'w+') as f:
f.write('bad')
os.makedirs(str(tmpdir / 'old' / 'QEMU'))
with open(str(tmpdir / 'old' / 'QEMU' / 'a.img'), 'w+') as f:
f.write('hello')
os.makedirs(str(tmpdir / 'old' / 'IOU'))
with open(str(tmpdir / 'old' / 'IOU' / 'b.img'), 'w+') as f:
f.write('world')
os.chdir(str(tmpdir / 'old'))
with tarfile.open(str(tmpdir / 'test.tar'), 'w') as tar:
tar.add('.', recursive=True)
body = aiohttp.FormData()
body.add_field('type', 'IMAGES')
body.add_field('file', open(str(tmpdir / 'test.tar'), 'rb'), content_type='application/x-gtar', filename='test.tar')
response = http_root.post('/upload', body=body, raw=True)
assert response.status == 200
with open(str(tmpdir / 'images' / 'QEMU' / 'a.img')) as f:
assert f.read() == 'hello'
with open(str(tmpdir / 'images' / 'IOU' / 'b.img')) as f:
assert f.read() == 'world'
assert 'a.img' in response.body.decode('utf-8')
assert 'b.img' in response.body.decode('utf-8')
assert not os.path.exists(str(tmpdir / 'images' / 'archive.tar'))
def test_upload_projects_backup(http_root, tmpdir):
Config.instance().set("Server", "projects_path", str(tmpdir / 'projects'))
os.makedirs(str(tmpdir / 'projects' / 'b'))
# An old b image that we need to replace
with open(str(tmpdir / 'projects' / 'b' / 'b.img'), 'w+') as f:
f.write('bad')
os.makedirs(str(tmpdir / 'old' / 'a'))
with open(str(tmpdir / 'old' / 'a' / 'a.img'), 'w+') as f:
f.write('hello')
os.makedirs(str(tmpdir / 'old' / 'b'))
with open(str(tmpdir / 'old' / 'b' / 'b.img'), 'w+') as f:
f.write('world')
os.chdir(str(tmpdir / 'old'))
with tarfile.open(str(tmpdir / 'test.tar'), 'w') as tar:
tar.add('.', recursive=True)
body = aiohttp.FormData()
body.add_field('type', 'PROJECTS')
body.add_field('file', open(str(tmpdir / 'test.tar'), 'rb'), content_type='application/x-gtar', filename='test.tar')
response = http_root.post('/upload', body=body, raw=True)
assert response.status == 200
with open(str(tmpdir / 'projects' / 'a' / 'a.img')) as f:
assert f.read() == 'hello'
with open(str(tmpdir / 'projects' / 'b' / 'b.img')) as f:
assert f.read() == 'world'
assert 'a.img' not in response.body.decode('utf-8')
assert 'b.img' not in response.body.decode('utf-8')
assert not os.path.exists(str(tmpdir / 'projects' / 'archive.tar'))
def test_backup_images(http_root, tmpdir, loop):
Config.instance().set('Server', 'images_path', str(tmpdir))
os.makedirs(str(tmpdir / 'QEMU'))
with open(str(tmpdir / 'QEMU' / 'a.img'), 'w+') as f:
f.write('hello')
with open(str(tmpdir / 'QEMU' / 'b.img'), 'w+') as f:
f.write('world')
response = http_root.get('/backup/images.tar', raw=True)
assert response.status == 200
assert response.headers['CONTENT-TYPE'] == 'application/x-gtar'
with open(str(tmpdir / 'images.tar'), 'wb+') as f:
print(len(response.body))
f.write(response.body)
tar = tarfile.open(str(tmpdir / 'images.tar'), 'r')
os.makedirs(str(tmpdir / 'extract'))
os.chdir(str(tmpdir / 'extract'))
# Extract to current working directory
tar.extractall()
tar.close()
assert os.path.exists(os.path.join('QEMU', 'a.img'))
assert open(os.path.join('QEMU', 'a.img')).read() == 'hello'
assert os.path.exists(os.path.join('QEMU', 'b.img'))
assert open(os.path.join('QEMU', 'b.img')).read() == 'world'
def test_backup_projects(http_root, tmpdir, loop):
Config.instance().set('Server', 'projects_path', str(tmpdir))
os.makedirs(str(tmpdir / 'a'))
with open(str(tmpdir / 'a' / 'a.gns3'), 'w+') as f:
f.write('hello')
os.makedirs(str(tmpdir / 'b'))
with open(str(tmpdir / 'b' / 'b.gns3'), 'w+') as f:
f.write('world')
response = http_root.get('/backup/projects.tar', raw=True)
assert response.status == 200
assert response.headers['CONTENT-TYPE'] == 'application/x-gtar'
with open(str(tmpdir / 'projects.tar'), 'wb+') as f:
f.write(response.body)
tar = tarfile.open(str(tmpdir / 'projects.tar'), 'r')
os.makedirs(str(tmpdir / 'extract'))
os.chdir(str(tmpdir / 'extract'))
# Extract to current working directory
tar.extractall()
tar.close()
assert os.path.exists(os.path.join('a', 'a.gns3'))
assert open(os.path.join('a', 'a.gns3')).read() == 'hello'
assert os.path.exists(os.path.join('b', 'b.gns3'))
assert open(os.path.join('b', 'b.gns3')).read() == 'world'