-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.sh
executable file
·58 lines (52 loc) · 1.67 KB
/
init.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
#!/bin/bash
#
set -euo pipefail
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_PASSWORD' 'example'
# (will allow for "$XYZ_PASSWORD_FILE" to fill in the value of
# "$XYZ_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}"
export "$var"="$val"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
export "$var"="$val"
fi
unset "$fileVar"
}
envs=(
ROOT_USER
ROOT_PASSWORD
DB_HOST
NEW_DATABASE
NEW_USER
NEW_PASSWORD
)
for e in "${envs[@]}"; do
file_env "$e"
done
if [ -z ${ROOT_USER+x} ]; then echo "ROOT_USER is unset"; exit 1; fi
if [ -z ${ROOT_PASSWORD+x} ]; then echo "ROOT_PASSWORD is unset"; exit 1; fi
if [ -z ${DB_HOST+x} ]; then echo "DB_HOST is unset"; exit 1; fi
if [ -z ${NEW_USER+x} ]; then echo "NEW_USER is unset"; exit 1; fi
if [ -z ${NEW_PASSWORD+x} ]; then echo "NEW_PASSWORD is unset"; exit 1; fi
echo 'Waiting, to give containers time to link...'
sleep 10
echo 'Creating database...'
mysql -u $ROOT_USER -p$ROOT_PASSWORD -h $DB_HOST --connect_timeout 10 -e "CREATE DATABASE $NEW_DATABASE;"
echo 'Done.'
echo 'Creating user...'
mysql -u $ROOT_USER -p$ROOT_PASSWORD -h $DB_HOST --connect_timeout 10 -e "grant usage on *.* to $NEW_USER identified by '$NEW_PASSWORD';"
echo 'Done.'
echo 'Granting user privileges...'
mysql -u $ROOT_USER -p$ROOT_PASSWORD -h $DB_HOST --connect_timeout 10 -e "grant all privileges on $NEW_DATABASE.* to $NEW_USER;"
echo 'All done.'