Selaa lähdekoodia

Setting up (to be refined) docker logic

Kirk Trombley 5 vuotta sitten
vanhempi
commit
00a24d89f4
7 muutettua tiedostoa jossa 191 lisäystä ja 19 poistoa
  1. 2 0
      .dockerignore
  2. 27 0
      Dockerfile
  3. 50 0
      nginx.conf
  4. 91 0
      run-docker.sh
  5. 13 18
      server/app.py
  6. 7 0
      start-gunicorn-and-nginx.sh
  7. 1 1
      ui/src/App.js

+ 2 - 0
.dockerignore

@@ -0,0 +1,2 @@
+**/.venv/
+**/*.pyc

+ 27 - 0
Dockerfile

@@ -0,0 +1,27 @@
+FROM nginx
+
+LABEL maintainer="Kirk Trombley <ktrom3894@gmail.com>"
+
+RUN apt update \
+ && apt install --no-install-recommends --no-install-suggests -y python3 python3-pip \
+ && pip3 install --upgrade pip setuptools
+
+RUN pip3 install gunicorn
+
+WORKDIR /app
+
+COPY server/* ./
+
+RUN pip3 install -r requirements.txt
+
+COPY nginx.conf /etc/nginx/nginx.conf
+
+COPY ui/build /usr/share/nginx/html
+
+COPY start-gunicorn-and-nginx.sh .
+
+EXPOSE 80
+
+STOPSIGNAL SIGTERM
+
+CMD ./start-gunicorn-and-nginx.sh

+ 50 - 0
nginx.conf

@@ -0,0 +1,50 @@
+user  nginx;
+worker_processes  1;
+
+error_log  /var/log/nginx/error.log warn;
+pid        /var/run/nginx.pid;
+
+events {
+    worker_connections  1024;
+}
+
+http {
+    include       /etc/nginx/mime.types;
+    default_type  application/octet-stream;
+
+    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+                      '$status $body_bytes_sent "$http_referer" '
+                      '"$http_user_agent" "$http_x_forwarded_for"';
+
+    access_log  /var/log/nginx/access.log  main;
+
+    sendfile        on;
+    #tcp_nopush     on;
+
+    keepalive_timeout  65;
+
+    gzip  on;
+
+    upstream backend {
+        server unix:/app/ggsh.sock;
+    }
+
+    server {
+        listen       80;
+        server_name  localhost;
+
+        location /api {
+            proxy_pass http://backend/;
+        }
+
+        location / {
+            root   /usr/share/nginx/html;
+            index  index.html index.htm;
+        }
+
+        error_page   500 502 503 504  /50x.html;
+        location = /50x.html {
+            root   /usr/share/nginx/html;
+        }
+    }
+}

+ 91 - 0
run-docker.sh

@@ -0,0 +1,91 @@
+#!/usr/bin/env sh
+
+if [ $# -lt 1 ] || [ "$1" == "help" ] || [ "$1" == "h" ]
+then
+    echo "usage: $0 (help|status|clean|run|logs) [container-name]"
+    echo "  help   - print this message"
+    echo "  status - report whether a container named container-name can be found, and whether or not it is running"
+    echo "  clean  - kill and remove any existing containers matching container-name"
+    echo "  run    - kill and remove any existing containers matching container-name, build the current directory into a new image, and deploy a new container container-name"
+    echo "  logs   - tail the logs of the container named container-name, equivalent to docker logs -f container-name"
+    echo "  container-name defaults to ggsh-instance"
+    echo "  Run as root to ensure docker can be used, or set FORCE_NOROOT=* to force the script to run anyway."
+    exit 1
+fi
+
+if [ "$EUID" -ne 0 ] && [ -z "$FORCE_NOROOT" ]
+then
+    echo "$0 must be run as root to use docker. Set FORCE_NOROOT=* to force the script to run anyway."
+    exit 1
+fi
+
+if [ $# -gt 1 ]
+then
+    CONTAINER_NAME=$2
+else
+    CONTAINER_NAME="ggsh-instance"
+fi
+
+status_check() {
+    docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2> /dev/null
+}
+
+clean_container() {
+    STATUS=$(status_check)
+    if [ "$STATUS" = "true" ]
+    then
+        echo "Container $CONTAINER_NAME is running and will be stopped and removed."
+        docker kill $CONTAINER_NAME
+        docker rm $CONTAINER_NAME
+    elif [ "$STATUS" = "false" ]
+    then
+        echo "Container $CONTAINER_NAME is stopped and will be removed."
+        docker rm $CONTAINER_NAME
+    else
+        echo "No existing container $CONTAINER_NAME could be found, no cleanup will be performed."
+    fi
+}
+
+case $1 in
+    "l"|"logs")
+        STATUS=$(status_check)
+        if [ "$STATUS" = "true" ] || [ "$STATUS" = "false" ]
+        then
+            docker logs -f $CONTAINER_NAME
+        else
+            echo "No existing container $CONTAINER_NAME could be found."
+        fi
+    ;;
+    "r"|"run")
+        clean_container
+        echo "Building UI"
+        pushd ui
+        yarn build
+        popd
+        echo "Building GeoGuessr Self Host image as ggsh:latest"
+        docker build -t ggsh:latest .
+        echo "Executing new container $CONTAINER_NAME using ggsh:latest"
+        docker run -p6070:80 --name $CONTAINER_NAME -d ggsh
+        echo "Geoguessr Self Host UI accessible at http://localhost:6070/"
+        echo "Geoguessr Self Host API accessible at http://localhost:6070/api/"
+    ;;
+    "c"|"clean")
+        clean_container
+    ;;
+    "s"|"status")
+        STATUS=$(status_check)
+        if [ "$STATUS" = "true" ]
+        then
+            echo "Existing container $CONTAINER_NAME is running."
+        elif [ "$STATUS" = "false" ]
+        then
+            echo "Existing container $CONTAINER_NAME is stopped."
+        else
+            echo "No existing container $CONTAINER_NAME could be found."
+        fi
+    ;;
+    *)
+        echo "Unknown option $1, must be one of help, status, clean, run, logs."
+        exit 1
+    ;;
+esac

+ 13 - 18
server/app.py

@@ -5,26 +5,21 @@ from db import db
 from misc_api import misc
 from game_api import game
 
+app = Flask(__name__)
 
-def init_app():
-    app = Flask(__name__)
+with open("secrets.toml") as infile:
+    secrets = toml.load(infile)
+app.secret_key = secrets["secret_key"]
+app.config["GROUP_PASS"] = secrets["group_pass"]
+app.config["GOOGLE_API_KEY"] = secrets["google_api_key"]
+app.config["SQLALCHEMY_DATABASE_URI"] = secrets["db_uri"]
+app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
 
-    with open("secrets.toml") as infile:
-        secrets = toml.load(infile)
-    app.secret_key = secrets["secret_key"]
-    app.config["GROUP_PASS"] = secrets["group_pass"]
-    app.config["GOOGLE_API_KEY"] = secrets["google_api_key"]
-    app.config["SQLALCHEMY_DATABASE_URI"] = secrets["db_uri"]
-    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
-
-    app.register_blueprint(misc)
-    app.register_blueprint(game, url_prefix="/game")
-
-    db.init_app(app)
-    db.create_all(app=app)
-
-    return app
+app.register_blueprint(misc)
+app.register_blueprint(game, url_prefix="/game")
 
+db.init_app(app)
+db.create_all(app=app)
 
 if __name__ == "__main__":
-    init_app().run("0.0.0.0", 5000, debug=True)
+    app.run("0.0.0.0", 5000, debug=True)

+ 7 - 0
start-gunicorn-and-nginx.sh

@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+
+echo "Starting gunicorn on port 8080 on separate process"
+gunicorn --workers=1 --bind=unix:/app/ggsh.sock app:app &
+
+echo "Starting nginx daemon"
+nginx -g "daemon off;"

+ 1 - 1
ui/src/App.js

@@ -8,7 +8,7 @@ function App() {
       <header className="App-header">
         <img src={logo} className="App-logo" alt="logo" />
         <p>
-          Edit <code>src/App.js</code> and save to reload.
+          Edit <code>src/App.js</code> and save to reload. Testing!
         </p>
         <a
           className="App-link"