From ec44d70c7b01ea407ce777ace19ecf7fb3ea1a61 Mon Sep 17 00:00:00 2001 From: Joe Bowen Date: Thu, 15 May 2014 09:27:46 -0600 Subject: [PATCH 1/3] Fixed VPCS base_script_file setting --- gns3server/modules/vpcs/__init__.py | 26 ++++-------------------- gns3server/modules/vpcs/schemas.py | 13 ++++++------ gns3server/modules/vpcs/vpcs_device.py | 28 +++++++++++++------------- 3 files changed, 25 insertions(+), 42 deletions(-) diff --git a/gns3server/modules/vpcs/__init__.py b/gns3server/modules/vpcs/__init__.py index 1b416889..37c7e583 100644 --- a/gns3server/modules/vpcs/__init__.py +++ b/gns3server/modules/vpcs/__init__.py @@ -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,7 @@ 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) + vpcs_instance = VPCSDevice(vpcs_path, base_script_file, 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 +351,6 @@ class VPCS(IModule): Optional request parameters: - any setting to update - - script_file_base64 (script-file base64 encoded) Response parameters: - updated settings @@ -366,26 +368,6 @@ class VPCS(IModule): return 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") - config = "!\n" + config.replace("\r", "") - config = config.replace('%h', vpcs_instance.name) - config_path = os.path.join(vpcs_instance.working_dir, "script-file") - try: - with open(config_path, "w") as f: - log.info("saving 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) - - 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 d1061384..fbfd13f2 100644 --- a/gns3server/modules/vpcs/schemas.py +++ b/gns3server/modules/vpcs/schemas.py @@ -30,7 +30,12 @@ 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, + }, }, "required": ["path"] } @@ -67,15 +72,11 @@ VPCS_UPDATE_SCHEMA = { "type": "string", "minLength": 1, }, - "script_file": { + "base_script_file": { "description": "path to the VPCS startup configuration file", "type": "string", "minLength": 1, }, - "script_file_base64": { - "description": "startup configuration base64 encoded", - "type": "string" - }, }, "required": ["id"] } diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index af4ba19d..3e59fbba 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -45,7 +45,7 @@ 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 @@ -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)) From f79b2b061b93b46339c5234b4ec66ed59c499bcf Mon Sep 17 00:00:00 2001 From: Joe Bowen Date: Fri, 16 May 2014 10:15:11 -0600 Subject: [PATCH 2/3] Updated vpcs to allow up to 512 interfaces to start --- gns3server/modules/vpcs/vpcs_device.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/vpcs/vpcs_device.py b/gns3server/modules/vpcs/vpcs_device.py index 3e59fbba..94282fde 100644 --- a/gns3server/modules/vpcs/vpcs_device.py +++ b/gns3server/modules/vpcs/vpcs_device.py @@ -47,11 +47,11 @@ 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 <= 255) + # find an instance identifier (1 <= id <= 512) # This 255 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) From cef8a3f116758ab9ebda4aa83d51a2bd9e1a3f18 Mon Sep 17 00:00:00 2001 From: Joe Bowen Date: Fri, 16 May 2014 11:42:43 -0600 Subject: [PATCH 3/3] Added base64 transmission of script_file --- gns3server/modules/vpcs/__init__.py | 39 +++++++++++++++++++++++++- gns3server/modules/vpcs/schemas.py | 10 ++++++- gns3server/modules/vpcs/vpcs_device.py | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) 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):