Skip to content

Commit

Permalink
Create db if doesn't exist (#37)
Browse files Browse the repository at this point in the history
* provides a mechanism for database initialization within the container image

* only create file if it doesn't exist yet

* rootless -> root container because of Litestream permissions issues

I have no idea why, but Litestream sets a replicated file's
permissions to 600 [0], which makes it impossible to access
from a rootless container, if the file is being exposed through
a volume.

[0] https://github.com/benbjohnson/litestream/blob/5be467a478adcffc5b3999b9503cc676c2bf09f1/internal/internal.go#L62

* init db within __main__.py instead of outside script
  • Loading branch information
cmelone authored May 1, 2024
1 parent 547f80a commit d3f5f1c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ COPY requirements.txt /requirements.txt
RUN /venv/bin/pip install --disable-pip-version-check -r /requirements.txt

# Copy the virtualenv into a distroless image
FROM gcr.io/distroless/python3-debian12:nonroot
FROM gcr.io/distroless/python3-debian12:latest
COPY --from=build /venv /venv
COPY ./gantry /app/gantry
COPY ./db /app/db
WORKDIR /app
ENTRYPOINT ["/venv/bin/python", "-m", "gantry"]
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
6 changes: 5 additions & 1 deletion gantry/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

async def init_db(app: web.Application):
db = await aiosqlite.connect(os.environ["DB_FILE"])
await db.execute("PRAGMA foreign_keys = ON;")
# create a database with the schema if it doesn't exist
# otherwise, this is a noop
with open("db/schema.sql") as f:
await db.executescript(f.read())
await db.commit()
app["db"] = db
yield
await db.close()
Expand Down

0 comments on commit d3f5f1c

Please sign in to comment.