From 4ef3aae193ddf5f2564c87a1ddb0d79037d784b9 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 15 Dec 2023 23:46:07 +0000 Subject: [PATCH] Make util scripts work on both docker-compose v1 and v2 --- utils/bash.sh | 5 ++++- utils/common.sh | 15 ++++++++++++++- utils/create_migration.sh | 9 ++++++--- utils/db.sh | 5 ++++- utils/downgrade_migration.sh | 5 ++++- utils/reload.sh | 5 ++++- utils/restore_bk.sh | 3 ++- utils/run_migrations.sh | 5 ++++- utils/tests.sh | 5 ++++- utils/tests_cov.sh | 5 ++++- utils/update.sh | 2 ++ utils/update_trans.sh | 2 ++ 12 files changed, 54 insertions(+), 12 deletions(-) mode change 100644 => 100755 utils/common.sh diff --git a/utils/bash.sh b/utils/bash.sh index e39e0627..3c110b5b 100755 --- a/utils/bash.sh +++ b/utils/bash.sh @@ -2,4 +2,7 @@ # Open SSH to app instance -docker exec -it contentdb_app_1 bash +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec -it "$(container app)" bash diff --git a/utils/common.sh b/utils/common.sh old mode 100644 new mode 100755 index c9c6a313..f84681b0 --- a/utils/common.sh +++ b/utils/common.sh @@ -1,3 +1,16 @@ +#!/bin/bash + +set -e + +docker_compose_version=$(docker-compose --version --short) +docker_compose_major=${docker_compose_version:0:1} + +if [[ $docker_compose_major == "2" ]]; then + sep="-" +else + sep="_" +fi + container() { - contentdb-$1 + echo "contentdb${sep}$1${sep}1" } diff --git a/utils/create_migration.sh b/utils/create_migration.sh index 4c739371..c16ac8d8 100755 --- a/utils/create_migration.sh +++ b/utils/create_migration.sh @@ -2,8 +2,11 @@ # Create a database migration, and copy it back to the host. -docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db migrate" -docker exec -u root contentdb_app_1 sh -c "cp /home/cdb/migrations/versions/* /source/migrations/versions/" +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec "$(container app)" sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db migrate" +docker exec -u root "$(container app)" sh -c "cp /home/cdb/migrations/versions/* /source/migrations/versions/" USER=$(whoami) -sudo chown -R $USER:$USER migrations/versions +sudo chown -R "$USER:$USER" migrations/versions diff --git a/utils/db.sh b/utils/db.sh index 99eeadb1..27ffb4b3 100755 --- a/utils/db.sh +++ b/utils/db.sh @@ -2,4 +2,7 @@ # Open SQL console for the database -docker exec -it contentdb_db_1 psql contentdb contentdb +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec -it "$(container db)" psql contentdb contentdb diff --git a/utils/downgrade_migration.sh b/utils/downgrade_migration.sh index e94e1628..cf34d6dc 100755 --- a/utils/downgrade_migration.sh +++ b/utils/downgrade_migration.sh @@ -2,4 +2,7 @@ # Create a database migration, and copy it back to the host. -docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db downgrade" +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec "$(container app)" sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db downgrade" diff --git a/utils/reload.sh b/utils/reload.sh index 5d2f2d11..0211c4b3 100755 --- a/utils/reload.sh +++ b/utils/reload.sh @@ -2,4 +2,7 @@ # Hot/live reload - only works in debug mode -docker exec contentdb_app_1 sh -c "cp -r /source/* ." +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec "$(container app)" sh -c "cp -r /source/* ." diff --git a/utils/restore_bk.sh b/utils/restore_bk.sh index 1c54e30f..5bff4226 100755 --- a/utils/restore_bk.sh +++ b/utils/restore_bk.sh @@ -3,6 +3,7 @@ # Restores backup set -e +. "${BASH_SOURCE%/*}/common.sh" if [ -z "$1" ]; then echo "Usage: ./utils/restore_bk.sh path/to/backup.sql" @@ -24,4 +25,4 @@ if [[ "$BKFILE" == *.gpg ]]; then fi echo "Importing backup from $BKFILE" -cat $BKFILE | docker exec -i contentdb_db_1 psql contentdb contentdb +cat $BKFILE | docker exec -i "$(container db)" psql contentdb contentdb diff --git a/utils/run_migrations.sh b/utils/run_migrations.sh index 6618919f..4fd95771 100755 --- a/utils/run_migrations.sh +++ b/utils/run_migrations.sh @@ -2,5 +2,8 @@ # Run all pending migrations +set -e +. "${BASH_SOURCE%/*}/common.sh" + ./utils/reload.sh -docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db upgrade" +docker exec "$(container app)" sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db upgrade" diff --git a/utils/tests.sh b/utils/tests.sh index 2330a234..c1556792 100755 --- a/utils/tests.sh +++ b/utils/tests.sh @@ -1,3 +1,6 @@ #!/bin/sh -docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --disable-warnings" +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec "$(container app)" sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --disable-warnings" diff --git a/utils/tests_cov.sh b/utils/tests_cov.sh index 82df3d15..2f7d8939 100755 --- a/utils/tests_cov.sh +++ b/utils/tests_cov.sh @@ -1,3 +1,6 @@ #!/bin/sh -docker exec contentdb_app_1 sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --cov=app --disable-warnings" +set -e +. "${BASH_SOURCE%/*}/common.sh" + +docker exec "$(container app)" sh -c "FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py python -m pytest app/tests/ --cov=app --disable-warnings" diff --git a/utils/update.sh b/utils/update.sh index c70a0524..377ea803 100755 --- a/utils/update.sh +++ b/utils/update.sh @@ -5,6 +5,8 @@ # This is for production use. See reload.sh for debug mode hot/live reloading. # +set -e + docker-compose build app docker-compose build worker diff --git a/utils/update_trans.sh b/utils/update_trans.sh index b51b7460..cb821323 100755 --- a/utils/update_trans.sh +++ b/utils/update_trans.sh @@ -1,4 +1,6 @@ #!/bin/bash +set -e + pybabel extract -F babel.cfg -k lazy_gettext -o translations/messages.pot . pybabel update -i translations/messages.pot -d translations --no-fuzzy-matching