-
Notifications
You must be signed in to change notification settings - Fork 908
/
startContainers.sh
executable file
·119 lines (102 loc) · 3.42 KB
/
startContainers.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
#!/bin/bash
host="0.0.0.0"
mysqlUser="root"
mysqlPassword="root"
mysqlContainerName="iddd-mysql"
mysqlPort="3306"
rabbitmqNodeName="$(hostname)"
rabbitmqContainerName="iddd-rabbitmq"
rabbitmqManagementHttpPort="8080"
containers[0]="${mysqlContainerName}"
containers[1]="${rabbitmqContainerName}"
function start() {
echo "Starting MySQL Server container..."
docker rm -f "${mysqlContainerName}"
docker run --name "${mysqlContainerName}" -p "${mysqlPort}":3306 -e MYSQL_ROOT_PASSWORD="${mysqlPassword}" -d mysql
echo "Waiting for MySQL Server to be up and running..."
waitForContainer "${mysqlContainerName}" "mysqld: ready for connections."
local testSqlFiles="$(find $(pwd) -name *.sql | grep -i "test")"
local sqlFiles="$(find $(pwd) -name *.sql | grep -vi "test")"
if which mysql > /dev/null; then
for sql in ${testSqlFiles}; do
echo "Importing [${sql}]"
$(mysql --host="${host}" --port=3306 --protocol=TCP --user="${mysqlUser}" --password="${mysqlPassword}" < ${sql})
done
for sql in ${sqlFiles}; do
echo "Importing [${sql}]"
$(mysql --host="${host}" --port=3306 --protocol=TCP --user="${mysqlUser}" --password="${mysqlPassword}" < ${sql})
done
else
echo -e
echo "!! mysql command not found"
echo "!! You need to import the following SQL files into MySQL Server, yourself:"
for sql in ${testSqlFiles}; do
echo ${sql}
done
for sql in ${sqlFiles}; do
echo ${sql}
done
echo -e
echo "You can find MySQL Server on [localhost] port [${mysqlPort}]"
echo -e
read -rsp "Press any key to continue..."
echo -e
fi
echo "Starting RabbitMQ container..."
docker rm -f "${rabbitmqContainerName}"
docker run --name "${rabbitmqContainerName}" -p 5672:5672 -p "${rabbitmqManagementHttpPort}":15672 -e RABBITMQ_NODENAME="${rabbitmqNodeName}" -d rabbitmq:3-management
echo "Waiting for RabbitMQ to be up and running..."
waitForContainer "${rabbitmqContainerName}" "Server startup complete;"
echo -e
echo "RabbitMQ Management available at [http://localhost:${rabbitmqManagementHttpPort}]"
echo "(Login with user/pass of [guest/guest])"
}
function status() {
docker ps -a | head -1
for name in ${containers[@]}; do
docker ps -a | grep "${name}"
done
}
function stop() {
for name in ${containers[@]}; do
echo "Stopping container [${name}]..."
docker stop "${name}" > /dev/null 2>&1
done
}
function usage() {
echo "Usage: $(basename $0) <command> [<args>]"
echo -e
echo "Available commands:"
echo " start Start external dependencies for this project in Docker containers"
echo " (incl. MySQL Server and RabbitMQ)"
echo " stop Stop the Docker containers"
echo -e
}
function requires() {
local command=$1
which ${command} > /dev/null 2>&1 || { echo "!! This script requires [${command}] to be installed" && exit 1; }
}
function waitForContainer() {
local containerName="$1"
local logMsg="$2"
until docker logs ${containerName} 2>&1 | grep "${logMsg}" > /dev/null; do
sleep 1s
done
}
requires 'docker'
command=$1; shift
case "${command}" in
start)
start
;;
stop)
stop
;;
status)
status
;;
*)
usage
exit 1
;;
esac