From 4aa4b723d0d8e51d4782da0f1acff095dcb0fe68 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Mon, 7 Mar 2016 10:21:19 +0200 Subject: [PATCH 1/5] Dockerfile: pin the version of Vumi installed --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 871766129..8c3c12c5f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ FROM praekeltfoundation/python-base MAINTAINER Praekelt Foundation -RUN pip install vumi +ENV VUMI_VERSION "0.6.2" +RUN pip install vumi==$VUMI_VERSION From 9ca07473e5815114b0d882110f98ab552106a0f4 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Mon, 7 Mar 2016 13:35:47 +0200 Subject: [PATCH 2/5] Add Docker entrypoint script and readme * Move to common directory so Docker hub will pick everything up --- Dockerfile => docker/Dockerfile | 6 ++++ docker/README.md | 60 +++++++++++++++++++++++++++++++++ docker/vumi-entrypoint.sh | 38 +++++++++++++++++++++ 3 files changed, 104 insertions(+) rename Dockerfile => docker/Dockerfile (53%) create mode 100644 docker/README.md create mode 100755 docker/vumi-entrypoint.sh diff --git a/Dockerfile b/docker/Dockerfile similarity index 53% rename from Dockerfile rename to docker/Dockerfile index 8c3c12c5f..520c3a497 100644 --- a/Dockerfile +++ b/docker/Dockerfile @@ -3,3 +3,9 @@ MAINTAINER Praekelt Foundation ENV VUMI_VERSION "0.6.2" RUN pip install vumi==$VUMI_VERSION + +COPY ./vumi-entrypoint.sh /app/vumi-entrypoint.sh +WORKDIR /app + +ENTRYPOINT ["eval-args.sh", "dinit", "/app/vumi-entrypoint.sh"] +CMD [] diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 000000000..9a5e2850d --- /dev/null +++ b/docker/README.md @@ -0,0 +1,60 @@ +# Vumi Docker image +Documentation for Vumi is available online at http://vumi.readthedocs.org/ and in the `docs` directory of the repository. + +## Usage +The Docker image provides an entrypoint for Vumi that allows the configuration of Vumi workers via environment variables. + +### Environment variables +#### Basic options: +* `TWISTD_APPLICATION`: the application to launch with `twistd`, i.e. the command to pass to `twistd` (default: `vumi_worker`) +* `WORKER_CLASS`: the Vumi worker class to use +* `CONFIG_FILE`: the path to the YAML configuration file to use +* `SENTRY_DSN`: the Sentry DSN to use for reporting errors + +#### AMQP options: +AMQP/RabbitMQ options can be set via environment variables. At a minimum, the `AMQP_HOST` variable must be set or else none of the other variables will take effect. +* `AMQP_HOST`: the address for the RabbitMQ server +* `AMQP_PORT`: the port for the RabbitMQ server (default: `5672`) +* `AMQP_VHOST`: the name of the RabbitMQ vhost to use (default: `/`) +* `AMQP_USERNAME`: the username to authenticate with RabbitMQ (default: `guest`) +* `AMQP_PASSWORD`: the password to authenticate with RabbitMQ (default: `guest`) + +#### `VUMI_OPT_` options +Additional options can be passed to Vumi via variables that start with `VUMI_OPT_`. These options will be converted to `--set-option` CLI options. For example, the environment variable `VUMI_OPT_BUCKET=1` will result in the CLI option `--set-option=bucket:1`. + +### `/app` directory +The `/app` directory is created and set as the current working directory. This is the directory where the files (e.g. YAML config) for your application should be put. + +### Examples +Running a built-in worker class without a config file: +```shell +docker run --rm -it \ + -e WORKER_CLASS=vumi.blinkenlights.MetricTimeBucket \ + -e AMQP_HOST=rabbitmq.service.consul \ + -e VUMI_OPT_BUCKETS=3 \ + -e VUMI_OPT_BUCKET_SIZE=10 \ + praekeltfoundation/vumi +``` + +Dockerfile for an image with an external worker class and a config file: +```dockerfile +FROM praekeltfoundation/vumi +RUN pip install vumi-http-api +COPY ./my-config.yaml /app/my-config.yaml +ENV WORKER_CLASS="vumi_http_api.VumiApiWorker" \ + CONFIG_FILE="my-config.yaml" \ + AMQP_HOST="rabbitmq.service.consul" +EXPOSE 8000 +``` + +Dockerfile for an image with custom arguments: +```dockerfile +FROM praekeltfoundation/vumi +RUN pip install go-metrics-api +COPY ./my-config.yaml /app/my-config.yaml +ENV TWISTD_APPLICATION="cyclone" +EXPOSE 8000 +CMD ["--app", "go_metrics.server.MetricsApi", \ + "--port", "8000", \ + "--app-opts", "my-config.yaml"] +``` diff --git a/docker/vumi-entrypoint.sh b/docker/vumi-entrypoint.sh new file mode 100755 index 000000000..8b97ba7aa --- /dev/null +++ b/docker/vumi-entrypoint.sh @@ -0,0 +1,38 @@ +#!/bin/bash -e + +TWISTD_APPLICATION="${TWISTD_APPLICATION:-vumi_worker}" + +WORKER_CLASS_OPT="" +if [ -n "$WORKER_CLASS" ]; do + WORKER_CLASS_OPT="--worker-class $WORKER_CLASS" +done + +CONFIG_OPT="" +if [ -n "$CONFIG_FILE" ]; do + CONFIG_OPT="--config $CONFIG_FILE" +done + +AMQP_OPTS="" +if [ -n "$AMQP_HOST" ]; do + AMQP_OPTS="--hostname $AMQP_HOST \ + --port ${AMQP_PORT:-5672} \ + --vhost ${AMQP_VHOST:-/} \ + --username ${AMQP_USERNAME:-guest} \ + --password ${AMQP_PASSWORD:-guest}" +done + +SENTRY_OPT="" +if [ -n "$SENTRY_DSN" ]; do + SENTRY_OPT="--sentry $SENTRY_DSN" +done + +SET_OPTS=$(env | grep ^VUMI_OPT_ | sed -e 's/^VUMI_OPT_//' -e 's/=/ /' | awk '{printf("%s=%s:%s ", "--set-option", tolower($1), $2);}') + +exec twistd --nodaemon \ + "$TWISTD_APPLICATION" \ + "$WORKER_CLASS_OPT" \ + "$CONFIG_OPT" \ + "$AMQP_OPTS" \ + "$SENTRY_OPT" \ + "$SET_OPTS" \ + "$@" From 66bc224d2225837ed19057cc0ff0312c98cbeb72 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Mon, 7 Mar 2016 13:41:27 +0200 Subject: [PATCH 3/5] Rename TWISTD_APPLICATION to TWISTD_COMMAND, tweak readme --- docker/README.md | 8 ++++---- docker/vumi-entrypoint.sh | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docker/README.md b/docker/README.md index 9a5e2850d..cfa6ba571 100644 --- a/docker/README.md +++ b/docker/README.md @@ -6,13 +6,13 @@ The Docker image provides an entrypoint for Vumi that allows the configuration o ### Environment variables #### Basic options: -* `TWISTD_APPLICATION`: the application to launch with `twistd`, i.e. the command to pass to `twistd` (default: `vumi_worker`) +* `TWISTD_COMMAND`: the command to pass to `twistd` (default: `vumi_worker`) * `WORKER_CLASS`: the Vumi worker class to use * `CONFIG_FILE`: the path to the YAML configuration file to use * `SENTRY_DSN`: the Sentry DSN to use for reporting errors #### AMQP options: -AMQP/RabbitMQ options can be set via environment variables. At a minimum, the `AMQP_HOST` variable must be set or else none of the other variables will take effect. +AMQP/RabbitMQ options can be set via environment variables. At a minimum, the `AMQP_HOST` variable must be set or else none of the other AMQP variables will take effect. * `AMQP_HOST`: the address for the RabbitMQ server * `AMQP_PORT`: the port for the RabbitMQ server (default: `5672`) * `AMQP_VHOST`: the name of the RabbitMQ vhost to use (default: `/`) @@ -20,7 +20,7 @@ AMQP/RabbitMQ options can be set via environment variables. At a minimum, the `A * `AMQP_PASSWORD`: the password to authenticate with RabbitMQ (default: `guest`) #### `VUMI_OPT_` options -Additional options can be passed to Vumi via variables that start with `VUMI_OPT_`. These options will be converted to `--set-option` CLI options. For example, the environment variable `VUMI_OPT_BUCKET=1` will result in the CLI option `--set-option=bucket:1`. +Additional options can be passed to Vumi via variables that start with `VUMI_OPT_`. These variables will be converted to `--set-option` CLI options. For example, the environment variable `VUMI_OPT_BUCKET=1` will result in the CLI option `--set-option=bucket:1`. ### `/app` directory The `/app` directory is created and set as the current working directory. This is the directory where the files (e.g. YAML config) for your application should be put. @@ -52,7 +52,7 @@ Dockerfile for an image with custom arguments: FROM praekeltfoundation/vumi RUN pip install go-metrics-api COPY ./my-config.yaml /app/my-config.yaml -ENV TWISTD_APPLICATION="cyclone" +ENV TWISTD_COMMAND="cyclone" EXPOSE 8000 CMD ["--app", "go_metrics.server.MetricsApi", \ "--port", "8000", \ diff --git a/docker/vumi-entrypoint.sh b/docker/vumi-entrypoint.sh index 8b97ba7aa..12456c84e 100755 --- a/docker/vumi-entrypoint.sh +++ b/docker/vumi-entrypoint.sh @@ -1,6 +1,6 @@ #!/bin/bash -e -TWISTD_APPLICATION="${TWISTD_APPLICATION:-vumi_worker}" +TWISTD_COMMAND="${TWISTD_COMMAND:-vumi_worker}" WORKER_CLASS_OPT="" if [ -n "$WORKER_CLASS" ]; do @@ -29,7 +29,7 @@ done SET_OPTS=$(env | grep ^VUMI_OPT_ | sed -e 's/^VUMI_OPT_//' -e 's/=/ /' | awk '{printf("%s=%s:%s ", "--set-option", tolower($1), $2);}') exec twistd --nodaemon \ - "$TWISTD_APPLICATION" \ + "$TWISTD_COMMAND" \ "$WORKER_CLASS_OPT" \ "$CONFIG_OPT" \ "$AMQP_OPTS" \ From 8b947be0052279c2894d777517d77ff421d602a2 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Mon, 7 Mar 2016 13:48:50 +0200 Subject: [PATCH 4/5] vumi-entrypoint.sh: Fix if statements --- docker/vumi-entrypoint.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docker/vumi-entrypoint.sh b/docker/vumi-entrypoint.sh index 12456c84e..c2b54bc01 100755 --- a/docker/vumi-entrypoint.sh +++ b/docker/vumi-entrypoint.sh @@ -3,28 +3,28 @@ TWISTD_COMMAND="${TWISTD_COMMAND:-vumi_worker}" WORKER_CLASS_OPT="" -if [ -n "$WORKER_CLASS" ]; do +if [ -n "$WORKER_CLASS" ]; then WORKER_CLASS_OPT="--worker-class $WORKER_CLASS" -done +fi CONFIG_OPT="" -if [ -n "$CONFIG_FILE" ]; do +if [ -n "$CONFIG_FILE" ]; then CONFIG_OPT="--config $CONFIG_FILE" -done +fi AMQP_OPTS="" -if [ -n "$AMQP_HOST" ]; do +if [ -n "$AMQP_HOST" ]; then AMQP_OPTS="--hostname $AMQP_HOST \ --port ${AMQP_PORT:-5672} \ --vhost ${AMQP_VHOST:-/} \ --username ${AMQP_USERNAME:-guest} \ --password ${AMQP_PASSWORD:-guest}" -done +fi SENTRY_OPT="" -if [ -n "$SENTRY_DSN" ]; do +if [ -n "$SENTRY_DSN" ]; then SENTRY_OPT="--sentry $SENTRY_DSN" -done +fi SET_OPTS=$(env | grep ^VUMI_OPT_ | sed -e 's/^VUMI_OPT_//' -e 's/=/ /' | awk '{printf("%s=%s:%s ", "--set-option", tolower($1), $2);}') From ef8134bb007f2ac5b966759862b23822939f3706 Mon Sep 17 00:00:00 2001 From: Jamie Hewland Date: Mon, 7 Mar 2016 14:51:14 +0200 Subject: [PATCH 5/5] vumi-entrypoint.sh: Unquote exec args so empty strings aren't args --- docker/vumi-entrypoint.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docker/vumi-entrypoint.sh b/docker/vumi-entrypoint.sh index c2b54bc01..e18f4fb2d 100755 --- a/docker/vumi-entrypoint.sh +++ b/docker/vumi-entrypoint.sh @@ -29,10 +29,10 @@ fi SET_OPTS=$(env | grep ^VUMI_OPT_ | sed -e 's/^VUMI_OPT_//' -e 's/=/ /' | awk '{printf("%s=%s:%s ", "--set-option", tolower($1), $2);}') exec twistd --nodaemon \ - "$TWISTD_COMMAND" \ - "$WORKER_CLASS_OPT" \ - "$CONFIG_OPT" \ - "$AMQP_OPTS" \ - "$SENTRY_OPT" \ - "$SET_OPTS" \ + $TWISTD_COMMAND \ + $WORKER_CLASS_OPT \ + $CONFIG_OPT \ + $AMQP_OPTS \ + $SENTRY_OPT \ + $SET_OPTS \ "$@"