Skip to content

Commit

Permalink
dokku deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
rebkwok committed Dec 14, 2023
1 parent 7f33589 commit d4a2c4e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 31 deletions.
97 changes: 90 additions & 7 deletions DEPLOY.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Deployment
## Deployment

ebmbot is deployed on smallweb1 and is managed by systemd.
Deployment uses `dokku` and requires the environment variables defined in `dotenv-sample`.
It is deployed to our `dokku3` instance.

* To deploy: `fab deploy`
* To view logs: `sudo journalctl -u app.ebmbot.* -b -f`
It runs as a single dokku app named `bennettbot`, with multiple processes for each
service (bot, dispatcher and webserver) as defined in the `Procfile`.

## Creating the Slack app
(This should only need to be done once).
Expand Down Expand Up @@ -71,10 +72,27 @@ events. We need to add some more:
If you update any scopes after installing the app, you'll need to reinstall it (slack will
usually prompt for this).

### Set environment variables
## Configuring the dokku app
(This should only need to be done once).

### Create app

On dokku3, as the `dokku` user:

```sh
dokku$ dokku apps:create bennettbot
```

Copy `dotenv-sample` to `.env` and update with relevant environment variables. See also
comments in `ebmbot/settings.py`.
### Create storage for sqlite db and logs
```sh
dokku$ mkdir -p /var/lib/dokku/data/storage/bennettbot/logs
dokku$ chown dokku:dokku /var/lib/dokku/data/storage/bennettbot
dokku$ dokku storage:mount bennettbot /var/lib/dokku/data/storage/bennettbot/:/storage
```

### Configure app environment variables

See also comments in `ebmbot/settings.py`.

The following slack environment variables need to be set:
- `SLACK_LOGS_CHANNEL`: channel where scheduled job notifications will be posted
Expand All @@ -98,3 +116,68 @@ project information.
This is the path to credentials for the [email protected]
service account:
- `GCP_CREDENTIALS_PATH`

The path for logs; set this to a directory in the dokku mounted storage so the logs
persist outside of the containers.
- LOGS_DIR

The path for the sqlite db file; set this to a file in the dokku mounted storage
- DB_PATH

Set each env varible with:
```sh
dokku$ dokku config:set bennettbot ENVVAR_NAME=value
```

e.g.
```sh
dokku$ dokku config:set bennettbot LOG_DIR=/storage/logs
dokku$ dokku config:set bennettbot LOG_DIR=/storage/bennettbod.
```

### Configure network

(If creating the app, deploy first before doing this step.)


```
dokku network:create bennettbot-network
dokku network:set bennettbot attach-post-deploy bennettbot-network
dokku config:set bennettbot WEBSERVER_URL=bennettbot.webserver:9999
```
https://dokku.com/docs/networking/network/#attaching-an-app-to-a-network
https://www.joseferben.com/posts/running-multiple-services-on-dokku/

### Map port 9999 for incoming github hooks
https://dokku.com/docs/networking/port-management/

dokku ports:add bennettbot http:9999:9999


## Deployment

Merges to the `main` branch will trigger an auto-deploy via GitHub actions.

Note this deploys by building the prod docker image (see `docker/docker-compose.yaml`) and using the dokku [git:from-image](https://dokku.com/docs/deployment/methods/git/#initializing-an-app-repository-from-a-docker-image) command.


### Manually deploying

To deploy manually:

```
# build prod image locally
just docker/build prod
# tag image and push
docker tag bennettbot ghcr.io/ebmdatalab/bennettbot:latest
docker push ghcr.io/ebmdatalab/bennettbot:latest
# get the SHA for the latest image
SHA=$(docker inspect --format='{{index .RepoDigests 0}}' ghcr.io/ebmdatalab/bennettbot:latest)
```

On dokku3, as the `dokku` user:
```
dokku$ dokku git:from-image bennettbot <SHA>
```
3 changes: 3 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bot: python -m ebmbot.bot
dispatcher: python -m ebmbot.dispatcher
web: python -m ebmbot.webserver
24 changes: 1 addition & 23 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
# should speed up the build in CI, where we have a cold cache
cache_from: # should speed up the build in CI, where we have a cold cache
- ghcr.io/opensafely-core/base-docker
- ghcr.io/opensafely-core/bennettbot
- ghcr.io/ebmdatalab/bennettbot
args:
# this makes the image work for later cache_from: usage
- BUILDKIT_INLINE_CACHE=1
Expand All @@ -29,27 +29,6 @@ services:
# use dockers builitin PID daemon
init: true

bot:
extends:
service: prod
container_name: bennettbot-bot
command: python -m ebmbot.bot

dispatcher:
extends:
service: prod
container_name: bennettbot-dispatcher
command: python -m ebmbot.dispatcher

webserver:
extends:
service: prod
container_name: bennettbot-webserver
command: python -m ebmbot.webserver
# host:container ports: container port should match the port in WEBHOOK_ORIGIN
ports:
- "9999:9999"

# main development service
dev:
extends:
Expand Down Expand Up @@ -88,7 +67,6 @@ services:
ports:
- "1234:1234"


test:
extends:
service: dev
Expand Down
2 changes: 1 addition & 1 deletion ebmbot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

APPLICATION_ROOT = Path(__file__).resolve().parent.parent
# If running in docker, this is /app/
EBMBOT_PATH = env.str("EBMBOT_PATH")
EBMBOT_PATH = env.str("EBMBOT_PATH", default=APPLICATION_ROOT)

DB_PATH = env.path("DB_PATH", default=APPLICATION_ROOT / "ebmbot.db")
WORKSPACE_DIR = env.path("WORKSPACE_DIR", default=APPLICATION_ROOT / "workspace")
Expand Down

0 comments on commit d4a2c4e

Please sign in to comment.