mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Close, commit and delete supported for projects in controller
This commit is contained in:
19 files changed
+560
-117
No files matched your search
@@ -15,6 +15,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
from ..config import Config
|
||||
|
||||
@@ -24,6 +26,7 @@ class Controller:
|
||||
|
||||
def __init__(self):
|
||||
self._hypervisors = {}
|
||||
self._projects = {}
|
||||
|
||||
def isEnabled(self):
|
||||
"""
|
||||
@@ -36,7 +39,8 @@ class Controller:
|
||||
"""
|
||||
Add a server to the dictionnary of hypervisors controlled by GNS3
|
||||
"""
|
||||
self._hypervisors[hypervisor.id] = hypervisor
|
||||
if hypervisor.id not in self._hypervisors:
|
||||
self._hypervisors[hypervisor.id] = hypervisor
|
||||
|
||||
@property
|
||||
def hypervisors(self):
|
||||
@@ -45,6 +49,35 @@ class Controller:
|
||||
"""
|
||||
return self._hypervisors
|
||||
|
||||
@asyncio.coroutine
|
||||
def addProject(self, project):
|
||||
"""
|
||||
Add a server to the dictionnary of projects controlled by GNS3
|
||||
"""
|
||||
if project.id not in self._projects:
|
||||
self._projects[project.id] = project
|
||||
for hypervisor in self._hypervisors.values():
|
||||
yield from project.addHypervisor(hypervisor)
|
||||
|
||||
def getProject(self, project_id):
|
||||
"""
|
||||
Return a server or raise a 404
|
||||
"""
|
||||
try:
|
||||
return self._projects[project_id]
|
||||
except KeyError:
|
||||
raise aiohttp.web.HTTPNotFound(text="Project ID {} doesn't exist".format(project_id))
|
||||
|
||||
def removeProject(self, project):
|
||||
del self._projects[project.id]
|
||||
|
||||
@property
|
||||
def projects(self):
|
||||
"""
|
||||
:returns: The dictionnary of hypervisors managed by GNS3
|
||||
"""
|
||||
return self._projects
|
||||
|
||||
@staticmethod
|
||||
def instance():
|
||||
"""
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import aiohttp
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from ..controller.controller_error import ControllerError
|
||||
from ..config import Config
|
||||
@@ -49,7 +52,7 @@ class Hypervisor:
|
||||
|
||||
# If the hypervisor is local but the hypervisor id is local
|
||||
# it's a configuration issue
|
||||
if hypervisor_id == "local" and Config.instance().get_section_config("Hypervisor")["local"] is False:
|
||||
if hypervisor_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
|
||||
raise HypervisorError("The local hypervisor is started without --local")
|
||||
|
||||
@property
|
||||
@@ -76,3 +79,23 @@ class Hypervisor:
|
||||
"connected": self._connected,
|
||||
"version": self._version
|
||||
}
|
||||
|
||||
@asyncio.coroutine
|
||||
def _httpQuery(self, method, path, data=None):
|
||||
with aiohttp.Timeout(10):
|
||||
with aiohttp.ClientSession() as session:
|
||||
url = "{}://{}:{}/v2/hypervisor{}".format(self._protocol, self._host, self._port, path)
|
||||
headers = {'content-type': 'application/json'}
|
||||
if hasattr(data, '__json__'):
|
||||
data = data.__json__()
|
||||
data = json.dumps(data)
|
||||
response = yield from session.request(method, url, headers=headers, data=data)
|
||||
print(response.status)
|
||||
assert response.status < 300
|
||||
body = yield from response.read()
|
||||
yield from response.release()
|
||||
return body
|
||||
|
||||
@asyncio.coroutine
|
||||
def post(self, path, data):
|
||||
yield from self._httpQuery("POST", path, data)
|
||||
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
|
||||
class Project:
|
||||
"""
|
||||
A project inside controller
|
||||
|
||||
:param project_id: force project identifier (None by default auto generate an UUID)
|
||||
:param path: path of the project. (None use the standard directory)
|
||||
:param temporary: boolean to tell if the project is a temporary project (destroy when closed)
|
||||
"""
|
||||
|
||||
def __init__(self, name=None, project_id=None, path=None, temporary=False):
|
||||
|
||||
self._name = name
|
||||
if project_id is None:
|
||||
self._id = str(uuid4())
|
||||
else:
|
||||
try:
|
||||
UUID(project_id, version=4)
|
||||
except ValueError:
|
||||
raise aiohttp.web.HTTPBadRequest(text="{} is not a valid UUID".format(project_id))
|
||||
self._id = project_id
|
||||
self._path = path
|
||||
self._temporary = temporary
|
||||
self._hypervisors = set()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def temporary(self):
|
||||
return self._temporary
|
||||
|
||||
@property
|
||||
def path(self):
|
||||
return self._path
|
||||
|
||||
@asyncio.coroutine
|
||||
def addHypervisor(self, hypervisor):
|
||||
self._hypervisors.add(hypervisor)
|
||||
yield from hypervisor.post("/projects", self)
|
||||
|
||||
@asyncio.coroutine
|
||||
def close(self):
|
||||
for hypervisor in self._hypervisors:
|
||||
yield from hypervisor.post("/projects/{}/close".format(self._id))
|
||||
|
||||
@asyncio.coroutine
|
||||
def commit(self):
|
||||
for hypervisor in self._hypervisors:
|
||||
yield from hypervisor.post("/projects/{}/commit".format(self._id))
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete(self):
|
||||
for hypervisor in self._hypervisors:
|
||||
yield from hypervisor.delete("/projects/{}".format(self._id))
|
||||
|
||||
def __json__(self):
|
||||
|
||||
return {
|
||||
"name": self._name,
|
||||
"project_id": self._id,
|
||||
"temporary": self._temporary,
|
||||
"path": self._path
|
||||
}
|
||||
Reference in new issue
Block a user