Skip to content

Commit dd96de9

Browse files
MarceloRobertnuclearcatgustavobtflores
authored
Feature: new deploy setup (#1751)
* fix: correct default for trees file If the ingester was initialized with an empty trees_file argument it was not defaulting correctly since None was passed to the function * docker-compose: Add dashboard ingester as separate service The ingester is used to insert data into the database, so we always want it running smoothly in the project while also not consuming resources or having its resources consumed by the backend as if it were just a cron job Also fixes signal handling when running the ingester command within docker and avoids running cronjobs in the new service. Co-authored-by: Marcelo Robert Santos <marcelo.santos@profusion.mobi> * feat: add pending items processing as a docker service Follows the addition of the ingester service. This new service will consume the items generated by the ingester aggregation and organize them into the listing tables. It is done as a new service instead of a cronjob in order to avoid over-execution and multiple processes being started Also adds a dockerignore file to avoid building cache folder/files and the folder for submission testing * docker-compose: Remove cloud-sql Removed old configuration for Google Cloud because it has been replaced with Azure database Co-authored-by: Marcelo Robert Santos <marcelo.santos@profusion.mobi> * chore: remove gcloud from docs * feat: update trees file with ingester startup Replaces the use of a cronjob to update the tree-names file with updating it with the ingester. This also lets the command be called within the code. * refactor: separate commands containers further Gives each command their specific env file and also separates the backend entrypoint * refactor: simplify database env vars Reverts to using only a single set of variables for the database setup; Removes the json format of the database env vars * chore: add dashboard database setup script and initialization (#1750) * chore: add dashboard database setup script and initialization * refactor: update database user and name variables in setup scripts * fix: use correct image names in compose Corrects the image names setting the right variables for the images that can be pulled from kernelci/dashboard * refactor: consolidate database initialization into setup script --------- Co-authored-by: Denys Fedoryshchenko <denys.f@collabora.com> Co-authored-by: Gustavo Flores <gustavo.flores@profusion.mobi>
1 parent f433386 commit dd96de9

33 files changed

Lines changed: 512 additions & 331 deletions

.env.backend.example

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,21 @@ DEBUG_DB_VARS=${DEBUG_DB_VARS:-False}
1010
ENABLE_LOGGING=${ENABLE_LOGGING:-False} # Logs all prints
1111
DEBUG_SQL_QUERY=${DEBUG_SQL_QUERY:-False} # Logs the SQL queries made
1212

13-
DB_DEFAULT_PASSWORD_FILE=/run/secrets/postgres_password_secret
14-
DB_DEFAULT_HOST=cloudsql-proxy
15-
DB_DEFAULT_USER=${DB_DEFAULT_USER:-kernelci}
16-
DB_DEFAULT_NAME=${DB_DEFAULT_NAME:-kcidb}
17-
18-
DASH_DB_NAME=${DASH_DB_NAME:-dashboard}
19-
DASH_DB_USER=${DASH_DB_USER:-admin}
20-
DASH_DB_PASSWORD=${DASH_DB_PASSWORD:-admin}
21-
DASH_DB_HOST=dashboard_db
22-
DASH_DB_PORT=${DASH_DB_PORT:-5432}
23-
24-
# Tells the backend to use DASH_DB as the default database
25-
USE_DASHBOARD_DB=${USE_DASHBOARD_DB:-False}
13+
DB_NAME=${DB_DEFAULT_NAME:-dashboard}
14+
DB_USER=${DB_DEFAULT_USER:-admin}
15+
DB_PASSWORD=db_password
16+
DB_HOST=dashboard_db # Docker can't connect to the ssh tunnel host directly.
17+
DB_PORT=${DB_DEFAULT_PORT:-5432}
18+
DB_ENGINE=${DB_DEFAULT_ENGINE:-django.db.backends.postgresql}
19+
DB_OPTIONS_CONNECT_TIMEOUT=${DB_OPTIONS_CONNECT_TIMEOUT:-16}
2620

2721
# Check docs/monitoring.md docs for more context
2822
PROMETHEUS_METRICS_ENABLED=False
2923
PROMETHEUS_METRICS_PORT=8001
3024
PROMETHEUS_MULTIPROC_DIR=/tmp/metrics
25+
INGESTER_METRICS_PORT=8002
26+
27+
BACKEND_VOLUME_DIR=/volume_data
3128

3229
## Variables used for the notifications command. Check docs/notifications.md
3330
# EMAIL_HOST_USER="youruser@host" # (optional)

.env.db.example

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
POSTGRES_USER=${DASH_DB_USER:-admin}
2-
POSTGRES_PASSWORD=${DASH_DB_PASSWORD:-admin}
3-
POSTGRES_DB=${DASH_DB_NAME:-dashboard}
1+
POSTGRES_USER=${DB_USER:-admin}
2+
POSTGRES_PASSWORD=${DB_PASSWORD:-admin}
3+
POSTGRES_DB=${DB_NAME:-dashboard}
4+
5+
APP_DB_USER=${APP_DB_USER:-dashboard}
6+
APP_DB=${APP_DB:-dashboard}

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Top-level variables that should be used just for the docker-compose setup.
2+
INGESTER_METRICS_PORT=8002
3+
INGESTER_TREE_NAMES_FILE_DIR=../data
4+
INGESTER_SPOOL_DIR=../spool

.env.ingester.example

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
DEBUG=${DEBUG:-False}
2+
DEBUG_DB_VARS=${DEBUG_DB_VARS:-False}
3+
ENABLE_LOGGING=${ENABLE_LOGGING:-False} # Logs all prints
4+
DEBUG_SQL_QUERY=${DEBUG_SQL_QUERY:-False} # Logs the SQL queries made
5+
6+
DB_NAME=${DB_NAME:-dashboard}
7+
DB_USER=${DB_USER:-admin}
8+
DB_PASSWORD=db_password
9+
DB_HOST=${DB_HOST:-dashboard_db} # Docker can't connect to the ssh tunnel host directly.
10+
DB_PORT=${DB_PORT:-5432}
11+
DB_ENGINE=${DB_ENGINE:-django.db.backends.postgresql}
12+
DB_OPTIONS_CONNECT_TIMEOUT=16
13+
14+
BACKEND_VOLUME_DIR=/volume_data
15+
16+
# Check docs/monitoring.md docs for more context
17+
PROMETHEUS_METRICS_ENABLED=False
18+
PROMETHEUS_METRICS_PORT=8001
19+
PROMETHEUS_MULTIPROC_DIR=/tmp/metrics

.env.pending_aggregations.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
DEBUG=${DEBUG:-False}
2+
DEBUG_DB_VARS=${DEBUG_DB_VARS:-False}
3+
ENABLE_LOGGING=${ENABLE_LOGGING:-False} # Logs all prints
4+
DEBUG_SQL_QUERY=${DEBUG_SQL_QUERY:-False} # Logs the SQL queries made
5+
6+
DB_NAME=${DB_NAME:-dashboard}
7+
DB_USER=${DB_USER:-admin}
8+
DB_PASSWORD=db_password
9+
DB_HOST=${DB_HOST:-dashboard_db} # Docker can't connect to the ssh tunnel host directly.
10+
DB_PORT=${DB_PORT:-5432}
11+
DB_ENGINE=${DB_ENGINE:-django.db.backends.postgresql}
12+
DB_OPTIONS_CONNECT_TIMEOUT=16

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ application_default_credentials.json
33
.vscode
44
.env
55
.env.backend
6+
.env.ingester
67
.env.db
8+
.env.pending_aggregations
79
.env.proxy
810
.idea
911

README.md

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pnpm preview
5353

5454
Create a .env file in the base directory,
5555
```sh
56-
cp .env.backend.example .env.bakckend
56+
cp .env.backend.example .env.backend
5757
```
5858

5959
Create a secret key for Django:
@@ -62,32 +62,17 @@ export DJANGO_SECRET_KEY=$(openssl rand -base64 22)
6262
```
6363
We are not using sessions or anything like that right now, so changing the secret key won't be a big deal.
6464

65+
Since the production *database* is not open for the public, we use ssh tunneling with a whitelist to access it. This means that the docker setup currently can't access it, but we have a local database that is connected automatically if you don't change the env vars.
6566

66-
Add a `application_default_credentials.json` file with your ADC in the root of the project.
67-
```sh
68-
gcloud auth application-default login
69-
cp ~/.config/gcloud/application_default_credentials.json .
70-
```
71-
**Important**: Check the `application_default_credentials.json` file permissions with `ls -l` to see if docker has access to it.
72-
73-
After setting up your connection with Google Cloud with the following commands:
74-
75-
```sh
76-
cloud-sql-proxy kernelci-production:us-central1:postgresql2 &
77-
gcloud auth application-default login
78-
```
79-
80-
If it doesn't work, check the [Configure ADC with your Google Account](https://cloud.google.com/docs/authentication/provide-credentials-adc#google-idp) documentation.
81-
82-
Create a secret file with the database password:
67+
If you do use docker, you should create a secret file with the database password:
8368
```sh
8469
mkdir -p backend/runtime/secrets
8570
echo <password> > backend/runtime/secrets/postgres_password_secret
8671
```
8772

88-
If you are going to use a database user other than `kernelci`, set it to `DB_DEFAULT_USER`:
73+
If you are going to use a database user other than `kernelci`, set it to `DB_USER`:
8974
```sh
90-
export DB_DEFAULT_USER=<user>
75+
export DB_USER=<user>
9176
```
9277

9378
If you are setting up instance different than production KernelCI dashboard, you need to define CORS_ALLOWED_ORIGINS. On .env.backend:
@@ -119,17 +104,15 @@ Or you can also run the env exports and docker compose within the root user by r
119104
> Tip: you can create a quick script to set all the necessary envs and start the services. This will also allow docker to see the environment variables correclty. Example:
120105
121106
```sh
122-
export DB_DEFAULT_USER=email@email.com
107+
export DB_USER=email@email.com
123108
export DJANGO_SECRET_KEY=$(openssl rand -base64 22)
124-
export DB_DEFAULT_NAME=kcidb
109+
export DB_NAME=kcidb
125110
export DISCORD_WEBHOOK_URL="https://discord.com/api/webhooks/..."
126111

127112
docker compose up --build
128113
```
129114

130-
> [Note] If you are going to run using only the local database, the DB_DEFAULT_NAME should be `dashboard` and the `DB_DEFAULT_USER` and `DB_DEFAULT_PASSWORD` should be `admin` (for now).
131-
> After you define those values, also set the env var `USE_DASHBOARD_DB` to True, setting the local database as the default one.
132-
> You could also set the DB_DEFAULT variables to point to the local database and leave `USE_DASHBOARD_DB` as False.
115+
> [Note] If you are going to run using only the local database, the DB_NAME should be `dashboard` and the `DB_USER` and `DB_PASSWORD` should be `admin` (for now). This simply follows what is going to be setup by the `dashboard_db` service on docker compose.
133116
134117

135118
## Deploying to production

backend/Dockerfile

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ FROM alpine:3.20 as backend-base
66
WORKDIR /
77

88
ENV POETRY_HOME=/opt/poetry
9+
ENV PYTHONOPTIMIZE=${PYTHONOPTIMIZE:-2}
10+
ENV PYTHONUNBUFFERED=${PYTHONUNBUFFERED:-1}
911

1012
RUN apk update \
1113
&& apk add --no-cache \
@@ -21,10 +23,9 @@ RUN apk update \
2123
&& $POETRY_HOME/bin/pip install poetry==1.8.3 \
2224
&& ln -s $POETRY_HOME/bin/poetry /bin/poetry
2325

24-
FROM backend-base
25-
COPY . /backend
26-
COPY ./utils/docker/entrypoint.sh /entrypoint.sh
26+
FROM backend-base as kernelci-django-backend
2727
WORKDIR /backend
28+
COPY . /backend
2829

2930
ARG INSTALL_DEV_DEPS=false
3031
RUN if [ "$INSTALL_DEV_DEPS" = "true" ]; then \
@@ -36,20 +37,8 @@ RUN if [ "$INSTALL_DEV_DEPS" = "true" ]; then \
3637
ENV DJANGO_APP="kernelCI"
3738

3839
# Precompile Python modules
39-
RUN O='0 1 2' \
40-
PY_MAJMIN=`poetry run python -c "import sys; print('%s.%s'%sys.version_info[0:2])"` \
41-
PY_D=`poetry env info --path` \
42-
D="$PY_D $DJANGO_APP"; \
43-
for N in $O; do \
44-
echo "compile python $PY_MAJMIN byte code at -O$N: $D"; \
45-
PYTHONOPTIMIZE=$N poetry run python -m compileall -q $D || exit 1; \
46-
done
47-
48-
# Expose both application and metrics ports
49-
EXPOSE 8000 8001
50-
51-
# gunicorn is added here but is run at the end of the entrypoint.
52-
# This was done here such that we can run a custom command
53-
# in the backend container without using gunicorn
54-
CMD ["gunicorn", "kernelCI.wsgi:application", "--workers=5", "--forwarded-allow-ips=*", "--bind=0.0.0.0:8000", "--timeout=250"]
55-
ENTRYPOINT ["/entrypoint.sh"]
40+
RUN PY_MAJMIN=`poetry run python -c "import sys; print('%s.%s'%sys.version_info[0:2])"` \
41+
PY_D=`poetry env info --path` \
42+
D="$PY_D $DJANGO_APP"; \
43+
echo "compile python $PY_MAJMIN byte code at $D"; \
44+
poetry run python -m compileall -q $D || exit 1;

backend/README.md

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,44 +41,28 @@ It's possible to export `DEBUG_SQL_QUERY=True` if you want to see which SQL quer
4141

4242
### Databases
4343

44-
For the main database, the backend uses a `DB_DEFAULT` variable that must have a JSON string such as:
44+
For the main database, the backend uses a series of `DB_` environment variables that have to be set such as:
4545

4646
```sh
47-
DB_DEFAULT="{
48-
\"ENGINE\": \"${DB_DEFAULT_ENGINE:=django_prometheus.db.backends.postgresql}\",
49-
\"NAME\": \"${DB_DEFAULT_NAME:=kcidb}\",
50-
\"USER\": \"${DB_DEFAULT_USER:=<your-email-here>}\",
51-
\"PASSWORD\": \"<your-password-here-don't-forget-to-scape-special-characters>\",
52-
\"HOST\": \"${DB_DEFAULT_HOST:=127.0.0.1}\",
53-
\"PORT\": \"${DB_DEFAULT_PORT:=5432}\",
54-
\"CONN_MAX_AGE\": ${DB_DEFAULT_CONN_MAX_AGE:=null},
55-
\"OPTIONS\": {
56-
\"connect_timeout\": ${DB_DEFAULT_TIMEOUT:=10}
57-
}
58-
}"
47+
export DB_NAME=kcidb
48+
export DB_USER=your-email-here
49+
export DB_PASSWORD=your-password-here-dont-forget-to-scape-special-characters
50+
export DB_HOST=127.0.0.1
51+
export DB_PORT=5432
52+
export DB_ENGINE=django_prometheus.db.backends.postgresql
53+
export DB_OPTIONS_CONNECT_TIMEOUT=16
5954
```
6055
> [!NOTE]
6156
> It is possible to have authentication issues when escaping special characters. In some cases, it is necessary to add more than one backslash, while in others, no addition is needed. To assist with this, you can export `DEBUG_DB_VARS=True` to check the database connection info in the terminal, allowing you to determine if the characters got escaped as intended. **This variable should NOT be set to True in production**.
6257

63-
Along with the main database, the backend also connects to a secondary, local db made while we transition between database providers. It uses a simpler environment variable structure:
64-
```sh
65-
DASH_DB_NAME=dashboard
66-
DASH_DB_USER=admin
67-
DASH_DB_PASSWORD=admin
68-
DASH_DB_HOST=127.0.0.1
69-
DASH_DB_PORT=5434 # Note that this is not 5432, avoids conflict with the proxy port.
70-
```
71-
72-
If you have both connections and you want to use the local one as the default, the env var `USE_DASHBOARD_DB` can be exported as `True` for a quick flag change.
73-
7458
#### SQLite
7559

7660
Before running the server, you must also update a local SQLite database used for caching. You can simply run the [migrate-cache-db.sh](./migrate-cache-db.sh) script, which will update the migrations if needed and apply them.
7761

7862

7963
## Running the server
8064

81-
After connecting to the database proxy, execute the server with:
65+
After connecting to the database, execute the server with:
8266

8367
```sh
8468
poetry run python3 manage.py runserver
@@ -89,7 +73,7 @@ poetry run python3 manage.py runserver
8973
We have a couple of useful scripts:
9074

