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

autopep8 et upload files

This commit is contained in:
Julien Duponchelle 2015-02-23 17:21:39 +01:00
parent 259f6249e2
commit c9314ec509
16 changed files with 99 additions and 24 deletions

View File

@ -16,4 +16,3 @@
from gns3server.handlers.api import * from gns3server.handlers.api import *
from gns3server.handlers.upload_handler import UploadHandler from gns3server.handlers.upload_handler import UploadHandler

View File

@ -30,6 +30,7 @@ from ...schemas.dynamips_vm import VM_CONFIGS_SCHEMA
from ...modules.dynamips import Dynamips from ...modules.dynamips import Dynamips
from ...modules.project_manager import ProjectManager from ...modules.project_manager import ProjectManager
class DynamipsVMHandler: class DynamipsVMHandler:
""" """
@ -387,4 +388,3 @@ class DynamipsVMHandler:
idlepc = yield from dynamips_manager.auto_idlepc(vm) idlepc = yield from dynamips_manager.auto_idlepc(vm)
response.set_status(200) response.set_status(200)
response.json({"idlepc": idlepc}) response.json({"idlepc": idlepc})

View File

@ -15,6 +15,10 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import stat
from ..config import Config
from ..web.route import Route from ..web.route import Route
from ..schemas.version import VERSION_SCHEMA from ..schemas.version import VERSION_SCHEMA
from ..version import __version__ from ..version import __version__
@ -30,5 +34,38 @@ class UploadHandler:
api_version=None api_version=None
) )
def index(request, response): def index(request, response):
response.template("upload.html") files = []
for filename in os.listdir(UploadHandler.image_directory()):
if os.path.isfile(os.path.join(UploadHandler.image_directory(), filename)):
if filename[0] != ".":
files.append(filename)
response.template("upload.html", files=files, image_path=UploadHandler.image_directory())
@classmethod
@Route.post(
r"/upload",
description="Manage upload of GNS3 images",
api_version=None
)
def upload(request, response):
data = yield from request.post()
destination_path = os.path.join(UploadHandler.image_directory(), data["file"].filename)
try:
os.makedirs(UploadHandler.image_directory(), exist_ok=True)
with open(destination_path, "wb+") as f:
f.write(data["file"].file.read())
print(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(500)
return
response.redirect("/upload")
@staticmethod
def image_directory():
server_config = Config.instance().get_section_config("Server")
return os.path.expanduser(server_config.get("image_directory", "~/GNS3/images"))

View File

@ -357,10 +357,10 @@ class Dynamips(BaseManager):
raise DynamipsError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e)) raise DynamipsError("Could not create an UDP connection to {}:{}: {}".format(rhost, rport, e))
# check if we have an allocated NIO UDP auto # check if we have an allocated NIO UDP auto
#nio = node.hypervisor.get_nio_udp_auto(lport) #nio = node.hypervisor.get_nio_udp_auto(lport)
#if not nio: # if not nio:
# otherwise create an NIO UDP # otherwise create an NIO UDP
nio = NIOUDP(node.hypervisor, lport, rhost, rport) nio = NIOUDP(node.hypervisor, lport, rhost, rport)
#else: # else:
# nio.connect(rhost, rport) # nio.connect(rhost, rport)
elif nio_settings["type"] == "nio_generic_ethernet": elif nio_settings["type"] == "nio_generic_ethernet":
ethernet_device = nio_settings["ethernet_device"] ethernet_device = nio_settings["ethernet_device"]
@ -462,9 +462,9 @@ class Dynamips(BaseManager):
slot_id = int(name[-1]) slot_id = int(name[-1])
adapter_name = value adapter_name = value
adapter = ADAPTER_MATRIX[adapter_name]() adapter = ADAPTER_MATRIX[adapter_name]()
if vm.slots[slot_id] and type(vm.slots[slot_id]) != type(adapter): if vm.slots[slot_id] and not isinstance(vm.slots[slot_id], type(adapter)):
yield from vm.slot_remove_binding(slot_id) yield from vm.slot_remove_binding(slot_id)
if type(vm.slots[slot_id]) != type(adapter): if not isinstance(vm.slots[slot_id], type(adapter)):
yield from vm.slot_add_binding(slot_id, adapter) yield from vm.slot_add_binding(slot_id, adapter)
elif name.startswith("slot") and value is None: elif name.startswith("slot") and value is None:
slot_id = int(name[-1]) slot_id = int(name[-1])
@ -474,9 +474,9 @@ class Dynamips(BaseManager):
wic_slot_id = int(name[-1]) wic_slot_id = int(name[-1])
wic_name = value wic_name = value
wic = WIC_MATRIX[wic_name]() wic = WIC_MATRIX[wic_name]()
if vm.slots[0].wics[wic_slot_id] and type(vm.slots[0].wics[wic_slot_id]) != type(wic): if vm.slots[0].wics[wic_slot_id] and not isinstance(vm.slots[0].wics[wic_slot_id], type(wic)):
yield from vm.uninstall_wic(wic_slot_id) yield from vm.uninstall_wic(wic_slot_id)
if type(vm.slots[0].wics[wic_slot_id]) != type(wic): if not isinstance(vm.slots[0].wics[wic_slot_id], type(wic)):
yield from vm.install_wic(wic_slot_id, wic) yield from vm.install_wic(wic_slot_id, wic)
elif name.startswith("wic") and value is None: elif name.startswith("wic") and value is None:
wic_slot_id = int(name[-1]) wic_slot_id = int(name[-1])

