-
Notifications
You must be signed in to change notification settings - Fork 16
/
install_salt.sh
5348 lines (4514 loc) · 200 KB
/
install_salt.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
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
996
997
998
999
1000
#!/bin/sh -
#======================================================================================================================
# vim: softtabstop=4 shiftwidth=4 expandtab fenc=utf-8 spell spelllang=en cc=120
#======================================================================================================================
#
# FILE: bootstrap-salt.sh
#
# DESCRIPTION: Bootstrap salt installation for various systems/distributions
#
# BUGS: https://github.com/saltstack/salt-bootstrap/issues
#
# COPYRIGHT: (c) 2012-2014 by the SaltStack Team, see AUTHORS.rst for more
# details.
#
# LICENSE: Apache 2.0
# ORGANIZATION: SaltStack (saltstack.org)
# CREATED: 10/15/2012 09:49:37 PM WEST
#======================================================================================================================
set -o nounset # Treat unset variables as an error
__ScriptVersion="2015.05.07"
__ScriptName="bootstrap-salt.sh"
#======================================================================================================================
# Environment variables taken into account.
#----------------------------------------------------------------------------------------------------------------------
# * BS_COLORS: If 0 disables colour support
# * BS_PIP_ALLOWED: If 1 enable pip based installations(if needed)
# * BS_ECHO_DEBUG: If 1 enable debug echo which can also be set by -D
# * BS_SALT_ETC_DIR: Defaults to /etc/salt (Only tweak'able on git based installations)
# * BS_KEEP_TEMP_FILES: If 1, don't move temporary files, instead copy them
# * BS_FORCE_OVERWRITE: Force overriding copied files(config, init.d, etc)
# * BS_UPGRADE_SYS: If 1 and an option, upgrade system. Default 0.
# * BS_GENTOO_USE_BINHOST: If 1 add `--getbinpkg` to gentoo's emerge
# * BS_SALT_MASTER_ADDRESS: The IP or DNS name of the salt-master the minion should connect to
# * BS_SALT_GIT_CHECKOUT_DIR: The directory where to clone Salt on git installations
#======================================================================================================================
#======================================================================================================================
# LET THE BLACK MAGIC BEGIN!!!!
#======================================================================================================================
# Bootstrap script truth values
BS_TRUE=1
BS_FALSE=0
# Default sleep time used when waiting for daemons to start, restart and checking for these running
__DEFAULT_SLEEP=3
__DEFAULT_SLEEP_ORIGINAL="${__DEFAULT_SLEEP}"
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __detect_color_support
# DESCRIPTION: Try to detect color support.
#----------------------------------------------------------------------------------------------------------------------
_COLORS=${BS_COLORS:-$(tput colors 2>/dev/null || echo 0)}
__detect_color_support() {
if [ $? -eq 0 ] && [ "$_COLORS" -gt 2 ]; then
RC="\033[1;31m"
GC="\033[1;32m"
BC="\033[1;34m"
YC="\033[1;33m"
EC="\033[0m"
else
RC=""
GC=""
BC=""
YC=""
EC=""
fi
}
__detect_color_support
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: echoerr
# DESCRIPTION: Echo errors to stderr.
#----------------------------------------------------------------------------------------------------------------------
echoerror() {
printf "${RC} * ERROR${EC}: %s\n" "$@" 1>&2;
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: echoinfo
# DESCRIPTION: Echo information to stdout.
#----------------------------------------------------------------------------------------------------------------------
echoinfo() {
printf "${GC} * INFO${EC}: %s\n" "$@";
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: echowarn
# DESCRIPTION: Echo warning informations to stdout.
#----------------------------------------------------------------------------------------------------------------------
echowarn() {
printf "${YC} * WARN${EC}: %s\n" "$@";
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: echodebug
# DESCRIPTION: Echo debug information to stdout.
#----------------------------------------------------------------------------------------------------------------------
echodebug() {
if [ "$_ECHO_DEBUG" -eq $BS_TRUE ]; then
printf "${BC} * DEBUG${EC}: %s\n" "$@";
fi
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: check_pip_allowed
# DESCRIPTION: Simple function to let the users know that -P needs to be
# used.
#----------------------------------------------------------------------------------------------------------------------
check_pip_allowed() {
if [ $# -eq 1 ]; then
_PIP_ALLOWED_ERROR_MSG=$1
else
_PIP_ALLOWED_ERROR_MSG="pip based installations were not allowed. Retry using '-P'"
fi
if [ "$_PIP_ALLOWED" -eq $BS_FALSE ]; then
echoerror "$_PIP_ALLOWED_ERROR_MSG"
usage
exit 1
fi
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __check_config_dir
# DESCRIPTION: Checks the config directory, retrieves URLs if provided.
#----------------------------------------------------------------------------------------------------------------------
__check_config_dir() {
CC_DIR_NAME="$1"
CC_DIR_BASE=$(basename "${CC_DIR_NAME}")
case "$CC_DIR_NAME" in
http://*|https://*)
__fetch_url "/tmp/${CC_DIR_BASE}" "${CC_DIR_NAME}"
CC_DIR_NAME="/tmp/${CC_DIR_BASE}"
;;
ftp://*)
__fetch_url "/tmp/${CC_DIR_BASE}" "${CC_DIR_NAME}"
CC_DIR_NAME="/tmp/${CC_DIR_BASE}"
;;
*)
if [ ! -e "${CC_DIR_NAME}" ]; then
echo "null"
return 0
fi
;;
esac
case "$CC_DIR_NAME" in
*.tgz|*.tar.gz)
tar -zxf "${CC_DIR_NAME}" -C /tmp
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".tgz")
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".tar.gz")
CC_DIR_NAME="/tmp/${CC_DIR_BASE}"
;;
*.tbz|*.tar.bz2)
tar -xjf "${CC_DIR_NAME}" -C /tmp
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".tbz")
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".tar.bz2")
CC_DIR_NAME="/tmp/${CC_DIR_BASE}"
;;
*.txz|*.tar.xz)
tar -xJf "${CC_DIR_NAME}" -C /tmp
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".txz")
CC_DIR_BASE=$(basename "${CC_DIR_BASE}" ".tar.xz")
CC_DIR_NAME="/tmp/${CC_DIR_BASE}"
;;
esac
echo "${CC_DIR_NAME}"
}
#----------------------------------------------------------------------------------------------------------------------
# Handle command line arguments
#----------------------------------------------------------------------------------------------------------------------
_KEEP_TEMP_FILES=${BS_KEEP_TEMP_FILES:-$BS_FALSE}
_TEMP_CONFIG_DIR="null"
_SALTSTACK_REPO_URL="git://github.com/saltstack/salt.git"
_SALT_REPO_URL=${_SALTSTACK_REPO_URL}
_TEMP_KEYS_DIR="null"
_INSTALL_MASTER=$BS_FALSE
_INSTALL_SYNDIC=$BS_FALSE
_INSTALL_MINION=$BS_TRUE
_INSTALL_CLOUD=$BS_FALSE
_START_DAEMONS=$BS_TRUE
_ECHO_DEBUG=${BS_ECHO_DEBUG:-$BS_FALSE}
_CONFIG_ONLY=$BS_FALSE
_PIP_ALLOWED=${BS_PIP_ALLOWED:-$BS_FALSE}
_SALT_ETC_DIR=${BS_SALT_ETC_DIR:-/etc/salt}
_PKI_DIR=${_SALT_ETC_DIR}/pki
_FORCE_OVERWRITE=${BS_FORCE_OVERWRITE:-$BS_FALSE}
_GENTOO_USE_BINHOST=${BS_GENTOO_USE_BINHOST:-$BS_FALSE}
_EPEL_REPO=${BS_EPEL_REPO:-epel}
__EPEL_REPOS_INSTALLED=${BS_FALSE}
_UPGRADE_SYS=${BS_UPGRADE_SYS:-$BS_FALSE}
_INSECURE_DL=${BS_INSECURE_DL:-$BS_FALSE}
_WGET_ARGS=${BS_WGET_ARGS:-}
_CURL_ARGS=${BS_CURL_ARGS:-}
_FETCH_ARGS=${BS_FETCH_ARGS:-}
_ENABLE_EXTERNAL_ZMQ_REPOS=${BS_ENABLE_EXTERNAL_ZMQ_REPOS:-$BS_FALSE}
_SALT_MASTER_ADDRESS=${BS_SALT_MASTER_ADDRESS:-null}
_SALT_MINION_ID="null"
# __SIMPLIFY_VERSION is mostly used in Solaris based distributions
__SIMPLIFY_VERSION=$BS_TRUE
_LIBCLOUD_MIN_VERSION="0.14.0"
_PY_REQUESTS_MIN_VERSION="2.0"
_EXTRA_PACKAGES=""
_HTTP_PROXY=""
__SALT_GIT_CHECKOUT_DIR=${BS_SALT_GIT_CHECKOUT_DIR:-/tmp/git/salt}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: usage
# DESCRIPTION: Display usage information.
#----------------------------------------------------------------------------------------------------------------------
usage() {
cat << EOT
Usage : ${__ScriptName} [options] <install-type> <install-type-args>
Installation types:
- stable (default)
- stable [version] (ubuntu specific)
- daily (ubuntu specific)
- testing (redhat specific)
- git
Examples:
- ${__ScriptName}
- ${__ScriptName} stable
- ${__ScriptName} stable 2014.7
- ${__ScriptName} daily
- ${__ScriptName} testing
- ${__ScriptName} git
- ${__ScriptName} git develop
- ${__ScriptName} git v0.17.0
- ${__ScriptName} git 8c3fadf15ec183e5ce8c63739850d543617e4357
Options:
-h Display this message
-v Display script version
-n No colours.
-D Show debug output.
-c Temporary configuration directory
-g Salt repository URL. (default: git://github.com/saltstack/salt.git)
-G Instead of cloning from git://github.com/saltstack/salt.git, clone from https://github.com/saltstack/salt.git (Usually necessary on systems which have the regular git protocol port blocked, where https usually is not)
-k Temporary directory holding the minion keys which will pre-seed
the master.
-s Sleep time used when waiting for daemons to start, restart and when checking
for the services running. Default: ${__DEFAULT_SLEEP}
-M Also install salt-master
-S Also install salt-syndic
-N Do not install salt-minion
-X Do not start daemons after installation
-C Only run the configuration function. This option automatically
bypasses any installation.
-P Allow pip based installations. On some distributions the required salt
packages or its dependencies are not available as a package for that
distribution. Using this flag allows the script to use pip as a last
resort method. NOTE: This only works for functions which actually
implement pip based installations.
-F Allow copied files to overwrite existing(config, init.d, etc)
-U If set, fully upgrade the system prior to bootstrapping salt
-K If set, keep the temporary files in the temporary directories specified
with -c and -k.
-I If set, allow insecure connections while downloading any files. For
example, pass '--no-check-certificate' to 'wget' or '--insecure' to 'curl'
-A Pass the salt-master DNS name or IP. This will be stored under
${_SALT_ETC_DIR}/minion.d/99-master-address.conf
-i Pass the salt-minion id. This will be stored under
${_SALT_ETC_DIR}/minion_id
-L Install the Apache Libcloud package if possible(required for salt-cloud)
-p Extra-package to install while installing salt dependencies. One package
per -p flag. You're responsible for providing the proper package name.
-H Use the specified http proxy for the installation
-Z Enable external software source for newer ZeroMQ(Only available for RHEL/CentOS/Fedora based distributions)
EOT
} # ---------- end of function usage ----------
while getopts ":hvnDc:Gg:k:MSNXCPFUKIA:i:Lp:H:Z" opt
do
case "${opt}" in
h ) usage; exit 0 ;;
v ) echo "$0 -- Version $__ScriptVersion"; exit 0 ;;
n ) _COLORS=0; __detect_color_support ;;
D ) _ECHO_DEBUG=$BS_TRUE ;;
c ) _TEMP_CONFIG_DIR=$(__check_config_dir "$OPTARG")
# If the configuration directory does not exist, error out
if [ "$_TEMP_CONFIG_DIR" = "null" ]; then
echoerror "Unsupported URI scheme for $OPTARG"
exit 1
fi
if [ ! -d "$_TEMP_CONFIG_DIR" ]; then
echoerror "The configuration directory ${_TEMP_CONFIG_DIR} does not exist."
exit 1
fi
;;
g ) _SALT_REPO_URL=$OPTARG ;;
G ) if [ "${_SALT_REPO_URL}" = "${_SALTSTACK_REPO_URL}" ]; then
_SALTSTACK_REPO_URL="https://github.com/saltstack/salt.git"
_SALT_REPO_URL=${_SALTSTACK_REPO_URL}
else
_SALTSTACK_REPO_URL="https://github.com/saltstack/salt.git"
fi
;;
k ) _TEMP_KEYS_DIR="$OPTARG"
# If the configuration directory does not exist, error out
if [ ! -d "$_TEMP_KEYS_DIR" ]; then
echoerror "The pre-seed keys directory ${_TEMP_KEYS_DIR} does not exist."
exit 1
fi
;;
M ) _INSTALL_MASTER=$BS_TRUE ;;
S ) _INSTALL_SYNDIC=$BS_TRUE ;;
N ) _INSTALL_MINION=$BS_FALSE ;;
X ) _START_DAEMONS=$BS_FALSE ;;
C ) _CONFIG_ONLY=$BS_TRUE ;;
P ) _PIP_ALLOWED=$BS_TRUE ;;
F ) _FORCE_OVERWRITE=$BS_TRUE ;;
U ) _UPGRADE_SYS=$BS_TRUE ;;
K ) _KEEP_TEMP_FILES=$BS_TRUE ;;
I ) _INSECURE_DL=$BS_TRUE ;;
A ) _SALT_MASTER_ADDRESS=$OPTARG ;;
i ) _SALT_MINION_ID=$OPTARG ;;
L ) _INSTALL_CLOUD=$BS_TRUE ;;
p ) _EXTRA_PACKAGES="$_EXTRA_PACKAGES $OPTARG" ;;
H ) _HTTP_PROXY="$OPTARG" ;;
Z) _ENABLE_EXTERNAL_ZMQ_REPOS=$BS_TRUE ;;
\?) echo
echoerror "Option does not exist : $OPTARG"
usage
exit 1
;;
esac # --- end of case ---
done
shift $((OPTIND-1))
__check_unparsed_options() {
shellopts="$1"
# grep alternative for SunOS
if [ -f /usr/xpg4/bin/grep ]; then
grep='/usr/xpg4/bin/grep'
else
grep='grep'
fi
unparsed_options=$( echo "$shellopts" | ${grep} -E '(^|[[:space:]])[-]+[[:alnum:]]' )
if [ "$unparsed_options" != "" ]; then
usage
echo
echoerror "options are only allowed before install arguments"
echo
exit 1
fi
}
# Check that we're actually installing one of minion/master/syndic
if [ "$_INSTALL_MINION" -eq $BS_FALSE ] && [ "$_INSTALL_MASTER" -eq $BS_FALSE ] && [ "$_INSTALL_SYNDIC" -eq $BS_FALSE ] && [ "$_CONFIG_ONLY" -eq $BS_FALSE ]; then
echowarn "Nothing to install or configure"
exit 0
fi
if [ "$_CONFIG_ONLY" -eq $BS_TRUE ] && [ "$_TEMP_CONFIG_DIR" = "null" ]; then
echoerror "In order to run the script in configuration only mode you also need to provide the configuration directory."
exit 1
fi
# Check that we're installing a minion if we're being passed a master address
if [ "$_INSTALL_MINION" -eq $BS_FALSE ] && [ "$_SALT_MASTER_ADDRESS" != "null" ]; then
echoerror "Don't pass a master address (-A) if no minion is going to be bootstrapped."
exit 1
fi
# Check that we're installing a minion if we're being passed a master address
if [ "$_INSTALL_MINION" -eq $BS_FALSE ] && [ "$_SALT_MINION_ID" != "null" ]; then
echoerror "Don't pass a minion id (-i) if no minion is going to be bootstrapped."
exit 1
fi
# Define installation type
if [ "$#" -eq 0 ];then
ITYPE="stable"
else
__check_unparsed_options "$*"
ITYPE=$1
shift
fi
# Check installation type
if [ "$(echo "$ITYPE" | egrep '(stable|testing|daily|git)')" = "" ]; then
echoerror "Installation type \"$ITYPE\" is not known..."
exit 1
fi
# If doing a git install, check what branch/tag/sha will be checked out
if [ "$ITYPE" = "git" ]; then
if [ "$#" -eq 0 ];then
GIT_REV="develop"
else
__check_unparsed_options "$*"
GIT_REV="$1"
shift
fi
# If doing stable install, check if version specified
elif [ "$ITYPE" = "stable" ]; then
if [ "$#" -eq 0 ];then
STABLE_REV="latest"
else
__check_unparsed_options "$*"
if [ "$(echo "$1" | egrep '^(latest|1\.6|1\.7|2014\.1|2014\.7|2015\.5)$')" = "" ]; then
echo "Unknown stable version: $1 (valid: 1.6, 1.7, 2014.1, 2014.7, 2015.5, latest)"
exit 1
else
STABLE_REV="$1"
shift
fi
fi
fi
# Check for any unparsed arguments. Should be an error.
if [ "$#" -gt 0 ]; then
__check_unparsed_options "$*"
usage
echo
echoerror "Too many arguments."
exit 1
fi
# whoami alternative for SunOS
if [ -f /usr/xpg4/bin/id ]; then
whoami='/usr/xpg4/bin/id -un'
else
whoami='whoami'
fi
# Root permissions are required to run this script
if [ "$(${whoami})" != "root" ]; then
echoerror "Salt requires root privileges to install. Please re-run this script as root."
exit 1
fi
# Export the http_proxy configuration to our current environment
if [ "${_HTTP_PROXY}" != "" ]; then
export http_proxy="$_HTTP_PROXY"
export https_proxy="$_HTTP_PROXY"
fi
# Let's discover how we're being called
# shellcheck disable=SC2009
CALLER=$(ps -a -o pid,args | grep $$ | grep -v grep | tr -s ' ' | cut -d ' ' -f 3)
if [ "${CALLER}x" = "${0}x" ]; then
CALLER="PIPED THROUGH"
fi
echoinfo "${CALLER} ${0} -- Version ${__ScriptVersion}"
#echowarn "Running the unstable version of ${__ScriptName}"
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __exit_cleanup
# DESCRIPTION: Cleanup any leftovers after script has ended
#
#
# http://www.unix.com/man-page/POSIX/1posix/trap/
#
# Signal Number Signal Name
# 1 SIGHUP
# 2 SIGINT
# 3 SIGQUIT
# 6 SIGABRT
# 9 SIGKILL
# 14 SIGALRM
# 15 SIGTERM
#----------------------------------------------------------------------------------------------------------------------
__exit_cleanup() {
EXIT_CODE=$?
if [ "$ITYPE" = "git" ] && [ -d "${__SALT_GIT_CHECKOUT_DIR}" ]; then
if [ $_KEEP_TEMP_FILES -eq $BS_FALSE ]; then
# Clean up the checked out repository
echodebug "Cleaning up the Salt Temporary Git Repository"
rm -rf "${__SALT_GIT_CHECKOUT_DIR}"
else
echowarn "Not cleaning up the Salt Temporary git repository on request"
echowarn "Note that if you intend to re-run this script using the git approach, you might encounter some issues"
fi
fi
# Remove the logging pipe when the script exits
echodebug "Removing the logging pipe $LOGPIPE"
rm -f "$LOGPIPE"
# Kill tee when exiting, CentOS, at least requires this
# shellcheck disable=SC2009
TEE_PID=$(ps ax | grep tee | grep "$LOGFILE" | awk '{print $1}')
[ "$TEE_PID" = "" ] && exit $EXIT_CODE
echodebug "Killing logging pipe tee's with pid(s): $TEE_PID"
# We need to trap errors since killing tee will cause a 127 errno
# We also do this as late as possible so we don't "mis-catch" other errors
__trap_errors() {
echoinfo "Errors Trapped: $EXIT_CODE"
# Exit with the "original" exit code, not the trapped code
exit $EXIT_CODE
}
trap "__trap_errors" INT QUIT ABRT KILL QUIT TERM
# Now we're "good" to kill tee
kill -s TERM "$TEE_PID"
# In case the 127 errno is not triggered, exit with the "original" exit code
exit $EXIT_CODE
}
trap "__exit_cleanup" EXIT INT
# Define our logging file and pipe paths
LOGFILE="/tmp/$( echo $__ScriptName | sed s/.sh/.log/g )"
LOGPIPE="/tmp/$( echo $__ScriptName | sed s/.sh/.logpipe/g )"
# Create our logging pipe
# On FreeBSD we have to use mkfifo instead of mknod
mknod "$LOGPIPE" p >/dev/null 2>&1 || mkfifo "$LOGPIPE" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echoerror "Failed to create the named pipe required to log"
exit 1
fi
# What ever is written to the logpipe gets written to the logfile
tee < "$LOGPIPE" "$LOGFILE" &
# Close STDOUT, reopen it directing it to the logpipe
exec 1>&-
exec 1>"$LOGPIPE"
# Close STDERR, reopen it directing it to the logpipe
exec 2>&-
exec 2>"$LOGPIPE"
# Handle the insecure flags
if [ "$_INSECURE_DL" -eq $BS_TRUE ]; then
_CURL_ARGS="${_CURL_ARGS} --insecure"
_WGET_ARGS="${_WGET_ARGS} --no-check-certificate"
_FETCH_ARGS="${_FETCH_ARGS} --no-verify-peer"
fi
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __fetch_url
# DESCRIPTION: Retrieves a URL and writes it to a given path
#----------------------------------------------------------------------------------------------------------------------
__fetch_url() {
# shellcheck disable=SC2086
curl $_CURL_ARGS -s -o "$1" "$2" >/dev/null 2>&1 ||
wget $_WGET_ARGS -q -O "$1" "$2" >/dev/null 2>&1 ||
fetch $_FETCH_ARGS -q -o "$1" "$2" >/dev/null 2>&1 ||
fetch -q -o "$1" "$2" >/dev/null 2>&1 # Pre FreeBSD 10
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_hardware_info
# DESCRIPTION: Discover hardware information
#----------------------------------------------------------------------------------------------------------------------
__gather_hardware_info() {
if [ -f /proc/cpuinfo ]; then
CPU_VENDOR_ID=$(awk '/vendor_id|Processor/ {sub(/-.*$/,"",$3); print $3; exit}' /proc/cpuinfo )
elif [ -f /usr/bin/kstat ]; then
# SmartOS.
# Solaris!?
# This has only been tested for a GenuineIntel CPU
CPU_VENDOR_ID=$(/usr/bin/kstat -p cpu_info:0:cpu_info0:vendor_id | awk '{print $2}')
else
CPU_VENDOR_ID=$( sysctl -n hw.model )
fi
# shellcheck disable=SC2034
CPU_VENDOR_ID_L=$( echo "$CPU_VENDOR_ID" | tr '[:upper:]' '[:lower:]' )
CPU_ARCH=$(uname -m 2>/dev/null || uname -p 2>/dev/null || echo "unknown")
CPU_ARCH_L=$( echo "$CPU_ARCH" | tr '[:upper:]' '[:lower:]' )
}
__gather_hardware_info
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_os_info
# DESCRIPTION: Discover operating system information
#----------------------------------------------------------------------------------------------------------------------
__gather_os_info() {
OS_NAME=$(uname -s 2>/dev/null)
OS_NAME_L=$( echo "$OS_NAME" | tr '[:upper:]' '[:lower:]' )
OS_VERSION=$(uname -r)
# shellcheck disable=SC2034
OS_VERSION_L=$( echo "$OS_VERSION" | tr '[:upper:]' '[:lower:]' )
}
__gather_os_info
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __parse_version_string
# DESCRIPTION: Parse version strings ignoring the revision.
# MAJOR.MINOR.REVISION becomes MAJOR.MINOR
#----------------------------------------------------------------------------------------------------------------------
__parse_version_string() {
VERSION_STRING="$1"
PARSED_VERSION=$(
echo "$VERSION_STRING" |
sed -e 's/^/#/' \
-e 's/^#[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\)\(\.[0-9][0-9]*\).*$/\1/' \
-e 's/^#[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/' \
-e 's/^#[^0-9]*\([0-9][0-9]*\).*$/\1/' \
-e 's/^#.*$//'
)
echo "$PARSED_VERSION"
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __derive_debian_numeric_version
# DESCRIPTION: Derive the numeric version from a Debian version string.
#----------------------------------------------------------------------------------------------------------------------
__derive_debian_numeric_version() {
NUMERIC_VERSION=""
INPUT_VERSION="$1"
if echo "$INPUT_VERSION" | grep -q '^[0-9]'; then
NUMERIC_VERSION="$INPUT_VERSION"
elif [ -z "$INPUT_VERSION" -a -f "/etc/debian_version" ]; then
INPUT_VERSION="$(cat /etc/debian_version)"
fi
if [ -z "$NUMERIC_VERSION" ]; then
if [ "$INPUT_VERSION" = "wheezy/sid" ]; then
# I've found an EC2 wheezy image which did not tell its version
NUMERIC_VERSION=$(__parse_version_string "7.0")
elif [ "$INPUT_VERSION" = "jessie/sid" ]; then
# Let's start detecting the upcoming Debian 8 (Jessie)
NUMERIC_VERSION=$(__parse_version_string "8.0")
else
echowarn "Unable to parse the Debian Version (codename: '$INPUT_VERSION')"
fi
fi
echo "$NUMERIC_VERSION"
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __unquote_string
# DESCRIPTION: Strip single or double quotes from the provided string.
#----------------------------------------------------------------------------------------------------------------------
__unquote_string() {
echo "${@}" | sed "s/^\([\"']\)\(.*\)\1\$/\2/g"
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __camelcase_split
# DESCRIPTION: Convert CamelCased strings to Camel_Cased
#----------------------------------------------------------------------------------------------------------------------
__camelcase_split() {
echo "${@}" | sed -r 's/([^A-Z-])([A-Z])/\1 \2/g'
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __strip_duplicates
# DESCRIPTION: Strip duplicate strings
#----------------------------------------------------------------------------------------------------------------------
__strip_duplicates() {
echo "${@}" | tr -s '[:space:]' '\n' | awk '!x[$0]++'
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __sort_release_files
# DESCRIPTION: Custom sort function. Alphabetical or numerical sort is not
# enough.
#----------------------------------------------------------------------------------------------------------------------
__sort_release_files() {
KNOWN_RELEASE_FILES=$(echo "(arch|centos|debian|ubuntu|fedora|redhat|suse|\
mandrake|mandriva|gentoo|slackware|turbolinux|unitedlinux|lsb|system|\
oracle|os)(-|_)(release|version)" | sed -r 's:[[:space:]]::g')
primary_release_files=""
secondary_release_files=""
# Sort know VS un-known files first
for release_file in $(echo "${@}" | sed -r 's:[[:space:]]:\n:g' | sort --unique --ignore-case); do
match=$(echo "$release_file" | egrep -i "${KNOWN_RELEASE_FILES}")
if [ "${match}" != "" ]; then
primary_release_files="${primary_release_files} ${release_file}"
else
secondary_release_files="${secondary_release_files} ${release_file}"
fi
done
# Now let's sort by know files importance, max important goes last in the max_prio list
max_prio="redhat-release centos-release oracle-release"
for entry in $max_prio; do
if [ "$(echo "${primary_release_files}" | grep "$entry")" != "" ]; then
primary_release_files=$(echo "${primary_release_files}" | sed -e "s:\(.*\)\($entry\)\(.*\):\2 \1 \3:g")
fi
done
# Now, least important goes last in the min_prio list
min_prio="lsb-release"
for entry in $min_prio; do
if [ "$(echo "${primary_release_files}" | grep "$entry")" != "" ]; then
primary_release_files=$(echo "${primary_release_files}" | sed -e "s:\(.*\)\($entry\)\(.*\):\1 \3 \2:g")
fi
done
# Echo the results collapsing multiple white-space into a single white-space
echo "${primary_release_files} ${secondary_release_files}" | sed -r 's:[[:space:]]+:\n:g'
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_linux_system_info
# DESCRIPTION: Discover Linux system information
#----------------------------------------------------------------------------------------------------------------------
__gather_linux_system_info() {
DISTRO_NAME=""
DISTRO_VERSION=""
# Let's test if the lsb_release binary is available
rv=$(lsb_release >/dev/null 2>&1)
if [ $? -eq 0 ]; then
DISTRO_NAME=$(lsb_release -si)
if [ "${DISTRO_NAME}" = "Scientific" ]; then
DISTRO_NAME="Scientific Linux"
elif [ "$(echo "$DISTRO_NAME" | grep RedHat)" != "" ]; then
# Let's convert CamelCase to Camel Case
DISTRO_NAME=$(__camelcase_split "$DISTRO_NAME")
elif [ "${DISTRO_NAME}" = "openSUSE project" ]; then
# lsb_release -si returns "openSUSE project" on openSUSE 12.3
DISTRO_NAME="opensuse"
elif [ "${DISTRO_NAME}" = "SUSE LINUX" ]; then
if [ "$(lsb_release -sd | grep -i opensuse)" != "" ]; then
# openSUSE 12.2 reports SUSE LINUX on lsb_release -si
DISTRO_NAME="opensuse"
else
# lsb_release -si returns "SUSE LINUX" on SLES 11 SP3
DISTRO_NAME="suse"
fi
elif [ "${DISTRO_NAME}" = "EnterpriseEnterpriseServer" ]; then
# This the Oracle Linux Enterprise ID before ORACLE LINUX 5 UPDATE 3
DISTRO_NAME="Oracle Linux"
elif [ "${DISTRO_NAME}" = "OracleServer" ]; then
# This the Oracle Linux Server 6.5
DISTRO_NAME="Oracle Linux"
elif [ "${DISTRO_NAME}" = "AmazonAMI" ]; then
DISTRO_NAME="Amazon Linux AMI"
elif [ "${DISTRO_NAME}" = "Arch" ]; then
DISTRO_NAME="Arch Linux"
return
fi
rv=$(lsb_release -sr)
[ "${rv}" != "" ] && DISTRO_VERSION=$(__parse_version_string "$rv")
elif [ -f /etc/lsb-release ]; then
# We don't have the lsb_release binary, though, we do have the file it parses
DISTRO_NAME=$(grep DISTRIB_ID /etc/lsb-release | sed -e 's/.*=//')
rv=$(grep DISTRIB_RELEASE /etc/lsb-release | sed -e 's/.*=//')
[ "${rv}" != "" ] && DISTRO_VERSION=$(__parse_version_string "$rv")
fi
if [ "$DISTRO_NAME" != "" ] && [ "$DISTRO_VERSION" != "" ]; then
# We already have the distribution name and version
return
fi
# shellcheck disable=SC2035,SC2086
for rsource in $(__sort_release_files "$(
cd /etc && /bin/ls *[_-]release *[_-]version 2>/dev/null | env -i sort | \
sed -e '/^redhat-release$/d' -e '/^lsb-release$/d'; \
echo redhat-release lsb-release
)"); do
[ -L "/etc/${rsource}" ] && continue # Don't follow symlinks
[ ! -f "/etc/${rsource}" ] && continue # Does not exist
n=$(echo "${rsource}" | sed -e 's/[_-]release$//' -e 's/[_-]version$//')
shortname=$(echo "${n}" | tr '[:upper:]' '[:lower:]')
if [ "$shortname" = "debian" ]; then
rv=$(__derive_debian_numeric_version "$(cat /etc/${rsource})")
else
rv=$( (grep VERSION "/etc/${rsource}"; cat "/etc/${rsource}") | grep '[0-9]' | sed -e 'q' )
fi
[ "${rv}" = "" ] && [ "$shortname" != "arch" ] && continue # There's no version information. Continue to next rsource
v=$(__parse_version_string "$rv")
case $shortname in
redhat )
if [ "$(egrep 'CentOS' /etc/${rsource})" != "" ]; then
n="CentOS"
elif [ "$(egrep 'Scientific' /etc/${rsource})" != "" ]; then
n="Scientific Linux"
elif [ "$(egrep 'Red Hat Enterprise Linux' /etc/${rsource})" != "" ]; then
n="<R>ed <H>at <E>nterprise <L>inux"
else
n="<R>ed <H>at <L>inux"
fi
;;
arch ) n="Arch Linux" ;;
centos ) n="CentOS" ;;
debian ) n="Debian" ;;
ubuntu ) n="Ubuntu" ;;
fedora ) n="Fedora" ;;
suse ) n="SUSE" ;;
mandrake*|mandriva ) n="Mandriva" ;;
gentoo ) n="Gentoo" ;;
slackware ) n="Slackware" ;;
turbolinux ) n="TurboLinux" ;;
unitedlinux ) n="UnitedLinux" ;;
oracle ) n="Oracle Linux" ;;
system )
while read -r line; do
[ "${n}x" != "systemx" ] && break
case "$line" in
*Amazon*Linux*AMI*)
n="Amazon Linux AMI"
break
esac
done < "/etc/${rsource}"
;;
os )
nn="$(__unquote_string "$(grep '^ID=' /etc/os-release | sed -e 's/^ID=\(.*\)$/\1/g')")"
rv="$(__unquote_string "$(grep '^VERSION_ID=' /etc/os-release | sed -e 's/^VERSION_ID=\(.*\)$/\1/g')")"
[ "${rv}" != "" ] && v=$(__parse_version_string "$rv") || v=""
case $(echo "${nn}" | tr '[:upper:]' '[:lower:]') in
amzn )
# Amazon AMI's after 2014.9 match here
n="Amazon Linux AMI"
;;
arch )
n="Arch Linux"
v="" # Arch Linux does not provide a version.
;;
debian )
n="Debian"
v=$(__derive_debian_numeric_version "$v")
;;
* )
n=${nn}
;;
esac
;;
* ) n="${n}" ;
esac
DISTRO_NAME=$n
DISTRO_VERSION=$v
break
done
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_sunos_system_info
# DESCRIPTION: Discover SunOS system info
#----------------------------------------------------------------------------------------------------------------------
__gather_sunos_system_info() {
if [ -f /sbin/uname ]; then
DISTRO_VERSION=$(/sbin/uname -X | awk '/[kK][eE][rR][nN][eE][lL][iI][dD]/ { print $3}')
fi
DISTRO_NAME=""
if [ -f /etc/release ]; then
while read -r line; do
[ "${DISTRO_NAME}" != "" ] && break
case "$line" in
*OpenIndiana*oi_[0-9]*)
DISTRO_NAME="OpenIndiana"
DISTRO_VERSION=$(echo "$line" | sed -nr "s/OpenIndiana(.*)oi_([[:digit:]]+)(.*)/\2/p")
break
;;
*OpenSolaris*snv_[0-9]*)
DISTRO_NAME="OpenSolaris"
DISTRO_VERSION=$(echo "$line" | sed -nr "s/OpenSolaris(.*)snv_([[:digit:]]+)(.*)/\2/p")
break
;;
*Oracle*Solaris*[0-9]*)
DISTRO_NAME="Oracle Solaris"
DISTRO_VERSION=$(echo "$line" | sed -nr "s/(Oracle Solaris) ([[:digit:]]+)(.*)/\2/p")
break
;;
*Solaris*)
DISTRO_NAME="Solaris"
# Let's make sure we not actually on a Joyent's SmartOS VM since some releases
# don't have SmartOS in `/etc/release`, only `Solaris`
uname -v | grep joyent >/dev/null 2>&1
if [ $? -eq 0 ]; then
DISTRO_NAME="SmartOS"
fi
break
;;
*NexentaCore*)
DISTRO_NAME="Nexenta Core"
break
;;
*SmartOS*)
DISTRO_NAME="SmartOS"
break
;;
*OmniOS*)
DISTRO_NAME="OmniOS"
DISTRO_VERSION=$(echo "$line" | awk '{print $3}')
__SIMPLIFY_VERSION=$BS_FALSE
break
;;
esac
done < /etc/release
fi
if [ "${DISTRO_NAME}" = "" ]; then
DISTRO_NAME="Solaris"
DISTRO_VERSION=$(
echo "${OS_VERSION}" |
sed -e 's;^4\.;1.;' \
-e 's;^5\.\([0-6]\)[^0-9]*$;2.\1;' \
-e 's;^5\.\([0-9][0-9]*\).*;\1;'
)
fi
if [ "${DISTRO_NAME}" = "SmartOS" ]; then
VIRTUAL_TYPE="smartmachine"
if [ "$(zonename)" = "global" ]; then
VIRTUAL_TYPE="global"
fi
fi
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_bsd_system_info
# DESCRIPTION: Discover OpenBSD, NetBSD and FreeBSD systems information
#----------------------------------------------------------------------------------------------------------------------
__gather_bsd_system_info() {
DISTRO_NAME=${OS_NAME}
DISTRO_VERSION=$(echo "${OS_VERSION}" | sed -e 's;[()];;' -e 's/-.*$//')
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __gather_system_info
# DESCRIPTION: Discover which system and distribution we are running.
#----------------------------------------------------------------------------------------------------------------------
__gather_system_info() {
case ${OS_NAME_L} in
linux )
__gather_linux_system_info
;;
sunos )
__gather_sunos_system_info
;;
openbsd|freebsd|netbsd )
__gather_bsd_system_info
;;
* )
echoerror "${OS_NAME} not supported.";
exit 1
;;
esac
}
#--- FUNCTION -------------------------------------------------------------------------------------------------------
# NAME: __ubuntu_derivatives_translation
# DESCRIPTION: Map Ubuntu derivatives to their Ubuntu base versions.
# If distro has a known Ubuntu base version, use those install
# functions by pretending to be Ubuntu (i.e. change global vars)
#----------------------------------------------------------------------------------------------------------------------
# shellcheck disable=SC2034
__ubuntu_derivatives_translation() {
UBUNTU_DERIVATIVES="(trisquel|linuxmint|linaro|elementary_os)"
# Mappings
trisquel_6_ubuntu_base="12.04"
linuxmint_13_ubuntu_base="12.04"
linuxmint_14_ubuntu_base="12.10"
#linuxmint_15_ubuntu_base="13.04"
# Bug preventing add-apt-repository from working on Mint 15:
# https://bugs.launchpad.net/linuxmint/+bug/1198751
linuxmint_16_ubuntu_base="13.10"
linuxmint_17_ubuntu_base="14.04"
linaro_12_ubuntu_base="12.04"
elementary_os_02_ubuntu_base="12.04"
# Translate Ubuntu derivatives to their base Ubuntu version
match=$(echo "$DISTRO_NAME_L" | egrep ${UBUNTU_DERIVATIVES})
if [ "${match}" != "" ]; then
case $match in
"elementary_os")