-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathentrypoint.sh
executable file
·131 lines (107 loc) · 4.3 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
#!/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
DB_DRIVER=pgsql
: ${DB_HOST:='postgres'}
: ${DB_PORT:='5432'}
: ${DB_NAME:='postgres'}
: ${DB_USER:='postgres'}
: ${DB_PASS:='postgres'}
: ${ADMIN_USER:='admin'} # DO NOT export!
: ${ADMIN_PASSWORD:='changeme'} # DO NOT export!
DRUPAL_HASH_SALT=`cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1`
PGHOST=${DB_HOST};
PGPORT=${DB_PORT};
PGDATABASE=${DB_NAME};
PGUSER=${DB_USER};
PGPASSWORD=${DB_PASS};
export PGHOST PGPORT PGDATABASE PGUSER PGPASSWORD
export DB_DRIVER DB_HOST DB_PORT DB_NAME DB_USER DB_PASS DRUPAL_HASH_SALT
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} DRUPAL_HASH_SALT=${DRUPAL_HASH_SALT}" >> /etc/profile
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} DRUPAL_HASH_SALT=${DRUPAL_HASH_SALT}" >> /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
DB_CONNECTABLE=$(PGPASSWORD=$DB_PASS psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -l >/dev/null 2>&1; echo "$?")
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 [ "$(PGPASSWORD=$DB_PASS psql -U $DB_USER -h $DB_HOST -p $DB_PORT postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" )" != '1' ]
then
echo "Database $DB_NAME does not exist, creating it"
echo "CREATE DATABASE $DB_NAME;" | psql -U $DB_USER -h $DB_HOST -p $DB_PORT postgres;
fi
# Add possibly missing files/dirs if sites dir is empty (files needed by drush pm-list)
if [ ! -e /var/www/html/sites/default ]; then
mkdir -p /var/www/html/sites/default/
fi
if [ ! -e /var/www/html/sites/default/settings.php ]; then
cp /etc/tripal/settings.php /var/www/html/sites/default/settings.php
fi
# Check if tables are there and that drush works
DB_LOADED=$(PGPASSWORD=$DB_PASS psql -U "$DB_USER" -h "$DB_HOST" -p "$DB_PORT" -tAc "SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'organism');")
DRUSH_OK=`export ENABLE_MEMCACHE=0 && drush pm-list > /dev/null 2>&1; echo "$?"`
if [[ $DRUSH_OK != "0" && $DB_LOADED != "t" ]]
then
run_scripts setup
echo "=> Done installing site!"
if [ $EXTRA_SETUP_SCRIPT ]; then
echo "=> WARNING: The usage of EXTRA_SETUP_SCRIPT is deprecated. Put your script into /scripts/post-setup.d/"
. $EXTRA_SETUP_SCRIPT
echo "=> Successfully ran extra setup script ${EXTRA_SETUP_SCRIPT}."
fi
elif [[ $DRUSH_OK == "0" && $DB_LOADED != "t" ]]
then
echo "=> Error: 'drush pm-list' ok but could not find chado tables. Something is wrong in the install. Exiting."
exit 1
elif [[ $DRUSH_OK != "0" && $DB_LOADED == "t" ]]
then
echo "=> Error: 'drush pm-list' fails but the database is not empty. Trying to upgrade db, just in case."
export ENABLE_MEMCACHE=0 && drush updb -y
echo "drush updb finished, retrying pm-list"
DRUSH_OK=`export ENABLE_MEMCACHE=0 && drush pm-list > /dev/null 2>&1; echo "$?"`
if [[ $DRUSH_OK != "0" ]]
then
echo "=> Error: 'drush pm-list' fails again but the database is not empty. Something is wrong in the install. Exiting."
export ENABLE_MEMCACHE=0 && drush pm-list
exit $?
fi
else
echo "=> Skipped setup - database ${DB_NAME} already ready."
fi
###
run_scripts pre-launch
unset ADMIN_USER
unset ADMIN_PASSWORD
exec apache2-foreground
exit 1