-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdockersand.sh
executable file
·162 lines (153 loc) · 5.98 KB
/
dockersand.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
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
# *****************************************************************************
# This file is part of dirtsand.
#
# dirtsand is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# dirtsand is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with dirtsand. If not, see <http://www.gnu.org/licenses/>.
# *****************************************************************************
# --- CONFIGURABLE ---
COMPOSE_FILE="docker-compose.yml"
# The directory this script is in. If you customize this file outside of the repo, hardcode the
# path to the DIRTSAND sources.
DS_DIR=$(dirname $(readlink -f "$0"))
# The prefix for the containers. Defaults to the name of the DIRTSAND source directory ("dirtsand"),
# so the containers are usually: dirtsand_moul_1 and dirtsand_db_1. Change this to something else
# if you intend to run multiple shards on the same machine.
PROJECT_NAME=$(basename $DS_DIR)
# The command to invoke to run Docker Compose
# compose v2 does not always install the `docker-compose` backward-compatible
# alias and needs instead to be run as `docker compose`
DOCKER_COMPOSE="docker-compose"
do_help() {
echo "Available commands:"
echo " attach - attach to the DIRTSAND console"
echo " build - rebuild the DIRTSAND containers from the source and compose files"
echo " restart - restart the DIRTSAND containers"
echo " start - start the DIRTSAND containers"
echo " status - check if the DIRTSAND containers are running"
echo " stop - stop the DIRTSAND containers"
}
check_docker() {
command -v docker > /dev/null
if [[ $? -ne 0 ]]; then
echo -e "\e[31mERROR:\e[0m docker is not installed."
exit 1
fi
command -v docker-compose > /dev/null
if [[ $? -ne 0 ]]; then
docker compose > /dev/null
if [[ $? -eq 0 ]]; then
DOCKER_COMPOSE="docker compose"
else
echo -e "\e[31mERROR:\e[0m docker-compose is not installed."
exit 1
fi
fi
}
# docker-compose requires the CWD have the docker-compose.yml file.
pushd "$DS_DIR" > /dev/null
trap "popd > /dev/null" EXIT
if [[ ! -n $1 ]];then
echo "dockersand - DIRTSAND for Docker"
do_help
elif [[ $1 = "help" ]]; then
echo "dockersand - DIRTSAND for Docker"
do_help
elif [[ $1 = "attach" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep "${PROJECT_NAME}[-_]moul[-_][0-9]*" > /dev/null
if [[ $? -ne 0 ]]; then
echo -e "\e[33mWARNING\e[0m: dockersand is not running! Starting..."
$DOCKER_COMPOSE -p $PROJECT_NAME -f $COMPOSE_FILE up -d
fi
CONTAINER_NAME=$($DOCKER_COMPOSE ps | grep -oh ".*[-_]moul[-_][0-9]*")
if [[ $? -ne 0 ]]; then
echo -e "\e[31mERROR\e[0m: Unable to determine DIRTSAND container name. Sorry :("
exit 1
fi
echo "Attaching to DIRTSAND... Press Ctrl+P Ctrl+Q to detatch!"
docker attach $CONTAINER_NAME
elif [[ $1 = "build" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep "${PROJECT_NAME}[-_]moul[-_][0-9]*" > /dev/null
RUNNING=$?
if [[ $RUNNING -eq 0 ]]; then
echo -e "\e[36mStopping dockersand...\e[0m"
$DOCKER_COMPOSE -p $PROJECT_NAME down
fi
# Hacky, but what can you do?
mkdir -p build/authserv build/dat build/etc build/fileserv build/SDL
$DOCKER_COMPOSE -p $PROJECT_NAME -f $COMPOSE_FILE build
if [[ $? -ne 0 ]]; then
echo -e "\e[31mFAILED\e[0m"
exit 1
fi
if [[ $RUNNING -eq 0 ]]; then
echo -e "\e[36mStarting dockersand...\e[0m"
$DOCKER_COMPOSE -p $PROJECT_NAME -f $COMPOSE_FILE up -d
if [[ $? -eq 0 ]]; then
echo -e "\e[32mSUCCESS\e[0m: To manage DIRTSAND, use ./dockersand.sh attach"
else
echo -e "\e[31mFAILED\e[0m"
fi
fi
elif [[ $1 = "restart" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep dirtsand > /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\e[36mRestarting dockersand...\e[0m"
$DOCKER_COMPOSE -p $PROJECT_NAME restart
else
echo -e "\e[33mWARNING\e[0m: dockersand is not running! Starting..."
$DOCKER_COMPOSE -p $PROJECT_NAME -f $COMPOSE_FILE up -d
fi
if [[ $? -eq 0 ]]; then
echo -e "\e[32mSUCCESS\e[0m: To manage DIRTSAND, use ./dockersand.sh attach"
else
echo -e "\e[31mFAILED\e[0m"
fi
elif [[ $1 = "start" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep "${PROJECT_NAME}[-_]moul[-_][0-9]*" > /dev/null
if [[ $? -ne 0 ]]; then
echo -e "\e[36mStarting dockersand...\e[0m"
$DOCKER_COMPOSE -p $PROJECT_NAME -f $COMPOSE_FILE up -d
if [[ $? -eq 0 ]]; then
echo -e "\e[32mSUCCESS\e[0m: To manage DIRTSAND, use ./dockersand.sh attach"
else
echo -e "\e[31mFAILED\e[0m"
fi
else
echo -e "\e[33mWARNING\e[0m: dockersand is already running!"
fi
elif [[ $1 = "status" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep "${PROJECT_NAME}[-_]moul[-_][0-9]*" > /dev/null
if [[ $? -eq 0 ]]; then
echo -e "dockersand is \e[32mUP\e[0m"
else
echo -e "dockersand is \e[31mDOWN\e[0m"
fi
elif [[ $1 = "stop" ]]; then
check_docker
$DOCKER_COMPOSE -p $PROJECT_NAME ps | grep "${PROJECT_NAME}[-_]moul[-_][0-9]*" > /dev/null
if [[ $? -eq 0 ]]; then
echo -e "\e[36mStopping dockersand...\e[0m"
$DOCKER_COMPOSE -p $PROJECT_NAME down
else
echo -e "\e[33mWARNING\e[0m: dockersand is not running!"
fi
else
echo -e "\e[31mERROR\e[0m: Unrecognized option $1"
do_help
fi