9175
* [migrate-cache-db.sh](./migrate-cache-db.sh) will create and apply migrations for the cache SQLite database. This runs automatically when running on docker, but you have to run it mannually otherwise.
92-
* [migrate-app-db.sh](./migrate-app-db.sh) will create and apply migrations for the main app, used for the secondary database. This does not run automatically in order to avoid potential problems. For this script, be aware of the `USE_DASHBOARD_DB` environment variable, because that variable will change the name of the databases (`dashboard_db` becomes `default` and `default` becomes `kcidb`) and so you'll have to change the command accordingly.
76+
* [migrate-app-db.sh](./migrate-app-db.sh) will create and apply migrations for the main app, generally used for the local database. This does not run automatically in order to avoid potential problems. Be aware that *if* you have write permissions to the production database it will be changed, so double check that you are connected to the right one (but usually developers won't have write access to the production database anyway).
9377
9478
It is important to note that Django automatically creates migrations based on changes to the models when running the first command of the scripts above. You can edit the migrations manually, and you can also run the commands by hand if you want more control over it.
9579
@@ -236,7 +220,7 @@ In order to debug backend in PyCharm, just follow these steps:
236220
- in `Run` session of the dialog, select `script`, then find the script `manage.py` at the `backend` folder
237221
- at `script` name input, just enter `runserver`
238222
- at `Environment Variables`, enter the following values:
239-
- `DB_DEFAULT`: `{"ENGINE": "django.db.backends.postgresql", "NAME": "playground_kcidb", "USER": "<youremail>@profusion.mobi", "PASSWORD": "<yourpassword>", "HOST": "127.0.0.1", "PORT": "5432", "CONN_MAX_AGE": null, "OPTIONS": {"connect_timeout": 2, "sslmode": "disable"}}`
223+
- `DB_ENGINE`: `django.db.backends.postgresql`, `DB_NAME`: `dashboard`, `DB_USER`: `<youremail>@profusion.mobi`, `DB_PASSWORD`: `<yourpassword>`, `DB_HOST`: `127.0.0.1`, `DB_PORT`: `5432`, `DB_OPTIONS_CONNECT_TIMEOUT`: `16`
240224
- `DEBUG`: `True`
241225
242226
Quote character in password field is escaped normally with `\"` .

backend/data/notifications/example-subscription.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tree: # The filename and the name of the tree may follow the trees-name.yaml file, but does not matter in the code
1+
tree: # The filename and the name of the tree may follow the tree-names.yaml file, but does not matter in the code
22
url: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git # the git_repository_url of the tree
33
default_recipients:
44
- laura.nao@collabora.com

0 commit comments

Comments
 (0)