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

Support uploading iourc

This commit is contained in:
Julien Duponchelle 2015-03-17 16:31:45 +01:00
parent 964ea0f577
commit 66cdf39ea2
7 changed files with 98 additions and 3 deletions

View File

@ -61,7 +61,8 @@ class IOUHandler:
ram=request.json.get("ram"), ram=request.json.get("ram"),
nvram=request.json.get("nvram"), nvram=request.json.get("nvram"),
l1_keepalives=request.json.get("l1_keepalives"), l1_keepalives=request.json.get("l1_keepalives"),
initial_config=request.json.get("initial_config_content") initial_config=request.json.get("initial_config_content"),
iourc_content=request.json.get("iourc_content")
) )
vm.path = request.json.get("path", vm.path) vm.path = request.json.get("path", vm.path)
response.set_status(201) response.set_status(201)

View File

@ -20,6 +20,7 @@ import logging
import aiohttp import aiohttp
import shutil import shutil
import asyncio import asyncio
import tempfile
from ..utils.asyncio import wait_run_in_executor from ..utils.asyncio import wait_run_in_executor
@ -45,6 +46,7 @@ class BaseVM:
self._project = project self._project = project
self._manager = manager self._manager = manager
self._console = console self._console = console
self._temporary_directory = None
if self._console is not None: if self._console is not None:
self._console = self._manager.port_manager.reserve_tcp_port(self._console) self._console = self._manager.port_manager.reserve_tcp_port(self._console)
@ -61,6 +63,9 @@ class BaseVM:
def __del__(self): def __del__(self):
self.close() self.close()
if self._temporary_directory is not None:
if os.path.exists(self._temporary_directory):
shutil.rmtree(self._temporary_directory)
@property @property
def project(self): def project(self):
@ -124,6 +129,12 @@ class BaseVM:
return self._project.vm_working_directory(self) return self._project.vm_working_directory(self)
@property
def temporary_directory(self):
if self._temporary_directory is None:
self._temporary_directory = tempfile.mkdtemp()
return self._temporary_directory
def create(self): def create(self):
""" """
Creates the VM. Creates the VM.

View File

@ -67,6 +67,7 @@ class IOUVM(BaseVM):
:params nvram: Nvram KB :params nvram: Nvram KB
:params l1_keepalives: Always up ethernet interface: :params l1_keepalives: Always up ethernet interface:
:params initial_config: Content of the initial configuration file :params initial_config: Content of the initial configuration file
:params iourc_content: Content of the iourc file if no licence is installed on server
""" """
def __init__(self, name, vm_id, project, manager, def __init__(self, name, vm_id, project, manager,
@ -76,7 +77,8 @@ class IOUVM(BaseVM):
ethernet_adapters=None, ethernet_adapters=None,
serial_adapters=None, serial_adapters=None,
l1_keepalives=None, l1_keepalives=None,
initial_config=None): initial_config=None,
iourc_content=None):
super().__init__(name, vm_id, project, manager, console=console) super().__init__(name, vm_id, project, manager, console=console)
@ -99,6 +101,7 @@ class IOUVM(BaseVM):
self._ram = 256 if ram is None else ram # Megabytes self._ram = 256 if ram is None else ram # Megabytes
self._l1_keepalives = False if l1_keepalives is None else l1_keepalives # used to overcome the always-up Ethernet interfaces (not supported by all IOSes). self._l1_keepalives = False if l1_keepalives is None else l1_keepalives # used to overcome the always-up Ethernet interfaces (not supported by all IOSes).
self.iourc_content = iourc_content
if initial_config is not None: if initial_config is not None:
self.initial_config = initial_config self.initial_config = initial_config
@ -209,7 +212,8 @@ class IOUVM(BaseVM):
"nvram": self._nvram, "nvram": self._nvram,
"l1_keepalives": self._l1_keepalives, "l1_keepalives": self._l1_keepalives,
"initial_config": self.relative_initial_config_file, "initial_config": self.relative_initial_config_file,
"use_default_iou_values": self._use_default_iou_values} "use_default_iou_values": self._use_default_iou_values,
"iourc_path": self.iourc_path}
# return the relative path if the IOU image is in the images_path directory # return the relative path if the IOU image is in the images_path directory
server_config = self.manager.config.get_section_config("Server") server_config = self.manager.config.get_section_config("Server")
@ -250,6 +254,10 @@ class IOUVM(BaseVM):
path = os.path.join(self.working_dir, "iourc") path = os.path.join(self.working_dir, "iourc")
if os.path.exists(path): if os.path.exists(path):
return path return path
# look for the iourc file in the temporary dir.
path = os.path.join(self.temporary_directory, "iourc")
if os.path.exists(path):
return path
return iourc_path return iourc_path
@property @property
@ -322,6 +330,17 @@ class IOUVM(BaseVM):
def application_id(self): def application_id(self):
return self._manager.get_application_id(self.id) return self._manager.get_application_id(self.id)
@property
def iourc_content(self):
with open(os.path.join(self.temporary_directory, "iourc")) as f:
return f.read()
@iourc_content.setter
def iourc_content(self, value):
if value is not None:
with open(os.path.join(self.temporary_directory, "iourc"), "w+") as f:
f.write(value)
@asyncio.coroutine @asyncio.coroutine
def _library_check(self): def _library_check(self):
""" """

