diff --git a/gns3server/controller/topology.py b/gns3server/controller/topology.py index bf6eb7f6..46a235dc 100644 --- a/gns3server/controller/topology.py +++ b/gns3server/controller/topology.py @@ -17,10 +17,12 @@ import os import json -import jsonschema import uuid import shutil +import zipfile import aiohttp +import jsonschema + from ..version import __version__ from ..schemas.topology import TOPOLOGY_SCHEMA @@ -108,6 +110,8 @@ def _convert_1_3_later(topo, topo_path): """ topo_dir = os.path.dirname(topo_path) + _convert_snapshots(topo_dir) + new_topo = { "type": "topology", "revision": GNS3_FILE_FORMAT_REVISION, @@ -448,3 +452,34 @@ def _create_cloud(node, old_node, icon): node["properties"]["ports"] = ports node["properties"]["interfaces"] = [] + + +def _convert_snapshots(topo_dir): + """ + Convert 1.x snapshot to the new format + """ + old_snapshots_dir = os.path.join(topo_dir, "project-files", "snapshots") + if os.path.exists(old_snapshots_dir): + new_snapshots_dir = os.path.join(topo_dir, "snapshots") + os.makedirs(new_snapshots_dir) + + for snapshot in os.listdir(old_snapshots_dir): + snapshot_dir = os.path.join(old_snapshots_dir, snapshot) + if os.path.isdir(snapshot_dir): + is_gns3_topo = False + + # In .gns3project fileformat the .gns3 should be name project.gns3 + for file in os.listdir(snapshot_dir): + if file.endswith(".gns3"): + shutil.move(os.path.join(snapshot_dir, file), os.path.join(snapshot_dir, "project.gns3")) + is_gns3_topo = True + + if is_gns3_topo: + snapshot_arc = os.path.join(new_snapshots_dir, snapshot + ".gns3project") + with zipfile.ZipFile(snapshot_arc, 'w') as myzip: + for root, dirs, files in os.walk(snapshot_dir): + for file in files: + myzip.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), snapshot_dir), compress_type=zipfile.ZIP_DEFLATED) + shutil.copy(snapshot_arc, "/tmp/test.zip") + + shutil.rmtree(old_snapshots_dir) diff --git a/tests/test_topologies.py b/tests/test_topologies.py index 322aa127..7a1186eb 100644 --- a/tests/test_topologies.py +++ b/tests/test_topologies.py @@ -81,6 +81,15 @@ def test_convert(directory, tmpdir): if not file_path.endswith(".gns3"): assert os.stat(file_path).st_size == os.stat(os.path.join(os.path.join(root, file))).st_size, "File {} is different".format(os.path.join(directory, file)) + # Check if we don't have unexpected file in work directory + for root, dirs, files in os.walk(work_directory): + for file in files: + directory = os.path.relpath(root, work_directory) + file_path = os.path.join(after_directory, directory, file) + # .backup are created by the conversion process + if not file.endswith(".backup"): + assert os.path.exists(file_path), "{} should not be here".format(os.path.join(directory, file)) + compare_dict("/", work_topology, after_topology) diff --git a/tests/topologies/1_5_snapshot/after/1_5_snapshot.gns3 b/tests/topologies/1_5_snapshot/after/1_5_snapshot.gns3 new file mode 100644 index 00000000..dd611145 --- /dev/null +++ b/tests/topologies/1_5_snapshot/after/1_5_snapshot.gns3 @@ -0,0 +1,45 @@ +{ + "type" : "topology", + "version" : "2.0.0dev1", + "topology" : { + "nodes" : [ + { + "compute_id" : "local", + "z" : 1, + "y" : -51, + "node_id" : "992c28fd-4bc3-4508-9300-48600148f64a", + "x" : -128, + "console" : 5000, + "node_type" : "vpcs", + "name" : "PC1", + "symbol" : ":/symbols/vpcs_guest.svg", + "console_type" : "telnet", + "properties" : { + "startup_script_path" : "startup.vpc" + }, + "label" : { + "y" : -25, + "style" : "font-family: TypeWriter;font-size: 10;font-weight: bold;fill: #000000;fill-opacity: 1.0;", + "x" : 18, + "text" : "PC1", + "rotation" : 0 + } + } + ], + "links" : [], + "drawings" : [], + "computes" : [ + { + "name" : "Local", + "host" : "127.0.0.1", + "compute_id" : "local", + "protocol" : "http", + "port" : 3080 + } + ] + }, + "revision" : 5, + "name" : "1_5_snapshot", + "project_id" : "17912669-dd40-4843-b348-689b8f34f09d", + "auto_start" : false +} diff --git a/tests/topologies/1_5_snapshot/after/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc b/tests/topologies/1_5_snapshot/after/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc new file mode 100644 index 00000000..d46a9c6f --- /dev/null +++ b/tests/topologies/1_5_snapshot/after/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc @@ -0,0 +1 @@ +set pcname PC1 diff --git a/tests/topologies/1_5_snapshot/after/snapshots/onedevice_280716_174116.gns3project b/tests/topologies/1_5_snapshot/after/snapshots/onedevice_280716_174116.gns3project new file mode 100644 index 00000000..64330d85 Binary files /dev/null and b/tests/topologies/1_5_snapshot/after/snapshots/onedevice_280716_174116.gns3project differ diff --git a/tests/topologies/1_5_snapshot/before/1_5_snapshot.gns3 b/tests/topologies/1_5_snapshot/before/1_5_snapshot.gns3 new file mode 100644 index 00000000..20a770b1 --- /dev/null +++ b/tests/topologies/1_5_snapshot/before/1_5_snapshot.gns3 @@ -0,0 +1,52 @@ +{ + "auto_start": false, + "name": "1_5_snapshot", + "project_id": "17912669-dd40-4843-b348-689b8f34f09d", + "revision": 4, + "topology": { + "nodes": [ + { + "description": "VPCS device", + "id": 1, + "label": { + "color": "#ff000000", + "font": "TypeWriter,10,-1,5,75,0,0,0,0,0", + "text": "PC1", + "x": 18.5859375, + "y": -25.0 + }, + "ports": [ + { + "adapter_number": 0, + "id": 1, + "name": "Ethernet0", + "port_number": 0 + } + ], + "properties": { + "console": 5000, + "name": "PC1", + "startup_script_path": "startup.vpc" + }, + "server_id": 2, + "symbol": ":/symbols/vpcs_guest.svg", + "type": "VPCSDevice", + "vm_id": "992c28fd-4bc3-4508-9300-48600148f64a", + "x": -128.5, + "y": -51.5 + } + ], + "servers": [ + { + "host": "127.0.0.1", + "id": 2, + "local": true, + "port": 3080, + "protocol": "http", + "vm": false + } + ] + }, + "type": "topology", + "version": "1.5.1" +} diff --git a/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc new file mode 100644 index 00000000..d46a9c6f --- /dev/null +++ b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc @@ -0,0 +1 @@ +set pcname PC1 diff --git a/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/screenshot.png b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/screenshot.png new file mode 100644 index 00000000..3c70d40a Binary files /dev/null and b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/screenshot.png differ diff --git a/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/testsnapshot.gns3 b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/testsnapshot.gns3 new file mode 100644 index 00000000..1f6d6865 --- /dev/null +++ b/tests/topologies/1_5_snapshot/before/project-files/snapshots/onedevice_280716_174116/testsnapshot.gns3 @@ -0,0 +1,52 @@ +{ + "auto_start": false, + "name": "testsnapshot", + "project_id": "17912669-dd40-4843-b348-689b8f34f09d", + "revision": 4, + "topology": { + "nodes": [ + { + "description": "VPCS device", + "id": 1, + "label": { + "color": "#ff000000", + "font": "TypeWriter,10,-1,5,75,0,0,0,0,0", + "text": "PC1", + "x": 18.5859375, + "y": -25.0 + }, + "ports": [ + { + "adapter_number": 0, + "id": 1, + "name": "Ethernet0", + "port_number": 0 + } + ], + "properties": { + "console": 5000, + "name": "PC1", + "startup_script_path": "startup.vpc" + }, + "server_id": 2, + "symbol": ":/symbols/vpcs_guest.svg", + "type": "VPCSDevice", + "vm_id": "992c28fd-4bc3-4508-9300-48600148f64a", + "x": -128.5, + "y": -51.5 + } + ], + "servers": [ + { + "host": "127.0.0.1", + "id": 2, + "local": true, + "port": 3080, + "protocol": "http", + "vm": false + } + ] + }, + "type": "topology", + "version": "1.5.1" +} \ No newline at end of file diff --git a/tests/topologies/1_5_snapshot/before/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc b/tests/topologies/1_5_snapshot/before/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc new file mode 100644 index 00000000..d46a9c6f --- /dev/null +++ b/tests/topologies/1_5_snapshot/before/project-files/vpcs/992c28fd-4bc3-4508-9300-48600148f64a/startup.vpc @@ -0,0 +1 @@ +set pcname PC1