View File

@ -31,6 +31,7 @@ log = logging.getLogger(__name__)
class ATMSwitch(Device): class ATMSwitch(Device):
""" """
Dynamips ATM switch. Dynamips ATM switch.
@ -79,7 +80,6 @@ class ATMSwitch(Device):
new_name=new_name)) new_name=new_name))
self._name = new_name self._name = new_name
@property @property
def nios(self): def nios(self):
""" """

View File

@ -25,6 +25,7 @@ from .device import Device
class Bridge(Device): class Bridge(Device):
""" """
Dynamips bridge. Dynamips bridge.

View File

@ -17,6 +17,7 @@
class Device: class Device:
""" """
Base device for switches and hubs Base device for switches and hubs

View File

@ -29,6 +29,7 @@ log = logging.getLogger(__name__)
class EthernetHub(Bridge): class EthernetHub(Bridge):
""" """
Dynamips Ethernet hub (based on Bridge) Dynamips Ethernet hub (based on Bridge)

View File

@ -31,6 +31,7 @@ log = logging.getLogger(__name__)
class EthernetSwitch(Device): class EthernetSwitch(Device):
""" """
Dynamips Ethernet switch. Dynamips Ethernet switch.

View File

@ -30,6 +30,7 @@ log = logging.getLogger(__name__)
class FrameRelaySwitch(Device): class FrameRelaySwitch(Device):
""" """
Dynamips Frame Relay switch. Dynamips Frame Relay switch.

View File

@ -63,7 +63,7 @@ DEVICE_UPDATE_SCHEMA = {
"vlan": {"description": "VLAN number", "vlan": {"description": "VLAN number",
"type": "integer", "type": "integer",
"minimum": 1 "minimum": 1
}, },
}, },
"required": ["port", "type", "vlan"], "required": ["port", "type", "vlan"],
"additionalProperties": False "additionalProperties": False
@ -108,7 +108,7 @@ DEVICE_OBJECT_SCHEMA = {
"vlan": {"description": "VLAN number", "vlan": {"description": "VLAN number",
"type": "integer", "type": "integer",
"minimum": 1 "minimum": 1
}, },
}, },
"required": ["port", "type", "vlan"], "required": ["port", "type", "vlan"],
"additionalProperties": False "additionalProperties": False
@ -306,7 +306,7 @@ DEVICE_NIO_SCHEMA = {
"vlan": {"description": "VLAN number", "vlan": {"description": "VLAN number",
"type": "integer", "type": "integer",
"minimum": 1 "minimum": 1
}, },
}, },
"required": ["type", "vlan"], "required": ["type", "vlan"],
"additionalProperties": False "additionalProperties": False

View File

