forked from saltstack/salt-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-salt.sh
executable file
·3130 lines (2673 loc) · 106 KB
/
bootstrap-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
#===============================================================================
#
# FILE: bootstrap-salt.sh
#
# DESCRIPTION: Bootstrap salt installation for various systems/distributions
#
# BUGS: https://github.com/saltstack/salty-vagrant/issues
# AUTHOR: Pedro Algarvio (s0undt3ch), [email protected]
# Alec Koumjian (akoumjian), [email protected]
# Geoff Garside (geoffgarside), [email protected]
# LICENSE: Apache 2.0
# ORGANIZATION: Salt Stack (saltstack.org)
# CREATED: 10/15/2012 09:49:37 PM WEST
#===============================================================================
set -o nounset # Treat unset variables as an error
ScriptVersion="1.5.5"
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
# * BS_FORCE_OVERWRITE: Force overriding copied files(config, init.d, etc)
# * BS_GENTOO_USE_BINHOST: If 1 add `--getbinpkg` to gentoo's emerge
#===============================================================================
#===============================================================================
# LET THE BLACK MAGIC BEGIN!!!!
#===============================================================================
# Bootstrap script truth values
BS_TRUE=1
BS_FALSE=0
#--- 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}: $@\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 [ $PIP_ALLOWED -eq $BS_FALSE ]; then
echoerror "pip based installations were not allowed. Retry using '-P'"
usage
exit 1
fi
}
#=== FUNCTION ================================================================
# NAME: usage
# DESCRIPTION: Display usage information.
#===============================================================================
usage() {
cat << EOT
Usage : ${ScriptName} [options] <install-type> <install-type-args>
Installation types:
- stable (default)
- daily (ubuntu specific)
- git
Examples:
$ ${ScriptName}
$ ${ScriptName} stable
$ ${ScriptName} daily
$ ${ScriptName} git
$ ${ScriptName} git develop
$ ${ScriptName} git 8c3fadf15ec183e5ce8c63739850d543617e4357
Options:
-h Display this message
-v Display script version
-n No colours.
-D Show debug output.
-c Temporary configuration directory
-k Temporary directory holding the minion keys which will pre-seed
the master.
-M Also install salt-master
-S Also install salt-syndic
-N Do not install salt-minion
-C Only run the configuration function. This option automaticaly
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 works for functions which actually implement
pip based installations.
-F Allow copied files to overwrite existing(config, init.d, etc)
EOT
} # ---------- end of function usage ----------
#=== FUNCTION ================================================================
# NAME: __fetch_url
# DESCRIPTION: Retrieves a URL and writes it to a given path
#===============================================================================
__fetch_url() {
curl --insecure -s -o "$1" "$2" >/dev/null 2>&1 ||
wget --no-check-certificate -q -O "$1" "$2" >/dev/null 2>&1 ||
fetch -q -o "$1" "$2" >/dev/null 2>&1
}
#=== 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
#-----------------------------------------------------------------------
TEMP_CONFIG_DIR="null"
TEMP_KEYS_DIR="null"
INSTALL_MASTER=$BS_FALSE
INSTALL_SYNDIC=$BS_FALSE
INSTALL_MINION=$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}
BS_GENTOO_USE_BINHOST=${BS_GENTOO_USE_BINHOST:-$BS_FALSE}
# __SIMPLIFY_VERSION is mostly used in Solaris based distributions
__SIMPLIFY_VERSION=$BS_TRUE
while getopts ":hvnDc:k:MSNCPF" 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
;;
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 ;;
C ) CONFIG_ONLY=$BS_TRUE ;;
P ) PIP_ALLOWED=$BS_TRUE ;;
F ) FORCE_OVERWRITE=$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 '[-]+[[:alnum:]]' )
if [ "x$unparsed_options" != "x" ]; 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
# Define installation type
if [ "$#" -eq 0 ];then
ITYPE="stable"
else
__check_unparsed_options "$*"
ITYPE=$1
shift
fi
# Check installation type
if [ "$ITYPE" != "stable" ] && [ "$ITYPE" != "daily" ] && [ "$ITYPE" != "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="master"
else
__check_unparsed_options "$*"
GIT_REV="$1"
shift
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
CALLER=$(echo `ps -a -o pid,args | grep $$ | grep -v grep | tr -s ' '` | cut -d ' ' -f 2)
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=$?
# 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
TEE_PID=$(ps ax | grep tee | grep $LOGFILE | awk '{print $1}')
[ "x$TEE_PID" = "x" ] && 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
#--- 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
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)
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: __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|\
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 [ "x${match}" != "x" ]; 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"
for entry in $max_prio; do
if [ "x$(echo ${primary_release_files} | grep $entry)" != "x" ]; 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 $max_prio; do
if [ "x$(echo ${primary_release_files} | grep $entry)" != "x" ]; 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 [ "x$(echo "$DISTRO_NAME" | grep RedHat)" != "x" ]; then
# Let's convert CamelCase to Camel Case
DISTRO_NAME=$(__camelcase_split "$DISTRO_NAME")
fi
if [ "${DISTRO_NAME}" = "openSUSE project" ]; then
# lsb_release -si returns "openSUSE project" on openSUSE 12.3
DISTRO_NAME="opensuse"
fi
rv=$(lsb_release -sr)
[ "${rv}x" != "x" ] && 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}x" != "x" ] && DISTRO_VERSION=$(__parse_version_string "$rv")
fi
if [ "x$DISTRO_NAME" != "x" ] && [ "x$DISTRO_VERSION" != "x" ]; then
# We already have the distribution name and version
return
fi
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$//')
rv=$( (grep VERSION /etc/${rsource}; cat /etc/${rsource}) | grep '[0-9]' | sed -e 'q' )
[ "${rv}x" = "x" ] && continue # There's no version information. Continue to next rsource
v=$(__parse_version_string "$rv")
case $(echo ${n} | tr '[:upper:]' '[:lower:]') in
redhat )
if [ ".$(egrep 'CentOS' /etc/${rsource})" != . ]; then
n="CentOS"
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" ;;
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}x" != "x" ] && v=$(__parse_version_string "$rv") || v=""
case $(echo ${nn} | tr '[:upper:]' '[:lower:]') in
arch )
n="Arch Linux"
v="" # Arch Linux does not provide a version.
;;
debian )
n="Debian"
if [ "${v}x" = "x" ]; then
if [ "$(cat /etc/debian_version)" = "wheezy/sid" ]; then
# I've found an EC2 wheezy image which did not tell its version
v=$(__parse_version_string "7.0")
fi
else
echowarn "Unable to parse the Debian Version"
fi
;;
* )
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}x" != "x" ] && 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}x" = "x" ]; 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
}
__gather_system_info
echo
echoinfo "System Information:"
echoinfo " CPU: ${CPU_VENDOR_ID}"
echoinfo " CPU Arch: ${CPU_ARCH}"
echoinfo " OS Name: ${OS_NAME}"
echoinfo " OS Version: ${OS_VERSION}"
echoinfo " Distribution: ${DISTRO_NAME} ${DISTRO_VERSION}"
echo
# Let users know what's going to be installed/configured
if [ $INSTALL_MINION -eq $BS_TRUE ]; then
if [ $CONFIG_ONLY -eq $BS_FALSE ]; then
echoinfo "Installing minion"
else
echoinfo "Configuring minion"
fi
fi
if [ $INSTALL_MASTER -eq $BS_TRUE ]; then
if [ $CONFIG_ONLY -eq $BS_FALSE ]; then
echoinfo "Installing master"
else
echoinfo "Configuring master"
fi
fi
if [ $INSTALL_SYNDIC -eq $BS_TRUE ]; then
if [ $CONFIG_ONLY -eq $BS_FALSE ]; then
echoinfo "Installing syndic"
else
echoinfo "Configuring syndic"
fi
fi
# Simplify version naming on functions
if [ "x${DISTRO_VERSION}" = "x" ] || [ $__SIMPLIFY_VERSION -eq $BS_FALSE ]; then
DISTRO_MAJOR_VERSION=""
DISTRO_MINOR_VERSION=""
PREFIXED_DISTRO_MAJOR_VERSION=""
PREFIXED_DISTRO_MINOR_VERSION=""
else
DISTRO_MAJOR_VERSION="$(echo $DISTRO_VERSION | sed 's/^\([0-9]*\).*/\1/g')"
DISTRO_MINOR_VERSION="$(echo $DISTRO_VERSION | sed 's/^\([0-9]*\).\([0-9]*\).*/\2/g')"
PREFIXED_DISTRO_MAJOR_VERSION="_${DISTRO_MAJOR_VERSION}"
if [ "${PREFIXED_DISTRO_MAJOR_VERSION}" = "_" ]; then
PREFIXED_DISTRO_MAJOR_VERSION=""
fi
PREFIXED_DISTRO_MINOR_VERSION="_${DISTRO_MINOR_VERSION}"
if [ "${PREFIXED_DISTRO_MINOR_VERSION}" = "_" ]; then
PREFIXED_DISTRO_MINOR_VERSION=""
fi
fi
# Simplify distro name naming on functions
DISTRO_NAME_L=$(echo $DISTRO_NAME | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9_ ]//g' | sed -re 's/([[:space:]])+/_/g')
# Only Ubuntu has daily packages, let's let users know about that
if ([ "${DISTRO_NAME_L}" != "ubuntu" ] && [ $ITYPE = "daily" ]) && \
([ "${DISTRO_NAME_L}" != "trisquel" ] && [ $ITYPE = "daily" ]); then
echoerror "${DISTRO_NAME} does not have daily packages support"
exit 1
fi
#--- FUNCTION ----------------------------------------------------------------
# NAME: __function_defined
# DESCRIPTION: Checks if a function is defined within this scripts scope
# PARAMETERS: function name
# RETURNS: 0 or 1 as in defined or not defined
#-------------------------------------------------------------------------------
__function_defined() {
FUNC_NAME=$1
if [ "$(command -v $FUNC_NAME)x" != "x" ]; then
echoinfo "Found function $FUNC_NAME"
return 0
fi
echodebug "$FUNC_NAME not found...."
return 1
}
#--- FUNCTION ----------------------------------------------------------------
# NAME: __git_clone_and_checkout
# DESCRIPTION: (DRY) Helper function to clone and checkout salt to a
# specific revision.
#-------------------------------------------------------------------------------
__git_clone_and_checkout() {
SALT_GIT_CHECKOUT_DIR=/tmp/git/salt
[ -d /tmp/git ] || mkdir /tmp/git
cd /tmp/git
if [ -d $SALT_GIT_CHECKOUT_DIR ]; then
cd $SALT_GIT_CHECKOUT_DIR
git fetch || return 1
# Tags are needed because of salt's versioning, also fetch that
git fetch --tags || return 1
git reset --hard $GIT_REV || return 1
# Just calling `git reset --hard $GIT_REV` on a branch name that has
# already been checked out will not update that branch to the upstream
# HEAD; instead it will simply reset to itself. Check the ref to see
# if it is a branch name, check out the branch, and pull in the
# changes.
git branch -a | grep -q ${GIT_REV}
if [ $? -eq 0 ]; then
git pull --rebase || return 1
fi
else
git clone git://github.com/saltstack/salt.git || return 1
cd $SALT_GIT_CHECKOUT_DIR
git checkout $GIT_REV || return 1
fi
return 0
}
#--- FUNCTION ----------------------------------------------------------------
# NAME: __apt_get_noinput
# DESCRIPTION: (DRY) apt-get install with noinput options
#-------------------------------------------------------------------------------
__apt_get_noinput() {
apt-get install -y -o DPkg::Options::=--force-confold $@; return $?
}
#--- FUNCTION ----------------------------------------------------------------
# NAME: copyfile
# DESCRIPTION: Simple function to copy files. Overrides if asked.
#-------------------------------------------------------------------------------
copyfile() {
overwrite=$FORCE_OVERWRITE
if [ $# -eq 2 ]; then
sfile=$1
dfile=$2
elif [ $# -eq 3 ]; then
sfile=$1
dfile=$2
overwrite=$3
else
echoerror "Wrong number of arguments for copyfile()"
echoinfo "USAGE: copyfile <source> <dest> OR copyfile <source> <dest> <overwrite>"
exit 1
fi
# Does the source file exist?
if [ ! -f "$sfile" ]; then
echowarn "$sfile does not exist!"
return 1
fi
if [ ! -f "$dfile" ]; then
# The destination file does not exist, copy
echodebug "Copying $sfile to $dfile"
cp "$sfile" "$dfile" || return 1
elif [ -f "$dfile" ] && [ $overwrite -eq $BS_TRUE ]; then
# The destination exist and we're overwriting
echodebug "Overriding $dfile with $sfile"
cp -f "$sfile" "$dfile" || return 2
elif [ -f "$dfile" ] && [ $overwrite -ne $BS_TRUE ]; then
echodebug "Not overriding $dfile with $sfile"
fi
return 0
}
#--- FUNCTION ----------------------------------------------------------------
# NAME: movefile
# DESCRIPTION: Simple function to move files. Overrides if asked.
#-------------------------------------------------------------------------------
movefile() {
overwrite=$FORCE_OVERWRITE
if [ $# -eq 2 ]; then
sfile=$1
dfile=$2
elif [ $# -eq 3 ]; then
sfile=$1
dfile=$2
overwrite=$3
else
echoerror "Wrong number of arguments for movefile()"
echoinfo "USAGE: movefile <source> <dest> OR movefile <source> <dest> <overwrite>"
exit 1
fi
# Does the source file exist?
if [ ! -f "$sfile" ]; then
echowarn "$sfile does not exist!"
return 1
fi
if [ ! -f "$dfile" ]; then
# The destination file does not exist, copy
echodebug "Moving $sfile to $dfile"
mv "$sfile" "$dfile" || return 1
elif [ -f "$dfile" ] && [ $overwrite -eq $BS_TRUE ]; then
# The destination exist and we're overwriting
echodebug "Overriding $dfile with $sfile"
mv -f "$sfile" "$dfile" || return 1
elif [ -f "$dfile" ] && [ $overwrite -ne $BS_TRUE ]; then
echodebug "Not overriding $dfile with $sfile"
fi
return 0
}
##############################################################################
#
# Distribution install functions
#
# In order to install salt for a distribution you need to define:
#
# To Install Dependencies, which is required, one of:
# 1. install_<distro>_<major_version>_<install_type>_deps
# 2. install_<distro>_<major_version>_<minor_version>_<install_type>_deps
# 3. install_<distro>_<major_version>_deps
# 4 install_<distro>_<major_version>_<minor_version>_deps
# 5. install_<distro>_<install_type>_deps
# 6. install_<distro>_deps
#
# Optionally, define a salt configuration function, which will be called if
# the -c (config-dir) option is passed. One of:
# 1. config_<distro>_<major_version>_<install_type>_salt
# 2. config_<distro>_<major_version>_<minor_version>_<install_type>_salt
# 3. config_<distro>_<major_version>_salt
# 4 config_<distro>_<major_version>_<minor_version>_salt