-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
303 lines (248 loc) · 8.52 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import os
import subprocess # nosec
import threading
from typing import (
Any,
Dict,
Optional,
)
import requests_unixsocket # type: ignore
import sentry_sdk
import werkzeug
import woodchips
from dotenv import load_dotenv
from flask import (
Flask,
abort,
request,
)
from api import Api
from config import Config
from deployments import Deployment
from errors import HarveyError
from locks import (
lock_project,
retrieve_lock,
unlock_project,
)
from repos.deployments import (
retrieve_deployment,
retrieve_deployments,
)
from repos.locks import retrieve_locks
from repos.projects import retrieve_projects
from repos.webhooks import retrieve_webhook
from utils.utils import setup_logger
load_dotenv() # Must remain at the top of this file
APP = Flask(__name__)
REQUIRED_DOCKER_COMPOSE_VERSION = 'v2'
# Fixes segfaults on macOS, see the following for more details:
# - https://bugs.python.org/issue30385
# - https://github.com/unbit/uwsgi/issues/1722
# - https://github.com/Justintime50/harvey/issues/72
os.environ["no_proxy"] = "*"
def create_response_dict(message: str, success: Optional[bool] = False, status_code: Optional[int] = 500):
"""Response object that all Harvey responses return."""
return {
'message': message,
'success': success,
}, status_code
def log_error(error):
"""Logs an error with Woodchips."""
logger = woodchips.get(Config.logger_name)
logger.error(error)
@APP.errorhandler(401)
def not_authorized(error):
"""Return a 401 if the request is not authorized."""
return create_response_dict(
message='You are not authorized to access this endpoint. Please check your credentials and try again.',
success=False,
status_code=401,
)
@APP.errorhandler(404)
def not_found(error) -> Dict[str, Any]:
"""Return a 404 if the route is not found."""
return create_response_dict(
message='The endpoint you hit is either not valid or the record was not found.',
success=False,
status_code=404,
)
@APP.errorhandler(500)
def server_error(error) -> Dict[str, Any]:
"""Return a 500 if there is a problem with Harvey."""
return create_response_dict(
message='An error has occured due to something on our end.',
success=False,
status_code=500,
)
@APP.route('/health', methods=['GET'])
def harvey_healthcheck_endpoint():
"""Return a 200 if Harvey is running."""
return create_response_dict(
message='Ok',
success=True,
status_code=200,
)
@APP.route('/deployments', methods=['GET'])
@Api.check_api_key
def retrieve_deployments_endpoint():
"""Retrieves deployments from the Sqlite store.
- The keys will be `username-repo_name-commit_id`.
- The user can optionally pass a URL param of `page_size` to limit how many results are returned
- The user can optionally pass a URL param of `project` to filter what deployments get returned
"""
try:
return retrieve_deployments(request)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/deployments/<deployment_id>', methods=['GET'])
@Api.check_api_key
def retrieve_deployment_endpoint(deployment_id: str):
"""Retrieve a deployment's details by ID.
A `deployment_id` will be `username-repo_name-commit_id`.
"""
try:
return retrieve_deployment(deployment_id)
except Exception as error:
log_error(error)
return abort(500)
# Notably, we do not check the API key here because we'll check its presence later when we parse the webhook
@APP.route('/deploy', methods=['POST'])
def deploy_project_endpoint():
"""Deploy a project based on webhook data and the `docker-compose.yml` file.
This is the main entrypoint for Harvey.
"""
try:
return Api.parse_github_webhook(request)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/projects', methods=['GET'])
@Api.check_api_key
def retrieve_projects_endpoint():
"""Retrieves a list of project names from the git repos stored in Harvey."""
try:
return retrieve_projects(request)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/projects/<project_name>/redeploy', methods=['POST'])
@Api.check_api_key
def redeploy_project_endpoint(project_name):
"""Redeploy a project based on local webhook data stored in the database from a previous deploy."""
try:
webhook = retrieve_webhook(project_name)
if not webhook:
raise HarveyError(f'Webhook does not exist for {project_name}')
else:
threading.Thread(
name=project_name,
target=Deployment.run_deployment,
args=(webhook,),
).start()
return create_response_dict(
f'Redeploying {project_name}...',
success=True,
status_code=200,
)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/projects/<project_name>/webhook', methods=['GET'])
@Api.check_api_key
def retrieve_project_webhook_endpoint(project_name):
"""Retrieves the locally stored webhook of a project."""
try:
webhook = retrieve_webhook(project_name)
if not webhook:
return abort(404) # This will throw an exception in the except block below
else:
return create_response_dict(
webhook,
success=True,
status_code=200,
)
except werkzeug.exceptions.NotFound:
raise
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/projects/<project_name>/lock', methods=['PUT'])
@Api.check_api_key
def lock_project_endpoint(project_name: str):
"""Enables the deployment lock for a project."""
try:
return lock_project(project_name)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/projects/<project_name>/unlock', methods=['PUT'])
@Api.check_api_key
def unlock_project_endpoint(project_name: str):
"""Disables the deployment lock for a project."""
try:
return unlock_project(project_name)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/locks', methods=['GET'])
@Api.check_api_key
def retrieve_locks_endpoint():
"""Retrieves the list of locks"""
try:
return retrieve_locks(request)
except Exception as error:
log_error(error)
return abort(500)
@APP.route('/locks/<project_name>', methods=['GET'])
@Api.check_api_key
def retrieve_lock_endpoint(project_name: str):
"""Retrieves the lock status of a project by its name."""
try:
return retrieve_lock(project_name)
except Exception as error:
log_error(error)
return abort(404)
@APP.route('/threads', methods=['GET'])
@Api.check_api_key
def retrieve_threads_endpoint():
"""Retrieves a list of running threads for Harvey. Threads indicate ongoing deployments."""
threads = []
for thread in threading.enumerate():
threads.append(thread.name)
return {'threads': threads}
def bootstrap(debug_mode):
"""Bootstrap the Harvey instance on startup.
Any task required for Harvey to work that only needs to be instantiated once should go here.
"""
setup_logger()
# Ensure the correct Docker Compose version is available
docker_compose_version = subprocess.check_output( # nosec
['docker-compose', '--version'],
stderr=subprocess.STDOUT,
text=True,
timeout=3,
)
if REQUIRED_DOCKER_COMPOSE_VERSION not in docker_compose_version:
raise HarveyError(f'Harvey requires Docker Compose {REQUIRED_DOCKER_COMPOSE_VERSION}.')
# Setup Sentry
if Config.sentry_url:
sentry_sdk.init(
Config.sentry_url,
debug=debug_mode,
)
# Setup the directory for the SQLite databases
if not os.path.exists(Config.database_path):
os.makedirs(Config.database_path)
# Allows us to use requests_unixsocket via requests
requests_unixsocket.monkeypatch()
if __name__ == '__main__':
# These tasks take place when run via Flask
debug_mode = Config.log_level == 'DEBUG'
bootstrap(debug_mode)
APP.run(host=Config.host, port=Config.port, debug=debug_mode)
if __name__ != '__main__':
# These tasks take place when run via uWSGI, the remaining config can be found in `wsgi.py`
debug_mode = Config.log_level == 'DEBUG'
bootstrap(debug_mode)