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

Send command line used to start the VM to client

Add a command_line attribute to the VM object with
the command line used to start the VM.

Now /start return the object in order to get
this new attribute. And the HTTP status code
is 200 instead of 204 because 204 disallow body.

Support:
* Qemu
* Dynamips
* IOU

Ref https://github.com/GNS3/gns3-gui/issues/513
This commit is contained in:
Julien Duponchelle 2016-02-02 18:25:17 +01:00
parent 71c3bda0a5
commit fd22cd8361
No known key found for this signature in database
GPG Key ID: F1E2485547D4595D
16 changed files with 67 additions and 31 deletions

View File

@ -148,11 +148,12 @@ class IOUHandler:
"vm_id": "UUID for the instance" "vm_id": "UUID for the instance"
}, },
status_codes={ status_codes={
204: "Instance started", 200: "Instance started",
400: "Invalid request", 400: "Invalid request",
404: "Instance doesn't exist" 404: "Instance doesn't exist"
}, },
input=IOU_START_SCHEMA, input=IOU_START_SCHEMA,
output=IOU_OBJECT_SCHEMA,
description="Start a IOU instance") description="Start a IOU instance")
def start(request, response): def start(request, response):
@ -166,7 +167,7 @@ class IOUHandler:
print(vm.iourc_path) print(vm.iourc_path)
yield from vm.start() yield from vm.start()
response.set_status(204) response.json(vm)
@classmethod @classmethod
@Route.post( @Route.post(

View File

@ -146,11 +146,12 @@ class QEMUHandler:
"vm_id": "UUID for the instance" "vm_id": "UUID for the instance"
}, },
status_codes={ status_codes={
204: "Instance started", 200: "Instance started",
400: "Invalid request", 400: "Invalid request",
404: "Instance doesn't exist" 404: "Instance doesn't exist"
}, },
description="Start a Qemu VM instance") description="Start a Qemu VM instance",
output=QEMU_OBJECT_SCHEMA)
def start(request, response): def start(request, response):
qemu_manager = Qemu.instance() qemu_manager = Qemu.instance()
@ -161,7 +162,7 @@ class QEMUHandler:
if pm.check_hardware_virtualization(vm) is False: if pm.check_hardware_virtualization(vm) is False:
raise HTTPConflict(text="Cannot start VM with KVM enabled because hardware virtualization (VT-x/AMD-V) is already used by another software like VMware or VirtualBox") raise HTTPConflict(text="Cannot start VM with KVM enabled because hardware virtualization (VT-x/AMD-V) is already used by another software like VMware or VirtualBox")
yield from vm.start() yield from vm.start()
response.set_status(204) response.json(vm)
@classmethod @classmethod
@Route.post( @Route.post(

View File

@ -130,13 +130,14 @@ class VPCSHandler:
400: "Invalid request", 400: "Invalid request",
404: "Instance doesn't exist" 404: "Instance doesn't exist"
}, },
description="Start a VPCS instance") description="Start a VPCS instance",
output=VPCS_OBJECT_SCHEMA)
def start(request, response): def start(request, response):
vpcs_manager = VPCS.instance() vpcs_manager = VPCS.instance()
vm = vpcs_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) vm = vpcs_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
yield from vm.start() yield from vm.start()
response.set_status(204) response.json(vm)
@classmethod @classmethod
@Route.post( @Route.post(

View File

@ -58,6 +58,7 @@ class BaseVM:
self._hw_virtualization = False self._hw_virtualization = False
self._ubridge_hypervisor = None self._ubridge_hypervisor = None
self._vm_status = "stopped" self._vm_status = "stopped"
self._command_line = ""
if self._console is not None: if self._console is not None:
if console_type == "vnc": if console_type == "vnc":
@ -94,6 +95,17 @@ class BaseVM:
self._vm_status = status self._vm_status = status
self._project.emit("vm.{}".format(status), self) self._project.emit("vm.{}".format(status), self)
@property
def command_line(self):
"""Return command used to start the VM"""
return self._command_line
@command_line.setter
def command_line(self, command_line):
self._command_line = command_line
@property @property
def project(self): def project(self):
""" """

View File

@ -72,7 +72,6 @@ class IOUVM(BaseVM):
super().__init__(name, vm_id, project, manager, console=console) super().__init__(name, vm_id, project, manager, console=console)
self._command = []
self._iouyap_process = None self._iouyap_process = None
self._iou_process = None self._iou_process = None
self._iou_stdout_file = "" self._iou_stdout_file = ""
@ -220,7 +219,8 @@ class IOUVM(BaseVM):
"startup_config": self.relative_startup_config_file, "startup_config": self.relative_startup_config_file,
"private_config": self.relative_private_config_file, "private_config": self.relative_private_config_file,
"iourc_path": self.iourc_path, "iourc_path": self.iourc_path,
"use_default_iou_values": self._use_default_iou_values} "use_default_iou_values": self._use_default_iou_values,
"command_line": self.command_line}
# 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
iou_vm_info["path"] = self.manager.get_relative_image_path(self.path) iou_vm_info["path"] = self.manager.get_relative_image_path(self.path)
@ -502,13 +502,14 @@ class IOUVM(BaseVM):
if "IOURC" not in os.environ: if "IOURC" not in os.environ:
env["IOURC"] = iourc_path env["IOURC"] = iourc_path
self._command = yield from self._build_command() command = yield from self._build_command()
try: try:
log.info("Starting IOU: {}".format(self._command)) log.info("Starting IOU: {}".format(command))
self._iou_stdout_file = os.path.join(self.working_dir, "iou.log") self._iou_stdout_file = os.path.join(self.working_dir, "iou.log")
log.info("Logging to {}".format(self._iou_stdout_file)) log.info("Logging to {}".format(self._iou_stdout_file))
with open(self._iou_stdout_file, "w", encoding="utf-8") as fd: with open(self._iou_stdout_file, "w", encoding="utf-8") as fd:
self._iou_process = yield from asyncio.create_subprocess_exec(*self._command, self.command_line = ' '.join(command)
self._iou_process = yield from asyncio.create_subprocess_exec(*command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
cwd=self.working_dir, cwd=self.working_dir,

View File

@ -68,7 +68,6 @@ class QemuVM(BaseVM):
self._host = server_config.get("host", "127.0.0.1") self._host = server_config.get("host", "127.0.0.1")
self._monitor_host = server_config.get("monitor_host", "127.0.0.1") self._monitor_host = server_config.get("monitor_host", "127.0.0.1")
self._linked_clone = linked_clone self._linked_clone = linked_clone
self._command = []
self._process = None self._process = None
self._cpulimit_process = None self._cpulimit_process = None
self._monitor = None self._monitor = None
@ -867,15 +866,16 @@ class QemuVM(BaseVM):
# check if there is enough RAM to run # check if there is enough RAM to run
self.check_available_ram(self.ram) self.check_available_ram(self.ram)
self._command = yield from self._build_command() command = yield from self._build_command()
command_string = " ".join(shlex.quote(s) for s in self._command) command_string = " ".join(shlex.quote(s) for s in command)
try: try:
log.info("Starting QEMU with: {}".format(command_string)) log.info("Starting QEMU with: {}".format(command_string))
self._stdout_file = os.path.join(self.working_dir, "qemu.log") self._stdout_file = os.path.join(self.working_dir, "qemu.log")
log.info("logging to {}".format(self._stdout_file)) log.info("logging to {}".format(self._stdout_file))
with open(self._stdout_file, "w", encoding="utf-8") as fd: with open(self._stdout_file, "w", encoding="utf-8") as fd:
fd.write("Start QEMU with {}\n\nExecution log:\n".format(command_string)) fd.write("Start QEMU with {}\n\nExecution log:\n".format(command_string))
self._process = yield from asyncio.create_subprocess_exec(*self._command, self.command_line = ' '.join(command)
self._process = yield from asyncio.create_subprocess_exec(*command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
cwd=self.working_dir) cwd=self.working_dir)

View File

@ -59,7 +59,6 @@ class VPCSVM(BaseVM):
def __init__(self, name, vm_id, project, manager, console=None, startup_script=None): def __init__(self, name, vm_id, project, manager, console=None, startup_script=None):
super().__init__(name, vm_id, project, manager, console=console) super().__init__(name, vm_id, project, manager, console=console)
self._command = []
self._process = None self._process = None
self._vpcs_stdout_file = "" self._vpcs_stdout_file = ""
self._vpcs_version = None self._vpcs_version = None
@ -115,7 +114,8 @@ class VPCSVM(BaseVM):
"console": self._console, "console": self._console,
"project_id": self.project.id, "project_id": self.project.id,
"startup_script": self.startup_script, "startup_script": self.startup_script,
"startup_script_path": self.relative_startup_script} "startup_script_path": self.relative_startup_script,
"command_line": self.command_line}
@property @property
def relative_startup_script(self): def relative_startup_script(self):
@ -223,16 +223,17 @@ class VPCSVM(BaseVM):
if not self._ethernet_adapter.get_nio(0): if not self._ethernet_adapter.get_nio(0):
raise VPCSError("This VPCS instance must be connected in order to start") raise VPCSError("This VPCS instance must be connected in order to start")
self._command = self._build_command() command = self._build_command()
try: try:
log.info("Starting VPCS: {}".format(self._command)) log.info("Starting VPCS: {}".format(command))
self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log") self._vpcs_stdout_file = os.path.join(self.working_dir, "vpcs.log")
log.info("Logging to {}".format(self._vpcs_stdout_file)) log.info("Logging to {}".format(self._vpcs_stdout_file))
flags = 0 flags = 0
if sys.platform.startswith("win32"): if sys.platform.startswith("win32"):
flags = subprocess.CREATE_NEW_PROCESS_GROUP flags = subprocess.CREATE_NEW_PROCESS_GROUP
with open(self._vpcs_stdout_file, "w", encoding="utf-8") as fd: with open(self._vpcs_stdout_file, "w", encoding="utf-8") as fd:
self._process = yield from asyncio.create_subprocess_exec(*self._command, self.command_line = ' '.join(command)
self._process = yield from asyncio.create_subprocess_exec(*command,
stdout=fd, stdout=fd,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
cwd=self.working_dir, cwd=self.working_dir,

View File

@ -254,11 +254,16 @@ IOU_OBJECT_SCHEMA = {
"iourc_path": { "iourc_path": {
"description": "Path of the iourc file used by remote servers", "description": "Path of the iourc file used by remote servers",
"type": ["string", "null"] "type": ["string", "null"]
},
"command_line": {
"description": "Last command line used by GNS3 to start QEMU",
"type": "string"
} }
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name", "vm_id", "console", "project_id", "path", "md5sum", "serial_adapters", "ethernet_adapters", "required": ["name", "vm_id", "console", "project_id", "path", "md5sum", "serial_adapters", "ethernet_adapters",
"ram", "nvram", "l1_keepalives", "startup_config", "private_config", "use_default_iou_values"] "ram", "nvram", "l1_keepalives", "startup_config", "private_config", "use_default_iou_values",
"command_line"]
} }
IOU_CAPTURE_SCHEMA = { IOU_CAPTURE_SCHEMA = {

View File

@ -558,6 +558,10 @@ QEMU_OBJECT_SCHEMA = {
"description": "Additional QEMU options", "description": "Additional QEMU options",
"type": "string", "type": "string",
}, },
"command_line": {
"description": "Last command line used by GNS3 to start QEMU",
"type": "string"
}
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["vm_id", "required": ["vm_id",
@ -598,7 +602,8 @@ QEMU_OBJECT_SCHEMA = {
"cpu_throttling", "cpu_throttling",
"process_priority", "process_priority",
"options", "options",
"vm_directory"] "vm_directory",
"command_line"]
} }
QEMU_BINARY_FILTER_SCHEMA = { QEMU_BINARY_FILTER_SCHEMA = {

View File

@ -121,7 +121,11 @@ VPCS_OBJECT_SCHEMA = {
"description": "Path of the VPCS startup script relative to project directory", "description": "Path of the VPCS startup script relative to project directory",
"type": ["string", "null"] "type": ["string", "null"]
}, },
"command_line": {
"description": "Last command line used by GNS3 to start QEMU",
"type": "string"
}
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name", "vm_id", "status", "console", "project_id", "startup_script_path"] "required": ["name", "vm_id", "status", "console", "project_id", "startup_script_path", "command_line"]
} }

