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

Cleaner and generic way to set Qemu & IOU VM settings.

This commit is contained in:
grossmj 2015-04-12 15:09:37 -06:00
parent 7e8b0a48ab
commit de5e8f852d
5 changed files with 54 additions and 78 deletions

View File

@ -19,7 +19,6 @@ import os
from aiohttp.web import HTTPConflict
from ...web.route import Route
from ...modules.port_manager import PortManager
from ...schemas.iou import IOU_CREATE_SCHEMA
from ...schemas.iou import IOU_UPDATE_SCHEMA
from ...schemas.iou import IOU_OBJECT_SCHEMA
@ -52,20 +51,16 @@ class IOUHandler:
def create(request, response):
iou = IOU.instance()
vm = yield from iou.create_vm(request.json["name"],
vm = yield from iou.create_vm(request.json.pop("name"),
request.match_info["project_id"],
request.json.get("vm_id"),
console=request.json.get("console"),
serial_adapters=request.json.get("serial_adapters"),
ethernet_adapters=request.json.get("ethernet_adapters"),
ram=request.json.get("ram"),
nvram=request.json.get("nvram"),
use_default_iou_values=request.json.get("use_default_iou_values"),
l1_keepalives=request.json.get("l1_keepalives"),
initial_config=request.json.get("initial_config_content"),
iourc_content=request.json.get("iourc_content")
)
vm.path = request.json.get("path", vm.path)
console=request.json.get("console"))
for name, value in request.json.items():
if hasattr(vm, name) and getattr(vm, name) != value:
setattr(vm, name, value)
if "initial_config_content" in request.json:
vm.initial_config = request.json.get("initial_config_content")
response.set_status(201)
response.json(vm)
@ -109,18 +104,12 @@ class IOUHandler:
iou_manager = IOU.instance()
vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
vm.name = request.json.get("name", vm.name)
vm.console = request.json.get("console", vm.console)
vm.path = request.json.get("path", vm.path)
vm.ethernet_adapters = request.json.get("ethernet_adapters", vm.ethernet_adapters)
vm.serial_adapters = request.json.get("serial_adapters", vm.serial_adapters)
vm.ram = request.json.get("ram", vm.ram)
vm.nvram = request.json.get("nvram", vm.nvram)
vm.use_default_iou_values = request.json.get("use_default_iou_values", vm.use_default_iou_values)
vm.l1_keepalives = request.json.get("l1_keepalives", vm.l1_keepalives)
vm.initial_config = request.json.get("initial_config_content", vm.initial_config)
vm.iourc_content = request.json.get("iourc_content", None)
for name, value in request.json.items():
if hasattr(vm, name) and getattr(vm, name) != value:
setattr(vm, name, value)
if "initial_config_content" in request.json:
vm.initial_config = request.json.get("initial_config_content")
response.json(vm)
@classmethod
@ -273,7 +262,7 @@ class IOUHandler:
pcap_file_path = os.path.join(vm.project.capture_working_directory(), request.json["capture_file_name"])
if not vm.is_running():
raise HTTPConflict(text="You can't capture the traffic on a non started VM")
raise HTTPConflict(text="Cannot capture traffic on a non started VM")
yield from vm.start_capture(adapter_number, port_number, pcap_file_path, request.json["data_link_type"])
response.json({"pcap_file_path": str(pcap_file_path)})
@ -298,7 +287,7 @@ class IOUHandler:
vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
if not vm.is_running():
raise HTTPConflict(text="You can't capture the traffic on a non started VM")
raise HTTPConflict(text="Cannot capture traffic on a non started VM")
adapter_number = int(request.match_info["adapter_number"])
port_number = int(request.match_info["port_number"])

View File

@ -41,24 +41,21 @@ class QEMUHandler:
400: "Invalid request",
409: "Conflict"
},
description="Create a new Qemu.instance",
description="Create a new Qemu VM instance",
input=QEMU_CREATE_SCHEMA,
output=QEMU_OBJECT_SCHEMA)
def create(request, response):
qemu = Qemu.instance()
vm = yield from qemu.create_vm(request.json["name"],
vm = yield from qemu.create_vm(request.json.pop("name"),
request.match_info["project_id"],
request.json.get("vm_id"),
qemu_path=request.json.get("qemu_path"),
console=request.json.get("console"))
# Clear already used keys
map(request.json.__delitem__, ["name", "project_id", "vm_id",
"qemu_path", "console"])
for field in request.json:
setattr(vm, field, request.json[field])
for name, value in request.json.items():
if hasattr(vm, name) and getattr(vm, name) != value:
setattr(vm, name, value)
response.set_status(201)
response.json(vm)
@ -75,7 +72,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Get a Qemu.instance",
description="Get a Qemu VM instance",
output=QEMU_OBJECT_SCHEMA)
def show(request, response):
@ -96,15 +93,17 @@ class QEMUHandler:
404: "Instance doesn't exist",
409: "Conflict"
},
description="Update a Qemu.instance",
description="Update a Qemu VM instance",
input=QEMU_UPDATE_SCHEMA,
output=QEMU_OBJECT_SCHEMA)
def update(request, response):
qemu_manager = Qemu.instance()
vm = qemu_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
for field in request.json:
setattr(vm, field, request.json[field])
for name, value in request.json.items():
if hasattr(vm, name) and getattr(vm, name) != value:
setattr(vm, name, value)
response.json(vm)
@ -120,7 +119,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Delete a Qemu.instance")
description="Delete a Qemu VM instance")
def delete(request, response):
yield from Qemu.instance().delete_vm(request.match_info["vm_id"])
@ -138,7 +137,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Start a Qemu.instance")
description="Start a Qemu VM instance")
def start(request, response):
qemu_manager = Qemu.instance()
@ -158,7 +157,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Stop a Qemu.instance")
description="Stop a Qemu VM instance")
def stop(request, response):
qemu_manager = Qemu.instance()
@ -178,7 +177,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Reload a Qemu.instance")
description="Reload a Qemu VM instance")
def reload(request, response):
qemu_manager = Qemu.instance()
@ -198,7 +197,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Suspend a Qemu.instance")
description="Suspend a Qemu VM instance")
def suspend(request, response):
qemu_manager = Qemu.instance()
@ -218,7 +217,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Resume a Qemu.instance")
description="Resume a Qemu VM instance")
def resume(request, response):
qemu_manager = Qemu.instance()
@ -239,7 +238,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Add a NIO to a Qemu.instance",
description="Add a NIO to a Qemu VM instance",
input=QEMU_NIO_SCHEMA,
output=QEMU_NIO_SCHEMA)
def create_nio(request, response):
@ -265,7 +264,7 @@ class QEMUHandler:
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Remove a NIO from a Qemu.instance")
description="Remove a NIO from a Qemu VM instance")
def delete_nio(request, response):
qemu_manager = Qemu.instance()

