-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #44: custom postgres docker image #45
Merged
SonOfLope
merged 2 commits into
main
from
44-a-new-docker-image-is-available-containing-the-postgresql-database-used-by-fertiscan
Sep 13, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ on: | |
type: choice | ||
options: | ||
- nginx | ||
- postgres | ||
tag: | ||
required: true | ||
description: Version a tag l'image | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BB_URL= | ||
BB_SERVICE_ACCOUNT= | ||
BB_SERVICE_KEY= | ||
BB_INSTANCE_ID= | ||
BB_DATABASE_ID= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM postgres:latest | ||
|
||
RUN apt-get update && apt-get install -y python3 python3-requests curl | ||
RUN mkdir -p /app && chown postgres:postgres /app | ||
|
||
WORKDIR /app | ||
|
||
COPY fetch_schema.py /app/fetch_schema.py | ||
COPY entrypoint.sh /app/entrypoint.sh | ||
|
||
RUN chmod +x /app/entrypoint.sh | ||
|
||
RUN chmod 700 /app | ||
|
||
USER postgres | ||
|
||
ENTRYPOINT ["/app/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Custom Postgres image | ||
|
||
This repository contains a custom PostgreSQL Docker image that dynamically | ||
fetches the latest database schema from Bytebase during container startup. The | ||
container dynamically retrieves the latest schema for a specified `INSTANCE_ID` | ||
and `DATABASE_ID` from Bytebase at runtime, ensuring the PostgreSQL database | ||
is always up-to-date. | ||
|
||
## Environment Variables | ||
|
||
To configure the schema-fetching process, the following environment variables | ||
are required: | ||
|
||
- `BB_URL` – The base URL to access the Bytebase API. | ||
- `BB_SERVICE_ACCOUNT` – The service account email used for authentication with | ||
Bytebase. | ||
- `BB_SERVICE_KEY` – The API key or password associated with the service | ||
account. | ||
- `BB_INSTANCE_ID` – The ID of the Bytebase instance containing the desired | ||
schema. | ||
- `BB_DATABASE_ID` – The ID of the specific database schema to be fetched from | ||
Bytebase. | ||
|
||
## Usage example | ||
|
||
The following is an example of how this custom image can be used in a Docker | ||
Compose setup for the **Fertiscan** project: | ||
|
||
```yaml | ||
services: | ||
postgres: | ||
image: ghcr.io/ai-cfia/postgres:latest | ||
container_name: fertiscan-postgres | ||
environment: | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=postgres | ||
- POSTGRES_DB=fertiscan | ||
- BB_URL=${BB_URL} | ||
- BB_SERVICE_ACCOUNT=${BB_SERVICE_ACCOUNT} | ||
- BB_SERVICE_KEY=${BB_SERVICE_KEY} | ||
- INSTANCE_ID=${BB_INSTANCE_ID} | ||
- DATABASE_ID=${BB_DATABASE_ID} | ||
env_file: | ||
- .env | ||
ports: | ||
- "5432:5432" | ||
healthcheck: | ||
test: ["CMD", "pg_isready", "-U", "postgres"] | ||
timeout: 10s | ||
retries: 5 | ||
networks: | ||
- fertiscan-network | ||
volumes: | ||
- postgres-data:/var/lib/postgresql/data | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
python3 /app/fetch_schema.py | ||
|
||
exec postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
import requests | ||
|
||
BB_URL = os.getenv("BB_URL") | ||
BB_SERVICE_ACCOUNT = os.getenv("BB_SERVICE_ACCOUNT") | ||
BB_SERVICE_KEY = os.getenv("BB_SERVICE_KEY") | ||
INSTANCE_ID = os.getenv("BB_INSTANCE_ID") | ||
DATABASE_ID = os.getenv("BB_DATABASE_ID") | ||
|
||
def generate_token(): | ||
login_url = f"{BB_URL}/v1/auth/login" | ||
headers = { | ||
'Content-Type': 'application/json', | ||
'Accept-Encoding': 'deflate, gzip' | ||
} | ||
data = { | ||
"email": BB_SERVICE_ACCOUNT, | ||
"password": BB_SERVICE_KEY, | ||
"web": True | ||
} | ||
|
||
response = requests.post(login_url, json=data, headers=headers) | ||
if response.status_code == 200: | ||
return response.json().get('token') | ||
else: | ||
print(f"Failed to generate token: {response.status_code}, {response.text}") | ||
return None | ||
|
||
def fetch_fertiscan_dev_schema(): | ||
token = generate_token() | ||
if token: | ||
schema_url = f"{BB_URL}/v1/instances/{INSTANCE_ID}/databases/{DATABASE_ID}/schema" | ||
headers = { | ||
"Authorization": f"Bearer {token}", | ||
"Content-Type": "application/json" | ||
} | ||
|
||
response = requests.get(schema_url, headers=headers) | ||
if response.status_code == 200: | ||
with open('schema.sql', 'w') as file: | ||
file.write(response.json()['schema']) | ||
print("Schema saved to schema.sql") | ||
else: | ||
print(f"Failed to fetch schema: {response.status_code}, {response.text}") | ||
else: | ||
print("No token generated, cannot fetch schema.") | ||
|
||
if __name__ == "__main__": | ||
fetch_fertiscan_dev_schema() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use https://pypi.org/project/python-dotenv/ to fetch env values from Dockerfile
e.g