Skip to content

Commit

Permalink
✅ Add tests and docs for custom prestart script env var (#30)
Browse files Browse the repository at this point in the history
* ✅ Add tests for custom prestart script

* 📝 Add docs for custom prestart script

* 👷 Trigger Travis after migration
  • Loading branch information
tiangolo authored Apr 6, 2020
1 parent 5f8c9cd commit b53f225
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#! /usr/bin/env sh

echo "Custom prestart script"
41 changes: 29 additions & 12 deletions tests/test_02_app/test_custom_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
Expand All @@ -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()

0 comments on commit b53f225

Please sign in to comment.