View File

@ -61,25 +61,9 @@ class IOUVM(BaseVM):
:param project: Project instance
:param manager: Manager instance
:param console: TCP console port
:params ethernet_adapters: number of ethernet adapters
:params serial_adapters: number of serial adapters
:params ram: amount of RAM in MB
:params nvram: amount of NVRAM in KB
:params l1_keepalives: always keep the Ethernet interfaces up
:params initial_config: content of the initial configuration file
:params iourc_content: content of the iourc file if no licence is installed on the machine
"""
def __init__(self, name, vm_id, project, manager,
console=None,
ram=None,
nvram=None,
use_default_iou_values=None,
ethernet_adapters=None,
serial_adapters=None,
l1_keepalives=None,
initial_config=None,
iourc_content=None):
def __init__(self, name, vm_id, project, manager, console=None):
super().__init__(name, vm_id, project, manager, console=console)
@ -94,17 +78,13 @@ class IOUVM(BaseVM):
# IOU settings
self._ethernet_adapters = []
self._serial_adapters = []
self.ethernet_adapters = 2 if ethernet_adapters is None else ethernet_adapters # one adapter = 4 interfaces
self.serial_adapters = 2 if serial_adapters is None else serial_adapters # one adapter = 4 interfaces
self._use_default_iou_values = True if use_default_iou_values is None else use_default_iou_values # for RAM & NVRAM values
self._nvram = 128 if nvram is None else nvram # Kilobytes
self.ethernet_adapters = 2 # one adapter = 4 interfaces
self.serial_adapters = 2 # one adapter = 4 interfaces
self._use_default_iou_values = True # for RAM & NVRAM values
self._nvram = 128 # Kilobytes
self._initial_config = ""
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.iourc_content = iourc_content
if initial_config is not None:
self.initial_config = initial_config
self._ram = 256 # Megabytes
self._l1_keepalives = False # used to overcome the always-up Ethernet interfaces (not supported by all IOSes).
@asyncio.coroutine
def close(self):
@ -236,8 +216,7 @@ class IOUVM(BaseVM):
"nvram": self._nvram,
"l1_keepalives": self._l1_keepalives,
"initial_config": self.relative_initial_config_file,
"use_default_iou_values": self._use_default_iou_values,
"iourc_path": self.iourc_path}
"use_default_iou_values": self._use_default_iou_values}
# return the relative path if the IOU image is in the images_path directory
server_config = self.manager.config.get_section_config("Server")

View File

@ -70,8 +70,12 @@ IOU_CREATE_SCHEMA = {
"description": "Use default IOU values",
"type": ["boolean", "null"]
},
"initial_config": {
"description": "Path to the initial configuration of IOU",
"type": ["string", "null"]
},
"initial_config_content": {
"description": "Initial configuration of the IOU",
"description": "Initial configuration of IOU",
"type": ["string", "null"]
},
"iourc_content": {
@ -123,8 +127,12 @@ IOU_UPDATE_SCHEMA = {
"description": "Always up ethernet interface",
"type": ["boolean", "null"]
},
"initial_config": {
"description": "Path to the initial configuration of IOU",
"type": ["string", "null"]
},
"initial_config_content": {
"description": "Initial configuration of the IOU",
"description": "Initial configuration of IOU",
"type": ["string", "null"]
},
"use_default_iou_values": {

View File

@ -82,7 +82,8 @@ def test_vm(project, manager):
def test_vm_initial_config(project, manager):
vm = IOUVM("test", "00010203-0405-0607-0808-0a0b0c0d0e0f", project, manager, initial_config="hostname %h")
vm = IOUVM("test", "00010203-0405-0607-0808-0a0b0c0d0e0f", project, manager)
vm.initial_config = "hostname %h"
assert vm.name == "test"
assert vm.initial_config == "hostname test"
assert vm.id == "00010203-0405-0607-0808-0a0b0c0d0e0f"