Repository merge: reconcile ara-clients back into ara-server

This merge commit recovers the ara/clients directory from the
ara-clients project along with the commit history since it is being
retired.
This commit is contained in:
David Moreau Simard 2019-03-01 14:05:02 -05:00
commit 4fb1b9049f
No known key found for this signature in database
GPG Key ID: CBEB466764A9E621
5 changed files with 238 additions and 0 deletions

0
ara/clients/__init__.py Normal file
View File

108
ara/clients/http.py Normal file
View File

@ -0,0 +1,108 @@
# Copyright (c) 2018 Red Hat, Inc.
#
# This file is part of ARA: Ansible Run Analysis.
#
# ARA 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.
#
# ARA 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 ARA. If not, see <http://www.gnu.org/licenses/>.
# This is an "offline" API client that does not require standing up
# an API server and does not execute actual HTTP calls.
import json
import logging
import pbr.version
import requests
CLIENT_VERSION = pbr.version.VersionInfo("ara-clients").release_string()
class HttpClient(object):
def __init__(self, endpoint="http://127.0.0.1:8000", timeout=30):
self.log = logging.getLogger(__name__)
self.endpoint = endpoint
self.timeout = timeout
self.headers = {
"User-Agent": "ara-http-client_%s" % CLIENT_VERSION,
"Accept": "application/json",
"Content-Type": "application/json",
}
self.http = requests.Session()
self.http.headers.update(self.headers)
def _request(self, method, url, **payload):
# Use requests.Session to do the query
# The actual endpoint is:
# <endpoint> <url>
# http://127.0.0.1:8000 / api/v1/playbooks
return self.http.request(method, self.endpoint + url, timeout=self.timeout, **payload)
def get(self, url, **payload):
if payload:
return self._request("get", url, **payload)
else:
return self._request("get", url)
def patch(self, url, **payload):
return self._request("patch", url, data=json.dumps(payload))
def post(self, url, **payload):
return self._request("post", url, data=json.dumps(payload))
def put(self, url, **payload):
return self._request("put", url, data=json.dumps(payload))
def delete(self, url):
return self._request("delete", url)
class AraHttpClient(object):
def __init__(self, endpoint="http://127.0.0.1:8000", timeout=30):
self.log = logging.getLogger(__name__)
self.client = HttpClient(endpoint, timeout)
def _request(self, method, url, **kwargs):
func = getattr(self.client, method)
if method == "delete":
response = func(url)
else:
response = func(url, **kwargs)
if response.status_code >= 500:
self.log.error("Failed to {method} on {url}: {content}".format(method=method, url=url, content=kwargs))
self.log.debug("HTTP {status}: {method} on {url}".format(status=response.status_code, method=method, url=url))
if response.status_code not in [200, 201, 204]:
self.log.error("Failed to {method} on {url}: {content}".format(method=method, url=url, content=kwargs))
if response.status_code == 204:
return response
return response.json()
def get(self, endpoint, **kwargs):
return self._request("get", endpoint, params=kwargs)
def patch(self, endpoint, **kwargs):
return self._request("patch", endpoint, **kwargs)
def post(self, endpoint, **kwargs):
return self._request("post", endpoint, **kwargs)
def put(self, endpoint, **kwargs):
return self._request("put", endpoint, **kwargs)
def delete(self, endpoint, **kwargs):
return self._request("delete", endpoint)

98
ara/clients/offline.py Normal file
View File

@ -0,0 +1,98 @@
# Copyright (c) 2018 Red Hat, Inc.
#
# This file is part of ARA: Ansible Run Analysis.
#
# ARA 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.
#
# ARA 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 ARA. If not, see <http://www.gnu.org/licenses/>.
# This is an "offline" API client that does not require standing up
# an API server and does not execute actual HTTP calls.
import logging
import os
import threading
from django.core.handlers.wsgi import WSGIHandler
from django.core.servers.basehttp import ThreadedWSGIServer, WSGIRequestHandler
from ara.clients.http import AraHttpClient
class AraOfflineClient(AraHttpClient):
def __init__(self):
self.log = logging.getLogger(__name__)
# Validate that ara-server is available before letting Django attempt to
# import it
try:
import ara.server # noqa
except ImportError:
raise ImportError("AraOfflineClient requires ara-server to be installed.")
from django import setup as django_setup
from django.core.management import execute_from_command_line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ara.server.settings")
# Automatically create the database and run migrations (is there a better way?)
execute_from_command_line(["django", "migrate"])
# Set up the things Django needs
django_setup()
self._start_server()
super().__init__(endpoint="http://localhost:%d" % self.server_thread.port)
def _start_server(self):
self.server_thread = ServerThread("localhost")
self.server_thread.start()
# Wait for the live server to be ready
self.server_thread.is_ready.wait()
if self.server_thread.error:
raise self.server_thread.error
class ServerThread(threading.Thread):
def __init__(self, host, port=0):
self.host = host
self.port = port
self.is_ready = threading.Event()
self.error = None
super().__init__(daemon=True)
def run(self):
"""
Set up the live server and databases, and then loop over handling
HTTP requests.
"""
try:
# Create the handler for serving static and media files
self.httpd = self._create_server()
# If binding to port zero, assign the port allocated by the OS.
if self.port == 0:
self.port = self.httpd.server_address[1]
self.httpd.set_app(WSGIHandler())
self.is_ready.set()
self.httpd.serve_forever()
except Exception as e:
self.error = e
self.is_ready.set()
def _create_server(self):
return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)
class QuietWSGIRequestHandler(WSGIRequestHandler):
def log_message(*args):
pass

31
ara/clients/utils.py Normal file
View File

@ -0,0 +1,31 @@
# Copyright (c) 2018 Red Hat, Inc.
#
# This file is part of ARA Records Ansible.
#
# ARA 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.
#
# ARA 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 ARA. If not, see <http://www.gnu.org/licenses/>.
from ara.clients.http import AraHttpClient
from ara.clients.offline import AraOfflineClient
def get_client(client="offline", endpoint="http://127.0.0.1:8000", timeout=30):
"""
Returns a specified client configuration or one with sane defaults.
"""
if client == "offline":
return AraOfflineClient()
elif client == "http":
return AraHttpClient(endpoint=endpoint, timeout=timeout)
else:
raise ValueError(f"Unsupported API client: {client} (use 'http' or 'offline')")

View File

@ -5,3 +5,4 @@ djangorestframework>=3.9.1
django-cors-headers
django-filter
dynaconf[yaml]
requests>=2.14.2