Skip to content

Commit

Permalink
Removing FlaskGroup.cli + adding run.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmarieta committed Mar 4, 2024
1 parent 323fd19 commit 8c10d48
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 19 deletions.
6 changes: 3 additions & 3 deletions docker-compose.debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flask/.ebextensions/01_flask.config
Original file line number Diff line number Diff line change
@@ -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
11 changes: 1 addition & 10 deletions flask/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
socketio.run(application, debug=True, host='0.0.0.0')
10 changes: 10 additions & 0 deletions flask/init_db.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion nginx/conf.d/old_server_HTTPS.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ server {
upstream flask_nodes {
ip_hash;

server flask_1:5000;
server flask:5000;
server flask_2:5000;
}

Expand Down
2 changes: 1 addition & 1 deletion nginx/conf.d/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
upstream flask_nodes {
ip_hash;

server flask_1:5000;
server flask:5000;
server flask_2:5000;
}

Expand Down
65 changes: 65 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c10d48

Please sign in to comment.