From c23214b3ef235047710fba75851cdaf7c3ba8542 Mon Sep 17 00:00:00 2001 From: Justintime50 Date: Mon, 10 Aug 2020 00:12:30 -0600 Subject: [PATCH] Added retrieve all pipelines API endpoint --- CHANGELOG.md | 6 ++++++ app.py | 22 +++++++++++++++++++++- examples.py | 16 +++++++++++----- harvey/__init__.py | 1 + harvey/utils.py | 44 +++++++++++++++++++++++++------------------- 5 files changed, 64 insertions(+), 25 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7bac84e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +## CHANGELOG + +### v... (2020-08-09) + +* Added the ability to retrieve a list of pipeline ID's and their timestamps +* Restructured `utils` file classes and method names diff --git a/app.py b/app.py index 59866e9..fbadfce 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ # pylint: disable=W0511,W0612 import json import os +import time import hmac import hashlib # import multiprocessing @@ -17,6 +18,10 @@ DEBUG = os.getenv('DEBUG', 'True') +# TODO: Move all of the logic out of this file and into Harvey itself +# This file should only route requests to the proper functions + + @API.route('/harvey', methods=['POST']) def receive_webhook(): """Receive a Webhook - this is the entrypoint for Harvey""" @@ -56,7 +61,7 @@ def decode_webhook(data, signature): return hmac.compare_digest('sha1=' + mac.hexdigest(), signature) -@API.route('/pipeline/', methods=['GET']) +@API.route('/pipelines/', methods=['GET']) def retrieve_pipeline(pipeline_id): """Retrieve a pipeline's logs by ID""" file = f'{pipeline_id}.log' @@ -67,6 +72,21 @@ def retrieve_pipeline(pipeline_id): return response return abort(404) + +@API.route('/pipelines', methods=['GET']) +def retrieve_pipelines(): + """Retrieve a list of pipelines""" + # TODO: 1) Do not expose log paths here + # TODO: 2) Replace this with retrieving from a DB instead + pipelines = {'pipelines': []} + for root, dirs, files in os.walk(harvey.Global.PROJECTS_LOG_PATH, topdown=True): + for file in files: + timestamp = time.ctime(os.path.getmtime(os.path.join(root, file))) + pipelines['pipelines'].append( + f'{timestamp}: {os.path.join(root, file)}') + return json.dumps(pipelines, indent=4) + + # @API.route('/containers/create', methods=['POST']) # def create_container(): # """Create a Docker container""" diff --git a/examples.py b/examples.py index 5154075..3a04330 100644 --- a/examples.py +++ b/examples.py @@ -32,7 +32,7 @@ """Create a container""" # container = harvey.Container.create( # config = { -# "Image": "lala", +# "Image": "lala", # } # ) # print(container, "created") @@ -83,10 +83,16 @@ # ) """API Entrypoint (Webhook)""" -with open('./git_webhook.json', 'r') as file: - request = requests.post('http://127.0.0.1:5000/harvey', data=file, headers=harvey.Global.JSON_HEADERS) - print(request) +# with open('./git_webhook.json', 'r') as file: +# request = requests.post('http://127.0.0.1:5000/harvey', data=file, headers=harvey.Global.JSON_HEADERS) +# print(request) """Retrieve a Pipeline by ID""" -# request = requests.get('http://127.0.0.1:5000/pipeline/f599cde2f2a0ad562bb7982328fe0aeee9d22b1c', headers=harvey.Global.JSON_HEADERS) +# request = requests.get( +# 'http://127.0.0.1:5000/pipelines/f599cde2f2a0ad562bb7982328fe0aeee9d22b1c', headers=harvey.Global.JSON_HEADERS) # print(request.text) + +"""Retrieve all pipelines""" +request = requests.get( + 'http://127.0.0.1:5000/pipelines', headers=harvey.Global.JSON_HEADERS) +print(request.text) diff --git a/harvey/__init__.py b/harvey/__init__.py index 60e9548..54b3fce 100644 --- a/harvey/__init__.py +++ b/harvey/__init__.py @@ -6,4 +6,5 @@ from .messages import Message from .pipeline import Pipeline from .stage import Stage +from .utils import Utils, Logs from .webhook import Webhook diff --git a/harvey/utils.py b/harvey/utils.py index 2d36aad..6bb725a 100644 --- a/harvey/utils.py +++ b/harvey/utils.py @@ -7,10 +7,32 @@ class Utils(): - """Harvey Utilities used throughout the program""" + """Harvey Utilities used throughout the program + """ @classmethod - def project_logs(cls, final_output, webhook): - """Generate log file with all output""" + def kill(cls, final_output, webhook): + """A kill util to write everything to logs, send messages, + tear down Docker stuff, and quit + """ + Logs.generate_logs(final_output, webhook) + Message.slack(final_output) + sys.exit() + + @classmethod + def success(cls, final_output, webhook): + """Log output and send message on pipeline success + """ + Logs.generate_logs(final_output, webhook) + Message.slack(final_output) + + +class Logs(): + """Harvey pipeline log logic + """ + @classmethod + def generate_logs(cls, final_output, webhook): + """Generate log file with all output + """ if not os.path.exists(os.path.join(Global.PROJECTS_LOG_PATH, Global.repo_full_name(webhook))): os.makedirs(os.path.join(Global.PROJECTS_LOG_PATH, @@ -24,19 +46,3 @@ def project_logs(cls, final_output, webhook): final_output = 'Error: Harvey could not save the log file.' print(os_error) Utils.kill(final_output, webhook) - - @classmethod - def kill(cls, final_output, webhook): - """ - A kill util to write everything to logs, send messages, - tear down Docker stuff, and quit - """ - Utils.project_logs(final_output, webhook) - Message.slack(final_output) - sys.exit() - - @classmethod - def success(cls, final_output, webhook): - """Log output and send message on pipeline success""" - Utils.project_logs(final_output, webhook) - Message.slack(final_output)