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

Do not reload a project via /load if the project is already opened

Fix #785
This commit is contained in:
Julien Duponchelle 2016-11-14 18:45:44 +01:00
parent e996b825c6
commit 5166d894eb
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
2 changed files with 78 additions and 20 deletions

View File

@ -363,8 +363,8 @@ class Controller:
topo_data.pop("type") topo_data.pop("type")
if topo_data["project_id"] in self._projects: if topo_data["project_id"] in self._projects:
self.remove_project(self._projects[topo_data["project_id"]]) project = self._projects[topo_data["project_id"]]
else:
project = yield from self.add_project(path=os.path.dirname(path), status="closed", filename=os.path.basename(path), **topo_data) project = yield from self.add_project(path=os.path.dirname(path), status="closed", filename=os.path.basename(path), **topo_data)
if load or project.auto_open: if load or project.auto_open:
yield from project.open() yield from project.open()

View File

@ -25,7 +25,7 @@ import math
import aiohttp import aiohttp
import aiohttp.web import aiohttp.web
import asyncio import asyncio
import async_timeout import random
import coloredlogs import coloredlogs
import logging import logging
@ -73,7 +73,6 @@ async def query(method, path, body=None, **kwargs):
if body: if body:
kwargs["data"] = json.dumps(body) kwargs["data"] = json.dumps(body)
with async_timeout.timeout(10):
async with session.request(method, "http://localhost:3081/v2" + path, **kwargs) as response: async with session.request(method, "http://localhost:3081/v2" + path, **kwargs) as response:
if response.status == 409: if response.status == 409:
raise HTTPConflict(method, path, response) raise HTTPConflict(method, path, response)
@ -107,8 +106,10 @@ async def create_project():
await delete("/projects/" + project["project_id"]) await delete("/projects/" + project["project_id"])
elif project["project_id"] == PROJECT_ID: elif project["project_id"] == PROJECT_ID:
project_exists = True project_exists = True
tasks = []
for node in await get("/projects/" + PROJECT_ID + "/nodes"): for node in await get("/projects/" + PROJECT_ID + "/nodes"):
await delete("/projects/" + PROJECT_ID + "/nodes/" + node["node_id"]) tasks.append(delete_node(project, node))
await asyncio.gather(*tasks)
if project_exists: if project_exists:
response = await post("/projects/" + PROJECT_ID + "/open") response = await post("/projects/" + PROJECT_ID + "/open")
else: else:
@ -119,8 +120,9 @@ async def create_project():
async def create_node(project): async def create_node(project):
global node_i global node_i
response = await post("/projects/{}/nodes".format(project["project_id"]), body={ response = await post("/projects/{}/nodes".format(project["project_id"]), body={
"node_type": "vpcs", "node_type": "ethernet_switch",
"compute_id": "local", "compute_id": "local",
"symbol": ":/symbols/ethernet_switch.svg",
"name": "Node{}".format(node_i), "name": "Node{}".format(node_i),
"x": (math.floor((node_i - 1) % 12.0) * 100) - 500, "x": (math.floor((node_i - 1) % 12.0) * 100) - 500,
"y": (math.ceil((node_i) / 12.0) * 100) - 300 "y": (math.ceil((node_i) / 12.0) * 100) - 300
@ -129,15 +131,71 @@ async def create_node(project):
return response return response
async def delete_node(project, node):
await delete("/projects/{}/nodes/{}".format(project["project_id"], node["node_id"]))
async def create_link(project, nodes):
"""
Create all possible link of a node
"""
node1 = random.choice(list(nodes.values()))
for port in range(0, 8):
node2 = random.choice(list(nodes.values()))
if node1 == node2:
continue
data = {"nodes":
[
{
"adapter_number": 0,
"node_id": node1["node_id"],
"port_number": port
},
{
"adapter_number": 0,
"node_id": node2["node_id"],
"port_number": port
}
]
}
try:
await post("/projects/{}/links".format(project["project_id"]), body=data)
except HTTPConflict:
pass
async def build_topology(): async def build_topology():
global node_i global node_i
nodes = {} nodes = {}
project = await create_project() project = await create_project()
while True: while True:
rand = random.randint(0, 1000)
if rand < 500: # chance to create a new node
if len(nodes.keys()) < 255: # Limit of VPCS:
node = await create_node(project) node = await create_node(project)
nodes[node["node_id"]] = node nodes[node["node_id"]] = node
await asyncio.sleep(0.5) elif rand < 950: # create a link
if len(nodes.keys()) >= 2:
await create_link(project, nodes)
elif rand < 999: # chance to delete a node
continue
if len(nodes.keys()) > 0:
node = random.choice(list(nodes.values()))
await delete_node(project, node)
del nodes[node["node_id"]]
elif len(nodes.keys()) > 0: # % chance to delete all nodes
continue
node_i = 1
tasks = []
for node in nodes.values():
tasks.append(delete_node(project, node))
await asyncio.gather(*tasks)
nodes = {}
await asyncio.sleep(0.2)
async def main(loop): async def main(loop):
global session global session