View File

@ -139,7 +139,8 @@ def test_iou_start(server, vm):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock: with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"])) response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert mock.called assert mock.called
assert response.status == 204 assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_iou_start_with_iourc(server, vm, tmpdir): def test_iou_start_with_iourc(server, vm, tmpdir):
@ -148,7 +149,7 @@ def test_iou_start_with_iourc(server, vm, tmpdir):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock: with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True) response = server.post("/projects/{project_id}/iou/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), body=body, example=True)
assert mock.called assert mock.called
assert response.status == 204 assert response.status == 200
response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"])) response = server.get("/projects/{project_id}/iou/vms/{vm_id}".format(project_id=vm["project_id"], vm_id=vm["vm_id"]))
assert response.status == 200 assert response.status == 200

View File

@ -114,7 +114,8 @@ def test_qemu_start(server, vm):
with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.start", return_value=True) as mock: with asyncio_patch("gns3server.modules.qemu.qemu_vm.QemuVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True) response = server.post("/projects/{project_id}/qemu/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called assert mock.called
assert response.status == 204 assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_qemu_stop(server, vm): def test_qemu_stop(server, vm):

View File

@ -102,7 +102,8 @@ def test_vpcs_start(server, vm):
with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock: with asyncio_patch("gns3server.modules.vpcs.vpcs_vm.VPCSVM.start", return_value=True) as mock:
response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True) response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/start".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), example=True)
assert mock.called assert mock.called
assert response.status == 204 assert response.status == 200
assert response.json["name"] == "PC TEST 1"
def test_vpcs_stop(server, vm): def test_vpcs_stop(server, vm):

