From 8c10d48bb06785b63d6cc09e9697b8058782f678 Mon Sep 17 00:00:00 2001 From: Rmarieta Date: Mon, 4 Mar 2024 09:22:46 +0100 Subject: [PATCH] Removing FlaskGroup.cli + adding run.sh --- docker-compose.debug.yml | 6 +-- docker-compose.yml | 6 +-- flask/.ebextensions/01_flask.config | 2 +- flask/application.py | 11 +---- flask/init_db.py | 10 +++++ nginx/conf.d/old_server_HTTPS.conf | 2 +- nginx/conf.d/server.conf | 2 +- run.sh | 65 +++++++++++++++++++++++++++++ 8 files changed, 85 insertions(+), 19 deletions(-) create mode 100644 flask/init_db.py create mode 100644 run.sh diff --git a/docker-compose.debug.yml b/docker-compose.debug.yml index 04c5a13..3bf7179 100644 --- a/docker-compose.debug.yml +++ b/docker-compose.debug.yml @@ -24,9 +24,9 @@ services: timeout: 5s retries: 3 - flask_1: + flask: build: ./flask - container_name: flask_1 + container_name: flask env_file: - .env command: python application.py @@ -65,7 +65,7 @@ services: - ./nginx/nginx.conf:/etc/nginx/nginx.conf # - ./nginx/ssl-certs:/etc/nginx/ssl-certs depends_on: - - flask_1 + - flask - flask_2 # to persist container volumes diff --git a/docker-compose.yml b/docker-compose.yml index 8f1016c..242214b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -24,9 +24,9 @@ services: timeout: 5s retries: 3 - flask_1: + flask: build: ./flask - container_name: flask_1 + container_name: flask env_file: - .env depends_on: @@ -63,7 +63,7 @@ services: - ./nginx/nginx.conf:/etc/nginx/nginx.conf # - ./nginx/ssl-certs:/etc/nginx/ssl-certs depends_on: - - flask_1 + - flask - flask_2 # to persist container volumes diff --git a/flask/.ebextensions/01_flask.config b/flask/.ebextensions/01_flask.config index 13fde3a..e3e908c 100644 --- a/flask/.ebextensions/01_flask.config +++ b/flask/.ebextensions/01_flask.config @@ -1,4 +1,4 @@ container_commands: 01_initdb: - command: "source /var/app/venv/*/bin/activate && python3 application.py create_all" + command: "source /var/app/venv/*/bin/activate && python3 init_db.py" leader_only: true \ No newline at end of file diff --git a/flask/application.py b/flask/application.py index 2bb733c..43c6536 100644 --- a/flask/application.py +++ b/flask/application.py @@ -3,16 +3,8 @@ from flask import Flask, jsonify from app import create_app, db, socketio -from flask.cli import FlaskGroup application = create_app() -cli = FlaskGroup(application) - -# define that to create the tables having access to the app context from the container shell -@cli.command('create_all') -def create_all(): - with application.app_context(): - db.create_all() import os HOSTNAME = os.uname().nodename @@ -22,5 +14,4 @@ def get_hostname(): return jsonify({'hostname': HOSTNAME}) if __name__ == "__main__": - cli() - # socketio.run(application, debug=True, host='0.0.0.0') \ No newline at end of file + socketio.run(application, debug=True, host='0.0.0.0') \ No newline at end of file diff --git a/flask/init_db.py b/flask/init_db.py new file mode 100644 index 0000000..9f68182 --- /dev/null +++ b/flask/init_db.py @@ -0,0 +1,10 @@ +from gevent import monkey +monkey.patch_all() + +from flask import Flask +from app import create_app, db + +if __name__ == "__main__": + application = create_app() + with application.app_context(): + db.create_all() \ No newline at end of file diff --git a/nginx/conf.d/old_server_HTTPS.conf b/nginx/conf.d/old_server_HTTPS.conf index ddfe48f..1c7294f 100644 --- a/nginx/conf.d/old_server_HTTPS.conf +++ b/nginx/conf.d/old_server_HTTPS.conf @@ -9,7 +9,7 @@ server { upstream flask_nodes { ip_hash; - server flask_1:5000; + server flask:5000; server flask_2:5000; } diff --git a/nginx/conf.d/server.conf b/nginx/conf.d/server.conf index 14672ef..c69b4fd 100644 --- a/nginx/conf.d/server.conf +++ b/nginx/conf.d/server.conf @@ -2,7 +2,7 @@ upstream flask_nodes { ip_hash; - server flask_1:5000; + server flask:5000; server flask_2:5000; } diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..6c752b0 --- /dev/null +++ b/run.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +# default values +ENVIRONMENT="prod" +DETACH=false +VOLUMES=false + +# parse command line arguments +while [[ $# -gt 0 ]]; do + key="$1" + + case $key in + -e|--env) + ENVIRONMENT="$2" + shift + ;; + -d|--detach) + DETACH=true + ;; + -v|--volumes) + VOLUMES=true + ;; + *) + # unknown option + echo "Unknown option: $1" + exit 1 + ;; + esac + shift +done + +# run the corresponding docker-compose file +case $ENVIRONMENT in + "debug") + DOCKER_COMPOSE_FILE="docker-compose.debug.yml" + ;; + "dev") + DOCKER_COMPOSE_FILE="docker-compose.dev.yml" + ;; + "prod") + DOCKER_COMPOSE_FILE="docker-compose.prod.yml" + ;; + *) + echo "Invalid environment. Use 'debug', 'dev', or 'prod'." + exit 1 + ;; +esac + +# kill the previous containers and clear the volumes if requested +if [ "$VOLUMES" = false ]; then + docker-compose -f "$DOCKER_COMPOSE_FILE" down +else + docker-compose -f "$DOCKER_COMPOSE_FILE" down -v +fi + +# build and start the services +docker-compose -f "$DOCKER_COMPOSE_FILE" up --build -d + +# run create_all script +docker-compose -f "$DOCKER_COMPOSE_FILE" exec flask python init_db.py + +# if not in detached mode, show the real-time logs in the current shell +if [ "$DETACH" = false ]; then + docker-compose -f "$DOCKER_COMPOSE_FILE" logs -f +fi