Skip to content

Commit

Permalink
Bootstrap Metabase via entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gouline committed Jan 4, 2024
1 parent e6d398f commit 7589978
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 25 deletions.
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,5 @@ dev-install: build
.PHONY: dev-install

dev-sandbox:
( cd sandbox \
&& mkdir -p volumes \
&& cp -r fixtures/* volumes/ \
&& docker-compose up \
; docker-compose down )
( cd sandbox && docker-compose up --build ; docker-compose down )
.PHONY: dev-sandbox
3 changes: 3 additions & 0 deletions sandbox/.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ POSTGRES_PASSWORD=dbtmetabase1
POSTGRES_DB=dbtmetabase
POSTGRES_SCHEMA=public
POSTGRES_PORT=5432
MB_SETUP_TOKEN=2ffe1bc9-4bc5-4184-a10d-731517d8e4a3
MB_USER=[email protected]
MB_PASSWORD=dbtmetabase1
MB_PORT=3000
9 changes: 6 additions & 3 deletions sandbox/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
FROM python:3.11-slim-bullseye

RUN python3 -m pip install dbt-postgres

WORKDIR /app

COPY requirements.txt ./

RUN python3 -m pip install -r requirements.txt

COPY entrypoint.py ./
COPY dbt_project.yml ./
COPY models/ ./models/
COPY profiles.yml ./

CMD dbt run --profiles-dir .
ENTRYPOINT ["python3", "entrypoint.py"]
8 changes: 0 additions & 8 deletions sandbox/config.yml

This file was deleted.

26 changes: 17 additions & 9 deletions sandbox/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,57 @@ services:
- ${POSTGRES_PORT}:5432
networks:
- common
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER"]
interval: 5s
timeout: 5s
retries: 5
start_period: 10s
restart: always

metabase:
image: metabase/metabase:latest
environment:
- MB_DB_FILE=/metabase.db
- MB_SETUP_TOKEN=${MB_SETUP_TOKEN:-}
ports:
- ${MB_PORT}:3000
networks:
- common
volumes:
- ./volumes/metabase.db:/metabase.db
healthcheck:
test: ["CMD-SHELL", "curl --fail http://localhost:3000/api/health"]
interval: 10s
timeout: 5s
retries: 5
start_period: 20s
restart: always
depends_on:
postgres:
condition: service_healthy

dbt:
app:
build: .
command: init
environment:
- POSTGRES_USER=${POSTGRES_USER:-}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-}
- POSTGRES_DB=${POSTGRES_DB:-}
- POSTGRES_SCHEMA=${POSTGRES_SCHEMA:-}
- POSTGRES_PORT=${POSTGRES_PORT:-}
- POSTGRES_HOST=postgres
- MB_SETUP_TOKEN=${MB_SETUP_TOKEN:-}
- MB_USER=${MB_USER:-}
- MB_PASSWORD=${MB_PASSWORD:-}
- MB_PORT=${MB_PORT:-}
- MB_HOST=metabase
networks:
- common
volumes:
- ./target:/app/target
depends_on:
postgres:
condition: service_healthy
metabase:
condition: service_healthy

networks:
common:

volumes:
postgres-data:
75 changes: 75 additions & 0 deletions sandbox/entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python

import requests
from molot import target, envarg, envarg_int, evaluate, shell

POSTGRES_HOST = envarg("POSTGRES_HOST")
POSTGRES_PORT = envarg_int("POSTGRES_PORT")
POSTGRES_DB = envarg("POSTGRES_DB")
POSTGRES_USER = envarg("POSTGRES_USER")
POSTGRES_PASSWORD = envarg("POSTGRES_PASSWORD")
MB_HOST = envarg("MB_HOST")
MB_PORT = envarg_int("MB_PORT")
MB_SETUP_TOKEN = envarg("MB_SETUP_TOKEN")
MB_USER = envarg("MB_USER")
MB_PASSWORD = envarg("MB_PASSWORD")
MB_NAME = envarg("MB_NAME", "dbtmetabase")


@target(description="set up Metabase user and database")
def setup_metabase():
requests.post(
f"http://{MB_HOST}:{MB_PORT}/api/setup",
json={
"token": MB_SETUP_TOKEN,
"user": {
"site_name": MB_NAME,
"first_name": MB_NAME,
"last_name": None,
"email": MB_USER,
"password_confirm": MB_PASSWORD,
"password": MB_PASSWORD,
},
"database": {
"engine": "postgres",
"name": POSTGRES_DB,
"details": {
"host": POSTGRES_HOST,
"port": POSTGRES_PORT,
"dbname": POSTGRES_DB,
"user": POSTGRES_USER,
"password": POSTGRES_PASSWORD,
"schema-filters-type": "all",
"ssl": False,
"tunnel-enabled": False,
"advanced-options": False,
},
"is_on_demand": False,
"is_full_sync": True,
"is_sample": False,
"cache_ttl": None,
"refingerprint": False,
"auto_run_queries": True,
"schedules": {},
},
"prefs": {
"site_name": MB_NAME,
"site_locale": "en",
"allow_tracking": "false",
},
},
timeout=10,
).raise_for_status()


@target(description="run dbt project")
def run_dbt():
shell("dbt run --profiles-dir .")


@target(description="initial setup", depends=["setup_metabase", "run_dbt"])
def init():
pass


evaluate()
Binary file removed sandbox/fixtures/metabase.db/metabase.db.mv.db
Binary file not shown.
3 changes: 3 additions & 0 deletions sandbox/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
molot
requests
dbt-postgres

0 comments on commit 7589978

Please sign in to comment.