Skip to content

Commit 9df1302

Browse files
EvaBardouEvaBardou
andauthored
Add Dockerflow to the backend application (#1277)
* Add Dockerflow to the backend application * Update version.json before taskboot build * Fix CI * Apply Bastien's suggestions + Add tests Co-authored-by: EvaBardou <ebardou@teklia.com>
1 parent 9ab85a3 commit 9df1302

File tree

6 files changed

+99
-15
lines changed

6 files changed

+99
-15
lines changed

.taskcluster.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,11 @@ tasks:
291291
privileged: true
292292
maxRunTime: 3600
293293
image: "${taskboot_image}"
294-
env:
295-
GIT_REPOSITORY: ${repository}
296-
GIT_REVISION: ${head_rev}
297294
command:
298-
- taskboot
299-
- build
300-
- --image
301-
- mozilla/code-review
302-
- --tag
303-
- "${channel}"
304-
- --tag
305-
- "${head_rev}"
306-
- --write
307-
- /backend.tar
308-
- backend/Dockerfile
295+
- /bin/sh
296+
- -lxce
297+
- "git clone --quiet ${repository} /code-review && cd /code-review && git checkout ${head_rev} -b build
298+
&& backend/build.sh ${head_rev} ${head_branch} ${repository} ${channel}"
309299
artifacts:
310300
public/code-review-backend.tar:
311301
expires: {$fromNow: '2 weeks'}

backend/build.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
COMMIT_SHA=$1
3+
VERSION=$2
4+
SOURCE=$3
5+
CHANNEL=$4
6+
7+
# Create a version.json per https://github.com/mozilla-services/Dockerflow/blob/master/docs/version_object.md
8+
printf '{"commit": "%s", "version": "%s", "source": "%s", "build": "%s"}\n' \
9+
"$COMMIT_SHA" \
10+
"$VERSION" \
11+
"$SOURCE" \
12+
"${TASKCLUSTER_ROOT_URL}/tasks/${TASK_ID}" > backend/code_review_backend/version.json
13+
14+
# Run 'taskboot build' with our local copy of the Git repository where we updated the version.json with correct values.
15+
# To do so, we use '--target /path/to/existing/clone' instead of passing environment variables (GIT_REPOSITORY, GIT_REVISION)
16+
# to taskboot that would activate an automated clone.
17+
taskboot --target /code-review build --image mozilla/code-review --tag "$CHANNEL" --tag "$COMMIT_SHA" --write /backend.tar backend/Dockerfile

backend/code_review_backend/app/settings.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"django.contrib.staticfiles",
5959
"corsheaders",
6060
"rest_framework",
61+
"dockerflow.django",
6162
"code_review_backend.issues",
6263
"drf_yasg",
6364
]
@@ -71,6 +72,7 @@
7172
"django.contrib.auth.middleware.AuthenticationMiddleware",
7273
"django.contrib.messages.middleware.MessageMiddleware",
7374
"django.middleware.clickjacking.XFrameOptionsMiddleware",
75+
"dockerflow.django.middleware.DockerflowMiddleware",
7476
]
7577

7678
ROOT_URLCONF = "code_review_backend.app.urls"
@@ -181,10 +183,27 @@
181183
LOGGING = {
182184
"version": 1,
183185
"disable_existing_loggers": False,
184-
"handlers": {"console": {"class": "logging.StreamHandler"}},
186+
"formatters": {
187+
"json": {
188+
"()": "dockerflow.logging.JsonLogFormatter",
189+
"logger_name": "code_review_backend",
190+
},
191+
},
192+
"handlers": {
193+
"console": {"class": "logging.StreamHandler"},
194+
"json": {
195+
"class": "logging.StreamHandler",
196+
"formatter": "json",
197+
"level": "DEBUG",
198+
},
199+
},
185200
"loggers": {
186201
"django": {"handlers": ["console"], "level": "INFO"},
187202
"code_review_backend": {"handlers": ["console"], "level": "INFO"},
203+
"request.summary": {
204+
"handlers": ["json"],
205+
"level": "DEBUG",
206+
},
188207
},
189208
}
190209

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding: utf-8 -*-
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this
4+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
6+
import json
7+
8+
from django.conf import settings
9+
from django.test import TestCase
10+
11+
12+
class DockerflowEndpointsTestCase(TestCase):
13+
def setUp(self):
14+
self.old_setting = settings.DEBUG
15+
16+
def tearDown(self):
17+
settings.DEBUG = self.old_setting
18+
19+
def test_get_version(self):
20+
response = self.client.get("/__version__")
21+
self.assertEqual(response.status_code, 200)
22+
23+
with open(f"{settings.BASE_DIR}/version.json", "r") as version_file:
24+
self.assertEqual(response.json(), json.loads(version_file.read()))
25+
26+
def test_get_heartbeat_debug(self):
27+
settings.DEBUG = True
28+
29+
response = self.client.get("/__heartbeat__")
30+
self.assertEqual(response.status_code, 200)
31+
32+
# In DEBUG mode, we can retrieve checks details
33+
heartbeat = response.json()
34+
self.assertEqual(heartbeat["status"], "ok")
35+
self.assertTrue("checks" in heartbeat)
36+
self.assertTrue("details" in heartbeat)
37+
38+
def test_get_heartbeat(self):
39+
settings.DEBUG = False
40+
41+
response = self.client.get("/__heartbeat__")
42+
self.assertEqual(response.status_code, 200)
43+
44+
# When DEBUG is False, we can't retrieve checks details and the status is certainly
45+
# equal to "warning" because of the deployment checks that are added:
46+
# https://github.com/mozilla-services/python-dockerflow/blob/e316f0c5f0aa6d176a6d08d1f568f83658b51339/src/dockerflow/django/views.py#L45
47+
self.assertEqual(response.json(), {"status": "warning"})
48+
49+
def test_get_lbheartbeat(self):
50+
response = self.client.get("/__lbheartbeat__")
51+
self.assertEqual(response.status_code, 200)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commit": "stub",
3+
"version": "stub",
4+
"source": "https://github.com/mozilla/code-review",
5+
"build": "https://tools.taskcluster.net/task-inspector/#XXXXXXXXXXXXXXXXXX"
6+
}

backend/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ dj-database-url==1.0.0
33
Django==4.1
44
django-cors-headers==3.13.0
55
djangorestframework==3.13.1
6+
dockerflow==2022.7.0
67
drf-yasg==1.21.3
78
gunicorn==20.1.0
89
psycopg2-binary==2.9.3

0 commit comments

Comments
 (0)