mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-12-22 06:07:51 +00:00
Import / Export with images
Ref https://github.com/GNS3/gns3-gui/issues/1173
This commit is contained in:
parent
cd393491d5
commit
b7a859fa30
@ -368,7 +368,7 @@ class ProjectHandler:
|
|||||||
response.content_length = None
|
response.content_length = None
|
||||||
response.start(request)
|
response.start(request)
|
||||||
|
|
||||||
for data in project.export():
|
for data in project.export(include_images=bool(request.GET.get("images", "0"))):
|
||||||
response.write(data)
|
response.write(data)
|
||||||
yield from response.drain()
|
yield from response.drain()
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ from .port_manager import PortManager
|
|||||||
from ..config import Config
|
from ..config import Config
|
||||||
from ..utils.asyncio import wait_run_in_executor
|
from ..utils.asyncio import wait_run_in_executor
|
||||||
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -513,7 +514,7 @@ class Project:
|
|||||||
m.update(buf)
|
m.update(buf)
|
||||||
return m.hexdigest()
|
return m.hexdigest()
|
||||||
|
|
||||||
def export(self):
|
def export(self, include_images=False):
|
||||||
"""
|
"""
|
||||||
Export the project as zip. It's a ZipStream object.
|
Export the project as zip. It's a ZipStream object.
|
||||||
The file will be read chunk by chunk when you iterate on
|
The file will be read chunk by chunk when you iterate on
|
||||||
@ -539,7 +540,7 @@ class Project:
|
|||||||
path = os.path.join(root, file)
|
path = os.path.join(root, file)
|
||||||
# We rename the .gns3 project.gns3 to avoid the task to the client to guess the file name
|
# We rename the .gns3 project.gns3 to avoid the task to the client to guess the file name
|
||||||
if file.endswith(".gns3"):
|
if file.endswith(".gns3"):
|
||||||
z.writestr("project.gns3", self._export_project_file(path))
|
self._export_project_file(path, z, include_images)
|
||||||
else:
|
else:
|
||||||
# We merge the data from all server in the same project-files directory
|
# We merge the data from all server in the same project-files directory
|
||||||
vm_directory = os.path.join(self._path, "servers", "vm")
|
vm_directory = os.path.join(self._path, "servers", "vm")
|
||||||
@ -549,23 +550,53 @@ class Project:
|
|||||||
z.write(path, os.path.relpath(path, self._path))
|
z.write(path, os.path.relpath(path, self._path))
|
||||||
return z
|
return z
|
||||||
|
|
||||||
def _export_project_file(self, path):
|
def _export_images(self, image, type, z):
|
||||||
|
"""
|
||||||
|
Take a project file (.gns3) and export images to the zip
|
||||||
|
|
||||||
|
:param image: Image path
|
||||||
|
:param type: Type of image
|
||||||
|
:param z: Zipfile instance for the export
|
||||||
|
"""
|
||||||
|
from . import MODULES
|
||||||
|
|
||||||
|
for module in MODULES:
|
||||||
|
try:
|
||||||
|
img_directory = module.instance().get_images_directory()
|
||||||
|
except NotImplementedError:
|
||||||
|
# Some modules don't have images
|
||||||
|
continue
|
||||||
|
|
||||||
|
directory = os.path.split(img_directory)[-1:][0]
|
||||||
|
|
||||||
|
if os.path.exists(image):
|
||||||
|
path = image
|
||||||
|
else:
|
||||||
|
path = os.path.join(img_directory, image)
|
||||||
|
|
||||||
|
if os.path.exists(path):
|
||||||
|
arcname = os.path.join("images", directory, os.path.basename(image))
|
||||||
|
z.write(path, arcname)
|
||||||
|
break
|
||||||
|
|
||||||
|
def _export_project_file(self, path, z, include_images):
|
||||||
"""
|
"""
|
||||||
Take a project file (.gns3) and patch it for the export
|
Take a project file (.gns3) and patch it for the export
|
||||||
|
|
||||||
:returns: Content of the topology
|
:param path: Path of the .gns3
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with open(path) as f:
|
with open(path) as f:
|
||||||
topology = json.load(f)
|
topology = json.load(f)
|
||||||
if "topology" in topology and "nodes" in topology["topology"]:
|
if "topology" in topology and "nodes" in topology["topology"]:
|
||||||
for node in topology["topology"]["nodes"]:
|
for node in topology["topology"]["nodes"]:
|
||||||
if "properties" in node:
|
if "properties" in node and node["type"] != "DockerVM":
|
||||||
for prop, value in node["properties"].items():
|
for prop, value in node["properties"].items():
|
||||||
if prop.endswith("image"):
|
if prop.endswith("image"):
|
||||||
node["properties"][prop] = os.path.basename(value)
|
node["properties"][prop] = os.path.basename(value)
|
||||||
|
if include_images:
|
||||||
return json.dumps(topology).encode()
|
self._export_images(value, node["type"], z)
|
||||||
|
z.writestr("project.gns3", json.dumps(topology).encode())
|
||||||
|
|
||||||
def import_zip(self, stream, gns3vm=True):
|
def import_zip(self, stream, gns3vm=True):
|
||||||
"""
|
"""
|
||||||
@ -635,3 +666,23 @@ class Project:
|
|||||||
|
|
||||||
# Rename to a human distinctive name
|
# Rename to a human distinctive name
|
||||||
shutil.move(project_file, os.path.join(self.path, self.name + ".gns3"))
|
shutil.move(project_file, os.path.join(self.path, self.name + ".gns3"))
|
||||||
|
if os.path.exists(os.path.join(self.path, "images")):
|
||||||
|
self._import_images()
|
||||||
|
|
||||||
|
def _import_images(self):
|
||||||
|
"""
|
||||||
|
Copy images to the images directory or delete them if they
|
||||||
|
already exists.
|
||||||
|
"""
|
||||||
|
image_dir = self._config().get("images_path")
|
||||||
|
|
||||||
|
root = os.path.join(self.path, "images")
|
||||||
|
for (dirpath, dirnames, filenames) in os.walk(root):
|
||||||
|
for filename in filenames:
|
||||||
|
path = os.path.join(dirpath, filename)
|
||||||
|
dst = os.path.join(image_dir, os.path.relpath(path, root))
|
||||||
|
os.makedirs(os.path.dirname(dst), exist_ok=True)
|
||||||
|
shutil.move(path, dst)
|
||||||
|
|
||||||
|
# Cleanup the project
|
||||||
|
shutil.rmtree(root)
|
||||||
|
@ -310,7 +310,8 @@ def test_export_fix_path(tmpdir):
|
|||||||
{
|
{
|
||||||
"properties": {
|
"properties": {
|
||||||
"image": "/tmp/c3725-adventerprisek9-mz.124-25d.image"
|
"image": "/tmp/c3725-adventerprisek9-mz.124-25d.image"
|
||||||
}
|
},
|
||||||
|
"type": "C3725"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -331,6 +332,43 @@ def test_export_fix_path(tmpdir):
|
|||||||
assert topology["topology"]["nodes"][0]["properties"]["image"] == "c3725-adventerprisek9-mz.124-25d.image"
|
assert topology["topology"]["nodes"][0]["properties"]["image"] == "c3725-adventerprisek9-mz.124-25d.image"
|
||||||
|
|
||||||
|
|
||||||
|
def test_export_with_images(tmpdir):
|
||||||
|
"""
|
||||||
|
Fix absolute image path
|
||||||
|
"""
|
||||||
|
project = Project()
|
||||||
|
path = project.path
|
||||||
|
|
||||||
|
os.makedirs(str(tmpdir / "IOS"))
|
||||||
|
with open(str(tmpdir / "IOS" / "test.image"), "w+") as f:
|
||||||
|
f.write("AAA")
|
||||||
|
|
||||||
|
topology = {
|
||||||
|
"topology": {
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"image": "test.image"
|
||||||
|
},
|
||||||
|
"type": "C3725"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(os.path.join(path, "test.gns3"), 'w+') as f:
|
||||||
|
json.dump(topology, f)
|
||||||
|
|
||||||
|
with patch("gns3server.modules.Dynamips.get_images_directory", return_value=str(tmpdir / "IOS"),):
|
||||||
|
z = project.export(include_images=True)
|
||||||
|
with open(str(tmpdir / 'zipfile.zip'), 'wb') as f:
|
||||||
|
for data in z:
|
||||||
|
f.write(data)
|
||||||
|
|
||||||
|
with zipfile.ZipFile(str(tmpdir / 'zipfile.zip')) as myzip:
|
||||||
|
myzip.getinfo("images/IOS/test.image")
|
||||||
|
|
||||||
|
|
||||||
def test_export_with_vm(tmpdir):
|
def test_export_with_vm(tmpdir):
|
||||||
project = Project()
|
project = Project()
|
||||||
path = project.path
|
path = project.path
|
||||||
@ -432,3 +470,23 @@ def test_import(tmpdir):
|
|||||||
]
|
]
|
||||||
assert content["topology"]["nodes"][0]["server_id"] == 1
|
assert content["topology"]["nodes"][0]["server_id"] == 1
|
||||||
assert content["topology"]["nodes"][1]["server_id"] == 2
|
assert content["topology"]["nodes"][1]["server_id"] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_import_with_images(tmpdir):
|
||||||
|
|
||||||
|
project_id = str(uuid.uuid4())
|
||||||
|
project = Project(name="test", project_id=project_id)
|
||||||
|
|
||||||
|
with open(str(tmpdir / "test.image"), 'w+') as f:
|
||||||
|
f.write("B")
|
||||||
|
|
||||||
|
zip_path = str(tmpdir / "project.zip")
|
||||||
|
with zipfile.ZipFile(zip_path, 'w') as myzip:
|
||||||
|
myzip.write(str(tmpdir / "test.image"), "images/IOS/test.image")
|
||||||
|
|
||||||
|
with open(zip_path, "rb") as f:
|
||||||
|
project.import_zip(f)
|
||||||
|
|
||||||
|
# TEST import images
|
||||||
|
path = os.path.join(project._config().get("images_path"), "IOS", "test.image")
|
||||||
|
assert os.path.exists(path), path
|
||||||
|
Loading…
Reference in New Issue
Block a user