From 33e585dfe996c89de5cbcb47e7dcc6875e2e7311 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Mon, 24 Aug 2020 17:04:13 +0200
Subject: [PATCH] Docker config and docs. #1

---
 .gitignore                                    |  2 +
 Dockerfile                                    |  6 +-
 README.md                                     | 57 +++++++++++++-----
 .../gunicorn/sample-docker.gunicorn.conf.py   |  4 +-
 config/sample/sample-docker.engine-api.ini    | 60 +++++++++++++++++++
 run.sh                                        |  7 ++-
 6 files changed, 116 insertions(+), 20 deletions(-)
 create mode 100644 config/sample/sample-docker.engine-api.ini

diff --git a/.gitignore b/.gitignore
index 2733152..9b709d0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -72,3 +72,5 @@ python
 # Configurations
 config/engine-api.ini
 config/gunicorn.conf.py
+config/docker/engine-api.ini
+config/docker/gunicorn.conf.py
diff --git a/Dockerfile b/Dockerfile
index d4cfd45..5f412ab 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -10,14 +10,16 @@ RUN mkdir -p /var/log/aura
 COPY . /srv
 WORKDIR /srv
 RUN pip3 install --no-cache-dir -r requirements.txt
+RUN pip3 install --no-cache-dir -r contrib/mariadb-requirements.txt
+ENV PYTHONPATH "${PYTHONPATH}:/srv/src"
 
 # Create default config
 
-COPY config/sample/gunicorn/sample-docker.gunicorn.conf.py config/docker/gunicorn.conf.py
+COPY ./config/sample/gunicorn/sample-docker.gunicorn.conf.py /srv/config/gunicorn.conf.py
 
 # Start the API Server
 
 EXPOSE 8008
 ENTRYPOINT ["gunicorn"]
-CMD ["-c", "/srv/config/docker/gunicorn.conf.py", "src.app:app"]
+CMD ["-c", "/srv/config/gunicorn.conf.py", "src.app:app"]
 
