Skip to content

Commit

Permalink
Added retrieve all pipelines API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Aug 10, 2020
1 parent 1894a26 commit c23214b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 25 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 21 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=W0511,W0612
import json
import os
import time
import hmac
import hashlib
# import multiprocessing
Expand All @@ -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"""
Expand Down Expand Up @@ -56,7 +61,7 @@ def decode_webhook(data, signature):
return hmac.compare_digest('sha1=' + mac.hexdigest(), signature)


@API.route('/pipeline/<pipeline_id>', methods=['GET'])
@API.route('/pipelines/<pipeline_id>', methods=['GET'])
def retrieve_pipeline(pipeline_id):
"""Retrieve a pipeline's logs by ID"""
file = f'{pipeline_id}.log'
Expand All @@ -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"""
Expand Down
16 changes: 11 additions & 5 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"""Create a container"""
# container = harvey.Container.create(
# config = {
# "Image": "lala",
# "Image": "lala",
# }
# )
# print(container, "created")
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions harvey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 25 additions & 19 deletions harvey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit c23214b

Please sign in to comment.