From 7d9b1e7950021f44886ae2d3c0862f39a60eb43f Mon Sep 17 00:00:00 2001 From: Joshua May Date: Sat, 31 Aug 2019 16:14:57 -0700 Subject: [PATCH] Added Docker set up for local dev, with Postgres dependency --- .env | 1 - .env.example | 4 + .gitignore | 1 + Dockerfile | 4 +- README.md | 30 ++++-- docker-compose.development.yml | 16 +++ docker-compose.yml | 5 +- wait-for-it.sh | 178 +++++++++++++++++++++++++++++++++ 8 files changed, 225 insertions(+), 14 deletions(-) delete mode 100644 .env create mode 100644 .env.example create mode 100644 docker-compose.development.yml create mode 100644 wait-for-it.sh diff --git a/.env b/.env deleted file mode 100644 index fbfbe8e..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -FLASK_CONFIG=heroku diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..6e70c07 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +FLASK_CONFIG=heroku + +# default development database via Docker: +# DEV_DATABASE_URL=postgresql://postgres:plop@sirius-database/sirius-dev \ No newline at end of file diff --git a/.gitignore b/.gitignore index 9289f2a..037ab42 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Custom venv/ +.env # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/Dockerfile b/Dockerfile index 0aa4064..3dda9b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM debian:9.3-slim +FROM debian:10-slim WORKDIR /sirius @@ -22,6 +22,4 @@ EXPOSE 5000 ADD . . -RUN ./manage.py db upgrade - ENTRYPOINT honcho start diff --git a/README.md b/README.md index b5fafed..93dbf61 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# nord-sirius +# nord-sirius ![CloudBerg Little Printer](https://i.vimeocdn.com/video/222115839_1280x720.jpg) @@ -11,12 +11,13 @@ Want to try it out? We have an instance running at http://littleprinter.nordproj Want to get your Little Printer online? - Hack the Berg Cloud bridge. Follow our [step-by-step guide](https://docs.google.com/document/d/1JT1f2ClVdAnjrnby92V9ONBnN05EFQGYpLG5ijl5KRI/edit?usp=sharing) here. - - If you've already got a hacked bridge using the alpha.littleprinter.com - backend, use the guide above but start at step 6. + + - If you've already got a hacked bridge using the alpha.littleprinter.com + backend, use the guide above but start at step 6. - Once it's paired with this server, you can use Device Keys with our iOS app, -[Little Printers](https://itunes.apple.com/us/app/little-printers/id1393105914?ls=1&mt=8), -which is also [open source](https://github.com/nordprojects/littleprinters-ios-app)! + [Little Printers](https://itunes.apple.com/us/app/little-printers/id1393105914?ls=1&mt=8), + which is also [open source](https://github.com/nordprojects/littleprinters-ios-app)! ### Using the device key API @@ -30,12 +31,26 @@ interface, and go to that URL to view API documentation. We run an instance of this server on Heroku, at [littleprinter.nordprojects.co](https://littleprinter.nordprojects.co). You are welcome to use our instance! If you want to run it yourself, this app will run on Heroku, but you need a -static IP address due to the way the Berg Bridge connects. We have a droplet +static IP address due to the way the Berg Bridge connects. We have a droplet at Digital Ocean running nginx to forward the HTTPS connection on to Heroku. It should also be possible to run in Docker, dokku, or directly with gunicorn, but we don't use it that way :). +## Running locally via Docker + +There's a development Docker setup that adds Postgres for you, by running: + +```sh +docker-compose -f docker-compose.yml -f docker-compose.development.yml up +``` + +Or if you have your own database, you can configure the `DEV_DATABASE_URL` environment variable in `.env`, and then simply run: + +``` +docker-compose up +``` + ### Environment variables The server can be configured with the following variables: @@ -47,7 +62,7 @@ FLASK_CONFIG=... DATABASE_URL=... ``` -These can be set in the `.env` file in your checkout. +These can be set in the `.env` file, and an example is available in `.env.sample` in your checkout. ### Creating fake printers and friends @@ -106,7 +121,6 @@ flask_sockets gunicorn worker) which runs the websocket/bridge_address mapping in a global dictionary; it then loops forever, decoding messages as they come in. - ### Claim codes Devices are associated with an account when a user enters a "claim diff --git a/docker-compose.development.yml b/docker-compose.development.yml new file mode 100644 index 0000000..4f25812 --- /dev/null +++ b/docker-compose.development.yml @@ -0,0 +1,16 @@ +version: "3" + +services: + sirius: + depends_on: + - sirius-database + command: ["/wait-for-it.sh", "db:5342", "--", "./manage.py db upgrade"] + + sirius-database: + image: postgres + healthcheck: + test: "exit 0" + ports: + - "32770:5432" + environment: + - POSTGRES_PASSWORD=plop diff --git a/docker-compose.yml b/docker-compose.yml index 12f4f31..3728b4b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '2' +version: "3" services: sirius: @@ -8,4 +8,5 @@ services: ports: - 5000:5000 env_file: - - .env + - .env + command: "./manage.py db upgrade" diff --git a/wait-for-it.sh b/wait-for-it.sh new file mode 100644 index 0000000..071c2be --- /dev/null +++ b/wait-for-it.sh @@ -0,0 +1,178 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -h HOST | --host=HOST Host or IP under test + -p PORT | --port=PORT TCP port under test + Alternatively, you specify the host and port as host:port + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + WAITFORIT_start_ts=$(date +%s) + while : + do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z $WAITFORIT_HOST $WAITFORIT_PORT + WAITFORIT_result=$? + else + (echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1 + WAITFORIT_result=$? + fi + if [[ $WAITFORIT_result -eq 0 ]]; then + WAITFORIT_end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds" + break + fi + sleep 1 + done + return $WAITFORIT_result +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + else + timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT & + fi + WAITFORIT_PID=$! + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + WAITFORIT_hostport=(${1//:/ }) + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -h) + WAITFORIT_HOST="$2" + if [[ $WAITFORIT_HOST == "" ]]; then break; fi + shift 2 + ;; + --host=*) + WAITFORIT_HOST="${1#*=}" + shift 1 + ;; + -p) + WAITFORIT_PORT="$2" + if [[ $WAITFORIT_PORT == "" ]]; then break; fi + shift 2 + ;; + --port=*) + WAITFORIT_PORT="${1#*=}" + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + WAITFORIT_BUSYTIMEFLAG="-t" + +else + WAITFORIT_ISBUSY=0 + WAITFORIT_BUSYTIMEFLAG="" +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi -- 2.47.3