1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-30 20:28:08 +00:00

Reset MAC addresses when duplicating a project. Fixes #1522

This commit is contained in:
grossmj 2019-02-20 16:38:43 +07:00
parent 657698a961
commit 1ef1872f8e
3 changed files with 12 additions and 7 deletions

View File

@ -31,7 +31,7 @@ log = logging.getLogger(__name__)
@asyncio.coroutine @asyncio.coroutine
def export_project(project, temporary_dir, include_images=False, keep_compute_id=False, allow_all_nodes=False): def export_project(project, temporary_dir, include_images=False, keep_compute_id=False, allow_all_nodes=False, reset_mac_addresses=False):
""" """
Export a project to a zip file. Export a project to a zip file.
@ -42,6 +42,7 @@ def export_project(project, temporary_dir, include_images=False, keep_compute_id
:param include images: save OS images to the zip file :param include images: save OS images to the zip file
:param keep_compute_id: If false replace all compute id by local (standard behavior for .gns3project to make it portable) :param keep_compute_id: If false replace all compute id by local (standard behavior for .gns3project to make it portable)
:param allow_all_nodes: Allow all nodes type to be include in the zip even if not portable :param allow_all_nodes: Allow all nodes type to be include in the zip even if not portable
:param reset_mac_addresses: Reset MAC addresses for every nodes.
:returns: ZipStream object :returns: ZipStream object
""" """
@ -61,7 +62,7 @@ def export_project(project, temporary_dir, include_images=False, keep_compute_id
# First we process the .gns3 in order to be sure we don't have an error # First we process the .gns3 in order to be sure we don't have an error
for file in os.listdir(project._path): for file in os.listdir(project._path):
if file.endswith(".gns3"): if file.endswith(".gns3"):
yield from _patch_project_file(project, os.path.join(project._path, file), zstream, include_images, keep_compute_id, allow_all_nodes, temporary_dir) yield from _patch_project_file(project, os.path.join(project._path, file), zstream, include_images, keep_compute_id, allow_all_nodes, temporary_dir, reset_mac_addresses)
# Export the local files # Export the local files
for root, dirs, files in os.walk(project._path, topdown=True, followlinks=False): for root, dirs, files in os.walk(project._path, topdown=True, followlinks=False):
@ -160,7 +161,7 @@ def _is_exportable(path):
@asyncio.coroutine @asyncio.coroutine
def _patch_project_file(project, path, zstream, include_images, keep_compute_id, allow_all_nodes, temporary_dir): def _patch_project_file(project, path, zstream, include_images, keep_compute_id, allow_all_nodes, temporary_dir, reset_mac_addresses):
""" """
Patch a project file (.gns3) to export a project. Patch a project file (.gns3) to export a project.
The .gns3 file is renamed to project.gns3 The .gns3 file is renamed to project.gns3
@ -193,6 +194,10 @@ def _patch_project_file(project, path, zstream, include_images, keep_compute_id,
if "properties" in node and node["node_type"] != "docker": if "properties" in node and node["node_type"] != "docker":
for prop, value in node["properties"].items(): for prop, value in node["properties"].items():
# reset the MAC address
if reset_mac_addresses and prop in ("mac_addr", "mac_address"):
node["properties"][prop] = None
if node["node_type"] == "iou": if node["node_type"] == "iou":
if not prop == "path": if not prop == "path":
continue continue

View File

@ -929,7 +929,7 @@ class Project:
self.dump() self.dump()
try: try:
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True) zipstream = yield from export_project(self, tmpdir, keep_compute_id=True, allow_all_nodes=True, reset_mac_addresses=True)
project_path = os.path.join(tmpdir, "project.gns3p") project_path = os.path.join(tmpdir, "project.gns3p")
yield from wait_run_in_executor(self._create_duplicate_project_file, project_path, zipstream) yield from wait_run_in_executor(self._create_duplicate_project_file, project_path, zipstream)
with open(project_path, "rb") as f: with open(project_path, "rb") as f:

View File

@ -137,7 +137,7 @@ VM_CREATE_SCHEMA = {
}, },
"mac_addr": { "mac_addr": {
"description": "Base MAC address", "description": "Base MAC address",
"type": "string", "type": ["null", "string"],
"minLength": 1, "minLength": 1,
"pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$" "pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$"
}, },
@ -355,7 +355,7 @@ VM_UPDATE_SCHEMA = {
}, },
"mac_addr": { "mac_addr": {
"description": "Base MAC address", "description": "Base MAC address",
"type": "string", "type": ["null", "string"],
"minLength": 1, "minLength": 1,
"pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$" "pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$"
}, },
@ -595,7 +595,7 @@ VM_OBJECT_SCHEMA = {
}, },
"mac_addr": { "mac_addr": {
"description": "Base MAC address", "description": "Base MAC address",
"type": "string", "type": ["null", "string"]
#"minLength": 1, #"minLength": 1,
#"pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$" #"pattern": "^([0-9a-fA-F]{4}\\.){2}[0-9a-fA-F]{4}$"
}, },