mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-11-16 16:54:51 +02:00
Merge pull request #12 from joebowen/master
Fixes for the VPCS path setting and base_script_file setting
This commit is contained in:
commit
0f8d1bc6da
@ -275,6 +275,9 @@ class VPCS(IModule):
|
||||
name = None
|
||||
if "name" in request:
|
||||
name = request["name"]
|
||||
base_script_file = None
|
||||
if "base_script_file" in request:
|
||||
base_script_file = request["base_script_file"]
|
||||
vpcs_path = request["path"]
|
||||
|
||||
try:
|
||||
@ -285,7 +288,23 @@ class VPCS(IModule):
|
||||
except OSError as e:
|
||||
raise VPCSError("Could not create working directory {}".format(e))
|
||||
|
||||
vpcs_instance = VPCSDevice(vpcs_path, self._working_dir, host=self._host, name=name)
|
||||
# a new base-script-file has been pushed
|
||||
if "base_script_file_base64" in request:
|
||||
config = base64.decodestring(request["base_script_file_base64"].encode("utf-8")).decode("utf-8")
|
||||
config = "!\n" + config.replace("\r", "")
|
||||
#config = config.replace('%h', vpcs_instance.name)
|
||||
config_path = os.path.join(self._working_dir, "base-script-file")
|
||||
try:
|
||||
with open(config_path, "w") as f:
|
||||
log.info("saving base-script-file to {}".format(config_path))
|
||||
f.write(config)
|
||||
except OSError as e:
|
||||
raise VPCSError("Could not save the configuration {}: {}".format(config_path, e))
|
||||
# update the request with the new local base-script-file path
|
||||
request["base_script_file"] = os.path.basename(config_path)
|
||||
|
||||
vpcs_instance = VPCSDevice(vpcs_path, config_path, self._working_dir, host=self._host, name=name)
|
||||
|
||||
# find a console port
|
||||
if self._current_console_port > self._console_end_port_range:
|
||||
self._current_console_port = self._console_start_port_range
|
||||
@ -348,7 +367,7 @@ class VPCS(IModule):
|
||||
|
||||
Optional request parameters:
|
||||
- any setting to update
|
||||
- script_file_base64 (script-file base64 encoded)
|
||||
- base_script_file_base64 (script-file base64 encoded)
|
||||
|
||||
Response parameters:
|
||||
- updated settings
|
||||
@ -367,25 +386,25 @@ class VPCS(IModule):
|
||||
|
||||
response = {}
|
||||
try:
|
||||
# a new script-file has been pushed
|
||||
if "script_file_base64" in request:
|
||||
config = base64.decodestring(request["script_file_base64"].encode("utf-8")).decode("utf-8")
|
||||
# a new base-script-file has been pushed
|
||||
if "base_script_file_base64" in request:
|
||||
config = base64.decodestring(request["base_script_file_base64"].encode("utf-8")).decode("utf-8")
|
||||
config = "!\n" + config.replace("\r", "")
|
||||
config = config.replace('%h', vpcs_instance.name)
|
||||
config_path = os.path.join(vpcs_instance.working_dir, "script-file")
|
||||
config_path = os.path.join(vpcs_instance.working_dir, "base-script-file")
|
||||
try:
|
||||
with open(config_path, "w") as f:
|
||||
log.info("saving script-file to {}".format(config_path))
|
||||
log.info("saving base-script-file to {}".format(config_path))
|
||||
f.write(config)
|
||||
except OSError as e:
|
||||
raise VPCSError("Could not save the configuration {}: {}".format(config_path, e))
|
||||
# update the request with the new local script-file path
|
||||
request["script_file"] = os.path.basename(config_path)
|
||||
# update the request with the new local base-script-file path
|
||||
request["base_script_file"] = os.path.basename(config_path)
|
||||
|
||||
except VPCSError as e:
|
||||
self.send_custom_error(str(e))
|
||||
return
|
||||
|
||||
|
||||
# update the VPCS settings
|
||||
for name, value in request.items():
|
||||
if hasattr(vpcs_instance, name) and getattr(vpcs_instance, name) != value:
|
||||
|
@ -30,7 +30,16 @@ VPCS_CREATE_SCHEMA = {
|
||||
"description": "path to the VPCS executable",
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
}
|
||||
},
|
||||
"base_script_file": {
|
||||
"description": "path to the VPCS startup configuration file",
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
},
|
||||
"base_script_file_base64": {
|
||||
"description": "startup script file base64 encoded",
|
||||
"type": "string"
|
||||
},
|
||||
},
|
||||
"required": ["path"]
|
||||
}
|
||||
@ -67,13 +76,13 @@ VPCS_UPDATE_SCHEMA = {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
},
|
||||
"script_file": {
|
||||
"description": "path to the VPCS startup configuration file",
|
||||
"base_script_file": {
|
||||
"description": "path to the VPCS startup script file file",
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
},
|
||||
"script_file_base64": {
|
||||
"description": "startup configuration base64 encoded",
|
||||
"base_script_file_base64": {
|
||||
"description": "startup script file base64 encoded",
|
||||
"type": "string"
|
||||
},
|
||||
},
|
||||
|
@ -45,13 +45,13 @@ class VPCSDevice(object):
|
||||
|
||||
_instances = []
|
||||
|
||||
def __init__(self, path, working_dir, host="127.0.0.1", name=None):
|
||||
def __init__(self, path, base_script_file, working_dir, host="127.0.0.1", name=None):
|
||||
|
||||
# find an instance identifier (1 <= id <= 255)
|
||||
# This 255 limit is due to a restriction on the number of possible
|
||||
# find an instance identifier (1 <= id <= 512)
|
||||
# This 512 limit is due to a restriction on the number of possible
|
||||
# mac addresses given in VPCS using the -m option
|
||||
self._id = 0
|
||||
for identifier in range(1, 256):
|
||||
for identifier in range(1, 513):
|
||||
if identifier not in self._instances:
|
||||
self._id = identifier
|
||||
self._instances.append(self._id)
|
||||
@ -74,7 +74,7 @@ class VPCSDevice(object):
|
||||
self._started = False
|
||||
|
||||
# VPCS settings
|
||||
self._script_file = ""
|
||||
self._base_script_file = base_script_file
|
||||
self._ethernet_adapters = [EthernetAdapter()] # one adapter = 1 interfaces
|
||||
self._slots = self._ethernet_adapters
|
||||
|
||||
@ -93,7 +93,7 @@ class VPCSDevice(object):
|
||||
|
||||
vpcs_defaults = {"name": self._name,
|
||||
"path": self._path,
|
||||
"script_file": self._script_file,
|
||||
"base_script_file": self._base_script_file,
|
||||
"console": self._console}
|
||||
|
||||
return vpcs_defaults
|
||||
@ -432,29 +432,29 @@ class VPCSDevice(object):
|
||||
|
||||
command.extend(["-m", str(self._id)]) # The unique ID is used to set the mac address offset
|
||||
command.extend(["-i", str(1)]) # Option to start only one pc instance
|
||||
if self._script_file:
|
||||
command.extend([self._script_file])
|
||||
if self._base_script_file:
|
||||
command.extend([self._base_script_file])
|
||||
return command
|
||||
|
||||
@property
|
||||
def script_file(self):
|
||||
def base_script_file(self):
|
||||
"""
|
||||
Returns the script-file for this VPCS instance.
|
||||
|
||||
:returns: path to script-file file
|
||||
"""
|
||||
|
||||
return self._script_file
|
||||
return self._base_script_file
|
||||
|
||||
@script_file.setter
|
||||
def script_file(self, script_file):
|
||||
@base_script_file.setter
|
||||
def base_script_file(self, base_script_file):
|
||||
"""
|
||||
Sets the script-file for this VPCS instance.
|
||||
Sets the base-script-file for this VPCS instance.
|
||||
|
||||
:param script_file: path to script-file file
|
||||
:param base_script_file: path to base-script-file file
|
||||
"""
|
||||
|
||||
self._script_file = script_file
|
||||
log.info("VPCS {name} [id={id}]: script_file set to {config}".format(name=self._name,
|
||||
self._base_script_file = base_script_file
|
||||
log.info("VPCS {name} [id={id}]: base_script_file set to {config}".format(name=self._name,
|
||||
id=self._id,
|
||||
config=self._script_file))
|
||||
config=self._base_script_file))
|
||||
|
Loading…
Reference in New Issue
Block a user