Skip to content
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

API import scripts #72

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 2 additions & 49 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,56 +140,9 @@ jobs:
docker compose pull db
docker compose up --no-build --pull never --wait db

- name: Prepare data
- name: Prepare and build API container
run: |
docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/facility_functions.sql

docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/prepare_facilities.sql

docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/prepare_milestones.sql

- name: Extract data from database
run: |
docker compose exec db pg_dump \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
--table openrailwaymap_facilities_for_search \
--table openrailwaymap_ref \
--table openrailwaymap_milestones \
--table openrailwaymap_tracks_with_ref \
> api/api.sql

- name: Build container
run: |
docker compose up --build --wait api-import

docker compose stop api-import

DB_CONTAINER_ID="$(docker compose ps --all --format json | jq -r 'select(.Service == "api-import") | .ID')"
DB_IMAGE="$(docker compose ps --all --format json | jq -r 'select(.Service == "api-import") | .Image')"

# Persist and squash data in new image
docker cp "$DB_CONTAINER_ID:/var/lib/postgresql/postgres-data" api/postgres-data

docker compose build api
docker compose push api
api/prepare-api.sh

- name: Print import logs
if: ${{ always() }}
Expand Down
7 changes: 7 additions & 0 deletions SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Start the tile server:
docker compose up --build martin
```

Prepare and start the API:
```shell
api/prepare-api.sh
docker compose up api
```

Start the web server:
```shell
docker compose up --build martin-proxy
Expand Down Expand Up @@ -51,6 +57,7 @@ flyctl deploy --config martin-static.fly.toml --local-only

Build and deploy the API:
```shell
api/prepare-api.sh
flyctl deploy --config api.fly.toml --local-only api
```

Expand Down
4 changes: 4 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ COPY facility_functions.sql /sql/facility_functions.sql
COPY init.sh /docker-entrypoint-initdb.d/20-init.sh

ENV POSTGRES_DB gis
ENV POSTGRES_USER postgres
ENV POSTGRES_HOST 127.0.0.1
ENV POSTGRES_HOST_AUTH_METHOD trust
ENV PGDATA /var/lib/postgresql/postgres-data

Expand All @@ -26,6 +28,8 @@ COPY start.sh start.sh
COPY postgres-data /var/lib/postgresql/postgres-data

ENV POSTGRES_DB gis
ENV POSTGRES_USER postgres
ENV POSTGRES_HOST 127.0.0.1
ENV POSTGRES_HOST_AUTH_METHOD trust
ENV PGDATA /var/lib/postgresql/postgres-data

Expand Down
2 changes: 1 addition & 1 deletion api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from openrailwaymap_api.status_api import StatusAPI

def connect_db():
conn = psycopg2.connect(dbname='gis', user='postgres', host='127.0.0.1')
conn = psycopg2.connect(dbname=os.environ['POSTGRES_DB'], user=os.environ['POSTGRES_USER'], host=os.environ['POSTGRES_HOST'])
return conn

class OpenRailwayMapAPI:
Expand Down
57 changes: 57 additions & 0 deletions api/prepare-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bash

set -e
set -o pipefail
set -u

echo 'Preparing API'

echo 'Preparing facility functions'
docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/facility_functions.sql

echo 'Preparing facilities'
docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/prepare_facilities.sql

echo 'Preparing milestones'
docker compose exec -T db psql \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
< api/prepare_milestones.sql

echo 'Extract API data from database'
docker compose exec db pg_dump \
--host 127.0.0.1 \
--user postgres \
--port 5432 \
--dbname gis \
--table openrailwaymap_facilities_for_search \
--table openrailwaymap_ref \
--table openrailwaymap_milestones \
--table openrailwaymap_tracks_with_ref \
> api/api.sql

echo 'Importing API data into Postgres container'
docker compose up --build --wait api-import
docker compose stop api-import
DB_CONTAINER_ID="$(docker compose ps --all --format json | jq -r 'select(.Service == "api-import") | .ID')"
# Persist and squash data in new image
docker cp "$DB_CONTAINER_ID:/var/lib/postgresql/postgres-data" api/postgres-data

echo 'Building API container'
docker compose build api

echo 'Cleaning up'
rm -rf api/postgres-data
rm api/api.sql