mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-13 20:08:55 +00:00
Refactor VPCS script file loading
This allow to support moving the project on disk
This commit is contained in:
parent
869405738e
commit
41a8872819
@ -49,11 +49,10 @@ class VPCSVM(BaseVM):
|
||||
:param project: Project instance
|
||||
:param manager: parent VM Manager
|
||||
:param console: TCP console port
|
||||
:param script_file: A VPCS startup script
|
||||
:param startup_script: Content of vpcs startup script file
|
||||
"""
|
||||
|
||||
def __init__(self, name, vm_id, project, manager, console=None, script_file=None, startup_script=None):
|
||||
def __init__(self, name, vm_id, project, manager, console=None, startup_script=None):
|
||||
|
||||
super().__init__(name, vm_id, project, manager)
|
||||
|
||||
@ -64,7 +63,6 @@ class VPCSVM(BaseVM):
|
||||
self._started = False
|
||||
|
||||
# VPCS settings
|
||||
self._script_file = script_file
|
||||
if startup_script is not None:
|
||||
self.startup_script = startup_script
|
||||
self._ethernet_adapter = EthernetAdapter() # one adapter with 1 Ethernet interface
|
||||
@ -104,7 +102,6 @@ class VPCSVM(BaseVM):
|
||||
"vm_id": self.id,
|
||||
"console": self._console,
|
||||
"project_id": self.project.id,
|
||||
"script_file": self.script_file,
|
||||
"startup_script": self.startup_script}
|
||||
|
||||
@property
|
||||
@ -152,7 +149,7 @@ class VPCSVM(BaseVM):
|
||||
:param new_name: name
|
||||
"""
|
||||
|
||||
if self._script_file:
|
||||
if self.script_file:
|
||||
content = self.startup_script
|
||||
content = content.replace(self._name, new_name)
|
||||
self.startup_script = content
|
||||
@ -163,19 +160,15 @@ class VPCSVM(BaseVM):
|
||||
def startup_script(self):
|
||||
"""Return the content of the current startup script"""
|
||||
|
||||
if self._script_file is None:
|
||||
# If the default VPCS file exist we use it
|
||||
path = os.path.join(self.working_dir, 'startup.vpc')
|
||||
if os.path.exists(path):
|
||||
self._script_file = path
|
||||
else:
|
||||
return None
|
||||
script_file = self.script_file
|
||||
if script_file is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(self._script_file) as f:
|
||||
with open(script_file) as f:
|
||||
return f.read()
|
||||
except OSError as e:
|
||||
raise VPCSError("Can't read VPCS startup file '{}'".format(self._script_file))
|
||||
raise VPCSError("Can't read VPCS startup file '{}'".format(script_file))
|
||||
|
||||
@startup_script.setter
|
||||
def startup_script(self, startup_script):
|
||||
@ -185,17 +178,16 @@ class VPCSVM(BaseVM):
|
||||
:param startup_script The content of the vpcs startup script
|
||||
"""
|
||||
|
||||
if self._script_file is None:
|
||||
self._script_file = os.path.join(self.working_dir, 'startup.vpcs')
|
||||
try:
|
||||
with open(self._script_file, 'w+') as f:
|
||||
script_file = os.path.join(self.working_dir, 'startup.vpc')
|
||||
with open(script_file, 'w+') as f:
|
||||
if startup_script is None:
|
||||
f.write('')
|
||||
else:
|
||||
startup_script = startup_script.replace("%h", self._name)
|
||||
f.write(startup_script)
|
||||
except OSError as e:
|
||||
raise VPCSError("Can't write VPCS startup file '{}'".format(self._script_file))
|
||||
raise VPCSError("Can't write VPCS startup file '{}'".format(self.script_file))
|
||||
|
||||
@asyncio.coroutine
|
||||
def _check_vpcs_version(self):
|
||||
@ -413,8 +405,9 @@ class VPCSVM(BaseVM):
|
||||
command.extend(["-m", str(self._manager.get_mac_id(self.id))]) # the unique ID is used to set the MAC address offset
|
||||
command.extend(["-i", "1"]) # option to start only one VPC instance
|
||||
command.extend(["-F"]) # option to avoid the daemonization of VPCS
|
||||
if self._script_file:
|
||||
command.extend([self._script_file])
|
||||
|
||||
if self.script_file:
|
||||
command.extend([self.script_file])
|
||||
return command
|
||||
|
||||
@property
|
||||
@ -425,4 +418,9 @@ class VPCSVM(BaseVM):
|
||||
:returns: path to script-file
|
||||
"""
|
||||
|
||||
return self._script_file
|
||||
# If the default VPCS file exist we use it
|
||||
path = os.path.join(self.working_dir, 'startup.vpc')
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
else:
|
||||
return None
|
||||
|
@ -158,10 +158,6 @@ VPCS_OBJECT_SCHEMA = {
|
||||
"maxLength": 36,
|
||||
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
||||
},
|
||||
"script_file": {
|
||||
"description": "VPCS startup script",
|
||||
"type": ["string", "null"]
|
||||
},
|
||||
"startup_script": {
|
||||
"description": "Content of the VPCS startup script",
|
||||
"type": ["string", "null"]
|
||||
|
@ -34,7 +34,6 @@ def test_vpcs_create(server, project):
|
||||
assert response.route == "/{project_id}/vpcs/vms"
|
||||
assert response.json["name"] == "PC TEST 1"
|
||||
assert response.json["project_id"] == project.id
|
||||
assert response.json["script_file"] is None
|
||||
|
||||
|
||||
def test_vpcs_get(server, project, vm):
|
||||
|
@ -134,7 +134,7 @@ def test_port_remove_nio_binding(vm):
|
||||
def test_update_startup_script(vm):
|
||||
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
||||
vm.startup_script = content
|
||||
filepath = os.path.join(vm.working_dir, 'startup.vpcs')
|
||||
filepath = os.path.join(vm.working_dir, 'startup.vpc')
|
||||
assert os.path.exists(filepath)
|
||||
with open(filepath) as f:
|
||||
assert f.read() == content
|
||||
@ -143,7 +143,7 @@ def test_update_startup_script(vm):
|
||||
def test_update_startup_script(vm):
|
||||
content = "echo GNS3 VPCS\nip 192.168.1.2\n"
|
||||
vm.startup_script = content
|
||||
filepath = os.path.join(vm.working_dir, 'startup.vpcs')
|
||||
filepath = os.path.join(vm.working_dir, 'startup.vpc')
|
||||
assert os.path.exists(filepath)
|
||||
with open(filepath) as f:
|
||||
assert f.read() == content
|
||||
@ -192,11 +192,10 @@ def test_change_console_port(vm, port_manager):
|
||||
|
||||
|
||||
def test_change_name(vm, tmpdir):
|
||||
path = os.path.join(str(tmpdir), 'startup.vpcs')
|
||||
path = os.path.join(vm.working_dir, 'startup.vpc')
|
||||
vm.name = "world"
|
||||
with open(path, 'w+') as f:
|
||||
f.write("name world")
|
||||
vm._script_file = path
|
||||
vm.name = "hello"
|
||||
assert vm.name == "hello"
|
||||
with open(path) as f:
|
||||
|
Loading…
Reference in New Issue
Block a user