2016-03-03 15:02:27 +00:00
|
|
|
#!/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/>.
|
|
|
|
|
2016-03-10 09:32:07 +00:00
|
|
|
import aiohttp
|
|
|
|
import asyncio
|
|
|
|
import json
|
2016-03-03 15:02:27 +00:00
|
|
|
|
2016-03-04 15:11:31 +00:00
|
|
|
from ..controller.controller_error import ControllerError
|
|
|
|
from ..config import Config
|
2016-03-04 15:58:53 +00:00
|
|
|
from ..version import __version__
|
|
|
|
|
2016-03-04 15:11:31 +00:00
|
|
|
|
2016-03-04 15:55:59 +00:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-03-04 15:11:31 +00:00
|
|
|
|
2016-03-08 15:04:12 +00:00
|
|
|
class HypervisorError(ControllerError):
|
2016-03-04 15:11:31 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-03-08 15:04:12 +00:00
|
|
|
class Hypervisor:
|
2016-03-03 15:02:27 +00:00
|
|
|
"""
|
2016-03-08 15:04:12 +00:00
|
|
|
A GNS3 hypervisor.
|
2016-03-03 15:02:27 +00:00
|
|
|
"""
|
|
|
|
|
2016-03-08 15:04:12 +00:00
|
|
|
def __init__(self, hypervisor_id, protocol="http", host="localhost", port=8000, user=None, password=None):
|
|
|
|
log.info("Create hypervisor %s", hypervisor_id)
|
|
|
|
self._id = hypervisor_id
|
2016-03-03 15:02:27 +00:00
|
|
|
self._protocol = protocol
|
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
self._user = user
|
|
|
|
self._password = password
|
|
|
|
self._connected = False
|
2016-03-08 15:04:12 +00:00
|
|
|
# The remote hypervisor version
|
2016-03-04 15:58:53 +00:00
|
|
|
# TODO: For the moment it's fake we return the controller version
|
|
|
|
self._version = __version__
|
2016-03-03 15:02:27 +00:00
|
|
|
|
2016-03-08 15:04:12 +00:00
|
|
|
# If the hypervisor is local but the hypervisor id is local
|
2016-03-04 15:11:31 +00:00
|
|
|
# it's a configuration issue
|
2016-03-10 09:32:07 +00:00
|
|
|
if hypervisor_id == "local" and Config.instance().get_section_config("Server")["local"] is False:
|
2016-03-08 15:04:12 +00:00
|
|
|
raise HypervisorError("The local hypervisor is started without --local")
|
2016-03-04 15:11:31 +00:00
|
|
|
|
2016-03-03 15:02:27 +00:00
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
"""
|
2016-03-08 15:04:12 +00:00
|
|
|
:returns: Hypervisor identifier (string)
|
2016-03-03 15:02:27 +00:00
|
|
|
"""
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def host(self):
|
|
|
|
"""
|
2016-03-08 15:04:12 +00:00
|
|
|
:returns: Hypervisor host (string)
|
2016-03-03 15:02:27 +00:00
|
|
|
"""
|
|
|
|
return self._host
|
|
|
|
|
|
|
|
def __json__(self):
|
|
|
|
return {
|
2016-03-08 15:04:12 +00:00
|
|
|
"hypervisor_id": self._id,
|
2016-03-03 15:02:27 +00:00
|
|
|
"protocol": self._protocol,
|
|
|
|
"host": self._host,
|
|
|
|
"port": self._port,
|
|
|
|
"user": self._user,
|
|
|
|
"connected": self._connected,
|
|
|
|
"version": self._version
|
|
|
|
}
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-03-10 20:51:29 +00:00
|
|
|
def httpQuery(self, method, path, data=None):
|
2016-03-10 09:32:07 +00:00
|
|
|
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)
|
|
|
|
body = yield from response.read()
|
2016-03-14 15:51:47 +00:00
|
|
|
if body:
|
|
|
|
body = body.decode()
|
|
|
|
if response.status == 400:
|
|
|
|
raise aiohttp.web.HTTPBadRequest(text=body)
|
|
|
|
elif response.status == 401:
|
|
|
|
raise aiohttp.web.HTTPUnauthorized(text=body)
|
|
|
|
elif response.status == 403:
|
|
|
|
raise aiohttp.web.HTTPForbidden(text=body)
|
|
|
|
elif response.status == 404:
|
|
|
|
raise aiohttp.web.HTTPNotFound(text="{} not found on hypervisor".format(url))
|
|
|
|
elif response.status == 409:
|
|
|
|
raise aiohttp.web.HTTPConflict(text=body)
|
|
|
|
elif response.status >= 300:
|
|
|
|
raise NotImplemented("{} status code is not supported".format(e.status))
|
|
|
|
if body and len(body):
|
|
|
|
response.json = json.loads(body)
|
|
|
|
else:
|
|
|
|
response.json = {}
|
|
|
|
return response
|
2016-03-10 09:32:07 +00:00
|
|
|
|
|
|
|
@asyncio.coroutine
|
2016-03-10 09:57:14 +00:00
|
|
|
def post(self, path, data={}):
|
2016-03-14 15:51:47 +00:00
|
|
|
return (yield from self.httpQuery("POST", path, data))
|