-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathide_functions
executable file
·995 lines (918 loc) · 34.4 KB
/
ide_functions
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/bin/bash
# shellcheck disable=SC1090
# shellcheck disable=SC2181
# shellcheck disable=SC2153
log_debug() {
if [[ "${quiet}" != "true" ]]; then
if [ "$IDE_LOG_LEVEL" == "debug" ]; then
echo -e "$(date "+%d-%m-%Y %T") IDE debug: ${1}"
fi
fi
}
log_error() {
if [[ "${quiet}" != "true" ]]; then
echo -e "\e[31m$(date "+%d-%m-%Y %T") IDE error: $1\e[0m"
echo -e "\e[31m$(date "+%d-%m-%Y %T") IDE error: File: ${BASH_SOURCE[0]}\e[0m"
echo -e "\e[31m$(date "+%d-%m-%Y %T") IDE error: Line numbers: ${BASH_LINENO[*]}\e[0m"
fi
}
log_warn() {
if [[ "${quiet}" != "true" ]]; then
echo -e "\e[33m$(date "+%d-%m-%Y %T") IDE warn: $1\e[0m"
fi
}
log_info() {
if [[ "${quiet}" != "true" ]]; then
echo -e "\e[32m$(date "+%d-%m-%Y %T") IDE info: $1\e[0m"
fi
}
# http://stackoverflow.com/a/17841619
function join_by {
local IFS="$1"; shift; echo "$*"
}
# Returns an identificator that can be reused later in many places,
# e.g. as some file name or as docker container name.
# e.g. ide-example-ide-usage-2016-03-08_18-51-09-68509321
get_run_id() {
directory_name=$(basename "${PWD}")
directory_name_no_space="${directory_name/ /_}"
# It must contain a random number, the time is insufficient.
# It already was a problem when 2 ci jobs were started in the
# same second, they wrote to the same file and 2 processes were reading the
# same file (it concerns function: get_env_vars_file_name).
# I want to have a long random number, I know this method skews random numbers
# to the lower regions of boundaries and that it might not work in e.g.
# busybox docker image. But, is there bash in busybox? If we wanted to port IDE
# to work on busybox, this would probably demand more work than only
# other way of getting random numbers. See if this can help then:
# http://stackoverflow.com/questions/7642743/how-to-generate-random-numbers-in-the-busybox-shell
random1=$((RANDOM % 10000))
random2=$((RANDOM % 10000))
echo "ide-${directory_name_no_space}-$(date +%Y-%m-%d_%H-%M-%S)-${random1}${random2}"
}
# Returns a name of a file which will contain environment variables.
# e.g. /tmp/ide-environment-example-ide-usage-2016-03-08_18-51-09-68509321
# The file name must start with "ide-" and must not be put into a shared directory
# like /tmp/ide, because there may be 2 users sharing /tmp directory
# and we could not set proper permissions of /tmp/ide without sudo
# (same permissions as /tmp directory has) #12181.
get_env_vars_file_name() {
run_id="$1"
if [ -z "$run_id" ]; then
log_error "run_id not specified"
return 1;
fi
echo "/tmp/ide-environment-${run_id}"
}
# $1 is the environment variable name and does not start with IDE_
# Example: $1 is ABC. If IDE_ABC is already set, this method returns
# IDE_ABC=${IDE_ABC}. If IDE_ABC is not set, this method returns IDE_ABC=${ABC}.
help_save_blacklisted_variable() {
env_var="$1"
if [ -z "$env_var" ]; then
log_error "env_var not specified"
return 1;
fi
if [[ "$env_var" == "IDE_"* ]]; then
log_error "env_var starts with IDE_"
return 1;
fi
ide_env_var="IDE_${env_var}"
ide_env_var_value=$(printenv "${ide_env_var}")
if [ -z "$ide_env_var_value" ]; then
# "IDE_${env_var}" not set
env_var_value=$(printenv "${env_var}")
echo "${ide_env_var}=${env_var_value}"
else
echo "${ide_env_var}=${ide_env_var_value}"
fi
}
# Creates a file ($1) of format ENV_VAR_NAME="env var value" made up of current
# environment variables. Respects the collection of blacklisted variables ($2).
# If any env variable is blacklisted, it will be saved with "IDE_" prefix.
# If env var with "IDE_" prefix already exists, its value is taken, instead of
# the primary variable. E.g. PWD is blacklisted, so it will be saved as
# "IDE_PWD=/some/path", and that "/some/path" is a value of IDE_PWD if already
# exists, otherwise of PWD.
# Variables can be also blacklisted with asterix, e.g. BASH*. This means that
# any variable starting with BASH will be prefixed.
# Variables with IDE_ prefix cannot be blacklisted.
#
# $1 is a file path to which we save to, string
# $2 is the collection of blacklisted variables, string separated by commas
function save_environment_variables() {
env_file="$1"
if [ -z "$env_file" ]; then
log_error "env_file not specified"
return 1;
fi
log_debug "Writing environment variables to $env_file"
if [ -f "$env_file" ]; then
rm "$env_file"
fi
blacklisted_variables_from_user="$2"
if [ -z "$blacklisted_variables_from_user" ]; then
log_error "blacklisted_variables_from_user not specified"
return 1;
fi
# split blacklisted_variables_from_user with ',' as delimeter
blacklisted_variables=$(echo "$blacklisted_variables_from_user" | tr "," "\n")
for blacklisted_env_var in ${blacklisted_variables} ; do
# do not allow to blacklist any environment variable which is
# used by ide with "IDE_" prefix
if [[ "$blacklisted_env_var" == "WORK" ]]; then
log_error "blacklisted WORK environment variable"
return 1;
fi
if [[ "$blacklisted_env_var" == "IDENTITY" ]]; then
log_error "blacklisted IDENTITY environment variable"
return 1;
fi
if [[ "$blacklisted_env_var" == "DRIVER" ]]; then
log_error "blacklisted DRIVER environment variable"
return 1;
fi
if [[ "$blacklisted_env_var" == "DOCKER_IMAGE" ]]; then
log_error "blacklisted DOCKER_IMAGE environment variable"
return 1;
fi
if [[ "$blacklisted_env_var" == "DOCKER_OPTIONS" ]]; then
log_error "blacklisted DOCKER_OPTIONS environment variable"
return 1;
fi
done
# Get all the environment variables set.
# The env command with no arguments will print a list of the "exported"
# environment variables and their values. These variables are made visible to
# subprocesses. But the env command prints some variables in 2 lines, e.g.:
# COMP_WORDBREAKS=
# "'><;|&(:
# Thus, and also because I need variables' names separately, I prefer
# compgen -e.
host_env=$(compgen -e)
DOCKER_ENVS_ARRAY=()
for env_var in $host_env ; do
# now we are looking only for IDE_ variables, for which the no IDE_ variables
# are not set, e.g. IDE_ABC is set and ABC is not set. Without this,
# IDE_ABC would be skipped.
# * what if someone has variable WORK and it is not blacklisted?
# Then IDE_WORK will never be preserved. And that is fine.
# * what if someone has variable WORK and is it blacklisted?
# Its value will override IDE_WORK value. And that will make IDE fail
# but only in "ide in ide". -- do not allow to blacklist WORK and similar
# done above
if [[ "$env_var" == "IDE_"* ]]; then
env_var_without_prefix=$(echo "$env_var" | awk '{ gsub(/IDE_/, ""); print }')
skip="false"
for element in $host_env ; do
if [[ "$element" == "$env_var_without_prefix" ]]; then
skip="true"
log_debug "skipping $element == ${env_var_without_prefix}"
break;
fi
done
if [[ "$skip" == "true" ]]; then
log_debug "skipping ${env_var}"
# skip that variable in order not to save it twice
continue;
else
env_var_value=$(printenv "${env_var}")
DOCKER_ENVS_ARRAY+=("${env_var}=${env_var_value}")
log_debug "added ${env_var}=${env_var_value}"
fi
fi
# now consider only variables without IDE_ prefix
blacklisted="false"
for blacklisted_var in $blacklisted_variables ; do
if [[ "$blacklisted_var" == *"*" ]]; then
# this blacklisted variable ends with asterix, e.g. BASH*
# get its name without asterix, e.g. BASH
# echo "$blacklisted_var ends with *"
no_asterix_name=$(echo "$blacklisted_var" | awk '{ gsub(/\*/, ""); print }')
if [[ "$env_var" == "$no_asterix_name"* ]]; then
log_debug "${env_var} blacklisted because of $blacklisted_var"
var_name_value=$(help_save_blacklisted_variable "${env_var}")
DOCKER_ENVS_ARRAY+=("${var_name_value}")
blacklisted="true"
break;
fi
elif [[ "$env_var" == "$blacklisted_var" ]]; then
log_debug "${env_var} blacklisted"
var_name_value=$(help_save_blacklisted_variable "${env_var}")
DOCKER_ENVS_ARRAY+=("${var_name_value}")
blacklisted="true"
break;
fi
done
if [[ "$blacklisted" == "false" ]]; then
# too much noise, so commented out:
# log_debug "${env_var} not blacklisted"
# this variable was not blacklisted, no need to add prefix before its name
env_var_value=$(printenv "${env_var}")
DOCKER_ENVS_ARRAY+=("${env_var}=${env_var_value}")
fi
done
if [ -n "$DISPLAY" ]; then
# DISPLAY is set, enable running in graphical mode.
# No matter what was its value, always set it to: unix:0.0
DOCKER_ENVS_ARRAY+=("DISPLAY=unix:0.0")
fi
for env_var in "${DOCKER_ENVS_ARRAY[@]}" ; do
echo "$env_var" >> "$env_file"
done
}
# Echoes the docker run command.
# $1 is the file with environment variables to be mounted into docker container
# $2 is the true/false whether to run interactively
# $3 is docker container name
# $4 is docker image name
# $5 is IDE_WORK
# $6 is IDE_IDENTITY
# $7 is remove_containers, whether or not to remove docker container
# $8 is command to be run in docker container
# $9 are docker options
function construct_docker_command() {
ide_driver="$1"
env_file="$2"
run_interactively="$3"
container_name="$4"
image_name="$5"
ide_work="$6"
ide_work_inner="$7"
ide_identity="$8"
remove_containers="$9"
# can be unset
command="${10}"
# can be unset
ide_docker_options="${11}"
if [ -z "$env_file" ]; then
log_error "env_file not specified"
return 1;
fi
if [ -z "$run_interactively" ]; then
log_error "run_interactively not specified"
return 1;
fi
if [ -z "$container_name" ]; then
log_error "container_name not specified"
return 1;
fi
if [ -z "$image_name" ]; then
log_error "image_name not specified"
return 1;
fi
if [ -z "$ide_work" ]; then
log_error "ide_work not specified"
return 1;
fi
if [ -z "$ide_work_inner" ]; then
log_error "ide_work_inner not specified"
return 1;
fi
if [ -z "$ide_identity" ]; then
log_error "ide_identity not specified"
return 1;
fi
if [ -z "$remove_containers" ]; then
log_error "remove_containers not specified"
return 1;
fi
# initial part of the docker command
if [[ "${ide_driver}" == "docker" ]]; then
docker_cmd="docker run"
elif [[ "${ide_driver}" == "nvidia-docker" ]]; then
docker_cmd="nvidia-docker run"
else
log_error "ide_driver was set to ${ide_driver}, which is not supported"
return 1;
fi
if [[ "$remove_containers" == "true" ]]; then
docker_cmd+=" --rm"
fi
docker_cmd+=" -v ${ide_work}:${ide_work_inner} -v ${ide_identity}:/ide/identity:ro"
docker_cmd+=" --env-file=\"$env_file\""
if [ -n "$DISPLAY" ]; then
# DISPLAY is set, enable running in graphical mode
docker_cmd+=" -v /tmp/.X11-unix:/tmp/.X11-unix"
fi
if [ -n "$ide_docker_options" ]; then
docker_cmd+=" $ide_docker_options"
fi
if [[ "$run_interactively" == "true" ]]; then
docker_cmd+=" -ti"
fi
docker_cmd+=" --name ${container_name}"
if [ -n "$command" ]; then
# $command set
if [[ "$command" == "\""*"\"" ]] || [[ "$command" == *"\""* ]]; then
# $command is wrapped with double quotes already OR
# $command contains double quotes
docker_cmd+=" $image_name $command"
else
# no quotes at all or
# single quotes (treated as no quotes)
docker_cmd+=" $image_name \"$command\""
fi
else
# $command not set
# why? Because, if $command is set, then we want to quote it.
# And when $command is not set, we don't want quotes.
docker_cmd+=" $image_name"
fi
echo "$docker_cmd"
}
# Echoes the first part of docker-compose command.
# $1 is the file with environment variables to be mounted into docker container
# $2 is the docker-compose yaml file path
# $3 is docker-compose project name
# $4 is IDE_WORK
# $5 is ide_work_inner
# $6 is IDE_IDENTITY
function construct_docker_compose_command_part1() {
env_file="$1"
docker_compose_file="$2"
project_name="$3"
ide_work="$4"
ide_work_inner="$5"
ide_identity="$6"
if [ -z "$env_file" ]; then
log_error "env_file not specified"
return 1;
fi
if [ -z "$docker_compose_file" ]; then
log_error "docker_compose_file not specified"
return 1;
fi
if [ -z "$project_name" ]; then
log_error "project_name not specified"
return 1;
fi
if [ -z "$ide_work" ]; then
log_error "ide_work not specified"
return 1;
fi
if [ -z "$ide_work_inner" ]; then
log_error "ide_work_inner not specified"
return 1;
fi
if [ -z "$ide_identity" ]; then
log_error "ide_identity not specified"
return 1;
fi
docker_compose_cmd="IDE_WORK=\"${ide_work}\" IDE_IDENTITY=\"${ide_identity}\" ENV_FILE=\"${env_file}\" docker-compose -f ${docker_compose_file} -p ${project_name}"
echo "$docker_compose_cmd"
}
# Echoes the docker-compose run command.
# $1 is the file with environment variables to be mounted into docker container
# $2 is the docker-compose yaml file path
# $3 is docker-compose project name
# $4 is the true/false whether to run interactively
# $5 is IDE_WORK
# $6 is ide_work_inner
# $7 is IDE_IDENTITY
# $8 is command to be run in docker container
# $9 are docker compose options
function construct_docker_compose_run_command() {
env_file="$1"
docker_compose_file="$2"
project_name="$3"
run_interactively="$4"
ide_work="$5"
ide_work_inner="$6"
ide_identity="$7"
# can be unset
command="$8"
# can be unset
ide_docker_compose_options="$9"
# initial part of the docker compose command
docker_compose_cmd=$(construct_docker_compose_command_part1 "${env_file}" "${docker_compose_file}" "${project_name}" "${ide_work}" "${ide_work_inner}" "${ide_identity}")
if [[ "$?" != "0" ]]; then
log_error "$docker_compose_cmd"
return 1
fi
# first let construct_docker_compose_command_part1 fail, then fail here
if [ -z "$run_interactively" ]; then
log_error "run_interactively not specified"
return 1;
fi
docker_compose_cmd+=" run --rm"
if [[ "$run_interactively" == "false" ]]; then
docker_compose_cmd+=" -T"
fi
if [ -n "$ide_docker_compose_options" ]; then
docker_compose_cmd+=" $ide_docker_compose_options"
fi
docker_compose_cmd+=" default"
if [ -n "$command" ]; then
# $command set
if [[ "$command" == "\""*"\"" ]] || [[ "$command" == *"\""* ]]; then
# $command is wrapped with double quotes already OR
# $command contains double quotes
docker_compose_cmd+=" $command"
else
# no quotes at all or
# single quotes (treated as no quotes)
docker_compose_cmd+=" \"$command\""
fi
fi
echo "$docker_compose_cmd"
}
# Echoes the docker-compose stop command.
# $1 is the file with environment variables to be mounted into docker container
# $2 is docker-compose project name
# $3 is the docker-compose yaml file path
# $4 is IDE_WORK
# $5 is ide_work_inner
# $6 is IDE_IDENTITY
function construct_docker_compose_stop_command() {
env_file="$1"
docker_compose_file="$2"
project_name="$3"
ide_work="$4"
ide_work_inner="$5"
ide_identity="$6"
docker_compose_cmd=$(construct_docker_compose_command_part1 "${env_file}" "${docker_compose_file}" "${project_name}" "${ide_work}" "${ide_work_inner}" "${ide_identity}")
if [[ "$?" != "0" ]]; then
log_error "$docker_compose_cmd"
return 1
fi
docker_compose_cmd+=" stop"
echo "$docker_compose_cmd"
}
# Echoes the docker-compose rm command.
# $1 is the file with environment variables to be mounted into docker container
# $2 is docker-compose project name
# $3 is the docker-compose yaml file path
# $4 is IDE_WORK
# $5 is ide_work_inner
# $6 is IDE_IDENTITY
function construct_docker_compose_rm_command() {
env_file="$1"
docker_compose_file="$2"
project_name="$3"
ide_work="$4"
ide_work_inner="$5"
ide_identity="$6"
docker_compose_cmd=$(construct_docker_compose_command_part1 "${env_file}" "${docker_compose_file}" "${project_name}" "${ide_work}" "${ide_work_inner}" "${ide_identity}")
if [[ "$?" != "0" ]]; then
log_error "$docker_compose_cmd"
return 1
fi
docker_compose_cmd+=" rm -f"
echo "$docker_compose_cmd"
}
function verify_docker_compose_file(){
docker_compose_file="$1"
if [ -z "$docker_compose_file" ]; then
log_error "docker_compose_file not specified"
return 1;
fi
contents=$(cat "$docker_compose_file")
if [[ $(echo "$contents" | grep "IDE_IDENTITY" -c) == "0" ]]; then
log_error "$docker_compose_file does not contain IDE_IDENTITY"
exit 1
fi
if [[ $(echo "$contents" | grep "IDE_WORK" -c) == "0" ]]; then
log_error "$docker_compose_file does not contain IDE_WORK"
exit 1
fi
if [[ $(echo "$contents" | grep "ENV_FILE" -c) == "0" ]]; then
log_error "$docker_compose_file does not contain ENV_FILE"
exit 1
fi
if [[ $(echo "$contents" | grep "IDE_IDENTITY" | grep ":ro" -c) == "0" ]]; then
log_error "$docker_compose_file does not contain \":ro\" when mounting IDE_IDENTITY"
exit 1
fi
# This is the least I can do for now. I could use the command:
# `docker-compose -f some.yml config --services`, but it says that the
# current directory is not a file and fails.
if [[ $(echo "$contents" | grep "default:" -c) == "0" ]]; then
log_error "$docker_compose_file does not contain \"default\" container"
exit 1
fi
}
function verify_idefile() {
idefile="$1"
if [ -z "${idefile}" ]; then
log_error "idefile path set to zero-length string"
exit 1;
fi
if [ ! -f "${idefile}" ]; then
# do not get full path here with:
# idefile_full_path=$(readlink -f "$idefile")
# it will be null
log_warn "idefile: ${idefile} does not exist (relative to ${PWD})"
else
log_debug "idefile: ${idefile}"
source "${idefile}"
fi
if [ -z "${IDE_DRIVER}" ]; then
IDE_DRIVER="docker"
log_debug "IDE_DRIVER not set, setting to: $IDE_DRIVER"
fi
if [ "${IDE_DRIVER}" != "docker" ] && [ "${IDE_DRIVER}" != "docker-compose" ] && [ "${IDE_DRIVER}" != "nvidia-docker" ]; then
log_error "IDE_DRIVER set to $IDE_DRIVER, supported are: docker, docker-compose, nvidia-docker"
exit 1
fi
}
# Arguments:
# $1 -- all the options for IDE run command
function runfunc() {
# receiving array as bash function parameter, thanks to:
# http://stackoverflow.com/a/4017175
declare -a opts=("${!1}")
# default values
idefile="${PWD}/Idefile"
dryrun="false"
quiet="false"
if [ "$EUID" -eq 0 ]; then
log_warn "Running as root. This is highly inadvisable."
fi
# shellcheck disable=SC2128
if [ -z "$opts" ]; then
log_debug "opts for runfunc not specified and that is ok"
else
# parse options
counter=0
while [[ $counter -lt "${#opts[@]}" ]]; do
opt="${opts[$counter]}"
case $opt in
--idefile)
# This format is expected: --idefile /path/to/my_idefile
# so "--idefile" is under ${opts[$counter]}, while
# "/path/to/my_idefile" is under ${opts[$counter+1]}
idefile="${opts[$counter+1]}"
if [ ! -f "${idefile}" ]; then
# do not get full path here with:
# idefile_full_path=$(readlink -f "$idefile")
# it will be null
log_error "idefile set to custom file: ${idefile}, but does not exist (relative to ${PWD})"
exit 1
fi
counter=$((counter+1))
;;
--dryrun)
dryrun="true"
;;
--quiet)
quiet="true"
;;
--not_i | --force_not_interactive)
force_not_interactive="true"
;;
--no_rm)
remove_containers="false"
;;
# Do not include the case: --*), because we need to support double dash (--)
# and treat everything afterwards as a docker container command.
-[a-z]* | --[a-z]*)
# examples that match here: --invalid-option -invalid-option -a
log_error "Invalid option: '$opt'" >&2
exit 1
;;
*)
# we want to assign the rest of the array elements as 1 element
# http://stackoverflow.com/a/169517
cmd_words_array=()
# go from current value of counter up to opts array length -1
for i in $(seq "${counter}" $((${#opts[@]}-1))) ; do
next_part_of_cmd="${opts[$i]}"
# It's safe to make the assumption that an argument that contains
# white space must have been (and should be) quoted
# http://stackoverflow.com/a/1669493/4457564
#
# Otherwise, this: ide --dryrun -- -c "echo aaa"
# would result in docker run command: -c echo aaa.
whitespace="[[:space:]]"
# shellcheck disable=SC2066
for c in "$next_part_of_cmd"; do
if [[ $c =~ $whitespace ]]; then
if [[ $c == *"\""* ]]; then
# If quotes were used 2 times, we have to escape the
# inner quotes. E.g. end of ide command was:
# -- "/bin/bash -c \"echo aaa\" && echo bbb", so now c is:
# /bin/bash -c "echo aaa" && echo bbb
# and here we transform it into:
# /bin/bash -c \"echo aaa\" && echo bbb
c=${c//\"/\\\"}
fi
# add outer quotes
c=\"$c\"
fi
done
cmd_words_array+=("${c}")
done
command=$(join_by " " "${cmd_words_array[@]}")
break;
;;
esac
counter=$((counter+1))
done
fi
# verify options
verify_idefile "$idefile"
log_debug "dryrun: $dryrun"
log_debug "quiet: $quiet"
if [[ "$command" == "-- "* ]]; then
# If command starts with double dash and space, remove the first 3 characters.
command="${command:3}"
log_debug "command starts with double dash, trimmed to: ${command}"
fi
if [[ "$command" == *" " ]]; then
# If command ends with space, remove it.
# This is due to space in: command="$opt $*"
# This works correctly for a command with >1 words, e.g. "/bin/sh whoami"
# but it leaves the space for 1 word, e.g. "bin/sh" becomes "/bin/sh ".
command="${command::-1}"
log_debug "command ends with space, trimmed to: ${command}"
fi
log_debug "command: $command"
if [ -z "${remove_containers}" ]; then
remove_containers="true"
fi
log_debug "remove_containers: $remove_containers"
if [ -z "${IDE_WORK}" ]; then
IDE_WORK="$PWD"
log_debug "IDE_WORK not set, setting to: ${IDE_WORK}"
fi
if [ ! -d "${IDE_WORK}" ]; then
log_error "IDE_WORK set to ${IDE_WORK} which does not exist"
exit 1
fi
if [ -z "${IDE_WORK_INNER}" ]; then
IDE_WORK_INNER="/ide/work"
log_debug "IDE_WORK_INNER not set, setting to: ${IDE_WORK_INNER}"
fi
export IDE_WORK_INNER
find_uid_option="-uid"
if [[ -d /etc/apk ]]; then
find_uid_option="-user"
fi
if [[ $(find . -maxdepth 1 -name '.' "${find_uid_option}" 0 | wc -l) == "1" ]]; then
log_warn "IDE_WORK directory is owned by root. This is highly inadvisable."
fi
if [ -z "${IDE_IDENTITY}" ]; then
IDE_IDENTITY="${HOME}"
log_debug "IDE_IDENTITY not set, setting to: ${IDE_IDENTITY}"
fi
if [ ! -d "${IDE_IDENTITY}" ]; then
log_error "IDE_IDENTITY set to ${IDE_IDENTITY} which does not exist"
exit 1
fi
if [ -z "${IDE_VARS_BLACKLIST}" ]; then
IDE_VARS_BLACKLIST="BASH*,HOME,USERNAME,USER,LOGNAME,PATH,TERM,SHELL,MAIL,SUDO_*,WINDOWID,SSH_*,SESSION_*,GEM_HOME,GEM_PATH,GEM_ROOT,HOSTNAME,HOSTTYPE,IFS,PPID,PWD,OLDPWD,LC*"
log_debug "IDE_VARS_BLACKLIST not set, setting to: ${IDE_VARS_BLACKLIST}"
fi
if [ -t 0 ] && [[ -z "${force_not_interactive}" ]]; then
# shell is really interactive and "$force_not_interactive" is not set
run_interactively="true"
else
run_interactively="false"
fi
log_debug "run_interactively: ${run_interactively}"
# it serves here as an identificator, will be used as docker container name
# and as a part of env_file
run_id=$(get_run_id)
env_file=$(get_env_vars_file_name "${run_id}")
env_dir=$(dirname "${env_file}")
mkdir -p "${env_dir}"
save_environment_variables "$env_file" "${IDE_VARS_BLACKLIST}"
if [[ "${remove_containers}" != "true" ]]; then
# Just not removing docker container would be impractical, we'd have to
# parse output of ide in order to get container name. Thus, save the
# container name to a file.
echo "${run_id}" > "$(pwd)/iderc.txt"
log_info "written docker container name to $(pwd)/iderc.txt"
echo "IDE_RUN_ID=${run_id}" > "$(pwd)/iderc"
log_info "written docker container name to $(pwd)/iderc"
fi
if [[ "${IDE_DRIVER}" == "docker" ]] || [[ "${IDE_DRIVER}" == "nvidia-docker" ]]; then
if [ -z "${IDE_DOCKER_IMAGE}" ]; then
log_error "IDE_DOCKER_IMAGE not set"
exit 1;
fi
# construct docker command
# shellcheck disable=SC2153
docker_command=$(construct_docker_command "$IDE_DRIVER" "$env_file" "$run_interactively" "$run_id" "$IDE_DOCKER_IMAGE" "$IDE_WORK" "${IDE_WORK_INNER}" "$IDE_IDENTITY" "$remove_containers" "$command" "$IDE_DOCKER_OPTIONS")
log_info "docker command will be:\n${docker_command}"
on_control_c()
{
log_error "Ctrl+c caught by IDE."
# The following cleanup is needed because nothing stops that docker
# container when no Ctrl+c is pressed, because that container is run with:
# `docker run --rm`.
if [[ $(docker inspect -f "{{.State.Running}}" "$run_id" ) == "false" ]]; then
# If Ctrl+c was typed fast enough, docker container is not running, but
# it is created. If that is the case, remove it.
log_error "removing docker container: $run_id (it was created but not started)"
docker rm "$run_id"
else
log_error "stopping docker container: $run_id"
# this can still take some time (up to 10 seconds)
docker stop "$run_id"
fi
clean_func "${env_file}"
}
trap 'on_control_c' INT
# run docker run
if [[ "$dryrun" != "true" ]]; then
if [[ "$run_interactively" == "true" ]]; then
eval "${docker_command}"
else
# run in the background so that correct exit status (130) is returned
# and on_control_c is invoked faster
eval "${docker_command}" &
# if in below line there is no "$!", then the exit_status more below
# is incorrect
wait "$!"
fi
exit_status="$?"
fi
clean_func "${env_file}"
if [[ "$dryrun" != "true" ]]; then
log_debug "ide exit status: $exit_status"
if [[ "$exit_status" != "0" ]]; then
log_error "fail! exit status: $exit_status"
exit $exit_status
fi
fi
elif [[ "$IDE_DRIVER" == "docker-compose" ]]; then
if [[ "${remove_containers}" != "true" ]]; then
log_error "Not implemented feature: '--no_rm' for driver: $IDE_DRIVER"
exit 1
fi
if [ -z "$IDE_DOCKER_COMPOSE_FILE" ]; then
IDE_DOCKER_COMPOSE_FILE="${PWD}/docker-compose.yml"
log_debug "IDE_DOCKER_COMPOSE_FILE not set, setting to: $IDE_DOCKER_COMPOSE_FILE"
fi
if [ ! -f "$IDE_DOCKER_COMPOSE_FILE" ]; then
# do not get full path here (see comment on idefile)
log_error "IDE_DOCKER_COMPOSE_FILE set to $IDE_DOCKER_COMPOSE_FILE which does not exist"
exit 1;
fi
verify_docker_compose_file "$IDE_DOCKER_COMPOSE_FILE"
# construct docker-compose command
# shellcheck disable=SC2153
docker_compose_command=$(construct_docker_compose_run_command "$env_file" "$IDE_DOCKER_COMPOSE_FILE" "$run_id" "$run_interactively" "$IDE_WORK" "${IDE_WORK_INNER}" "$IDE_IDENTITY" "$command" "$IDE_DOCKER_COMPOSE_OPTIONS")
docker_compose_stop_command=$(construct_docker_compose_stop_command "$env_file" "$IDE_DOCKER_COMPOSE_FILE" "$run_id" "$IDE_WORK" "${IDE_WORK_INNER}" "$IDE_IDENTITY")
docker_compose_rm_command=$(construct_docker_compose_rm_command "$env_file" "$IDE_DOCKER_COMPOSE_FILE" "$run_id" "$IDE_WORK" "${IDE_WORK_INNER}" "$IDE_IDENTITY")
log_info "docker-compose run command will be:\n${docker_compose_command}"
log_debug "docker-compose stop command will be:\n${docker_compose_stop_command}"
log_debug "docker-compose rm command will be:\n${docker_compose_rm_command}"
# remove dashes and underscores and add "_default" (we always demand
# docker container named: "default")
expected_docker_network="${run_id//[-_]/}_default"
log_debug "expected docker network is:\n${expected_docker_network}"
on_control_c()
{
log_error "Ctrl+c caught by IDE."
# No need for additional cleaning, because it is done below and happens
# even if not Ctrl+c is pressed.
}
trap 'on_control_c' INT
# run docker-compose run
if [[ "$dryrun" != "true" ]]; then
if [[ "$run_interactively" == "true" ]]; then
eval "${docker_compose_command}"
else
# run in the background so that correct exit status (130) is returned
# and on_control_c is invoked faster
eval "${docker_compose_command}" &
# if in below line there is no "$!", then the exit_status more below
# is incorrect
wait "$!"
fi
exit_status="$?"
# even if all the containers are stopped, this won't fail
log_debug "Stopping"
eval "${docker_compose_stop_command} >&2"
log_debug "Removing"
eval "${docker_compose_rm_command} >&2"
clean_docker_networks_func "${expected_docker_network}"
fi
clean_func "${env_file}"
if [[ "$dryrun" != "true" ]]; then
log_debug "Ide exit status: $exit_status"
if [[ "$exit_status" != "0" ]]; then
log_error "fail! exit status: $exit_status"
exit $exit_status
fi
fi
fi
}
function clean_func() {
env_file="$1"
rm -rf "${env_file}"
log_debug "Removed environment file: ${env_file}"
}
function clean_docker_networks_func() {
expected_docker_network="$1"
# Docker network is created only if using docker-compose driver and
# only if using docker-compose file v2. So remove it only if exists.
if docker network inspect "${expected_docker_network}" >/dev/null 2>&1; then
docker network rm "${expected_docker_network}" >/dev/null
log_debug "Removed docker network: ${expected_docker_network}"
else
log_debug "No need to remove docker network: ${expected_docker_network}. It was not created)"
fi
# remove unused docker networks created in the past by ide
for network in $(docker network ls | awk '{print $2}'); do
if [[ "${network}" == "ide"* ]]; then
# docker network created by ide
if docker network inspect "${network}" | grep "Containers\": {}" >/dev/null; then
log_debug "Found unused docker network created by ide: ${network}"
docker network rm "${network}" >/dev/null
log_debug "Removed docker network: ${network}"
else
log_debug "Found docker network created by ide, but it is still in use: ${network}"
fi
fi
done
}
# Arguments:
# $1 -- all the options for IDE pull command
function pullfunc() {
# receiving array as bash function parameter, thanks to:
# http://stackoverflow.com/a/4017175
declare -a opts=("${!1}")
# default values
idefile="${PWD}/Idefile"
dryrun="false"
# shellcheck disable=SC2128
if [ -z "$opts" ]; then
log_debug "opts for pullfunc not specified and that is ok"
else
# parse options
counter=0
while [[ $counter -lt "${#opts[@]}" ]]; do
opt="${opts[$counter]}"
case $opt in
--idefile)
# This format is expected: --idefile /path/to/my_idefile
# so "--idefile" is under ${opts[$counter]}, while
# "/path/to/my_idefile" is under ${opts[$counter+1]}
idefile="${opts[$counter+1]}"
if [ ! -f "${idefile}" ]; then
# do not get full path here with:
# idefile_full_path=$(readlink -f "$idefile")
# it will be null
log_error "idefile set to custom file: ${idefile}, but does not exist (relative to ${PWD})"
exit 1
fi
counter=$((counter+1))
;;
--dryrun)
dryrun="true"
;;
--* | -* | *)
log_error "Invalid option: '$opt'" >&2
exit 1
;;
esac
counter=$((counter+1))
done
fi
# verify options
verify_idefile "$idefile"
log_debug "dryrun: $dryrun"
if [[ "$IDE_DRIVER" == "docker" ]] || [[ "$IDE_DRIVER" == "nvidia-docker" ]]; then
if [ -z "$IDE_DOCKER_IMAGE" ]; then
log_error "IDE_DOCKER_IMAGE not set"
exit 1;
fi
if [[ "$dryrun" != "true" ]]; then
if [[ $(docker images "${IDE_DOCKER_IMAGE}" --format "{{.ID}}" | wc -l) == "0" ]]; then
log_info "Pulling docker image: ${IDE_DOCKER_IMAGE}"
docker pull "${IDE_DOCKER_IMAGE}"
else
# if you have a local docker image which is not pushed to any docker
# registry, `docker pull` would return error
log_info "Image is up to date for ${IDE_DOCKER_IMAGE}"
fi
fi
elif [[ "$IDE_DRIVER" == "docker-compose" ]]; then
if [ -z "$IDE_DOCKER_COMPOSE_FILE" ]; then
IDE_DOCKER_COMPOSE_FILE="${PWD}/docker-compose.yml"
log_debug "IDE_DOCKER_COMPOSE_FILE not set, setting to: $IDE_DOCKER_COMPOSE_FILE"
fi
if [ ! -f "$IDE_DOCKER_COMPOSE_FILE" ]; then
# do not get full path here (see comment on idefile)
log_error "IDE_DOCKER_COMPOSE_FILE set to $IDE_DOCKER_COMPOSE_FILE which does not exist"
exit 1;
fi
if [[ "$dryrun" != "true" ]]; then
log_info "pulling docker images for: ${IDE_DOCKER_COMPOSE_FILE}"
# temporarily set 3 variables and create an empty env file,
# so that docker-compose does not error
empty_env_file="$(dirname "${IDE_DOCKER_COMPOSE_FILE}")/ide-dummyfile"
touch "${empty_env_file}"
# each of the IDE_WORK, IDE_IDENTITY, ENV_FILE must not be set to a 1
# word, because docker-compose v2 files support volumes named with 1 word
# and if such a volume does not exist, then there is an error like:
# ERROR: Named volume "dummy:/ide/identity:ro" is used in service "default" but no declaration was found in the volumes section.
IDE_WORK="${empty_env_file}" IDE_WORK_INNER="/ide/work" IDE_IDENTITY="${empty_env_file}" ENV_FILE="${empty_env_file}" docker-compose -f "${IDE_DOCKER_COMPOSE_FILE}" pull
rm "${empty_env_file}"
fi
fi
}