forked from lalcebo/docker-traefik-https
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geeko
executable file
·333 lines (271 loc) · 8.54 KB
/
geeko
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env bash
# shellcheck disable=SC2143
# shellcheck disable=SC2181
# shellcheck disable=SC2009
# shellcheck disable=SC2164
export LC_ALL=en_US.utf8
# change directory to script
cd $(dirname $(readlink -f "$0"))
# require apps
REQUIRE_APPS="docker,docker-compose,mkcert,openssl,diff"
# get the current user, on windows is on username env var.
USER=${USER:-$USERNAME}
# ascii escape characters
ESCAPE=$(echo -en "\033")
ERROR="${ESCAPE}[1;31m"
RESET=$(echo -en "${ESCAPE}[m\017")
START=$(echo -en "\015${ESCAPE}[C${ESCAPE}[10D")
# base path for certs
CERTS_PATH_TRAEFIK="config/traefik/certs"
# path to copy mkcert root ca
CA_ROOT_PATH=.data/rootCA.pem
# format to extract ip from container
DOCKER_IP_FORMAT='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
help() {
cat <<EOF
How to use: $(basename "$0") [argument]
-i Initial setup.
-s Start all containers.
-k Stops all containers.
-r Restart all containers.
-u Recreate containers (Shutdown or up only required containers).
-c <app> <cmd> Run a command inside container.
-m <app> <num> Scale container to num instances.
-h Show this help message.
Example:
$(basename "$0") -c app composer install
Bugs to <[email protected]>
EOF
}
function isWinPlatform() {
[ "$OSTYPE" == 'cygwin' ] || [ "$OSTYPE" == 'msys' ] || [ "$OSTYPE" == 'win32' ]
}
function isAppInstalled() {
test -x "$(which "$1" 2>/dev/null)" && return 0 || return 150
}
function isProjectInstalled() {
[ -e .env ] &&
[ -e config/traefik/config.yml ] &&
[ -e config/dnsmasq/dnsmasq.d/dnsmasq.conf ]
}
function checkSetup() {
isProjectInstalled || {
printf "${START}${ERROR}Run the initial setup first: <%s -i>${RESET}" "$(basename "$0")"
exit 150
}
}
function getAllHostsForMakeCerts() {
HOSTS=()
FILES=$(find . -type f -name '*.yml' -not -name "template*" -not -path "./.data/*" -not -path "./config/*")
for FILE in $FILES; do
# shellcheck disable=SC2016
# shellcheck disable=SC2179
HOSTS+=$(cat <"$FILE" | grep -Po '(?:`([^`]+)`)+' | tr '\n' ',' | sed 's/`//g')
done
echo "${HOSTS::-1}"
}
function createEnvFiles() {
# if os is windows use windows path format for home directory.
isWinPlatform && {
CURRENT_DIR=$PWD && cd ~ && HOME=$(pwd -W) && cd "$CURRENT_DIR" || exit
}
# ask values
read -rep "PROJECTS ROOT: " -i "$HOME/Projects" PROJECTS_ROOT
read -rep "SERVICES PASSWORD: " -i "ChangeMe$." ROOT_PASSWORD
echo
echo 'Creating projects root folders...'
mkdir -p "$PROJECTS_ROOT"
# create .env file
cat <.env.dist | {
sed -e 's,{PROJECTS_ROOT},'"$PROJECTS_ROOT"',g' |
sed -e 's,{ROOT_PASSWORD},'"$ROOT_PASSWORD"',g'
} >.env
echo 'Install the local CA in the system trust store...'
mkcert -install
# make ssl cert files
echo 'Create SSL certs files...'
mkcert -cert-file $CERTS_PATH_TRAEFIK/test.crt -key-file $CERTS_PATH_TRAEFIK/test.key $(getAllHostsForMakeCerts | tr ',' ' ')
}
function setup() {
echo
echo 'Start docker project setup...'
echo
if [[ -e .env ]]; then
read -p "The .env file exists, are you sure want to overwrite? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo
createEnvFiles
echo "Done!"
echo
fi
echo
else
createEnvFiles
echo
echo '--- Create Traefik, DNSMasq configs...'
echo
cp -f config/dnsmasq/dnsmasq.d/dnsmasq.conf.dist config/dnsmasq/dnsmasq.d/dnsmasq.conf
cp -f config/traefik/config.yml.dist config/traefik/config.yml
echo 'Open .env file and add any service do you want start.'
echo "Done!"
echo
fi
}
for APP in $(echo "$REQUIRE_APPS" | tr "," "\n"); do
isAppInstalled "$APP"
if [[ $? != 0 ]]; then
printf "${START}${ERROR}The application \"%s\" is not installed.${RESET}" "$APP"
exit $?
fi
done
# set path separator
isWinPlatform && PATH_SEPARATOR=';' || PATH_SEPARATOR=':'
isProjectInstalled && {
# export all environment vars
export $(cat < .env | grep -v ^# | xargs)
if [[ -z "$COMPOSE_FILE" ]]; then
echo 'Missing env var compose_file, see https://docs.docker.com/compose/reference/envvars/#compose_file'
exit 150
fi
# add all enable services
SERVICES=$(echo "$ENABLE_SERVICES" | tr ',' '\n')
for SERVICE in $SERVICES; do
COMPOSE_FILE="$COMPOSE_FILE$PATH_SEPARATOR./services/$SERVICE.yml"
done
# get all projects
PROJECTS=$(find "$PROJECTS_ROOT" -maxdepth 1 -type d | sed "s/${PROJECTS_ROOT//\//\\/}//g")
}
function addProjects() {
for PROJECT in $PROJECTS; do
PROJECT_PATH="projects/${PROJECT#\/}.yml"
export COMPOSE_FILE="${COMPOSE_FILE}${PATH_SEPARATOR}./${PROJECT_PATH}"
done
}
function createProjects() {
# remove all projects files
rm -f projects/*.yml
DNS_IP=$(docker inspect -f $DOCKER_IP_FORMAT dnsmasq)
for PROJECT in $PROJECTS; do
# create project yml file
PROJECT_PATH="projects/${PROJECT#\/}.yml"
PROJECT_ENV_PREFIX=$(echo "${PROJECT#\/}" | tr '[:lower:]' '[:upper:]')
PHP_VERSION=$(eval "echo \${${PROJECT_ENV_PREFIX}_PHP_VERSION:-8.3}")
cat < config/template/project.yml | {
sed -e 's,<docker_registry>,'"$DOCKER_REGISTRY"',g' |
sed -e 's,<php_version>,'"$PHP_VERSION"',g' |
sed -e 's,<dns_server>,'"$DNS_IP"',g' |
sed -e 's,<project_path>,'"$PROJECTS_ROOT/${PROJECT#\/}"',g' |
sed -e 's,<project_name>,'"${PROJECT#\/}"',g'
} > "$PROJECT_PATH"
done
}
function addCaRootToDataFolder() {
if [[ ! -e "$CA_ROOT_PATH" ]]; then
cp -f "$(mkcert -CAROOT)/rootCA.pem" "$CA_ROOT_PATH"
fi
}
function isContainerRunning() {
if [[ -z "$1" || -z "$(docker-compose ps --services | grep "\<$1\>")" ]]; then
echo
printf "${START}${ERROR}Container <<%s>> is not running.${RESET}" "$CONTAINER"
echo
exit 150
fi
}
function createCertFiles() {
addCaRootToDataFolder
LIST_HOSTS_CURRENT=$(openssl x509 -text -noout -in "$CERTS_PATH_TRAEFIK/test.crt" | grep -e 'DNS:' | sed 's/[DNS\: ]//g' | tr ',' '\n')
if [[ -n $(diff <(echo "$LIST_HOSTS_CURRENT") <(getAllHostsForMakeCerts | tr ',' '\n')) ]]; then
echo 'Update SSL certs files...'
mkcert -cert-file $CERTS_PATH_TRAEFIK/test.crt -key-file $CERTS_PATH_TRAEFIK/test.key $(getAllHostsForMakeCerts | tr ',' ' ')
# recreate proxy container for reload new certs
if [[ -n "$(docker-compose ps --services | grep "\<proxy\>")" ]]; then
# add tunnels file to composer to avoid found orphan containers warning.
docker-compose up -d --force-recreate --remove-orphans proxy
fi
fi
}
function startProjects() {
# create all projects yml files
createProjects
createCertFiles
addProjects
# start projects
docker-compose up -d --remove-orphans
}
function scaleContainer() {
addProjects
CONTAINER=$2
INSTANCES=$3
isContainerRunning "$CONTAINER"
docker-compose up -d --scale "$CONTAINER=$INSTANCES"
}
function commandContainer() {
addProjects
CONTAINER=$2
shift 2
isContainerRunning "$CONTAINER"
echo
echo "Run command on container <$CONTAINER>..."
echo
docker-compose exec -u "$(id -u)" "$CONTAINER" "$@"
}
function startContainers() {
docker-compose up -d --remove-orphans
startProjects
}
function stopContainers() {
docker-compose down --remove-orphans
}
function recreateContainers() {
createProjects
addProjects
createCertFiles
docker-compose up -d --remove-orphans
}
while getopts ":c:m:ihskru" arg; do
case $arg in
s)
checkSetup
createCertFiles
startContainers
exit 0
;;
k)
checkSetup
stopContainers
exit 0
;;
u)
checkSetup
createCertFiles
recreateContainers
exit 0
;;
r)
checkSetup
stopContainers
createCertFiles
startContainers
exit 0
;;
m)
checkSetup
scaleContainer "$@"
exit 0
;;
c)
checkSetup
commandContainer "$@"
exit 0
;;
i)
setup
exit 0
;;
h | *)
help
exit 0
;;
esac
done