View File

@ -73,6 +73,10 @@ IOU_CREATE_SCHEMA = {
"initial_config_content": { "initial_config_content": {
"description": "Initial configuration of the IOU", "description": "Initial configuration of the IOU",
"type": ["string", "null"] "type": ["string", "null"]
},
"iourc_content": {
"description": "Content of the iourc file, if a file exist on servers this variable is ignored. It's mostly for compatibility with < 1.3 releases",
"type": ["string", "null"]
} }
}, },
"additionalProperties": False, "additionalProperties": False,
@ -192,6 +196,10 @@ IOU_OBJECT_SCHEMA = {
"use_default_iou_values": { "use_default_iou_values": {
"description": "Use default IOU values", "description": "Use default IOU values",
"type": ["boolean", "null"] "type": ["boolean", "null"]
},
"iourc_path": {
"description": "Path of the iourc file used by remote servers",
"type": ["string", "null"]
} }
}, },
"additionalProperties": False, "additionalProperties": False,

View File

@ -75,6 +75,7 @@ def test_iou_create_with_params(server, project, base_params):
params["l1_keepalives"] = True params["l1_keepalives"] = True
params["initial_config_content"] = "hostname test" params["initial_config_content"] = "hostname test"
params["use_default_iou_values"] = True params["use_default_iou_values"] = True
params["iourc_content"] = "test"
response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True) response = server.post("/projects/{project_id}/iou/vms".format(project_id=project.id), params, example=True)
assert response.status == 201 assert response.status == 201
@ -92,6 +93,8 @@ def test_iou_create_with_params(server, project, base_params):
with open(initial_config_file(project, response.json)) as f: with open(initial_config_file(project, response.json)) as f:
assert f.read() == params["initial_config_content"] assert f.read() == params["initial_config_content"]
assert "iourc" in response.json["iourc_path"]
def test_iou_get(server, project, vm): def test_iou_get(server, project, vm):
response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True) response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)

View File

@ -367,3 +367,11 @@ def test_invalid_iou_file(loop, vm, iourc_file):
with pytest.raises(IOUError): with pytest.raises(IOUError):
os.remove(iourc_file) os.remove(iourc_file)
loop.run_until_complete(asyncio.async(vm._check_iou_licence())) loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
def test_iourc_content(vm):
vm.iourc_content = "test"
with open(os.path.join(vm.temporary_directory, "iourc")) as f:
assert f.read() == "test"

View File

@ -0,0 +1,45 @@
# -*- 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 pytest
import aiohttp
import asyncio
import os
from tests.utils import asyncio_patch
from unittest.mock import patch, MagicMock
from gns3server.modules.vpcs.vpcs_vm import VPCSVM
from gns3server.modules.vpcs.vpcs_error import VPCSError
from gns3server.modules.vpcs import VPCS
@pytest.fixture(scope="module")
def manager(port_manager):
m = VPCS.instance()
m.port_manager = port_manager
return m
@pytest.fixture(scope="function")
def vm(project, manager):
return VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
def test_temporary_directory(project, manager):
vm = VPCSVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
assert isinstance(vm.temporary_directory, str)