-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-command.sh
executable file
·83 lines (67 loc) · 2.62 KB
/
docker-command.sh
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
#!/bin/bash
set -u -e -o pipefail
[[ "${DEBUG:-False}" == "True" || "${XTRACE:-False}" == "True" ]] && set -x
#
# Check environment
#
python_version="$(python3 -c 'import platform; print(platform.python_version())' | cut -d '.' -f 1,2)"
if [ "${python_version}" != "${PYTHON_VERSION}" ]; then
echo "PYTHON_VERSION (${PYTHON_VERSION}) differs from version reported from python3 executable (${python_version})" 1>&2
exit 1
fi
if [ -z "${DATABASE_URL}" ]; then
echo "DATABASE_URL is not specified!" 1>&2 && exit 1;
fi
database_server_pattern='^postgresql[:][/][/]\K([\w][\w\d-]*([.][\w][\w\d-]*)*)[:]([\d]{2,4})(?=[/])'
database_server=$(echo ${DATABASE_URL} | grep -Po -e "${database_server_pattern}" || echo -n)
if [ -z "${database_server}" ]; then
echo "The database URL (DATABASE_URL) is malformed!" 1>&2 && exit 1;
fi
if [ -z "${DATABASE_USERNAME}" ]; then
echo "DATABASE_USERNAME is not specified!" 1>&2 && exit 1;
fi
if [ ! -f "${DATABASE_PASSWORD_FILE}" ]; then
echo "DATABASE_PASSWORD_FILE is not readable (or not specified)!" 1>&2 && exit 1;
fi
if [ ! -f "${SECRET_KEY_FILE}" ]; then
echo "SECRET_KEY_FILE does not exist!" 1>&2 && exit 1;
fi
if [ ! -f "${LOGGING_FILE_CONFIG}" ]; then
echo "LOGGING_FILE_CONFIG (configuration for Python logging) does not exist!" 1>&2 && exit 1;
fi
logging_file_config=${LOGGING_FILE_CONFIG}
if [ -n "${LOGGING_ROOT_LEVEL}" ]; then
logging_file_config="logging-$(echo ${HOSTNAME}| md5sum| head -c10).conf"
sed -e "/^\[logger_root\]/,/^\[.*/ { s/^level=.*/level=${LOGGING_ROOT_LEVEL}/ }" ${LOGGING_FILE_CONFIG} \
> ${logging_file_config}
fi
export FLASK_APP="catalogueapi"
export SECRET_KEY="$(cat ${SECRET_KEY_FILE})"
#
# Initialize database schema (if requested so)
#
wait-for-it -t '30' "${database_server}"
if [ "${DATABASE_INITIALIZE_SCHEMA:-False}" == "True" ]; then
echo "Generating database schema..." 1>&2
python -c 'import catalogueapi; catalogueapi.generate_db_schema()'
fi
#
# Start WSGI server
#
if [ "${FLASK_ENV}" == "development" ]; then
# Run a development server
LOGGING_FILE_CONFIG=${logging_file_config} exec /usr/local/bin/wsgi.py
else
# Run a production server (Gunicorn)
num_workers="4"
server_port="5000"
gunicorn_ssl_options=
if [ -n "${TLS_CERTIFICATE}" ] && [ -n "${TLS_KEY}" ]; then
gunicorn_ssl_options="--keyfile ${TLS_KEY} --certfile ${TLS_CERTIFICATE}"
server_port="5443"
fi
exec gunicorn --log-config ${logging_file_config} --access-logfile - \
--workers ${num_workers} \
--bind "0.0.0.0:${server_port}" ${gunicorn_ssl_options} \
'catalogueapi.app:create_app()'
fi