Skip to content

Commit

Permalink
Add alembic db management (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
pl-buiquang authored Jul 20, 2021
1 parent ba0faee commit 43cbd23
Show file tree
Hide file tree
Showing 26 changed files with 620 additions and 222 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install pydantic --no-binary pydantic
pip install -r requirements-dev.txt
pip install -r requirements.txt
- name: Generate binary Unix
if: matrix.os != 'windows-latest'
run: |
Expand Down
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ RUN mkdir -p examples/studies
COPY ./requirements.txt /conf/
COPY ./antarest /antarest
COPY ./resources /resources
COPY ./scripts /scripts
COPY ./alembic /alembic
COPY ./alembic.ini /alembic.ini

COPY ./antares-launcher /antares-launcher
RUN ln -s /antares-launcher/antareslauncher /antareslauncher
Expand All @@ -19,4 +22,4 @@ RUN cp /antares-launcher/requirements.txt /conf/antares-launcher/requirements.tx
RUN pip3 install --upgrade pip \
&& pip3 install -r /conf/requirements.txt

ENTRYPOINT gunicorn --config /conf/gunicorn.py --worker-class=uvicorn.workers.UvicornWorker antarest.wsgi:app
ENTRYPOINT ./scripts/start.sh
65 changes: 34 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,44 @@
First clone the projet:

```shell script
git clone https://github.com/AntaresSimulatorTeam/api-iso-antares.git
cd api-iso-antares
git clone https://github.com/AntaresSimulatorTeam/AntaREST.git
cd AntaREST
git submodule init
git submodule update
```

Install back dependencies

```shell script
python -m pip install --upgrade pip
pip install pydantic --no-binary pydantic
pip install -r requirements.txt # use requirements-dev.txt if building a single binary with pyinstaller
```

Build front

```shell script
cd webapp
npm install
cd ..
NODE_OPTIONS="--max-old-space-size=8192" ./scripts/build-front.sh
```


### Using pyinstaller

Linux system:

```shell script
pyinstaller -F api_iso_antares/main.py -n server --add-data resources:resources
git log -1 HEAD --format=%H > ./resources/commit_id
pyinstaller -F antarest/main.py -n server --additional-hooks-dir extra-hooks --add-data resources:resources
```

Windows system:

```shell script
pyinstaller -F api_iso_antares\main.py -n server --add-data ".\resources;.\resources"
git log -1 HEAD --format=%H > .\resources\commit_id
pyinstaller -F api_iso_antares\main.py -n server --additional-hooks-dir extra-hooks --add-data ".\resources;.\resources"
```

You can test the build is ok using:
Expand All @@ -41,7 +63,7 @@ dist\server.exe -v # Windows system
To build the docker image, use the following command:

```shell script
docker build --tag api-iso-antares -f docker/Dockerfile .
docker build --tag antarest -f docker/Dockerfile .
```

## Start the API
Expand All @@ -62,7 +84,7 @@ docker run \
-p 80:5000 \
-e GUNICORN_WORKERS=4 \
-v $STUDIES_ABSOLUTE_PATH:/studies \
api-iso-antares
antarest
```

* Setting the environment variable GUNICORN_WORKERS to *ALL_AVAILABLE* will make GUNICORN use 2 * nb_cpu +1 workers
Expand Down Expand Up @@ -115,42 +137,23 @@ The address (the port mostly) depends of the way you started the server. If you
To test the server, you can list the available studies in your workspace using:

```shell script
curl http://0.0.0.0:8080/studies
curl http://localhost:8080/v1/studies
```

Or data of a specific study using:

```shell script
curl http://0.0.0.0:8080/studies/{study_uuid}
curl http://localhost:8080/v1/studies/{study_uuid}
```

The current API handle hundreds of html end point (get and post) to manipulate your studies.
The best way to discover the API is using it's swagger documentation (see below).

## Swagger

The ANTARES API do not have a public UI swagger available for the moment.
Use the following command to save the swagger metadata of the API ANTARES into a json file.
The ANTARES API doc is available within the application (open your browser to `http://localhost:8080`)
You can also fetch the raw open api spec :

```shell script
curl http://0.0.0.0:8080/swagger > swagger.json
```

Then, use the script *script/swagger-ui.sh* to start a Swagger UI.

```shell script
chmod a+x ./script/swagger-ui.sh
./script/swagger-ui.sh
```

Do not forget to start the API ANTARES alongside (and to modify the port you decide to use into the Swagger UI).


## Strategies for JssonSchema Engines

| Strategy | Description |
|----------|---------------------------------------|
| S1 | Mix folder with complete set of zones |
| S... | |
| Sn | |

curl http://localhost:8080/openapi.json > swagger.json
```
89 changes: 89 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .

# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url =


[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks=black
# black.type=console_scripts
# black.entrypoint=black
# black.options=-l 79

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
1 change: 1 addition & 0 deletions alembic/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
91 changes: 91 additions & 0 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from logging.config import fileConfig
import os
from pathlib import Path

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from antarest.core.config import Config
from antarest import main


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
# uncomment this to have
#fileConfig(config.config_file_name)

config_path = os.getenv('ANTAREST_CONF') or main.get_default_config_path()
if config_path and Path(config_path).exists():
antarest_conf = Config.from_yaml_file(config_path)
config.set_main_option("sqlalchemy.url", antarest_conf.db_admin_url or antarest_conf.db_url)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata


target_metadata = main.Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True,
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata, render_as_batch=True,
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
Loading

0 comments on commit 43cbd23

Please sign in to comment.