From b53f2257794a739ad70988430feb70872574a973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Mon, 6 Apr 2020 15:50:59 +0200 Subject: [PATCH] :white_check_mark: Add tests and docs for custom prestart script env var (#30) * :white_check_mark: Add tests for custom prestart script * :memo: Add docs for custom prestart script * :construction_worker: Trigger Travis after migration --- README.md | 18 +++++++- .../app/custom_app/custom-prestart.sh | 3 ++ tests/test_02_app/test_custom_app.py | 41 +++++++++++++------ 3 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 tests/test_02_app/custom_app/app/custom_app/custom-prestart.sh diff --git a/README.md b/README.md index a5c86e8b..626746d0 100644 --- a/README.md +++ b/README.md @@ -286,6 +286,18 @@ You can set it like: docker run -d -p 80:8080 -e LOG_LEVEL="warning" myimage ``` +#### `PRE_START_PATH` + +The path where to find the pre-start script. + +By default, set to `/app/prestart.sh`. + +You can set it like: + +```bash +docker run -d -p 80:8080 -e PRE_START_PATH="/custom/script.sh" myimage +``` + ### Custom Gunicorn configuration file The image includes a default Gunicorn Python config file at `/gunicorn_conf.py`. @@ -300,7 +312,7 @@ You can override it by including a file in: ### Custom `/app/prestart.sh` -If you need to run anything before starting the app, you can add a file `prestart.sh` to the directory `/app`. The image will automatically detect and run it before starting everything. +If you need to run anything before starting the app, you can add a file `prestart.sh` to the directory `/app`. The image will automatically detect and run it before starting everything. For example, if you want to add Alembic SQL migrations (with SQLALchemy), you could create a `./app/prestart.sh` file in your code directory (that will be copied by your `Dockerfile`) with: @@ -324,6 +336,8 @@ If you need to run a Python script before starting the app, you could make the ` python /app/my_custom_prestart_script.py ``` +You can customize the location of the prestart script with the environment variable `PRE_START_PATH` described above. + ### Development live reload The default program that is run is at `/start.sh`. It does everything described above. @@ -383,6 +397,8 @@ All the image tags, configurations, environment variables and application option ### Latest Changes +* Add support for custom `PRE_START_PATH` env var. PR [#12](https://github.com/tiangolo/uvicorn-gunicorn-docker/pull/12) by [@mgfinch](https://github.com/mgfinch). + ### 0.5.0 * Refactor tests to use env vars and add image tags for each build date, like `tiangolo/uvicorn-gunicorn:python3.7-2019-10-15`. PR [#15](https://github.com/tiangolo/uvicorn-gunicorn-docker/pull/15). diff --git a/tests/test_02_app/custom_app/app/custom_app/custom-prestart.sh b/tests/test_02_app/custom_app/app/custom_app/custom-prestart.sh new file mode 100644 index 00000000..233747f8 --- /dev/null +++ b/tests/test_02_app/custom_app/app/custom_app/custom-prestart.sh @@ -0,0 +1,3 @@ +#! /usr/bin/env sh + +echo "Custom prestart script" diff --git a/tests/test_02_app/test_custom_app.py b/tests/test_02_app/test_custom_app.py index c8bc0386..6cac1c77 100644 --- a/tests/test_02_app/test_custom_app.py +++ b/tests/test_02_app/test_custom_app.py @@ -17,7 +17,7 @@ client = docker.from_env() -def verify_container(container, response_text): +def verify_container(container, response_text, prestart_str): config_data = get_config(container) assert config_data["workers_per_core"] == 1 assert config_data["host"] == "0.0.0.0" @@ -26,23 +26,40 @@ def verify_container(container, response_text): assert config_data["workers"] >= 2 assert config_data["bind"] == "0.0.0.0:80" logs = get_logs(container) - assert "Checking for script in /app/prestart.sh" in logs - assert "Running script /app/prestart.sh" in logs - assert ( - "Running inside /app/prestart.sh, you could add migrations to this file" in logs - ) + assert prestart_str in logs response = requests.get("http://127.0.0.1:8000") assert response.text == response_text @pytest.mark.parametrize( - "environment", + "environment,prestart_str", [ - {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, - {"APP_MODULE": "custom_app.custom_main:custom_var"}, + ( + {"MODULE_NAME": "custom_app.custom_main", "VARIABLE_NAME": "custom_var"}, + "Running inside /app/prestart.sh, you could add migrations to this file", + ), + ( + {"APP_MODULE": "custom_app.custom_main:custom_var"}, + "Running inside /app/prestart.sh, you could add migrations to this file", + ), + ( + { + "MODULE_NAME": "custom_app.custom_main", + "VARIABLE_NAME": "custom_var", + "PRE_START_PATH": "/app/custom_app/custom-prestart.sh", + }, + "Custom prestart script", + ), + ( + { + "APP_MODULE": "custom_app.custom_main:custom_var", + "PRE_START_PATH": "/app/custom_app/custom-prestart.sh", + }, + "Custom prestart script", + ), ], ) -def test_custom_app(environment): +def test_custom_app(environment, prestart_str): name = os.getenv("NAME") dockerfile = f"{name}.dockerfile" response_text = os.getenv("TEST_STR2") @@ -59,11 +76,11 @@ def test_custom_app(environment): detach=True, ) time.sleep(sleep_time) - verify_container(container, response_text) + verify_container(container, response_text, prestart_str) container.stop() # Test that everything works after restarting too container.start() time.sleep(sleep_time) - verify_container(container, response_text) + verify_container(container, response_text, prestart_str) container.stop() container.remove()