mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
186 files changed
+1173
-1206
No files matched your search
@@ -20,14 +20,14 @@ import aiohttp
|
||||
|
||||
from ..config import Config
|
||||
from .project import Project
|
||||
from .hypervisor import Hypervisor
|
||||
from .compute import Compute
|
||||
|
||||
|
||||
class Controller:
|
||||
"""The controller manage multiple gns3 hypervisors"""
|
||||
"""The controller manage multiple gns3 computes"""
|
||||
|
||||
def __init__(self):
|
||||
self._hypervisors = {}
|
||||
self._computes = {}
|
||||
self._projects = {}
|
||||
|
||||
def isEnabled(self):
|
||||
@@ -38,32 +38,32 @@ class Controller:
|
||||
return Config.instance().get_section_config("Server").getboolean("controller")
|
||||
|
||||
@asyncio.coroutine
|
||||
def addHypervisor(self, hypervisor_id, **kwargs):
|
||||
def addCompute(self, compute_id, **kwargs):
|
||||
"""
|
||||
Add a server to the dictionnary of hypervisors controlled by GNS3
|
||||
Add a server to the dictionnary of computes controlled by GNS3
|
||||
|
||||
:param kwargs: See the documentation of Hypervisor
|
||||
:param kwargs: See the documentation of Compute
|
||||
"""
|
||||
if hypervisor_id not in self._hypervisors:
|
||||
hypervisor = Hypervisor(hypervisor_id=hypervisor_id, controller=self, **kwargs)
|
||||
self._hypervisors[hypervisor_id] = hypervisor
|
||||
return self._hypervisors[hypervisor_id]
|
||||
if compute_id not in self._computes:
|
||||
compute = Compute(compute_id=compute_id, controller=self, **kwargs)
|
||||
self._computes[compute_id] = compute
|
||||
return self._computes[compute_id]
|
||||
|
||||
@property
|
||||
def hypervisors(self):
|
||||
def computes(self):
|
||||
"""
|
||||
:returns: The dictionnary of hypervisors managed by GNS3
|
||||
:returns: The dictionnary of computes managed by GNS3
|
||||
"""
|
||||
return self._hypervisors
|
||||
return self._computes
|
||||
|
||||
def getHypervisor(self, hypervisor_id):
|
||||
def getCompute(self, compute_id):
|
||||
"""
|
||||
Return an hypervisor or raise a 404
|
||||
Return an compute or raise a 404
|
||||
"""
|
||||
try:
|
||||
return self._hypervisors[hypervisor_id]
|
||||
return self._computes[compute_id]
|
||||
except KeyError:
|
||||
raise aiohttp.web.HTTPNotFound(text="Hypervisor ID {} doesn't exist".format(hypervisor_id))
|
||||
raise aiohttp.web.HTTPNotFound(text="Compute ID {} doesn't exist".format(compute_id))
|
||||
|
||||
@asyncio.coroutine
|
||||
def addProject(self, project_id=None, **kwargs):
|
||||
@@ -75,8 +75,8 @@ class Controller:
|
||||
if project_id not in self._projects:
|
||||
project = Project(project_id=project_id, **kwargs)
|
||||
self._projects[project.id] = project
|
||||
for hypervisor in self._hypervisors.values():
|
||||
yield from project.addHypervisor(hypervisor)
|
||||
for compute in self._computes.values():
|
||||
yield from project.addCompute(compute)
|
||||
return self._projects[project.id]
|
||||
return self._projects[project_id]
|
||||
|
||||
@@ -95,7 +95,7 @@ class Controller:
|
||||
@property
|
||||
def projects(self):
|
||||
"""
|
||||
:returns: The dictionnary of hypervisors managed by GNS3
|
||||
:returns: The dictionnary of computes managed by GNS3
|
||||
"""
|
||||
return self._projects
|
||||
|
||||
|
||||
@@ -29,19 +29,19 @@ import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class HypervisorError(ControllerError):
|
||||
class ComputeError(ControllerError):
|
||||
pass
|
||||
|
||||
|
||||
class Hypervisor:
|
||||
class Compute:
|
||||
"""
|
||||
A GNS3 hypervisor.
|
||||
A GNS3 compute.
|
||||
"""
|
||||
|
||||
def __init__(self, hypervisor_id, controller=None, protocol="http", host="localhost", port=8000, user=None, password=None):
|
||||
def __init__(self, compute_id, controller=None, protocol="http", host="localhost", port=8000, user=None, password=None):
|
||||
assert controller is not None
|
||||
log.info("Create hypervisor %s", hypervisor_id)
|
||||
self._id = hypervisor_id
|
||||
log.info("Create compute %s", compute_id)
|
||||
self._id = compute_id
|
||||
self._protocol = protocol
|
||||
self._host = host
|
||||
self._port = port
|
||||
@@ -50,11 +50,12 @@ class Hypervisor:
|
||||
self._connected = False
|
||||
self._controller = controller
|
||||
self._setAuth(user, password)
|
||||
self._session = aiohttp.ClientSession()
|
||||
|
||||
# If the hypervisor is local but the hypervisor id is local
|
||||
# If the compute is local but the compute id is local
|
||||
# it's a configuration issue
|
||||
if hypervisor_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
|
||||
raise HypervisorError("The local hypervisor is started without --local")
|
||||
if compute_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
|
||||
raise ComputeError("The local compute is started without --local")
|
||||
|
||||
asyncio.async(self._connect())
|
||||
|
||||
@@ -71,19 +72,18 @@ class Hypervisor:
|
||||
self._auth = aiohttp.BasicAuth(self._user, self._password)
|
||||
else:
|
||||
self._auth = None
|
||||
self._session = aiohttp.ClientSession(auth=self._auth)
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
"""
|
||||
:returns: Hypervisor identifier (string)
|
||||
:returns: Compute identifier (string)
|
||||
"""
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
"""
|
||||
:returns: Hypervisor host (string)
|
||||
:returns: Compute host (string)
|
||||
"""
|
||||
return self._host
|
||||
|
||||
@@ -105,7 +105,7 @@ class Hypervisor:
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
"hypervisor_id": self._id,
|
||||
"compute_id": self._id,
|
||||
"protocol": self._protocol,
|
||||
"host": self._host,
|
||||
"port": self._port,
|
||||
@@ -150,10 +150,10 @@ class Hypervisor:
|
||||
msg = json.loads(response.data)
|
||||
action = msg.pop("action")
|
||||
event = msg.pop("event")
|
||||
self._controller.emit(action, event, hypervisor_id=self.id, **msg)
|
||||
self._controller.emit(action, event, compute_id=self.id, **msg)
|
||||
|
||||
def _getUrl(self, path):
|
||||
return "{}://{}:{}/v2/hypervisor{}".format(self._protocol, self._host, self._port, path)
|
||||
return "{}://{}:{}/v2/compute{}".format(self._protocol, self._host, self._port, path)
|
||||
|
||||
@asyncio.coroutine
|
||||
def _runHttpQuery(self, method, path, data=None):
|
||||
@@ -167,7 +167,7 @@ class Hypervisor:
|
||||
data = data.__json__()
|
||||
data = json.dumps(data)
|
||||
|
||||
response = yield from self._session.request(method, url, headers=headers, data=data)
|
||||
response = yield from self._session.request(method, url, headers=headers, data=data, auth=self._auth)
|
||||
body = yield from response.read()
|
||||
if body:
|
||||
body = body.decode()
|
||||
@@ -176,11 +176,11 @@ class Hypervisor:
|
||||
if response.status == 400:
|
||||
raise aiohttp.web.HTTPBadRequest(text="Bad request {} {}".format(url, body))
|
||||
elif response.status == 401:
|
||||
raise aiohttp.web.HTTPUnauthorized(text="Invalid authentication for hypervisor {}".format(self.id))
|
||||
raise aiohttp.web.HTTPUnauthorized(text="Invalid authentication for compute {}".format(self.id))
|
||||
elif response.status == 403:
|
||||
raise aiohttp.web.HTTPForbidden(text="Forbidden {} {}".format(url, body))
|
||||
elif response.status == 404:
|
||||
raise aiohttp.web.HTTPNotFound(text="{} not found on hypervisor".format(url))
|
||||
raise aiohttp.web.HTTPNotFound(text="{} not found on compute".format(url))
|
||||
elif response.status == 409:
|
||||
raise aiohttp.web.HTTPConflict(text="Conflict {} {}".format(url, body))
|
||||
else:
|
||||
@@ -47,7 +47,7 @@ class Project:
|
||||
self._id = project_id
|
||||
self._path = path
|
||||
self._temporary = temporary
|
||||
self._hypervisors = set()
|
||||
self._computes = set()
|
||||
self._vms = {}
|
||||
self._links = {}
|
||||
self._listeners = set()
|
||||
@@ -69,19 +69,19 @@ class Project:
|
||||
return self._path
|
||||
|
||||
@asyncio.coroutine
|
||||
def addHypervisor(self, hypervisor):
|
||||
self._hypervisors.add(hypervisor)
|
||||
yield from hypervisor.post("/projects", self)
|
||||
def addCompute(self, compute):
|
||||
self._computes.add(compute)
|
||||
yield from compute.post("/projects", self)
|
||||
|
||||
@asyncio.coroutine
|
||||
def addVM(self, hypervisor, vm_id, **kwargs):
|
||||
def addVM(self, compute, vm_id, **kwargs):
|
||||
"""
|
||||
Create a vm or return an existing vm
|
||||
|
||||
:param kwargs: See the documentation of VM
|
||||
"""
|
||||
if vm_id not in self._vms:
|
||||
vm = VM(self, hypervisor, vm_id=vm_id, **kwargs)
|
||||
vm = VM(self, compute, vm_id=vm_id, **kwargs)
|
||||
yield from vm.create()
|
||||
self._vms[vm.id] = vm
|
||||
return vm
|
||||
@@ -130,18 +130,18 @@ class Project:
|
||||
|
||||
@asyncio.coroutine
|
||||
def close(self):
|
||||
for hypervisor in self._hypervisors:
|
||||
yield from hypervisor.post("/projects/{}/close".format(self._id))
|
||||
for compute in self._computes:
|
||||
yield from compute.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))
|
||||
for compute in self._computes:
|
||||
yield from compute.post("/projects/{}/commit".format(self._id))
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete(self):
|
||||
for hypervisor in self._hypervisors:
|
||||
yield from hypervisor.delete("/projects/{}".format(self._id))
|
||||
for compute in self._computes:
|
||||
yield from compute.delete("/projects/{}".format(self._id))
|
||||
|
||||
@contextmanager
|
||||
def queue(self):
|
||||
|
||||
@@ -40,15 +40,15 @@ class UDPLink(Link):
|
||||
port_number2 = self._vms[1]["port_number"]
|
||||
|
||||
# Reserve a UDP port on both side
|
||||
response = yield from vm1.hypervisor.post("/projects/{}/ports/udp".format(self._project.id))
|
||||
response = yield from vm1.compute.post("/projects/{}/ports/udp".format(self._project.id))
|
||||
self._vm1_port = response.json["udp_port"]
|
||||
response = yield from vm2.hypervisor.post("/projects/{}/ports/udp".format(self._project.id))
|
||||
response = yield from vm2.compute.post("/projects/{}/ports/udp".format(self._project.id))
|
||||
self._vm2_port = response.json["udp_port"]
|
||||
|
||||
# Create the tunnel on both side
|
||||
data = {
|
||||
"lport": self._vm1_port,
|
||||
"rhost": vm2.hypervisor.host,
|
||||
"rhost": vm2.compute.host,
|
||||
"rport": self._vm2_port,
|
||||
"type": "nio_udp"
|
||||
}
|
||||
@@ -56,7 +56,7 @@ class UDPLink(Link):
|
||||
|
||||
data = {
|
||||
"lport": self._vm2_port,
|
||||
"rhost": vm1.hypervisor.host,
|
||||
"rhost": vm1.compute.host,
|
||||
"rport": self._vm1_port,
|
||||
"type": "nio_udp"
|
||||
}
|
||||
|
||||
+14
-11
@@ -23,10 +23,10 @@ import uuid
|
||||
|
||||
class VM:
|
||||
|
||||
def __init__(self, project, hypervisor, vm_id=None, vm_type=None, name=None, console=None, console_type="telnet", properties={}):
|
||||
def __init__(self, project, compute, vm_id=None, vm_type=None, name=None, console=None, console_type="telnet", properties={}):
|
||||
"""
|
||||
:param project: Project of the VM
|
||||
:param hypervisor: Hypervisor server where the server will run
|
||||
:param compute: Hypervisor server where the server will run
|
||||
:param vm_id: UUID of the vm. Integer id
|
||||
:param vm_type: Type of emulator
|
||||
:param name: Name of the vm
|
||||
@@ -42,7 +42,7 @@ class VM:
|
||||
|
||||
self._name = name
|
||||
self._project = project
|
||||
self._hypervisor = hypervisor
|
||||
self._compute = compute
|
||||
self._vm_type = vm_type
|
||||
self._console = console
|
||||
self._console_type = console_type
|
||||
@@ -77,15 +77,15 @@ class VM:
|
||||
return self._project
|
||||
|
||||
@property
|
||||
def hypervisor(self):
|
||||
return self._hypervisor
|
||||
def compute(self):
|
||||
return self._compute
|
||||
|
||||
@property
|
||||
def host(self):
|
||||
"""
|
||||
:returns: Domain or ip for console connection
|
||||
"""
|
||||
return self._hypervisor.host
|
||||
return self._compute.host
|
||||
|
||||
@asyncio.coroutine
|
||||
def create(self):
|
||||
@@ -100,7 +100,7 @@ class VM:
|
||||
if data[key] is None:
|
||||
del data[key]
|
||||
|
||||
response = yield from self._hypervisor.post("/projects/{}/{}/vms".format(self._project.id, self._vm_type), data=data)
|
||||
response = yield from self._compute.post("/projects/{}/{}/vms".format(self._project.id, self._vm_type), data=data)
|
||||
|
||||
for key, value in response.json.items():
|
||||
if key == "console":
|
||||
@@ -132,22 +132,25 @@ class VM:
|
||||
yield from self.post("/suspend")
|
||||
|
||||
@asyncio.coroutine
|
||||
def post(self, path, data={}):
|
||||
def post(self, path, data=None):
|
||||
"""
|
||||
HTTP post on the VM
|
||||
"""
|
||||
return (yield from self._hypervisor.post("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path), data=data))
|
||||
if data:
|
||||
return (yield from self._compute.post("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path), data=data))
|
||||
else:
|
||||
return (yield from self._compute.post("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path)))
|
||||
|
||||
@asyncio.coroutine
|
||||
def delete(self, path):
|
||||
"""
|
||||
HTTP post on the VM
|
||||
"""
|
||||
return (yield from self._hypervisor.delete("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path)))
|
||||
return (yield from self._compute.delete("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path)))
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
"hypervisor_id": self._hypervisor.id,
|
||||
"compute_id": self._compute.id,
|
||||
"project_id": self._project.id,
|
||||
"vm_id": self._id,
|
||||
"vm_type": self._vm_type,
|
||||
|
||||
Reference in new issue
Block a user