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

Start / Stop / Suspend marche

This commit is contained in:
Julien Duponchelle 2016-04-14 12:22:10 +02:00
parent 2841b5769e
commit 3185baff0d
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8
3 changed files with 23 additions and 14 deletions

View File

@ -47,10 +47,9 @@ class Hypervisor:
self._port = port
self._user = None
self._password = None
self._setAuth(user, password)
self._connected = False
self._controller = controller
self._session = aiohttp.ClientSession()
self._setAuth(user, password)
# If the hypervisor is local but the hypervisor id is local
# it's a configuration issue
@ -72,6 +71,7 @@ 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):
@ -117,7 +117,8 @@ class Hypervisor:
def httpQuery(self, method, path, data=None):
if not self._connected:
yield from self._connect()
return (yield from self._runHttpQuery(method, path, data=data))
response = yield from self._runHttpQuery(method, path, data=data)
return response
@asyncio.coroutine
def _connect(self):
@ -140,7 +141,7 @@ class Hypervisor:
"""
Connect to the notification stream
"""
ws = yield from self._session.ws_connect(self._getUrl("/notifications/ws"), auth=self._auth)
ws = yield from self._session.ws_connect(self._getUrl("/notifications/ws"))
while True:
response = yield from ws.receive()
if response.tp == aiohttp.MsgType.closed or response.tp == aiohttp.MsgType.error:
@ -159,11 +160,14 @@ class Hypervisor:
with aiohttp.Timeout(10):
url = self._getUrl(path)
headers = {'content-type': 'application/json'}
if data:
if data == {}:
data = None
elif data is not None:
if hasattr(data, '__json__'):
data = data.__json__()
data = json.dumps(data)
response = yield from self._session.request(method, url, headers=headers, data=data, auth=self._auth)
response = yield from self._session.request(method, url, headers=headers, data=data)
body = yield from response.read()
if body:
body = body.decode()
@ -190,9 +194,14 @@ class Hypervisor:
response.json = {}
return response
@asyncio.coroutine
def get(self, path):
return (yield from self.httpQuery("GET", path))
@asyncio.coroutine
def post(self, path, data={}):
return (yield from self.httpQuery("POST", path, data))
response = yield from self.httpQuery("POST", path, data)
return response
@asyncio.coroutine
def delete(self, path):

View File

@ -115,21 +115,21 @@ class VM:
"""
Start a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/start".format(self._project.id, self._vm_type, self._id))
yield from self.post("/start")
@asyncio.coroutine
def stop(self):
"""
Stop a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/stop".format(self._project.id, self._vm_type, self._id))
yield from self.post("/stop")
@asyncio.coroutine
def suspend(self):
"""
Suspend a VM
"""
yield from self._hypervisor.post("/projects/{}/{}/vms/{}/suspend".format(self._project.id, self._vm_type, self._id))
yield from self.post("/suspend")
@asyncio.coroutine
def post(self, path, data={}):

View File

@ -243,12 +243,12 @@ class Route(object):
if vm_id is None:
vm_id = request.match_info["device_id"]
if "controller" in request.path:
type = "controller"
else:
if "hypervisor" in request.path:
type = "compute"
else:
type = "controller"
lock_key = "{}:{}:{}".format(type, request.match_info["project_id"], vm_id)
print(lock_key)
cls._vm_locks.setdefault(lock_key, {"lock": asyncio.Lock(), "concurrency": 0})
cls._vm_locks[lock_key]["concurrency"] += 1