-
Notifications
You must be signed in to change notification settings - Fork 0
/
start
executable file
·93 lines (70 loc) · 2.6 KB
/
start
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
#!/usr/bin/env bash
set -aeuo pipefail
DOTENV=.env
if [ ! -f "$DOTENV" ]; then
cp "${DOTENV}.default" "$DOTENV"
echo "Created boilerplate .env file.\n"
echo "********************************************************"
echo "*** IMPORTANT: You need to modify the .env file NOW. ***"
echo "*** Make your modifications, then re-run this script. "
echo "********************************************************"
exit 0
fi
source "$DOTENV"
if [ ! -v "TIMEZONE" ]; then
TIMEZONE=$(cat /etc/timezone | xargs)
fi
NODE_CONTAINER=${PROJECT}_node
DB_CONTAINER=${PROJECT}_db
STORAGE_CONTAINER=${PROJECT}_storage
# wait for mysql server to start accepting queries
init_mysql () {
docker exec -u root $DB_CONTAINER bash -c "chown -R mysql: /var/log/mysql"
echo "Waiting for MySQL server to start accepting queries..."
while [ 0 ]; do
cmd="mysql -u root -p${TINIFYD_DB_PASS} -e 'SELECT \"READY\"' 2> /dev/null"
result=$(docker exec $DB_CONTAINER bash -c "$cmd" || true)
sleep 2
if [[ "$result" =~ "READY" ]]; then
break
fi
done
if [ "$ENVIRONMENT" == "local" ]; then
docker exec $DB_CONTAINER bash -c "mysql -u root -p${TINIFYD_DB_PASS} -e \"SET GLOBAL general_log = 1\"";
docker exec $DB_CONTAINER bash -c "mysql -u root -p${TINIFYD_DB_PASS} -e \"SET GLOBAL general_log_file = '/var/log/mysql/mysql.log'\"";
docker exec $DB_CONTAINER bash -c "mysql -u root -p${TINIFYD_DB_PASS} -e \"SET GLOBAL general_log = 1\"";
docker exec $NODE_CONTAINER bash -c "rm -rf $TINIFYD_TEMP_PATH/*";
fi
}
npm_tasks () {
echo "Installing npm dependencies..."
docker exec $NODE_CONTAINER bash -c "npm install"
}
run_migrations () {
echo "Running migrations..."
docker exec $NODE_CONTAINER bash -c "bin/migrate up"
}
start_process () {
echo "Starting PM2 process manager"
docker exec $NODE_CONTAINER bash -c "bin/pm2 start .pm2.json "
}
containers_up () {
echo "Generating docker-compose.yml"
echo -e "# This file is auto-generated by the start script.\n---\n" > docker-compose.yml
envsubst < docker-compose-template.yml >> docker-compose.yml
docker-compose -f docker-compose.yml -p $PROJECT up -d
}
fix_timezones () {
containers="$NODE_CONTAINER $DB_CONTAINER"
for container in $containers; do
docker exec $container bash -c "echo \"$TIMEZONE\" > /etc/timezone"
docker exec $container bash -c "dpkg-reconfigure -f noninteractive tzdata"
done
}
containers_up
fix_timezones
npm_tasks
init_mysql
run_migrations
start_process
tail -f -n 0 var/log/pm2/stdout-0.log