-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkickstart.sh
executable file
·488 lines (385 loc) · 15.6 KB
/
kickstart.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/bin/bash
#
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# DO NOT EDIT THIS FILE! CHANGES WILL BE OVERWRITTEN ON UPDATE
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# Ready to use development containers. Just run ./kickstart.sh to start
# a development environment for this project.
#
# Config-File: .kick.yml
#
# Kickstart home: https://github.com/c7lab/kickstart
#
# Author: Matthias Leuffen <leuffen@continue.de>
#
# Error Handling.
set -o errtrace
trap 'on_error $LINENO' ERR;
PROGNAME=$(basename $0)
PROGPATH="$( cd "$(dirname "$0")" ; pwd -P )" # The absolute path to kickstart.sh
function on_error () {
echo "Error: ${PROGNAME} on line $1" 1>&2
exit 1
}
# You can overwrite these variables in your .kickstartconfig
KICKSTART_DOCKER_OPTS=""
KICKSTART_DOCKER_RUN_OPTS=""
KICKSTART_PORTS="80:80/tcp;4000:4000/tcp;4100:4100/tcp;4200:4200/tcp;4000:4000/udp"
KICKSTART_HOST_IP=$(hostname -I | awk '{print $1;}')
CONTAINER_NAME=${PWD##*/}
if test -t 1; then
# see if it supports colors...
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
export COLOR_NC='\e[0m' # No Color
export COLOR_WHITE='\e[1;37m'
export COLOR_BLACK='\e[0;30m'
export COLOR_BLUE='\e[0;34m'
export COLOR_LIGHT_BLUE='\e[1;34m'
export COLOR_GREEN='\e[0;32m'
export COLOR_LIGHT_GREEN='\e[1;32m'
export COLOR_CYAN='\e[0;36m'
export COLOR_LIGHT_CYAN='\e[1;36m'
export COLOR_RED='\e[0;31m'
export COLOR_LIGHT_RED='\e[1;31m'
export COLOR_PURPLE='\e[0;35m'
export COLOR_LIGHT_PURPLE='\e[1;35m'
export COLOR_BROWN='\e[0;33m'
export COLOR_YELLOW='\e[1;33m'
export COLOR_GRAY='\e[0;30m'
export COLOR_LIGHT_GRAY='\e[0;37m'
fi;
fi;
if [ "$DEV_CONTAINER_NAME" != "" ]
then
echo -e $COLOR_RED "\n[ERR] Are you trying to run kickstart.sh from inside a kickstart container?!"
echo "(Detected DEV_CONTAINER_NAME is set in environment)"
echo -e $COLOR_NC
exit 4;
fi;
command -v curl >/dev/null 2>&1 || { echo -e "$COLOR_LIGHT_RED I require curl but it's not installed (run: 'apt-get install curl'). Aborting.$COLOR_NC" >&2; exit 1; }
command -v docker >/dev/null 2>&1 || { echo -e "$COLOR_LIGHT_RED I require docker but it's not installed (see http://docker.io). Aborting.$COLOR_NC" >&2; exit 1; }
_KICKSTART_DOC_URL="https://github.com/infracamp/kickstart/"
_KICKSTART_UPGRADE_URL="https://raw.githubusercontent.com/infracamp/kickstart/master/dist/kickstart.sh"
_KICKSTART_RELEASE_NOTES_URL="https://raw.githubusercontent.com/infracamp/kickstart/master/dist/kickstart-release-notes.txt"
_KICKSTART_VERSION_URL="https://raw.githubusercontent.com/infracamp/kickstart/master/dist/kickstart-release.txt"
_KICKSTART_CURRENT_VERSION="1.2.0"
##
# This variables can be overwritten by ~/.kickstartconfig
#
KICKSTART_WIN_PATH=""
# Publish ports - separated by semikolon (define it in .kickstartconfig)
OFFLINE_MODE=0
if [ -e "$HOME/.kickstartconfig" ]
then
echo "Loading $HOME/.kickstartconfig"
. $HOME/.kickstartconfig
fi
if [ -e "$PROGPATH/.kickstartconfig" ]
then
echo "Loading $PROGPATH/.kickstartconfig (This is risky if you - abort if unsure)"
sleep 1
# @todo Search for .kickstartconfig in gitignore to verify the user wants this.
. $PROGPATH/.kickstartconfig
fi
_usage() {
echo -e $COLOR_NC "Usage: $0 [<arguments>] [<command>]
COMMANDS:
$0 :[command] [command2...]
Execute kick <command> and return (development mode)
$0 ci-build
Build the service and push to gitlab registry (gitlab_ci_runner)
$0 skel list|install [name]
List / Install a skeleton project (see http://github.com/infracamp/kickstart-skel)
$0 skel upgrade
Upgrade to the latest kickstart version
$0 wakeup
Try to start a previous image with same container name (faster startup)
EXAMPLES
$0 Just start a shell inside the container (default development usage)
$0 :test Execute commands defined in section 'test' of .kick.yml
$0 :debug Execute the container in debug-mode (don't execute kick-commands)
ARGUMENTS
-h Show this help
-t <tagName> --tag=<tagname> Run container with this tag (development)
-u --unflavored Run the container whithout running any scripts (develpment)
--offline Do not pull images nor ask for version upgrades
"
exit 1
}
_print_header() {
echo -e $COLOR_WHITE "
infracamp's
▄█ ▄█▄ ▄█ ▄████████ ▄█ ▄█▄ ▄████████ ███ ▄████████ ▄████████ ███
███ ▄███▀ ███ ███ ███ ███ ▄███▀ ███ ███ ▀█████████▄ ███ ███ ███ ███ ▀█████████▄
███▐██▀ ███▌ ███ █▀ ███▐██▀ ███ █▀ ▀███▀▀██ ███ ███ ███ ███ ▀███▀▀██
▄█████▀ ███▌ ███ ▄█████▀ ███ ███ ▀ ███ ███ ▄███▄▄▄▄██▀ ███ ▀
▀▀█████▄ ███▌ ███ ▀▀█████▄ ▀███████████ ███ ▀███████████ ▀▀███▀▀▀▀▀ ███
███▐██▄ ███ ███ █▄ ███▐██▄ ███ ███ ███ ███ ▀███████████ ███
███ ▀███▄ ███ ███ ███ ███ ▀███▄ ▄█ ███ ███ ███ ███ ███ ███ ███
███ ▀█▀ █▀ ████████▀ ███ ▀█▀ ▄████████▀ ▄████▀ ███ █▀ ███ ███ ▄████▀
▀ ▀ ███ ███
http://infracamp.org happy containers
" $COLOR_YELLOW "
+-------------------------------------------------------------------------------------------------------+
| Infracamp's Kickstart - DEVELOPER MODE |
| Version: $_KICKSTART_CURRENT_VERSION
| Flavour: $USE_PIPF_VERSION (defined in 'from:'-section of .kick.yml)"
KICKSTART_NEWEST_VERSION=`curl -s "$_KICKSTART_VERSION_URL"` || true
if [ "$KICKSTART_NEWEST_VERSION" != "$_KICKSTART_CURRENT_VERSION" ]
then
echo "| "
echo "| UPDATE AVAILABLE: Head Version: $KICKSTART_NEWEST_VERSION"
echo "| To Upgrade Version: Run ./kickstart.sh --upgrade "
echo "| "
sleep 5
fi;
echo "| More information: https://github.com/infracamp/kickstart "
echo "| Or ./kickstart.sh help |"
echo "+-------------------------------------------------------------------------------------------------------+"
}
run_shell() {
echo -e $COLOR_CYAN;
terminal="-it"
if [ ! -t 1 ]
then
# Switch to non-interactive terminal (ci-build etc)
terminal="-t"
fi;
if [ `docker ps | grep $CONTAINER_NAME | wc -l` -gt 0 ]
then
echo "[kickstart.sh] Container '$CONTAINER_NAME' already running"
echo "Starting shell... (please press enter)"
echo "";
shellarg="/bin/bash"
if [ "$ARGUMENT" != "" ]
then
shellarg="kick $ARGUMENT"
fi;
docker exec $terminal --user user -e "DEV_TTYID=[SUB]" $CONTAINER_NAME $shellarg
echo -e $COLOR_CYAN;
echo "<=== [kickstart.sh] Leaving container."
echo -e $COLOR_NC
exit 0;
fi
echo "[kickstart.s] Another container is already running!"
docker ps
echo ""
read -r -p "Your choice: (i)gnore, (s)hell, (k)ill, (a)bort?:" choice
case "$choice" in
i|I)
return 0;
;;
s|S)
echo "===> [kickstart.sh] Opening new shell: "
echo -e $COLOR_NC
docker exec $terminal --user user -e "DEV_TTYID=[SUB]" `docker ps | grep "/kickstart/" | cut -d" " -f1` /bin/bash
echo -e $COLOR_CYAN;
echo "<=== [kickstart.sh] Leaving container."
echo -e $COLOR_NC
exit
;;
k|K)
echo "Killing running kickstart containers..."
docker kill `docker ps | grep "/kickstart/" | cut -d" " -f1`
return 0;
;;
*)
echo 'Response not valid'
exit 3;
;;
esac
}
ask_user() {
echo "";
read -r -p "$1 (y|N)" choice
case "$choice" in
n|N)
echo "Abort!";
;;
y|Y)
return 0;
;;
*)
echo 'Response not valid';;
esac
exit 1;
}
_ci_build() {
echo "CI_BUILD: Building container.. (CI_* Env is preset by gitlab-ci-runner)";
BUILD_TAG=":$CI_BUILD_NAME"
if [ "$CI_REGISTRY" == "" ]
then
echo "[Error deploy]: Environment CI_REGISTRY not set"
exit 1
fi
CMD="docker build --pull -t $CI_REGISTRY_IMAGE$BUILD_TAG -f ./Dockerfile ."
echo "[Building] Running '$CMD' (MODE1)";
eval $CMD
echo "Logging in to: $CI_REGISTRY_USER @ $CI_REGISTRY"
echo "$CI_REGISTRY_PASSWORD" | docker login --username $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
docker push $CI_REGISTRY_IMAGE$BUILD_TAG
echo "Push successful..."
exit
}
DOCKER_OPT_PARAMS=$KICKSTART_DOCKER_RUN_OPTS;
run_container() {
echo -e $COLOR_GREEN"Loading container '$USE_PIPF_VERSION'..."
if [ "$OFFLINE_MODE" == "0" ]
then
docker pull "$USE_PIPF_VERSION"
fi;
if [ "$KICKSTART_WIN_PATH" != "" ]
then
# For Windows users: Rewrite Path of bash to Windows path
# Will work only on drive C:/
PROGPATH="${PROGPATH/\/mnt\/c\//$KICKSTART_WIN_PATH}"
fi
docker rm $CONTAINER_NAME || true
echo -e $COLOR_WHITE "==> [$0] STARTING CONTAINER (docker run): Running container in dev-mode..." $COLOR_NC
terminal="-it"
dev_uid=$UID
if [ ! -t 1 ]
then
# Switch to non-interactive terminal (ci-build etc)
terminal="-t"
dev_uid=1000
fi;
cmd="docker $KICKSTART_DOCKER_OPTS run $terminal \
-v \"$PROGPATH/:/opt/\" \
-e \"DEV_CONTAINER_NAME=$CONTAINER_NAME\" \
-e \"DEV_TTYID=[MAIN]\" \
-e \"DEV_UID=$dev_uid\" \
-e \"DOCKER_HOST_IP=$KICKSTART_HOST_IP\" \
-e \"TERM=$TERM\" \
-e \"DEV_MODE=1\" \
$DOCKER_OPT_PARAMS \
--name $CONTAINER_NAME \
$USE_PIPF_VERSION $ARGUMENT"
echo [exec] $cmd
eval $cmd
status=$?
if [[ $status -ne 0 ]]
then
echo -e $COLOR_RED
echo "[kickstart.sh][FAIL]: Container startup failed."
echo -e $COLOR_NC
exit $status
fi;
echo -e $COLOR_WHITE "<== [kickstart.sh] CONTAINER SHUTDOWN"
echo -e $COLOR_RED " Kickstart Exit - Goodbye" $COLOR_NC
exit 0;
}
ARGUMENT="";
# Parse the command parameters
ARGUMENT="";
while [ "$#" -gt 0 ]; do
case "$1" in
-t) USE_PIPF_VERSION="-t $2"; shift 2;;
--tag=*)
USE_PIPF_VERSION="-t ${1#*=}"; shift 1;;
--offline)
OFFLINE_MODE=1; shift 1;;
upgrade|--upgrade)
echo "Checking for updates from $_KICKSTART_UPGRADE_URL..."
curl "$_KICKSTART_RELEASE_NOTES_URL"
ask_user "Do you want to upgrade?"
echo "Writing to $0..."
curl "$_KICKSTART_UPGRADE_URL" -o "$0"
echo "Done"
echo "Calling on update trigger: $0 --on-after-update"
$0 --on-after-upgrade
echo -e "$COLOR_GREEN[kickstart.sh] Upgrade successful.$COLOR_NC"
exit 0;;
--on-after-upgrade)
exit 0;;
wakeup)
docker start -ai $CONTAINER_NAME
exit 0;;
skel)
if [ "$2" == "install" ]
then
ask_user "Do you want to overwrite existing files with skeleton?"
curl https://codeload.github.com/infracamp/kickstart-skel/tar.gz/master | tar -xzv --strip-components=2 kickstart-skel-master/$3/ -C ./
exit 0;
fi;
if [ "$2" == "" ] || [ "$2" == "list" ]
then
echo "------ List of available skeleton projects -------"
curl https://raw.githubusercontent.com/infracamp/kickstart-skel/master/skel.index.txt
echo ""
echo "--------------------------------------------------"
echo "Install a skeleton: $0 skel install <name>"
echo "";
else
echo "Unknown command: Available: $0 --skel list|install <name>"
exit 1
fi
exit 0;;
ci-build|--ci-build)
_ci_build $2 $3
exit0;;
help|-h|--help)
_usage
exit 0;;
--tag) echo "$1 requires an argument" >&2; exit 1;;
:*)
ARGUMENT="${1:1} ${@:2}"
break;;
-*) echo "unknown option: $1" >&2; exit 1;;
*)
echo "invalid command: $1 - see $0 help for more information" >&2; exit 2;;
esac
done
if [ -e "$HOME/.ssh" ]
then
echo "Mounting $HOME/.ssh..."
DOCKER_OPT_PARAMS="$DOCKER_OPT_PARAMS -v $HOME/.ssh:/home/user/.ssh";
fi
if [ -e "$HOME/.gitconfig" ]
then
echo "Mounting $HOME/.gitconfig..."
DOCKER_OPT_PARAMS="$DOCKER_OPT_PARAMS -v $HOME/.gitconfig:/home/user/.gitconfig";
fi
if [ -e "$HOME/.bash_history" ]
then
echo "Mounting $HOME/.bash_history..."
DOCKER_OPT_PARAMS="$DOCKER_OPT_PARAMS -v $HOME/.bash_history:/home/user/.bash_history";
fi
if [ -e "$PROGPATH/.env" ]
then
echo "Adding docker environment from $PROGPATH/.env (Development only)"
DOCKER_OPT_PARAMS="$DOCKER_OPT_PARAMS --env-file $PROGPATH/.env";
fi
# Ports to be exposed
IFS=';' read -r -a _ports <<< "$KICKSTART_PORTS"
for _port in "${_ports[@]}"
do
DOCKER_OPT_PARAMS="$DOCKER_OPT_PARAMS -p $_port"
done
if [ ! -f "$PROGPATH/.kick.yml" ]
then
echo -e $COLOR_RED "[ERR] Missing $PROGPATH/.kick.yml file." $COLOR_NC
ask_user "Do you want to create a new .kick.yml-file?"
echo "# Kickstart container config file - see https://gitub.com/c7lab/kickstart" > $PROGPATH/.kick.yml
echo "# Run ./kickstart.sh to start a development-container for this project" >> $PROGPATH/.kick.yml
echo "version: 1" >> $PROGPATH/.kick.yml
echo 'from: "infracamp/kickstart-flavor-gaia"' >> $PROGPATH/.kick.yml
echo "File created. See $_KICKSTART_DOC_URL for more information";
echo ""
sleep 2
fi
# Parse .kick.yml for line from: "docker/container:version"
USE_PIPF_VERSION=`cat $PROGPATH/.kick.yml | grep "^from:" | tr -d '"' | awk '{print $2}'`
if [ "$USE_PIPF_VERSION" == "" ]
then
echo -e $COLOR_RED "[ERR] .kick.yml file does not include 'from:' - directive." $COLOR_NC
exit 2
fi;
_print_header
if [ `docker ps | grep "/kickstart/" | wc -l` -gt 0 ]
then
run_shell
fi;
run_container