diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index 37c7e583..dd8d2827 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -288,7 +288,23 @@ class VPCS(IModule): except OSError as e: raise VPCSError("Could not create working directory {}".format(e)) - vpcs_instance = VPCSDevice(vpcs_path, base_script_file, 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 @@ -351,6 +367,7 @@ class VPCS(IModule): Optional request parameters: - any setting to update + - base_script_file_base64 (script-file base64 encoded) Response parameters: - updated settings @@ -368,6 +385,26 @@ class VPCS(IModule): return response = {} + try: + # 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, "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) + + 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: diff --git a/gns3server/modules/vpcs/schemas.py b/gns3server/modules/vpcs/schemas.py index fbfd13f2..2675b13b 100644 --- a/gns3server/modules/vpcs/schemas.py +++ b/gns3server/modules/vpcs/schemas.py @@ -36,6 +36,10 @@ VPCS_CREATE_SCHEMA = { "type": "string", "minLength": 1, }, + "base_script_file_base64": { + "description": "startup script file base64 encoded", + "type": "string" + }, }, "required": ["path"] } @@ -73,10 +77,14 @@ VPCS_UPDATE_SCHEMA = { "minLength": 1, }, "base_script_file": { - "description": "path to the VPCS startup configuration file", + "description": "path to the VPCS startup script file file", "type": "string", "minLength": 1, }, + "base_script_file_base64": { + "description": "startup script file base64 encoded", + "type": "string" + }, }, "required": ["id"] } diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index 94282fde..155445b6 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -48,7 +48,7 @@ class VPCSDevice(object): def __init__(self, path, base_script_file, working_dir, host="127.0.0.1", name=None): # find an instance identifier (1 <= id <= 512) - # This 255 limit is due to a restriction on the number of possible + # 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, 513):