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

Added base64 transmission of script_file

This commit is contained in:
Joe Bowen 2014-05-16 11:42:43 -06:00
parent f79b2b061b
commit cef8a3f116
3 changed files with 48 additions and 3 deletions

View File

@ -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:

View File

@ -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"]
}

View File

@ -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):