Skip to content

Commit

Permalink
Add docker config
Browse files Browse the repository at this point in the history
  • Loading branch information
ka7eh committed Mar 9, 2022
1 parent 389407e commit 703d3d5
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 18 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
.github
.mypy_cache
.pytest_cache
data
htmlcov
resources
venv
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[flake8]
max-line-length = 88
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache,venv
exclude = .git,__pycache__,__init__.py,.mypy_cache,.pytest_cache,venv,resources
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ venv/
.env

# Data
resources/
resources/*
!resources/.gitkeep
*.sqlite
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Setup and Run the Dev Server

> We recommend using a virtual environment for your project.
>
> You can either use [Conda](https://docs.conda.io/en/latest/) or [Python venv](https://docs.python.org/3/library/venv.html) to create a virtual environment.
- Make sure [Poetry](https://python-poetry.org/) is installed in your environment.
- Create a `.env` file in your project root directory. You can use `env-example` as a template and adjust the variables accordingly. See the Variable section below for more information.
- Install the dependencies: `poetry install`.
- Set up the database: `./scripts/migrations_forward.sh`.
- Set up CTSM repo: `./scripts/setup_ctsm.sh`.
- Run the dev server: `./scripts/run_dev_server.sh`.

### Variables

#### Adjustable Variables in `.env`:

| Variable | Required | Description | Default | Scope |
|:--------------:|:--------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------:|--------|
| CTSM_REPO | No | The CTSM repository to use. | https://github.com/ESCOMP/CTSM/ | API |
| CTSM_TAG | Yes | The CTSM repository tag to use | - | API |
| SQLITE_DB | No | The path to the SQLite file to use. If the file doesn't exist, it will be created at the given path. The default DB will be created in project root. | cases.sqlite | API |
| SQLITE_DB_TEST | No | Same as SQLITE_DB, but for testing. | cases_test.sqlite | API |
| DEBUG | No | Set `DEBUG` state, which is used for adjusting logging level and other debugging purposes. | False | API |
| PORT | No | The port to use for API service in docker | 8000 | Docker |
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""init
"""First migration
Revision ID: 142fa5e3980d
Revision ID: 0e26bdf1d521
Revises:
Create Date: 2022-03-08 13:27:21.005808+00:00
Create Date: 2022-03-09 10:57:39.491935+00:00
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "142fa5e3980d"
revision = "0e26bdf1d521"
down_revision = None
branch_labels = None
depends_on = None
Expand All @@ -25,22 +25,28 @@ def upgrade():
sa.Column("compset", sa.String(length=300), nullable=False),
sa.Column("res", sa.String(length=100), nullable=False),
sa.Column("driver", sa.String(length=5), nullable=False),
sa.Column("ctsm_tag", sa.String(length=20), nullable=False),
sa.Column("status", sa.String(length=20), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_cases_compset"), "cases", ["compset"], unique=False)
op.create_index(op.f("ix_cases_ctsm_tag"), "cases", ["ctsm_tag"], unique=False)
op.create_index(op.f("ix_cases_driver"), "cases", ["driver"], unique=False)
op.create_index(op.f("ix_cases_id"), "cases", ["id"], unique=False)
op.create_index(op.f("ix_cases_name"), "cases", ["name"], unique=False)
op.create_index(op.f("ix_cases_res"), "cases", ["res"], unique=False)
op.create_index(op.f("ix_cases_status"), "cases", ["status"], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_cases_status"), table_name="cases")
op.drop_index(op.f("ix_cases_res"), table_name="cases")
op.drop_index(op.f("ix_cases_name"), table_name="cases")
op.drop_index(op.f("ix_cases_id"), table_name="cases")
op.drop_index(op.f("ix_cases_driver"), table_name="cases")
op.drop_index(op.f("ix_cases_ctsm_tag"), table_name="cases")
op.drop_index(op.f("ix_cases_compset"), table_name="cases")
op.drop_table("cases")
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class Settings(BaseSettings):
DEBUG: bool = False

CTSM_TAG: str = "master"
CTSM_TAG: str
CTSM_REPO: AnyHttpUrl = "https://github.com/ESCOMP/CTSM/" # type: ignore

SQLITE_DB_TEST: str = "cases_test.sqlite"
Expand Down
1 change: 1 addition & 0 deletions app/models/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ class CaseModel(Base):
res: str = Column(String(100), nullable=False, index=True)
driver: str = Column(String(5), nullable=False, index=True)
ctsm_tag: str = Column(String(20), nullable=False, index=True)
status: str = Column(String(20), nullable=False, index=True)
12 changes: 12 additions & 0 deletions app/schemas/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ class Driver(str, Enum):
mct = "mct"


class Status(str, Enum):
initialised = "initialised"
created = "created"
setup = "setup"
built = "built"
submitted = "submitted"
succeeded = "succeeded"
failed = "failed"


class CaseSchemaBase(BaseModel):
id: int
name: str
compset: str
res: str
driver: Driver = Driver.noupc
ctsm_tag: str
status: Status = Status.initialised


class CaseSchema(CaseSchemaBase):
Expand Down
6 changes: 3 additions & 3 deletions app/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def check_ctsm(errors: List[str]) -> None:

proc = subprocess.run(["git", "describe"], cwd=CTSM_ROOT, capture_output=True)
if proc.returncode != 0 or proc.stdout.strip().decode("utf8") != settings.CTSM_TAG:
errors.append(f"CTSM is not setup correctly. Run `setup_ctsm` first.")
errors.append("CTSM is not setup correctly. Run `setup_ctsm` first.")


def check_dependencies() -> None:
Expand All @@ -43,7 +43,7 @@ def check_dependencies() -> None:
raise Exception("\n".join(errors))


def checkout_externals():
def checkout_externals() -> None:
proc = subprocess.run(["manage_externals/checkout_externals"], cwd=CTSM_ROOT)
if proc.returncode != 0:
logger.error(f"Could not checkout externals: {proc.stderr.decode('utf-8')}.")
Expand Down Expand Up @@ -74,7 +74,7 @@ def setup_ctsm() -> None:
"-b",
settings.CTSM_TAG,
settings.CTSM_REPO,
f"resources/ctsm",
"resources/ctsm",
],
capture_output=True,
)
Expand Down
Binary file removed cases.sqlite
Binary file not shown.
12 changes: 12 additions & 0 deletions docker-compose.dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: "3.9"

services:
api:
extends:
file: docker-compose.yaml
service: api
build:
context: .
dockerfile: ./docker/Dockerfile
volumes:
- .:/ctsm-api
10 changes: 10 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.9"

services:
api:
image: ghcr.io/ka7eh/ctsm-api:${VERSION:-latest}
restart: unless-stopped
ports:
- ${PORT:-8000}:8000
volumes:
- ./resources:/ctsm-api/resources
22 changes: 22 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3.10-bullseye

RUN apt update && apt upgrade -y

RUN pip install --upgrade pip setuptools
RUN pip install poetry
RUN poetry config virtualenvs.create false

COPY ./pyproject.toml /ctsm-api/pyproject.toml
COPY ./poetry.lock /ctsm-api/poetry.lock

WORKDIR /ctsm-api

RUN poetry install

COPY docker/entrypoint.sh /ctsm-api/docker/entrypoint.sh

RUN chmod +x /ctsm-api/docker/entrypoint.sh

EXPOSE 8000

CMD ["/ctsm-api/docker/entrypoint.sh"]
10 changes: 10 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

set -e

cd /ctsm-api

./scripts/setup_ctsm.sh
./scripts/migrations_forward.sh

uvicorn app.main:app --reload --host 0.0.0.0
11 changes: 10 additions & 1 deletion env-example
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
DEBUG=1

# DB
SQLITE_DB=cases.sqlite
SQLITE_DB_TEST=cases_test.sqlite
SQLITE_DB_TEST=cases_test.sqlite

# CTSM
CTSM_REPO=https://github.com/ESCOMP/CTSM/
CTSM_TAG=ctsm5.1.dev083

# Docker
PORT=8000
Empty file added resources/.gitkeep
Empty file.
4 changes: 2 additions & 2 deletions scripts/migrations_create.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /bin/bash

set -x
set -e

export PYTHONPATH=$PWD
export PYTHONPATH=$(dirname $(dirname $(realpath $0)))

alembic revision --autogenerate -m "${@}"
4 changes: 3 additions & 1 deletion scripts/migrations_forward.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#! /bin/bash

export PYTHONPATH=$PWD
set -e

export PYTHONPATH=$(dirname $(dirname $(realpath $0)))

# Forward migrations to the given revision. Default to `head` if a revision is not passed.
alembic upgrade "${1:-head}"
4 changes: 3 additions & 1 deletion scripts/migrations_reverse.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#! /bin/bash

export PYTHONPATH=$PWD
set -e

export PYTHONPATH=$(dirname $(dirname $(realpath $0)))

if [[ -z "$1" ]]; then
echo "Specify the revision to reverse to."
Expand Down
2 changes: 2 additions & 0 deletions scripts/run_dev_server.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash

set -e

uvicorn app.main:app --reload
4 changes: 2 additions & 2 deletions scripts/setup_ctsm.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /bin/bash

set -x
set -e

export PYTHONPATH=$PWD
export PYTHONPATH=$(dirname $(dirname $(realpath $0)))

python app/utils/dependencies.py

0 comments on commit 703d3d5

Please sign in to comment.