From 4c336ec7147cb5b9704318139660d7eb699c427d Mon Sep 17 00:00:00 2001 From: Mitsuo Takaki Date: Sun, 18 Mar 2018 22:17:29 -0700 Subject: [PATCH] Running some manual tests and updating Dockerfile --- Dockerfile | 9 ++++++++- cachet_url_monitor/configuration.py | 25 +++++++++++++++++++++---- dev_requirements.txt | 6 +++--- docker-compose.yml | 6 ------ requirements.txt | 6 +++--- 5 files changed, 35 insertions(+), 17 deletions(-) delete mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile index 58ada48..83f407d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,13 @@ -FROM python:2-onbuild +FROM python:2.7-alpine MAINTAINER Mitsuo Takaki +WORKDIR /usr/src/app + +COPY requirements.txt /usr/src/app/ +RUN pip install --no-cache-dir -r requirements.txt + +COPY cachet_url_monitor/* /usr/src/app/cachet_url_monitor/ + COPY config.yml /usr/src/app/config/ VOLUME /usr/src/app/config/ diff --git a/cachet_url_monitor/configuration.py b/cachet_url_monitor/configuration.py index f33d2b8..0340e2c 100644 --- a/cachet_url_monitor/configuration.py +++ b/cachet_url_monitor/configuration.py @@ -41,6 +41,16 @@ class ComponentNonexistentError(Exception): return repr('Component with id [%d] does not exist.' % (self.component_id,)) +class MetricNonexistentError(Exception): + """Exception raised when the component does not exist.""" + + def __init__(self, metric_id): + self.metric_id = metric_id + + def __str__(self): + return repr('Metric with id [%d] does not exist.' % (self.metric_id,)) + + def get_current_status(endpoint_url, component_id, headers): """Retrieves the current status of the component that is being monitored. It will fail if the component does not exist or doesn't respond with the expected data. @@ -89,7 +99,10 @@ class Configuration(object): self.api_url = os.environ.get('CACHET_API_URL') or self.data['cachet']['api_url'] self.component_id = os.environ.get('CACHET_COMPONENT_ID') or self.data['cachet']['component_id'] self.metric_id = os.environ.get('CACHET_METRIC_ID') or self.data['cachet'].get('metric_id') - self.default_metric_value = self.get_default_metric_value() + + if self.metric_id is not None: + self.default_metric_value = self.get_default_metric_value(self.metric_id) + # The latency_unit configuration is not mandatory and we fallback to seconds, by default. self.latency_unit = os.environ.get('LATENCY_UNIT') or self.data['cachet'].get('latency_unit') or 's' @@ -105,10 +118,14 @@ class Configuration(object): for expectation in self.expectations: self.logger.info('Registered expectation: %s' % (expectation,)) - def get_default_metric_value(self): + def get_default_metric_value(self, metric_id): """Returns default value for configured metric.""" - get_metric_request = requests.get('%s/metrics/%s' % (self.api_url, self.metric_id), headers=self.headers) - return get_metric_request.json()['data']['default_value'] + get_metric_request = requests.get('%s/metrics/%s' % (self.api_url, metric_id), headers=self.headers) + + if get_metric_request.ok: + return get_metric_request.json()['data']['default_value'] + else: + raise MetricNonexistentError(metric_id) def get_action(self): """Retrieves the action list from the configuration. If it's empty, returns an empty list. diff --git a/dev_requirements.txt b/dev_requirements.txt index f9e857c..cbe1a52 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,9 +1,9 @@ -PyYAML==3.11 +PyYAML==3.12 codacy-coverage==1.2.18 ipython==4.2.0 mock==2.0.0 pudb==2016.1 pytest==3.4.2 pytest-cov==2.5.1 -requests==2.9.1 -schedule==0.3.2 +requests==2.18.4 +schedule==0.5.0 diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 511cde9..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,6 +0,0 @@ -version: '2' -services: - monitor: - build: . - volumes: - - .:/usr/src/app/ diff --git a/requirements.txt b/requirements.txt index 37bcb9b..afb165a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -PyYAML==3.11 -requests==2.9.1 -schedule==0.3.2 +PyYAML==3.12 +requests==2.18.4 +schedule==0.5.0