-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-ctrl.sh
executable file
·233 lines (194 loc) · 6 KB
/
docker-ctrl.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
### HELP
function print_usage(){
echo "Docker compose control
docker-ctrl.sh [OPTIONS] ACTION PARAM
ACTIONS:
create Creates containers (docker-compose up)
stop Stops containers (docker-compose stop)
rm Remove docker containers&network (docker-compose down)
PARAMS:
all action applies to all available containers
db action only applied to database
elastic action only applied to elasticsearch
webapp action only applied to webapp
OPTIONS:
-p, --purge removes data from docker volumes for containers
-h, --help show usage"
}
### get all options
while [[ $# -gt 2 ]]
do
key="$1"
case $key in
-p|--purge)
PURGE=true
echo "purge volumes true"
shift # past argument
;;
-b|--force-build)
FORCEBUILD=true
echo "force building dockerfiles true"
shift # past argument
;;
-h|--help)
print_usage
exit 0
;;
-*)
echo "unkown option" # unknown option
show_help
return -1
;;
esac
# shift # past argument or value
done
ACTION=$1
PARAM=$2
### setting up useful variables
projecttag=p4c
allfilesopt="-f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.postgres.yml -f dockerfiles/docker-compose.elastic.yml -f dockerfiles/docker-compose.webapp.yml -f dockerfiles/docker-compose.testdb.yml "
cmd_opts=""
function remove_postgres_volume {
docker volume rm ${projecttag}_postgres-data
}
function remove_elastic_volume {
docker volume rm ${projecttag}_elastic-data
}
function purge_all_volumes() {
remove_postgres_volume
remove_elastic_volume
}
function create_all_containers() {
echo "creating containers postgres, elastic"
docker-compose ${allfilesopt} -p ${projecttag} up -d ${cmd_opts}
}
function shutdown_all_containers() {
echo "shutting down containers..."
docker-compose ${allfilesopt} -p ${projecttag} stop ${cmd_opts}
}
function remove_all_containers() {
echo "removing containers..."
docker-compose ${allfilesopt} -p ${projecttag} down ${cmd_opts}
}
function create_postgres_cotainer() {
echo "creating postgres container"
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.postgres.yml -p ${projecttag} up -d ${cmd_opts}
}
function shutdown_postgres_container() {
echo "shutting down containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.postgres.yml -p ${projecttag} stop ${cmd_opts}
}
function remove_postgres_container() {
echo "removing containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.postgres.yml -p ${projecttag} down ${cmd_opts}
}
function create_elastic_container() {
echo "creating elastic container"
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.elastic.yml -p ${projecttag} up -d ${cmd_opts}
}
function shutdown_elastic_containers() {
echo "shutting down containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.elastic.yml -p ${projecttag} stop ${cmd_opts}
}
function remove_elastic_containers() {
echo "removing containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.elastic.yml -p ${projecttag} down ${cmd_opts}
}
function create_webapp_container() {
echo "creating webapp container"
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.webapp.yml -p ${projecttag} up -d ${cmd_opts}
}
function shutdown_webapp_containers() {
echo "shutting down containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.webapp.yml -p ${projecttag} stop ${cmd_opts}
}
function remove_webapp_containers() {
echo "removing containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.webapp.yml -p ${projecttag} down ${cmd_opts}
}
function create_test_postgres_cotainer() {
echo "creating postgres container"
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.testdb.yml -p ${projecttag} up -d ${cmd_opts}
}
function shutdown_test_postgres_container() {
echo "shutting down containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.testdb.yml -p ${projecttag} stop ${cmd_opts}
}
function remove_test_postgres_container() {
echo "removing containers..."
docker-compose -f dockerfiles/docker-compose.yml -f dockerfiles/docker-compose.testdb.yml -p ${projecttag} down ${cmd_opts}
}
## REMOVE VOLUMES IF FLAG IS RAISED
if [ "$PURGE" ]; then
if [ "$PARAM" = "all" ]; then
remove_elastic_volume
remove_postgres_volume
elif [ "$PARAM" = "db" ]; then
remove_postgres_volume
elif [ "$PARAM" = "elastic" ]; then
remove_elastic_volume
fi
fi
## sets opts to --build
if [ "$FORCEBUILD" ]; then
cmd_opts="$cmd_opts --build"
fi
if [ "$ACTION" = "create" ]; then
if [ "$PARAM" = "all" ]; then
create_all_containers
fi
if [ "$PARAM" = "db" ]; then
create_postgres_cotainer
fi
if [ "$PARAM" = "elastic" ]; then
create_elastic_container
fi
if [ "$PARAM" = "webapp" ]; then
create_webapp_container
fi
if [ "$PARAM" = "testdb" ]; then
create_test_postgres_cotainer
fi
exit 0
elif [ "$ACTION" = "stop" ]; then
if [ "$PARAM" = "all" ]; then
shutdown_all_containers
fi
if [ "$PARAM" = "db" ]; then
shutdown_postgres_container
fi
if [ "$PARAM" = "elastic" ]; then
shutdown_elastic_containers
fi
if [ "$PARAM" = "webapp" ]; then
shutdown_webapp_containers
fi
if [ "$PARAM" = "testdb" ]; then
shutdown_test_postgres_container
fi
exit 0
elif [ "$ACTION" = "rm" ]; then
if [ "$PARAM" = "all" ]; then
shutdown_all_containers
remove_all_containers
fi
if [ "$PARAM" = "db" ]; then
shutdown_postgres_container
remove_postgres_container
fi
if [ "$PARAM" = "elastic" ]; then
shutdown_elastic_containers
remove_elastic_containers
fi
if [ "$PARAM" = "webapp" ]; then
shutdown_webapp_containers
remove_webapp_containers
fi
if [ "$PARAM" = "testdb" ]; then
shutdown_test_postgres_container
remove_test_postgres_container
fi
exit 0
fi
print_usage