Skip to content

Commit

Permalink
Merge starting a database container via a task into dev
Browse files Browse the repository at this point in the history
Fixes #18.
  • Loading branch information
gnn committed Oct 21, 2020
2 parents 2fb056f + ecbb751 commit e7f524b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/egon/data/airflow/Dockerfile.postgis
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM postgres:12

RUN apt-get update
RUN apt-get install -y postgresql-12-postgis-3



12 changes: 12 additions & 0 deletions src/egon/data/airflow/dags/pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from airflow.operators.python_operator import PythonOperator
from airflow.utils.dates import days_ago
import airflow

from egon.data.airflow.tasks import initdb

with airflow.DAG(
"egon-data-processing-pipeline",
description="The eGo^N data processing DAG.",
default_args={"start_date": days_ago(1)},
) as example:
setup = PythonOperator(task_id="initdb", python_callable=initdb)
18 changes: 18 additions & 0 deletions src/egon/data/airflow/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'
services:
egon-data-local-database:
image: postgres:12-postgis
container_name: egon-data-local-database
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile.postgis
ports:
- "127.0.0.1:54321:5432"
environment:
POSTGRES_DB: egon-data
POSTGRES_USER: egon
POSTGRES_PASSWORD: data
volumes:
- $HOME/docker/volumes/postgres/egon-data:/var/lib/postgresql/data
- ./entrypoints:/docker-entrypoint-initdb.d/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE EXTENSION postgis;
10 changes: 10 additions & 0 deletions src/egon/data/airflow/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os.path
import subprocess


def initdb():
""" Initialize the local database used for data processing. """
subprocess.run(
["docker-compose", "up", "-d", "--build"],
cwd=os.path.dirname(__file__),
)
14 changes: 14 additions & 0 deletions tests/test_egon-data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from importlib import import_module

from click.testing import CliRunner

from egon.data import __version__
Expand All @@ -19,3 +21,15 @@ def test_airflow():
runner = CliRunner()
result = runner.invoke(main, ["airflow", "--help"])
assert result.output == ""


def test_pipeline_and_tasks_importability():
error = None
for m in ["egon.data.airflow.dags.pipeline", "egon.data.airflow.tasks"]:
try:
import_module(m)
except Exception as e:
error = e
assert error is None, (
"\nDid not expect an error when importing:\n\n `{}`\n\nGot: {}"
).format(m, error)

0 comments on commit e7f524b

Please sign in to comment.