Start / Stop / Suspend marche

pull/565/head
Julien Duponchelle 9 years ago
parent 2841b5769e
commit 3185baff0d
No known key found for this signature in database
GPG Key ID: CE8B29639E07F5E8

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

@ -115,21 +115,21 @@ class VM:
""" """
Start a 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 @asyncio.coroutine
def stop(self): def stop(self):
""" """
Stop a VM 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 @asyncio.coroutine
def suspend(self): def suspend(self):
""" """
Suspend a VM 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 @asyncio.coroutine
def post(self, path, data={}): def post(self, path, data={}):

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

Loading…
Cancel
Save