Skip to content

Commit f59636d

Browse files
author
Roman Plevka
authored
Merge pull request #2 from rplevka/containerfile
containerize the app
2 parents 0c6ff4a + 387d57b commit f59636d

File tree

6 files changed

+79
-5
lines changed

6 files changed

+79
-5
lines changed

Containerfile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM quay.io/fedora/python-312:latest
2+
MAINTAINER https://github.com/SatelliteQE
3+
4+
ENV REKUPER_DIR="${HOME}/rekuper"
5+
6+
COPY --from=ghcr.io/astral-sh/uv:latest /uv /bin/uv
7+
8+
USER 1001
9+
COPY --chown=1001:0 / ${REKUPER_DIR}
10+
11+
WORKDIR ${REKUPER_DIR}
12+
RUN rm -f settings.yaml scripts/shovel/settings.yaml && \
13+
git config --global --add safe.directory ${REKUPER_DIR} && \
14+
uv pip install -r requirements.txt -r requirements-shovel.txt
15+
16+
CMD flask --app rekuper run --host 0.0.0.0

README.md

+44-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ graph TD;
88
```
99

1010
##Setup
11-
This is a standard flask app that utilizes Flask-migrate (sqlalchemy + alembic).
11+
This is a standard flask app that utilizes [Flask-migrate](https://flask-migrate.readthedocs.io/en/latest/index.html) ([sqlalchemy](https://www.sqlalchemy.org/) + [alembic](https://alembic.sqlalchemy.org/en/latest/)).
1212
It requires a database to be set up.
1313
To set it up, use the initialization script - `scripts/db_init/init-user-db.sh`.
1414
For a testing setup, you can setup a postgres db container with the schema precreated by running the `scripts/db_init_setup_db.sh` script.
@@ -26,3 +26,46 @@ start the app:
2626
flask --app rekuper run
2727
```
2828
The server uses port `5000` by default. The swagger UI should be accessible as a root page.
29+
30+
## Container setup
31+
Assumuing the database is also going to be run as local container:
32+
```
33+
# prepare common network
34+
podman network create rekuper
35+
```
36+
```
37+
# start db (ephemeral storage) container
38+
podman run \
39+
--name rekuper_db \
40+
--network rekuper \
41+
-e POSTGRES_PASSWORD=changeme \
42+
-v ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh:z \
43+
postgres:17
44+
```
45+
> [!IMPORTANT]
46+
> At this point, you started an unitialized database. You have to initialize it by running the migration scripts. For this purpose, we'll run a 1-shot `rekuper` container and override the default command
47+
>This should be use also on an initialized db every time there is a new migration.
48+
```
49+
podman run \
50+
--rm \
51+
--name rekuper_migrate \
52+
--network rekuper \
53+
-p 5000:5000 \
54+
-e REKUPER_DATABASE__HOST=rekuper_db:5432 \
55+
-e REKUPER_DATABASE__DB=telemetry \
56+
-e REKUPER_DATABASE__USERNAME=postgres \
57+
-e REKUPER_DATABASE__PASSWORD=changeme \
58+
localhost/rekuper:0.1 \
59+
flask --app rekuper db upgrade
60+
```
61+
Run the rekuper container properly this time
62+
```
63+
podman run \
64+
--network rekuper \
65+
-p 5000:5000 \
66+
-e REKUPER_DATABASE__HOST=rekuper_db:5432 \
67+
-e REKUPER_DATABASE__DB=telemetry \
68+
-e REKUPER_DATABASE__USERNAME=postgres \
69+
-e REKUPER_DATABASE__PASSWORD=changeme \
70+
localhost/rekuper:0.1
71+
```

config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dynaconf import Dynaconf
22

33
settings = Dynaconf(
4-
envvar_prefix="DYNACONF",
4+
envvar_prefix="REKUPER",
55
settings_files=['settings.yaml'],
66
)
77

rekuper.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from flask import Flask, request, jsonify
44
from flask_sqlalchemy import SQLAlchemy
55
from flask_migrate import Migrate
6-
from flask_restx import Api, Resource, fields, Namespace
7-
from sqlalchemy.exc import IntegrityError
6+
from flask_restx import Api, Resource, fields
7+
from sqlalchemy import text
8+
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
89

910
app = Flask('__name__')
1011
if settings.database.get('connection_string') is None:
@@ -34,6 +35,19 @@
3435
)
3536
ns = api.namespace('api', description='API operations')
3637

38+
def check_db_connection():
39+
try:
40+
# Execute a simple query
41+
db.session.execute(text('SELECT 1'))
42+
print("Database connection is valid.")
43+
except SQLAlchemyError as e:
44+
print(f"Database connection is not valid: {e}")
45+
raise SystemExit("Exiting due to database connection failure.")
46+
47+
# Call the function to check the database connection during startup
48+
with app.app_context():
49+
check_db_connection()
50+
3751
session_model = api.model('Session', {
3852
'id': fields.Integer(readOnly=True, description='The unique identifier of a Session'),
3953
'jenkins_job': fields.String(required=True, description='The Jenkins job'),

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ dynaconf
22
Flask
33
Flask-SQLAlchemy
44
Flask-Migrate
5+
flask-restx
56
psycopg2-binary
67
requests

scripts/db_init/setup_db.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
podman network create rekuper
2-
podman run --network rekuper --rm -e POSTGRES_PASSWORD=changeme -v ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh:z -p 25432:5432 postgres:17
2+
podman run --name rekuper_db --network rekuper --rm -e POSTGRES_PASSWORD=changeme -v ./init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh:z -p 25432:5432 postgres:17

0 commit comments

Comments
 (0)