@ -7,10 +7,10 @@
<br /> <br />
<input type="submit" value="Upload" /> <input type="submit" value="Upload" />
</form> </form>
{%if items%} {%if files%}
<h3>Files on {{host}}</h3> <h2>Files on {{gns3_host}}</h2>
{%for item in items%} {%for file in files%}
<p>{{path}}/{{item}}</a></p> <p>{{image_path}}/{{file}}</a></p>
{%endfor%} {%endfor%}
{%endif%} {%endif%}
{% endblock %} {% endblock %}

View File

@ -46,7 +46,7 @@ class Response(aiohttp.web.Response):
log.debug("%s", request.json) log.debug("%s", request.json)
log.info("Response: %d %s", self.status, self.reason) log.info("Response: %d %s", self.status, self.reason)
log.debug(dict(self.headers)) log.debug(dict(self.headers))
if hasattr(self, 'body') and self.body is not None: if hasattr(self, 'body') and self.body is not None and self.headers["CONTENT-TYPE"] == "application/json":
log.debug(json.loads(self.body.decode('utf-8'))) log.debug(json.loads(self.body.decode('utf-8')))
return super().start(request) return super().start(request)
@ -90,3 +90,11 @@ class Response(aiohttp.web.Response):
log.error("Invalid output query. JSON schema error: {}".format(e.message)) log.error("Invalid output query. JSON schema error: {}".format(e.message))
raise aiohttp.web.HTTPBadRequest(text="{}".format(e)) raise aiohttp.web.HTTPBadRequest(text="{}".format(e))
self.body = json.dumps(answer, indent=4, sort_keys=True).encode('utf-8') self.body = json.dumps(answer, indent=4, sort_keys=True).encode('utf-8')
def redirect(self, url):
"""
Redirect to url
:params url: Redirection URL
"""
raise aiohttp.web.HTTPFound(url)

View File

@ -82,6 +82,8 @@ 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)
# If it's a JSON api endpoint just register the endpoint an do nothing
if api_version is None: if api_version is None:
cls._path = path cls._path = path
else: else:
@ -107,6 +109,14 @@ class Route(object):
@asyncio.coroutine @asyncio.coroutine
def control_schema(request): def control_schema(request):
# This block is executed at each method call # This block is executed at each method call
# 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
try: try:
request = yield from parse_request(request, input_schema) request = yield from parse_request(request, input_schema)
response = Response(route=route, output_schema=output_schema) response = Response(route=route, output_schema=output_schema)

View File

@ -49,7 +49,7 @@ class Query:
return "http://{}:{}{}".format(self._host, self._port, path) return "http://{}:{}{}".format(self._host, self._port, path)
return "http://{}:{}/v{}{}".format(self._host, self._port, version, path) return "http://{}:{}/v{}{}".format(self._host, self._port, version, path)
def _fetch(self, method, path, body=None, api_version = 1, **kwargs): def _fetch(self, method, path, body=None, api_version=1, **kwargs):
"""Fetch an url, parse the JSON and return response """Fetch an url, parse the JSON and return response
Options: Options:

View File

@ -15,17 +15,33 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
This test suite check /version endpoint import aiohttp
It's also used for unittest the HTTP implementation. import os
""" from unittest.mock import patch
from gns3server.version import __version__ from gns3server.version import __version__
def test_version_index_upload(server): def test_index_upload(server):
response = server.get('/upload', api_version=None) response = server.get('/upload', api_version=None)
assert response.status == 200 assert response.status == 200
html = response.html html = response.html
assert "GNS3 Server" in html assert "GNS3 Server" in html
assert "Select & Upload" in html assert "Select & Upload" in html
def test_upload(server, tmpdir):
with open(str(tmpdir / "test"), "w+") as f:
f.write("TEST")
body = aiohttp.FormData()
body.add_field("file", open(str(tmpdir / "test"), "rb"), content_type="application/iou", filename="test2")
with patch("gns3server.config.Config.get_section_config", return_value={"image_directory": str(tmpdir)}):
response = server.post('/upload', api_version=None, body=body, raw=True)
with open(str(tmpdir / "test2")) as f:
assert f.read() == "TEST"
assert "test2" in response.body.decode("utf-8")