-
Notifications
You must be signed in to change notification settings - Fork 10
/
entrypoint.sh
executable file
·153 lines (123 loc) · 4.2 KB
/
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
set -e
function run_scripts () {
SCRIPTS_DIR="/scripts/$1.d"
SCRIPT_FILES_PATTERN="^${SCRIPTS_DIR}/[0-9][0-9][a-zA-Z0-9_-]+$"
SCRIPTS=$(find "$SCRIPTS_DIR" -type f -uid 0 -executable -regex "$SCRIPT_FILES_PATTERN" | sort)
if [ -n "$SCRIPTS" ] ; then
echo "=>> $1-scripts:"
for script in $SCRIPTS ; do
echo "=> $script"
. "$script"
done
fi
}
### auto-configure database from environment-variables
if [ -n "$MYSQL_PORT_3306_TCP" ] && [ -n "$POSTGRES_PORT_5432_TCP" ]; then
if [ -z "$DB_HOST" ] && [ -z "$DB_DRIVER" ]; then
echo "ERROR: A linked MySQL- and a linked Postgres-container were detected."
echo " You must set the DB_DRIVER-variable to either 'mysql' or 'pgsql'."
exit 1
fi
fi
if [ -n "$MYSQL_PORT_3306_TCP" ]; then
DB_DRIVER=mysql
if [ -z "$DB_HOST" ]; then
DB_HOST=$MYSQL_PORT_3306_TCP_ADDR
DB_PORT=$MYSQL_PORT_3306_TCP_PORT
else
echo >&2 "WARNING: Both DB_HOST and MYSQL_PORT_3306_TCP found."
echo >&2 " Connecting to DB_HOST ($DB_HOST)"
echo >&2 " instead of the linked mysql container."
fi
elif [ -n "$POSTGRES_PORT_5432_TCP" ]; then
DB_DRIVER=pgsql
if [ -z "$DB_HOST" ]; then
DB_HOST=$POSTGRES_PORT_5432_TCP_ADDR
DB_PORT=$POSTGRES_PORT_5432_TCP_PORT
else
echo >&2 "WARNING: Both DB_HOST and POSTGRES_PORT_5432_TCP found."
echo >&2 " Connecting to DB_HOST ($DB_HOST)"
echo >&2 " instead of the linked postgres container."
fi
fi
if [ -z "$DB_HOST" ]; then
echo >&2 "ERROR: missing DB_HOST and MYSQL_PORT_3306_TCP or POSTGRES_PORT_5432_TCP environment variables."
echo >&2 " Did you forget to --link some_mysql_container:mysql, --link some_postgres_container:postgres, or set an external db"
echo >&2 " with -e DB_HOST=hostname?"
exit 1
fi
### set database configuration defaults if necessary
: ${DB_DRIVER:='mysql'}
: ${DB_NAME:='drupal'}
if [ "${DB_DRIVER}" = 'mysql' ]; then
: ${DB_USER:='root'}
if [ "${DB_USER}" = 'root' ]; then
: ${DB_PASS:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
fi
: ${DB_PORT:='3306'}
elif [ "${DB_DRIVER}" = 'pgsql' ]; then
: ${DB_USER:='postgres'}
if [ "$DB_USER" = 'postgres' ]; then
: ${DB_PASS:=$POSTGRES_ENV_POSTGRES_PASSWORD}
fi
: ${DB_PORT:='5432'}
fi
if [ -z "$DB_PASS" ]; then
echo >&2 "ERROR: missing required DB_PASS environment variable"
echo >&2 " Did you forget to -e DB_PASS=... ?"
echo >&2
echo >&2 " (Also of interest might be DB_USER and DB_NAME.)"
exit 1
fi
### other defaults
: ${ADMIN_USER:='admin'} # DO NOT export!
: ${ADMIN_PASSWORD:='changeme'} # DO NOT export!
### store database-configuration
export DB_DRIVER DB_HOST DB_PORT DB_NAME DB_USER DB_PASS
echo -e "# Drupals's database configuration, parsed in /var/www/sites/default/settings.php\n
export DB_DRIVER=${DB_DRIVER} DB_HOST=${DB_HOST} DB_PORT=${DB_PORT} DB_NAME=${DB_NAME} DB_USER=${DB_USER} DB_PASS=${DB_PASS}" >> /etc/bash.bashrc
### connect to database
echo
echo "=> Trying to connect to a database using:"
echo " Database Driver: $DB_DRIVER"
echo " Database Host: $DB_HOST"
echo " Database Port: $DB_PORT"
echo " Database Username: $DB_USER"
echo " Database Password: $DB_PASS"
echo " Database Name: $DB_NAME"
echo
for ((i=0;i<20;i++))
do
if [[ $DB_DRIVER == "mysql" ]]; then
DB_CONNECTABLE=$(mysql -u"$DB_USER" -p"$DB_PASS" -h"$DB_HOST" -P"$DB_PORT" -e 'status' >/dev/null 2>&1; echo "$?")
elif [[ $DB_DRIVER == "pgsql" ]]; then
DB_CONNECTABLE=$(PGPASSWORD=$DB_PASS psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -l >/dev/null 2>&1; echo "$?")
fi
if [[ $DB_CONNECTABLE -eq 0 ]]; then
break
fi
sleep 3
done
if ! [[ $DB_CONNECTABLE -eq 0 ]]; then
echo "Cannot connect to database"
exit "${DB_CONNECTABLE}"
fi
### Initial setup if database doesn't exist
if ! drush pm-list > /dev/null 2>&1; then
run_scripts setup
echo "=> Done installing site!"
if [ $EXTRA_SETUP_SCRIPT ]; then
echo "=> WARNING: The usage of EXTRA_SETUP_SCRIPT is deprectated. Put your script into /scripts/post-setup.d/"
. $EXTRA_SETUP_SCRIPT
echo "=> Successfully ran extra setup script ${EXTRA_SETUP_SCRIPT}."
fi
else
echo "=> Skipped setup - database ${DB_NAME} already exists."
fi
###
run_scripts pre-launch
unset ADMIN_USER
unset ADMIN_PASSWORD
exec apache2-foreground
exit 1