View File

@ -112,11 +112,11 @@ def test_start(loop, vm, monkeypatch):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_iou_licence", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_iou_licence", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process): with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as mock_exec:
mock_process.returncode = None mock_process.returncode = None
loop.run_until_complete(asyncio.async(vm.start())) loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running() assert vm.is_running()
assert vm.command_line == ' '.join(mock_exec.call_args[0])
def test_start_with_iourc(loop, vm, monkeypatch, tmpdir): def test_start_with_iourc(loop, vm, monkeypatch, tmpdir):

View File

@ -112,9 +112,10 @@ def test_is_running(vm, running_subprocess_mock):
def test_start(loop, vm, running_subprocess_mock): def test_start(loop, vm, running_subprocess_mock):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=running_subprocess_mock): with asyncio_patch("asyncio.create_subprocess_exec", return_value=running_subprocess_mock) as mock:
loop.run_until_complete(asyncio.async(vm.start())) loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running() assert vm.is_running()
assert vm.command_line == ' '.join(mock.call_args[0])
def test_stop(loop, vm, running_subprocess_mock): def test_stop(loop, vm, running_subprocess_mock):

View File

@ -107,6 +107,7 @@ def test_start(loop, vm):
'-t', '-t',
'127.0.0.1') '127.0.0.1')
assert vm.is_running() assert vm.is_running()
assert vm.command_line == ' '.join(mock_exec.call_args[0])
(action, event) = queue.get_nowait() (action, event) = queue.get_nowait()
assert action == "vm.started" assert action == "vm.started"
assert event == vm assert event == vm