forked from betagouv/sqlalchemy-api-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlaah
executable file
·106 lines (78 loc) · 3.11 KB
/
sqlaah
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
set -o nounset
# =============================================
# Global config of the application
# =============================================
export COMMAND_NAME="sqlaah"
export DB_NAME="sqlalchemy_api_handler"
export GITHUB_NAME="betagouv/sqlalchemy-api-handler"
export MODULE_NAME="sqlalchemy_api_handler"
export PIP_NAME="SQLAlchemy-Api-Handler"
# =============================================
# Helper that describes all functionalities
# =============================================
if [[ $# -eq 0 ]] || [[ "$1" == "-h" ]]; then
echo "$(basename "$0") [-h] [-e env -b backend -f file c] -- program to deal with Pass Culture ecosystem
where:
-h show this help text"
exit 0
fi
# =============================================
# Get script parameters
# =============================================
CMD="$1"
shift
# Build the docker images
if [[ "$CMD" == "build" ]]; then
RUN='docker-compose build $*'
# Connect to Postgresql database in command line using psql
elif [[ "$CMD" == "psql" ]]; then
COLUMNS=${COLUMNS:-''};
RUN='docker exec -it $COMMAND_NAME-postgres bash -c "COLUMNS=\"'$COLUMNS'\" psql -U '$DB_NAME'_user '$DB_NAME' $*"'
# Publish package to test PyPi
elif [[ "$CMD" == "prepublish" ]]; then
VERSION=$(./$COMMAND_NAME version);
RUN='docker exec -it $COMMAND_NAME-flask bash -c "cd /opt/services/flaskapp &&
rm -rf build &&
rm -rf dist &&
PYTHONPATH=. python setup.py sdist bdist_wheel &&
sleep 1 &&
twine check dist/$PIP_NAME-$VERSION.tar.gz &&
twine upload --repository-url https://test.pypi.org/legacy/ dist/*"'
# Publish package to PyPi
elif [[ "$CMD" == "publish" ]]; then
RUN='docker exec -it $COMMAND_NAME-flask bash -c "cd /opt/services/flaskapp &&
PYTHONPATH=. twine upload dist/*"'
# Force docker-compose to build the docker images
elif [[ "$CMD" == "rebuild" ]]; then
RUN='docker-compose build --no-cache $*'
# Restart API after removing the database and files
elif [[ "$CMD" == "restart" ]]; then
RUN='rm -rf docker_data;
docker-compose down --volumes;
docker-compose up --force-recreate'
# Start with database
elif [[ "$CMD" == "start" ]]; then
RUN='docker-compose up $*'
# Create symlink to use COMMAND_NAME command (admin rights may be needed)
elif [[ "$CMD" == "symlink" ]]; then
RUN='cd /usr/local/bin && ln -sf '"$(pwd)"'/$COMMAND_NAME $COMMAND_NAME && chmod +x $COMMAND_NAME && chmod +x '"$(pwd)"'/$COMMAND_NAME'
# Run tests for API (Need start-backend to be successful and delete all data)
elif [[ "$CMD" == "test" ]]; then
if [[ $# == 0 ]]; then
PYTEST_ARGS="tests"
else
PYTEST_ARGS=$*
fi
RUN='docker exec $COMMAND_NAME-flask bash -c "cd /opt/services/flaskapp && PYTHONPATH=. pytest --color=yes -rsx -v $PYTEST_ARGS"'
# Get the last version of the library
elif [[ "$CMD" == "version" ]]; then
RUN='docker exec $COMMAND_NAME-flask bash -c "cd /opt/services/flaskapp && PYTHONPATH=. python -c \"import $MODULE_NAME; print($MODULE_NAME.__version__)\""'
eval $RUN
exit 0
fi
# =============================================
# Execute command
# =============================================
echo $RUN
eval $RUN