Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge c1834b1 into 08be2df
Browse files Browse the repository at this point in the history
  • Loading branch information
csomh committed Apr 5, 2019
2 parents 08be2df + c1834b1 commit bc410eb
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ LABEL \
ARG cacert_url=undefined

ENV WORKERS_NUM 8
# Explicit worker timeout is 30 seconds.
ENV WORKER_TIMEOUT 30

WORKDIR /src
RUN dnf -y install \
Expand All @@ -32,4 +34,4 @@ COPY . .
RUN pip3 install . --no-deps
USER 1001
EXPOSE 8080
ENTRYPOINT docker/install-ca.sh && gunicorn-3 --workers ${WORKERS_NUM} --bind 0.0.0.0:8080 --access-logfile=- --enable-stdio-inheritance omps.app:app
ENTRYPOINT docker/install-ca.sh && gunicorn-3 --workers ${WORKERS_NUM} --timeout ${WORKER_TIMEOUT} --bind 0.0.0.0:8080 --access-logfile=- --enable-stdio-inheritance omps.app:app
4 changes: 2 additions & 2 deletions omps/api/v1/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging

from flask import jsonify
from flask import jsonify, current_app
import requests
from requests.exceptions import RequestException

Expand Down Expand Up @@ -41,7 +41,7 @@ def _err(exc):
# quay.io status
try:
# try to retrieve API version to check if quay.io is alive
get_cnr_api_version()
get_cnr_api_version(current_app.config['REQUEST_TIMEOUT'])
except RequestException as e:
logger.error('Quay version check: %s', e)
quay_result = _err(e)
Expand Down
5 changes: 4 additions & 1 deletion omps/koji_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def __init__(self):
def initialize(self, conf):
self._kojihub_url = conf.kojihub_url
self._kojiroot_url = conf.kojiroot_url
self._session = koji.ClientSession(self._kojihub_url)
opts = dict()
if conf.request_timeout is not None:
opts['timeout'] = conf.request_timeout
self._session = koji.ClientSession(self._kojihub_url, opts=opts)

if not self._kojihub_url.endswith('/'):
self._kojihub_url += '/'
Expand Down
21 changes: 14 additions & 7 deletions omps/quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ def validate_conf(cls, organizations):

def __init__(self):
self._organizations = None
self._timeout = None

def initialize(self, config):
self.validate_conf(config.organizations)
self._organizations = config.organizations
self._timeout = config.request_timeout
for org_name, org_conf in self._organizations.items():
logger.info(
'Organization "%s" configured: public=%s, oauth_access=%s',
Expand All @@ -174,21 +176,24 @@ def get_org(self, organization, cnr_token):
organization,
cnr_token,
oauth_token=org_config.get('oauth_token'),
public=org_config.get('public', False)
public=org_config.get('public', False),
timeout=self._timeout
)


class QuayOrganization:
"""Class for operations on organization"""

def __init__(
self, organization, cnr_token, oauth_token=None, public=False
self, organization, cnr_token, oauth_token=None, public=False,
timeout=None
):
"""
:param organization: organization name
:param cnr_token: organization login token (cnr endpoint)
:param oauth_token: oauth_access_token
:param public: organization is public
:param int timeout: timeout for Quay requests in seconds
"""
self.logger = logging.getLogger(
'{0}[{1}]'.format(self.__class__.__name__, organization))
Expand All @@ -197,6 +202,7 @@ def __init__(
self._token = cnr_token
self._oauth_token = oauth_token
self._public = public
self._timeout = timeout

@property
def public(self):
Expand Down Expand Up @@ -254,7 +260,7 @@ def _get_repo_content(self, repo):
r=repo,
)
headers = {'Authorization': self._token}
res = requests.get(url, headers=headers)
res = requests.get(url, headers=headers, timeout=self._timeout)

res.raise_for_status()
return res.json()
Expand Down Expand Up @@ -364,7 +370,7 @@ def delete_release(self, repo, version):
'Deleting release %s/%s, v:%s',
self._organization, repo, version)

r = requests.delete(url, headers=headers)
r = requests.delete(url, headers=headers, timeout=self._timeout)

if r.status_code != requests.codes.ok:

Expand Down Expand Up @@ -397,22 +403,23 @@ def publish_repo(self, repo):
"Authorization": "Bearer {}".format(self._oauth_token)
}
self.logger.info("Publishing repository %s", repo)
r = requests.post(url, headers=headers, json=data)
r = requests.post(url, headers=headers, json=data, timeout=self._timeout)
if r.status_code != requests.codes.ok:
msg = get_error_msg(r)
self.logger.error("Publishing repository: %s", msg)
raise QuayPackageError(msg)


def get_cnr_api_version():
def get_cnr_api_version(timeout):
"""Returns quay's CNR api version
:param int timeout: timeout for Quay requests in seconds
:raises: HTTPError if version cannot be fetched
:rtype: str
:returns: API version
"""
url = "https://quay.io/cnr/version"
r = requests.get(url)
r = requests.get(url, timeout=timeout)
r.raise_for_status()
return r.json()['cnr-api']

Expand Down
7 changes: 6 additions & 1 deletion omps/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ class Config(object):
'type': dict,
'default': {},
'desc': 'Configuration of organizations'
}
},
'request_timeout': {
'type': int,
'default': None,
'desc': 'Timeout in seconds for Koji and Quay requests'
},
}

def __init__(self, conf_section_obj):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_quay.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,4 @@ class ConfClass:
@pytest.mark.usefixtures('mocked_quay_version')
def test_get_cnr_api_version():
"""Tests of quay.get_cnr_api_version function"""
assert get_cnr_api_version() == "0.0.1-test"
assert get_cnr_api_version(0) == "0.0.1-test"

0 comments on commit bc410eb

Please sign in to comment.