Skip to content

Commit

Permalink
provides a mechanism for database initialization within the container…
Browse files Browse the repository at this point in the history
… image
  • Loading branch information
cmelone committed Apr 23, 2024
1 parent 936c88e commit 6b9f5e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ RUN /venv/bin/pip install --disable-pip-version-check -r /requirements.txt
FROM gcr.io/distroless/python3-debian12:nonroot
COPY --from=build /venv /venv
COPY ./gantry /app/gantry
COPY ./db /db
WORKDIR /app
ENTRYPOINT ["/venv/bin/python", "-m", "gantry"]
19 changes: 19 additions & 0 deletions db/init_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sqlite3
import sys

# Usage: python init_db.py <db_path>
# loads in default tables and future schema changes into the database
db_path = sys.argv[1]

try:
conn = sqlite3.connect(db_path)
c = conn.cursor()
with open("schema.sql", "r") as f:
c.executescript(f.read())
conn.commit()
conn.close()
except sqlite3.Error as e:
print(e)
sys.exit(1)

print(f"Database initialized successfully to {db_path}")
4 changes: 2 additions & 2 deletions db/schema.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PRAGMA foreign_keys = ON;

CREATE TABLE nodes (
CREATE TABLE IF NOT EXISTS nodes (
id INTEGER PRIMARY KEY,
uuid TEXT NOT NULL UNIQUE,
hostname TEXT NOT NULL,
Expand All @@ -11,7 +11,7 @@ CREATE TABLE nodes (
instance_type TEXT NOT NULL
);

CREATE TABLE jobs (
CREATE TABLE IF NOT EXISTS jobs (
id INTEGER PRIMARY KEY,
pod TEXT NOT NULL UNIQUE,
node INTEGER NOT NULL,
Expand Down

0 comments on commit 6b9f5e0

Please sign in to comment.