Import project on the GNS3VM should work

This commit is contained in:
Julien Duponchelle committed 2016-07-22 16:05:22 +02:00
1 parent 19b035d7e4
commit fec3694710
4 files changed
+67 -2

No files matched your search

+2
View File
@@ -240,6 +240,8 @@ class Controller:
server_config = Config.instance().get_section_config("Server")
if compute_id == "local" and server_config.getboolean("local", False) is False:
raise aiohttp.web.HTTPNotFound(text="You try to use a node on the local server but the controller is not started with --local")
elif compute_id == "vm":
raise aiohttp.web.HTTPNotFound(text="You try to use a node on the GNS3 VM server but the GNS3 is not configured")
raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id))
@asyncio.coroutine
+1
View File
@@ -472,3 +472,4 @@ class Compute:
path = "/projects/{}/files".format(project.id)
res = yield from self.http_query("GET", path, timeout=120)
return res.json
+39
View File
@@ -66,10 +66,24 @@ def import_project(controller, project_id, stream):
# For some VM type we move them to the GNS3 VM if it's not a Linux host
if not sys.platform.startswith("linux"):
vm_created = False
for node in topology["topology"]["nodes"]:
if node["node_type"] in ("docker", "qemu", "iou"):
node["compute_id"] = "vm"
# Project created on the remote GNS3 VM?
if not vm_created:
compute = controller.get_compute("vm")
yield from compute.post("/projects", data={
"name": project_name,
"project_id": project_id,
})
vm_created = True
yield from _move_files_to_compute(compute, project_id, path, os.path.join("project-files", node["node_type"], node["node_id"]))
# And we dump the updated.gns3
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
@@ -84,6 +98,31 @@ def import_project(controller, project_id, stream):
return project
@asyncio.coroutine
def _move_files_to_compute(compute, project_id, directory, files_path):
"""
Move the files to a remote compute
"""
for (dirpath, dirnames, filenames) in os.walk(os.path.join(directory, files_path)):
for filename in filenames:
path = os.path.join(dirpath, filename)
dst = os.path.relpath(path, directory)
yield from _upload_file(compute, project_id, path, dst)
shutil.rmtree(directory)
@asyncio.coroutine
def _upload_file(compute, project_id, file_path, path):
"""
Upload a file to a remote project
:param file_path: File path on the controller file system
:param path: File path on the remote system relative to project directory
"""
path = "/projects/{}/files/path".format(project_id, path.replace("\\", "/"))
with open(file_path, "rb") as f:
yield from compute.http_query("POST", path, f, timeout=None)
def _import_images(controller, path):
"""
Copy images to the images directory or delete them if they