diff --git a/README.md b/README.md
index 6489845..f16892b 100644
--- a/README.md
+++ b/README.md
@@ -22,7 +22,9 @@
         - [Running with Systemd](#running-with-systemd)
         - [Running with Supervisor](#running-with-supervisor)
         - [Running with Docker](#running-with-docker)
-    - [Using the API](#using-the-api)
+            - [Creating a local image (Developers)](#creating-a-local-image-developers)
+            - [Publish new image (Developers)](#publish-new-image-developers)
+    - [Using the API (Developers)](#using-the-api-developers)
 - [About](#about)
 
 <!-- /TOC -->
@@ -218,7 +220,7 @@ If this is succeeding, you can now proceed to configure Engine API to run as a s
 
 The Systemd unit file configuration expects to be running under the user `engineuser`. To create such user type:
 
-```shell
+```bash
     sudo adduser engineuser
     sudo adduser engineuser sudo
 ```
@@ -228,32 +230,32 @@ Engine API installed under `/opt/aura/engine-api` and `engineuser` owning the fi
 
 Next login to `engineuser` and give it permissions to the unit file
 
-```shell
+```bash
   su engineuser
   sudo chmod 644 /etc/systemd/system/aura-engine-api.service
 ```
 
 Let's start the service
 
-```shell
+```bash
 sudo systemctl start aura-engine-api
 ```
 
 And check if it has started successfully
 
-```shell
+```bash
 sudo systemctl status aura-engine-api
 ```
 
 If you experience issues and need more information, check the syslog while starting the service
 
-```shell
+```bash
 tail -f /var/log/syslog
 ```
 
 You can stop or restart the service with one of these
 
-```shell
+```bash
 sudo systemctl stop aura-engine-api
 sudo systemctl restart aura-engine-api
 ```
@@ -267,20 +269,47 @@ can find an example Supervisor configuration file. Follow the initial steps of t
 
 ### Running with Docker
 
-To run the server in a Docker container, please execute the following from the root directory:
+Having the configuration files `engine-api.ini` and `gunicorn.conf.py` located in `./config/docker`,
+you can run the API server in a Docker container this way
 
 ```bash
-# Building the image
-./run.sh docker:build
+exec sudo docker run \
+    --network="host" \
+    --name engine-api \
+    --rm -d \
+    -u $UID:$GID \
+    -p 8008:8008 \
+    -v "$BASE_D":/srv \
+    -v "$BASE_D/config/docker":/srv/config \
+    --tmpfs /var/log/aura/ autoradio/engine-api
+```
 
-# Push the current image to dockerhub.com
-./run.sh docker:push
+The project also contains a convenience script to get started with a one-liner
 
-# Starting up a container
+```bash
+# Start up a container
 ./run.sh docker:api
 ```
 
-## Using the API
+#### Creating a local image (Developers)
+
+If you are a developer and want to create a local image, run
+
+```bash
+# Build the image
+./run.sh docker:build
+```
+
+#### Publish new image (Developers)
+
+If  you are developer and want to publish a new image to DockerHub, run
+
+```bash
+# Releasing the image to DockerHub
+./run.sh docker:push
+```
+
+## Using the API (Developers)
 
 You can find details on the available API endpoints here: https://app.swaggerhub.com/apis/AURA-Engine/engine-api/1.0.0
 
diff --git a/config/sample/gunicorn/sample-docker.gunicorn.conf.py b/config/sample/gunicorn/sample-docker.gunicorn.conf.py
index fef60f8..6db1bf9 100644
--- a/config/sample/gunicorn/sample-docker.gunicorn.conf.py
+++ b/config/sample/gunicorn/sample-docker.gunicorn.conf.py
@@ -18,8 +18,8 @@
 #       range.
 #
 
-pythonpath = "/opt/aura/engine-api"
-bind = '172.17.0.1:8008'
+pythonpath = "/srv"
+bind = '0.0.0.0:8008'
 backlog = 2048
 
 #
diff --git a/config/sample/sample-docker.engine-api.ini b/config/sample/sample-docker.engine-api.ini
new file mode 100644
index 0000000..9ec2ac2
--- /dev/null
+++ b/config/sample/sample-docker.engine-api.ini
@@ -0,0 +1,60 @@
+
+#######################
+# Engine API Settings #
+#######################
+
+
+[database]
+db_user="aura"
+db_name="aura_engine_api"
+db_pass="---SECRET--PASSWORD---"
+db_host="localhost"
+db_charset="utf8"
+
+[monitoring]
+logdir="./logs"
+# possible values: debug, info, warning, error, critical
+loglevel="info"
+debug_flask="false"
+
+[api]
+api_port=8008
+
+
+[federation]
+
+# Defines the engine number id for identification of record sources. Default values are:
+# 
+#   1 ... Engine 1 (main node)
+#   2 ... Engine 2 (main node, not needed for single deployment)
+#   0 ... Sync Host (sync node, not needed for single engine deployment)
+#
+# Engine API supports two deployment models:
+#
+#   - "main": Deployed together with some `engine` (Single instance or for redundant engines)
+#   - "sync": Independent deployment, in charge of syncing data of two main-nodes
+#
+# The `synch_host` identifies the host where data is gathered from/synced to, depended on the
+# chosen `node_type`.
+
+# NODE 1
+; host_id=1
+; sync_host="http://engine.sync.local:8008"
+
+# NODE 2
+; host_id=2
+; sync_host="http://engine.sync.local:8008"
+
+# NODE SYNC
+; host_id=0
+; main_host_1="http://engine1.local:8008"
+; main_host_2="http://engine2.local:8008"
+; default_source=1
+; sync_interval=3600
+; sync_batch_size=100
+; sync_step_sleep=2
+
+# API endpoints to sync data from main to child nodes
+sync_api_store_playlog="/api/v1/playlog/store"
+sync_api_store_healthlog="/api/v1/source/health"
+sync_api_get_playlog="/api/v1/playlog"
\ No newline at end of file
diff --git a/run.sh b/run.sh
index 4e10d2b..b4ced91 100755
--- a/run.sh
+++ b/run.sh
@@ -101,11 +101,14 @@ if [[ $docker == "true" ]]; then
 	### Runs Engine API using Gunicorn ###
 
 	if [[ $mode == "api" ]]; then
-		exec sudo docker run --name engine-api --rm -it \
+		exec sudo docker run \
+			--network="host" \
+			--name engine-api \
+			--rm -d \
 			-u $UID:$GID \
 			-p 8008:8008 \
 			-v "$BASE_D":/srv \
-			-v "$BASE_D/config/docker":/etc/aura  \
+			-v "$BASE_D/config/docker":/srv/config \
 			--tmpfs /var/log/aura/ autoradio/engine-api
 	fi
 
-- 
GitLab