forked from docker-library/redmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-entrypoint.sh
executable file
·130 lines (115 loc) · 3.84 KB
/
docker-entrypoint.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -e
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
case "$1" in
rails|rake|passenger)
if [ ! -f './config/database.yml' ]; then
file_env 'REDMINE_DB_MYSQL'
file_env 'REDMINE_DB_POSTGRES'
if [ "$MYSQL_PORT_3306_TCP" ] && [ -z "$REDMINE_DB_MYSQL" ]; then
export REDMINE_DB_MYSQL='mysql'
elif [ "$POSTGRES_PORT_5432_TCP" ] && [ -z "$REDMINE_DB_POSTGRES" ]; then
export REDMINE_DB_POSTGRES='postgres'
fi
if [ "$REDMINE_DB_MYSQL" ]; then
adapter='mysql2'
host="$REDMINE_DB_MYSQL"
file_env 'REDMINE_DB_PORT' '3306'
file_env 'REDMINE_DB_USERNAME' "${MYSQL_ENV_MYSQL_USER:-root}"
file_env 'REDMINE_DB_PASSWORD' "${MYSQL_ENV_MYSQL_PASSWORD:-${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}}"
file_env 'REDMINE_DB_DATABASE' "${MYSQL_ENV_MYSQL_DATABASE:-${MYSQL_ENV_MYSQL_USER:-redmine}}"
file_env 'REDMINE_DB_ENCODING' ''
elif [ "$REDMINE_DB_POSTGRES" ]; then
adapter='postgresql'
host="$REDMINE_DB_POSTGRES"
file_env 'REDMINE_DB_PORT' '5432'
file_env 'REDMINE_DB_USERNAME' "${POSTGRES_ENV_POSTGRES_USER:-postgres}"
file_env 'REDMINE_DB_PASSWORD' "${POSTGRES_ENV_POSTGRES_PASSWORD}"
file_env 'REDMINE_DB_DATABASE' "${POSTGRES_ENV_POSTGRES_DB:-${REDMINE_DB_USERNAME:-}}"
file_env 'REDMINE_DB_ENCODING' 'utf8'
else
echo >&2
echo >&2 'warning: missing REDMINE_DB_MYSQL or REDMINE_DB_POSTGRES environment variables'
echo >&2
echo >&2 '*** Using sqlite3 as fallback. ***'
echo >&2
adapter='sqlite3'
host='localhost'
file_env 'REDMINE_DB_PORT' ''
file_env 'REDMINE_DB_USERNAME' 'redmine'
file_env 'REDMINE_DB_PASSWORD' ''
file_env 'REDMINE_DB_DATABASE' 'sqlite/redmine.db'
file_env 'REDMINE_DB_ENCODING' 'utf8'
mkdir -p "$(dirname "$REDMINE_DB_DATABASE")"
chown -R redmine:redmine "$(dirname "$REDMINE_DB_DATABASE")"
fi
REDMINE_DB_ADAPTER="$adapter"
REDMINE_DB_HOST="$host"
echo "$RAILS_ENV:" > config/database.yml
for var in \
adapter \
host \
port \
username \
password \
database \
encoding \
; do
env="REDMINE_DB_${var^^}"
val="${!env}"
[ -n "$val" ] || continue
echo " $var: \"$val\"" >> config/database.yml
done
fi
# ensure the right database adapter is active in the Gemfile.lock
bundle install --without development test
if [ ! -s config/secrets.yml ]; then
file_env 'REDMINE_SECRET_KEY_BASE'
if [ "$REDMINE_SECRET_KEY_BASE" ]; then
cat > 'config/secrets.yml' <<-YML
$RAILS_ENV:
secret_key_base: "$REDMINE_SECRET_KEY_BASE"
YML
elif [ ! -f /usr/src/redmine/config/initializers/secret_token.rb ]; then
rake generate_secret_token
fi
fi
if [ "$1" != 'rake' -a -z "$REDMINE_NO_DB_MIGRATE" ]; then
gosu redmine rake db:migrate
fi
# https://www.redmine.org/projects/redmine/wiki/RedmineInstall#Step-8-File-system-permissions
chown -R redmine:redmine files log public/plugin_assets
chmod -R 755 files log tmp public/plugin_assets
if [ "$1" != 'rake' -a -n "$REDMINE_PLUGINS_MIGRATE" ]; then
gosu redmine rake redmine:plugins:migrate
fi
# remove PID file to enable restarting the container
rm -f /usr/src/redmine/tmp/pids/server.pid
if [ "$1" = 'passenger' ]; then
# Don't fear the reaper.
set -- tini -- "$@"
fi
set -- gosu redmine "$@"
;;
esac
exec "$@"