forked from jetpulp/jetdocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jetdocker.sh
executable file
·366 lines (305 loc) · 10 KB
/
jetdocker.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env bash
#
# This script is used to start and stop a docker compose configuration
#
#
VERSION=2.0.0
# Set JETDOCKER must be set during install
if [[ -z "$JETDOCKER" ]]; then
echo "$(tput setaf 1) The env var JETDOCKER must be set during install on the absolute path of your install, and be accessible in your shell."
fi
# Set JETDOCKER_CUSTOM to the path where your custom lib files
# exists, or else we will use the default custom/
if [[ -z "$JETDOCKER_CUSTOM" ]]; then
JETDOCKER_CUSTOM="$JETDOCKER/custom"
fi
# Set JETDOCKER_DOMAIN_NAME to domain name you want to use for your projects
# The default is localhost.tv (All subdomains on *.localhost.tv (except www)
# points to 127.0.0.1 (or 0:0:0:0:0:0:0:1 for IPv6). )
# Man can use 127.0.0.1.xip.io or anOtherIP.xip.io, see http://xip.io/
if [[ -z "$JETDOCKER_DOMAIN_NAME" ]]; then
JETDOCKER_DOMAIN_NAME="localhost.tv"
fi
## BOOTSTRAP ##
source "${JETDOCKER}/lib/oo-bootstrap.sh"
## MAIN ##
import util/log util/exception util/tryCatch util/namedParameters util/class util/log UI/Color
# Default ports for docker containers
export DOCKER_PORT_HTTP=81
export DOCKER_PORT_HTTPS=444
export DOCKER_PORT_MYSQL=3306
export DOCKER_PORT_POSTGRES=5432
export DOCKER_PORT_REDIS=6379
export DOCKER_PORT_RABBITMQ=5672
export DOCKER_PORT_MAILHOG=8025
# Defaut timeout for DB restoring waiting
export DB_RESTORE_TIMEOUT=3m0s
# Default mysql credentials
export MYSQL_ROOT_PASSWORD=root
export MYSQL_USER=root
export MYSQL_PASSWORD=root
# Default postgres credentials
export POSTGRES_ROOT_PASSWORD=root
export POSTGRES_USER=root
export POSTGRES_PASSWORD=root
# Defaut docker-compose startup service
JETDOCKER_UP_DEFAULT_SERVICE=web
# Defaut docker-compose startup service
JETDOCKER_DB_DEFAULT_SERVICE=db
# Defaut docker-compose startup service
JETDOCKER_INSTALL_BEFORE_STARTUP=false
JETDOCKER_INSTALL_AFTER_STARTUP=false
dockerComposeFile="";
dockerComposeInitialised=false;
projectPath=$(pwd)
# declare associative array for commands loaded in plugins
declare -A COMMANDS
declare -A COMMANDS_USAGE
declare -A COMMANDS_STANDALONE
#
# usage function
#
Jetdocker::Usage()
{
#Cette fonction affiche les consignes d'usage du script
echo ""
echo "$(UI.Color.Blue)Usage:$(UI.Color.Default) jetdocker [OPTIONS] COMMAND"
echo ""
echo "$(UI.Color.Yellow)Options:$(UI.Color.Default)"
echo " -c, --config string Location of project docker config files (default \"./docker\")"
echo " -D, --debug Enable debug mode"
echo " -h, --help Print help information and quit"
echo " -v, --version Print version information and quit"
echo ""
echo "$(UI.Color.Yellow)Commands:$(UI.Color.Default)"
COMMANDS_ORDERED=( $(
for el in "${!COMMANDS_USAGE[@]}"
do
echo "$el"
done | sort) )
for command in "${COMMANDS_ORDERED[@]}"
do
echo "${COMMANDS_USAGE[$command]}"
done
echo ""
echo "Run 'jetdocker COMMAND --help' for more information on a command."
}
Jetdocker::CheckLastExecutionOlderThanOneDay()
{
flagfile=/tmp/jetdocker${1}
if [ ! -f $flagfile ]; then
echo 0 > $flagfile
fi
lastupdate=$(cat $flagfile)
now=$(date +%s)
oneday=( 24*60*60 )
if [ $(( now - oneday)) -gt "$lastupdate" ]; then
echo "$now" > "$flagfile"
echo "true"
else
echo "false"
fi
}
COMMANDS['update']='Jetdocker::Update' # Function name
COMMANDS_USAGE['20']=" update Update jetdocker to the latest version"
COMMANDS_STANDALONE['update']='Jetdocker::Update' # Function name
Jetdocker::Update()
{
Log "Jetdocker::Update"
previousPath=$(pwd)
cd "$JETDOCKER" || exit
echo "$(UI.Color.Green)"
echo "Upgrading jetdocker"
echo ""
if git pull --rebase --stat origin master
then
echo ""
echo "Jetdocker upgraded"
else
echo ""
echo "$(UI.Color.Red)There was an error updating jetdocker"
fi
echo "$(UI.Color.Default)"
cd "$previousPath" || exit
Jetdocker::UpdateCustom
}
Jetdocker::UpdateCustom() {
Log "Jetdocker::UpdateCustom"
}
Jetdocker::CheckProject()
{
Log "Jetdocker::CheckProject"
envShPath=$(find $optConfigPath -type f -name "env.sh")
Log "env.sh in $envShPath"
optConfigPath=$(dirname $envShPath)
Log "go in $optConfigPath"
if [ -d "$optConfigPath" ]; then
cd "$optConfigPath" || exit
fi
Log "We're in $optConfigPath directory"
# Check there's a docker-compose file in current directory
if [ ! -f 'docker-compose.yml' ]; then
echo ""
echo "$(UI.Color.Red) docker-compose.yml file doesn't exist in $(pwd)!"
echo ""
exit 1
fi
Log "docker-compose.yml file exist"
# Check there's a env.sh file in current directory
if [ ! -f 'env.sh' ]; then
echo ""
echo "$(UI.Color.Red) env.sh file doesn't exist in $(pwd)!"
echo ""
exit 1
fi
Log "env.sh file exist"
# Source env.sh
# shellcheck disable=SC1091
source "env.sh"
if Jetdocker::FunctionExists init; then
Log "init function exist"
init
else
echo ""
echo "$(UI.Color.Red) Function init does not exist in env.sh, it must be implemented ! "
echo ""
exit 1
fi
}
#
# Check if the function exists
#
Jetdocker::FunctionExists()
{
Log "Jetdocker::FunctionExists"
type "$1" 2>/dev/null | grep -q 'is a function'
if [ $? -eq 0 ]; then
return 0
fi
#on different OS the message language is not the same
type "$1" 2>/dev/null | grep -q 'est une fonction'
if [ $? -eq 0 ]; then
return 0
fi
return 1
}
#
# (Re)generate SSL certificate (in docker volume), if not exist or if force argument passed
#
Jetdocker::GenerateSSLCertificate() {
Log "Jetdocker::GenerateSSLCertificate"
try {
docker volume inspect jetdocker-ssl-certificate | grep "$JETDOCKER_DOMAIN_NAME" > /dev/null 2>&1
} catch {
try {
Log "remove volume jetdocker-ssl-certificate"
docker volume rm jetdocker-ssl-certificate
} catch {
Log "No jetdocker-ssl-certificate volume to remove"
}
Jetdocker::GenerateRootCACertificate
Log "Generate volume jetdocker-ssl-certificate"
docker volume create jetdocker-ssl-certificate --label "$JETDOCKER_DOMAIN_NAME"
docker pull jetpulp/jetdocker-ssl-certificate:latest
docker run --rm -e COMMON_NAME="*.$JETDOCKER_DOMAIN_NAME" -e KEY_NAME=jetdocker-ssl-certificate -v jetdocker-ssl-certificate:/certs -v "$JETDOCKER/cacerts:/cacerts" jetpulp/jetdocker-ssl-certificate
}
#recopie les certificats généré dans le dossier courrant du host afin de pouvoir les utiliser dans browser-sync en local
Log "local copy SSL certificate"
docker run --rm --user "$(id -u):$(id -g)" -v jetdocker-ssl-certificate:/certs -v $(pwd):/certs_host jetpulp/jetdocker-ssl-certificate bash -c "cp -Rf certs/ certs_host/"
touch .gitignore
grep -q -F certs .gitignore || echo 'certs' >> .gitignore
}
#
# (Re)generate RootCA certificate
#
# cacerts/jetdockerRootCA.crt has been generated without passphrase :
# > ssh-keygen -q -t rsa -f jetdockerRootCA.key -N ''
# > openssl req -new -x509 -sha256 -days 3650 -key jetdockerRootCA.key -out jetdockerRootCA.crt
#
Jetdocker::GenerateRootCACertificate() {
Log "Jetdocker::GenerateRootCACertificate"
Log "cp ${JETDOCKER}/cacerts/jetdockerRootCA.crt ${JETDOCKER}/cacerts/rootCA.crt"
cp "${JETDOCKER}/cacerts/jetdockerRootCA.crt" "${JETDOCKER}/cacerts/rootCA.crt"
Log "cp ${JETDOCKER}/cacerts/jetdockerRootCA.key ${JETDOCKER}/cacerts/rootCA.key"
cp "${JETDOCKER}/cacerts/jetdockerRootCA.key" "${JETDOCKER}/cacerts/rootCA.key"
}
# Load all of the config files in ~/.jetdocker/plugins and in custom/plugins that end in .sh
pluginsFiles="$(ls $JETDOCKER_CUSTOM/jetdocker.sh 2> /dev/null) $(ls $JETDOCKER_CUSTOM/plugins/*.sh 2> /dev/null) $(ls $JETDOCKER/plugins/*.sh)"
declare -A loadedPlugins
for pluginfile in $pluginsFiles; do
pluginfilename=$(basename "${pluginfile}")
if [ ! -n "${loadedPlugins[$pluginfilename]}" ]; then
Log "source $pluginfile"
source $pluginfile
loadedPlugins[$pluginfilename]=true;
fi
done
optDebug=false
optConfigPath=docker
optVersion=false
optHelpJetdocker=false
# Analyse des arguments de la ligne de commande grâce à l'utilitaire getopts
while getopts ":vhDc:-:" opt ; do
case $opt in
D ) optDebug=true;;
c ) optConfigPath=$OPTARG;;
v ) optVersion=true;;
h ) optHelpJetdocker=true;;
- ) case $OPTARG in
debug ) optDebug=true;;
config ) optConfigPathg=$2;shift;;
help ) optHelpJetdocker=true;;
version ) optVersion;;
* ) echo "$(UI.Color.Red)illegal option --$OPTARG"
Jetdocker::Usage
exit 1;;
esac;;
? ) echo "$(UI.Color.Red)illegal option -$opt"
Jetdocker::Usage
exit 1;;
esac
done
shift $((OPTIND - 1))
export DEBUG=${optDebug}
namespace jetdocker
${DEBUG} && Log::AddOutput jetdocker DEBUG
${DEBUG} && docker --version
${DEBUG} && docker-compose --version
Log "optDebug = ${optDebug}"
Log "optConfigPath = ${optConfigPath}"
Log "optVersion = ${optVersion}"
Log "optHelpJetdocker = ${optHelpJetdocker}"
Log "JETDOCKER = ${JETDOCKER}"
Log "JETDOCKER_CUSTOM = ${JETDOCKER_CUSTOM}"
Log "JETDOCKER_DOMAIN_NAME = ${JETDOCKER_DOMAIN_NAME}"
${optVersion} && {
echo "Jetdocker v$VERSION"
exit 0
}
${optHelpJetdocker} && {
Jetdocker::Usage
exit 0
}
#Get command, and pass command line to his function
if [ "$1" != "" ] && [ -n "${COMMANDS[$1]}" ]; then
command=$1
commandFunction=${COMMANDS[$1]}
shift
else
echo "$(UI.Color.Red)illegal command \"$1\""
Jetdocker::Usage
exit 1
fi
if [ "$(Jetdocker::CheckLastExecutionOlderThanOneDay)" == "true" ]; then
Jetdocker::Update
fi
if [ -n "${COMMANDS_STANDALONE[$command]}" ]; then
"$commandFunction" "$@"
exit $?
fi
Jetdocker::CheckProject
if [ $? -eq 1 ]; then
exit 1
fi
Jetdocker::GenerateSSLCertificate false
"$commandFunction" "$@"