mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-11 16:41:04 +00:00
Fix BadZipFile: File is not a zip file
This commit is contained in:
parent
bfb82a9618
commit
420168015c
@ -52,98 +52,101 @@ def import_project(controller, project_id, stream, location=None, name=None, kee
|
|||||||
if location and ".gns3" in location:
|
if location and ".gns3" in location:
|
||||||
raise aiohttp.web.HTTPConflict(text="The destination path should not contain .gns3")
|
raise aiohttp.web.HTTPConflict(text="The destination path should not contain .gns3")
|
||||||
|
|
||||||
with zipfile.ZipFile(stream) as myzip:
|
try:
|
||||||
|
with zipfile.ZipFile(stream) as myzip:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
topology = json.loads(myzip.read("project.gns3").decode())
|
topology = json.loads(myzip.read("project.gns3").decode())
|
||||||
|
|
||||||
# We import the project on top of an existing project (snapshots)
|
# We import the project on top of an existing project (snapshots)
|
||||||
if topology["project_id"] == project_id:
|
if topology["project_id"] == project_id:
|
||||||
project_name = topology["name"]
|
project_name = topology["name"]
|
||||||
else:
|
|
||||||
# If the project name is already used we generate a new one
|
|
||||||
if name:
|
|
||||||
project_name = controller.get_free_project_name(name)
|
|
||||||
else:
|
else:
|
||||||
project_name = controller.get_free_project_name(topology["name"])
|
# If the project name is already used we generate a new one
|
||||||
except KeyError:
|
if name:
|
||||||
raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing")
|
project_name = controller.get_free_project_name(name)
|
||||||
|
else:
|
||||||
|
project_name = controller.get_free_project_name(topology["name"])
|
||||||
|
except KeyError:
|
||||||
|
raise aiohttp.web.HTTPConflict(text="Can't import topology the .gns3 is corrupted or missing")
|
||||||
|
|
||||||
if location:
|
if location:
|
||||||
path = location
|
path = location
|
||||||
else:
|
|
||||||
projects_path = controller.projects_directory()
|
|
||||||
path = os.path.join(projects_path, project_name)
|
|
||||||
os.makedirs(path, exist_ok=True)
|
|
||||||
myzip.extractall(path)
|
|
||||||
|
|
||||||
topology = load_topology(os.path.join(path, "project.gns3"))
|
|
||||||
topology["name"] = project_name
|
|
||||||
# To avoid unexpected behavior (project start without manual operations just after import)
|
|
||||||
topology["auto_start"] = False
|
|
||||||
topology["auto_open"] = False
|
|
||||||
topology["auto_close"] = True
|
|
||||||
|
|
||||||
# Modify the compute id of the node depending of compute capacity
|
|
||||||
if not keep_compute_id:
|
|
||||||
# For some VM type we move them to the GNS3 VM if possible
|
|
||||||
# unless it's a linux host without GNS3 VM
|
|
||||||
if not sys.platform.startswith("linux") or controller.has_compute("vm"):
|
|
||||||
for node in topology["topology"]["nodes"]:
|
|
||||||
if node["node_type"] in ("docker", "qemu", "iou"):
|
|
||||||
node["compute_id"] = "vm"
|
|
||||||
else:
|
else:
|
||||||
for node in topology["topology"]["nodes"]:
|
projects_path = controller.projects_directory()
|
||||||
node["compute_id"] = "local"
|
path = os.path.join(projects_path, project_name)
|
||||||
|
os.makedirs(path, exist_ok=True)
|
||||||
|
myzip.extractall(path)
|
||||||
|
|
||||||
compute_created = set()
|
topology = load_topology(os.path.join(path, "project.gns3"))
|
||||||
for node in topology["topology"]["nodes"]:
|
topology["name"] = project_name
|
||||||
|
# To avoid unexpected behavior (project start without manual operations just after import)
|
||||||
|
topology["auto_start"] = False
|
||||||
|
topology["auto_open"] = False
|
||||||
|
topology["auto_close"] = True
|
||||||
|
|
||||||
if node["compute_id"] != "local":
|
# Modify the compute id of the node depending of compute capacity
|
||||||
# Project created on the remote GNS3 VM?
|
if not keep_compute_id:
|
||||||
if node["compute_id"] not in compute_created:
|
# For some VM type we move them to the GNS3 VM if possible
|
||||||
compute = controller.get_compute(node["compute_id"])
|
# unless it's a linux host without GNS3 VM
|
||||||
yield from compute.post("/projects", data={
|
if not sys.platform.startswith("linux") or controller.has_compute("vm"):
|
||||||
"name": project_name,
|
for node in topology["topology"]["nodes"]:
|
||||||
"project_id": project_id,
|
if node["node_type"] in ("docker", "qemu", "iou"):
|
||||||
})
|
node["compute_id"] = "vm"
|
||||||
compute_created.add(node["compute_id"])
|
else:
|
||||||
|
for node in topology["topology"]["nodes"]:
|
||||||
|
node["compute_id"] = "local"
|
||||||
|
|
||||||
yield from _move_files_to_compute(compute, project_id, path, os.path.join("project-files", node["node_type"], node["node_id"]))
|
compute_created = set()
|
||||||
|
for node in topology["topology"]["nodes"]:
|
||||||
|
|
||||||
# Generate a new node id
|
if node["compute_id"] != "local":
|
||||||
node_old_to_new = {}
|
# Project created on the remote GNS3 VM?
|
||||||
for node in topology["topology"]["nodes"]:
|
if node["compute_id"] not in compute_created:
|
||||||
if "node_id" in node:
|
compute = controller.get_compute(node["compute_id"])
|
||||||
node_old_to_new[node["node_id"]] = str(uuid.uuid4())
|
yield from compute.post("/projects", data={
|
||||||
_move_node_file(path, node["node_id"], node_old_to_new[node["node_id"]])
|
"name": project_name,
|
||||||
node["node_id"] = node_old_to_new[node["node_id"]]
|
"project_id": project_id,
|
||||||
else:
|
})
|
||||||
node["node_id"] = str(uuid.uuid4())
|
compute_created.add(node["compute_id"])
|
||||||
|
|
||||||
# Update link to use new id
|
yield from _move_files_to_compute(compute, project_id, path, os.path.join("project-files", node["node_type"], node["node_id"]))
|
||||||
for link in topology["topology"]["links"]:
|
|
||||||
link["link_id"] = str(uuid.uuid4())
|
|
||||||
for node in link["nodes"]:
|
|
||||||
node["node_id"] = node_old_to_new[node["node_id"]]
|
|
||||||
|
|
||||||
# Generate new drawings id
|
# Generate a new node id
|
||||||
for drawing in topology["topology"]["drawings"]:
|
node_old_to_new = {}
|
||||||
drawing["drawing_id"] = str(uuid.uuid4())
|
for node in topology["topology"]["nodes"]:
|
||||||
|
if "node_id" in node:
|
||||||
|
node_old_to_new[node["node_id"]] = str(uuid.uuid4())
|
||||||
|
_move_node_file(path, node["node_id"], node_old_to_new[node["node_id"]])
|
||||||
|
node["node_id"] = node_old_to_new[node["node_id"]]
|
||||||
|
else:
|
||||||
|
node["node_id"] = str(uuid.uuid4())
|
||||||
|
|
||||||
# And we dump the updated.gns3
|
# Update link to use new id
|
||||||
dot_gns3_path = os.path.join(path, project_name + ".gns3")
|
for link in topology["topology"]["links"]:
|
||||||
# We change the project_id to avoid erasing the project
|
link["link_id"] = str(uuid.uuid4())
|
||||||
topology["project_id"] = project_id
|
for node in link["nodes"]:
|
||||||
with open(dot_gns3_path, "w+") as f:
|
node["node_id"] = node_old_to_new[node["node_id"]]
|
||||||
json.dump(topology, f, indent=4)
|
|
||||||
os.remove(os.path.join(path, "project.gns3"))
|
|
||||||
|
|
||||||
if os.path.exists(os.path.join(path, "images")):
|
# Generate new drawings id
|
||||||
_import_images(controller, path)
|
for drawing in topology["topology"]["drawings"]:
|
||||||
|
drawing["drawing_id"] = str(uuid.uuid4())
|
||||||
|
|
||||||
project = yield from controller.load_project(dot_gns3_path, load=False)
|
# And we dump the updated.gns3
|
||||||
return project
|
dot_gns3_path = os.path.join(path, project_name + ".gns3")
|
||||||
|
# We change the project_id to avoid erasing the project
|
||||||
|
topology["project_id"] = project_id
|
||||||
|
with open(dot_gns3_path, "w+") as f:
|
||||||
|
json.dump(topology, f, indent=4)
|
||||||
|
os.remove(os.path.join(path, "project.gns3"))
|
||||||
|
|
||||||
|
if os.path.exists(os.path.join(path, "images")):
|
||||||
|
_import_images(controller, path)
|
||||||
|
|
||||||
|
project = yield from controller.load_project(dot_gns3_path, load=False)
|
||||||
|
return project
|
||||||
|
except zipfile.BadZipFile:
|
||||||
|
raise aiohttp.web.HTTPConflict(text="Can't import topology the file is corrupted or not a GNS3 project (invalid zip)")
|
||||||
|
|
||||||
|
|
||||||
def _move_node_file(path, old_id, new_id):
|
def _move_node_file(path, old_id, new_id):
|
||||||
|
Loading…
Reference in New Issue
Block a user