Perform a small refactor

This commit is contained in:
Scott Wallace 2020-10-25 09:12:23 +00:00
parent 0cb79d734a
commit beb470fe4b
3 changed files with 6 additions and 9 deletions

View file

@ -1,4 +1,3 @@
# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim-buster FROM python:3.8-slim-buster
# Keeps Python from generating .pyc files in the container # Keeps Python from generating .pyc files in the container
@ -7,7 +6,6 @@ ENV PYTHONDONTWRITEBYTECODE 1
# Turns off buffering for easier container logging # Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1 ENV PYTHONUNBUFFERED 1
# Install pip requirements
ADD requirements.txt . ADD requirements.txt .
RUN python -m pip install -r requirements.txt RUN python -m pip install -r requirements.txt
@ -15,13 +13,11 @@ WORKDIR /app
COPY alertify.py /app COPY alertify.py /app
COPY src /app/src COPY src /app/src
# Switching to a non-root user, please refer to https://aka.ms/vscode-docker-python-user-rights
RUN useradd appuser && chown -R appuser /app RUN useradd appuser && chown -R appuser /app
USER appuser USER appuser
EXPOSE 8080 EXPOSE 8080
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "alertify.py"] CMD ["python", "alertify.py"]
HEALTHCHECK --interval=30s --timeout=3s --retries=1 \ HEALTHCHECK --interval=30s --timeout=3s --retries=1 \

View file

@ -56,8 +56,6 @@ if __name__ == '__main__':
args = parse_cli() args = parse_cli()
logging.info('Version: %s', alertify.__version__)
# forwarder = alertify.Alertify(args.config) # forwarder = alertify.Alertify(args.config)
forwarder = alertify.Alertify() forwarder = alertify.Alertify()
@ -69,6 +67,8 @@ if __name__ == '__main__':
# Invert the sense of 'healthy' for Unix CLI usage # Invert the sense of 'healthy' for Unix CLI usage
return not forwarder.healthcheck.report() return not forwarder.healthcheck.report()
logging.info('Version: %s', alertify.__version__)
if forwarder.config.verbose: if forwarder.config.verbose:
logging.debug('Parsed config:') logging.debug('Parsed config:')
for key, val in forwarder.config.items(): for key, val in forwarder.config.items():

View file

@ -4,6 +4,7 @@ Module to handle communication with the Gotify server
import http.client import http.client
import json import json
import logging import logging
import socket
class Gotify: class Gotify:
@ -35,8 +36,8 @@ class Gotify:
try: try:
self.api.request(method, url, body=body, headers=headers) self.api.request(method, url, body=body, headers=headers)
response = self.api.getresponse() response = self.api.getresponse()
except ConnectionRefusedError as error: except (ConnectionRefusedError, socket.gaierror) as error:
logging.error(error) logging.error('Connection error: %s', error)
return { return {
'status': error.errno, 'status': error.errno,
'reason': error.strerror, 'reason': error.strerror,
@ -52,7 +53,7 @@ class Gotify:
try: try:
resp_obj['json'] = json.loads(rawbody.decode()) resp_obj['json'] = json.loads(rawbody.decode())
except json.decoder.JSONDecodeError as error: except json.decoder.JSONDecodeError as error:
logging.error(error) logging.error('Could not parse JSON: %s', error)
logging.debug('Returned from Gotify:\n%s', json.dumps(resp_obj, indent=2)) logging.debug('Returned from Gotify:\n%s', json.dumps(resp_obj, indent=2))
logging.debug('Status: %s, Reason: %s', resp_obj['status'], resp_obj['reason']) logging.debug('Status: %s, Reason: %s', resp_obj['status'], resp_obj['reason'])