-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_system
206 lines (178 loc) · 7.03 KB
/
.bash_system
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
#!/bin/sh
function system_detect {
# set defalut values
OS=$(uname)
DIST="unknown"
DIST_BASED_ON="unknown"
PSEUDO_NAME="unknown"
VERSION="unknown"
ARCH=$(uname -m)
ARCH_NAME="unknown" # can be "i386" or "amd64"
KERNEL=$(uname -r)
CONTAINER=""
# Mac OS X
if [[ "$OSTYPE" == "darwin"* ]]; then
OS="unix"
DIST="macosx"
DIST_BASED_ON="bsd"
[[ $(sw_vers -productVersion | \grep 10.8 | wc -l) -gt 0 ]] && PSEUDO_NAME="Mountain Lion"
[[ $(sw_vers -productVersion | \grep 10.9 | wc -l) -gt 0 ]] && PSEUDO_NAME="Mavericks"
[[ $(sw_vers -productVersion | \grep 10.10 | wc -l) -gt 0 ]] && PSEUDO_NAME="Yosemite"
[[ $(sw_vers -productVersion | \grep 10.11 | wc -l) -gt 0 ]] && PSEUDO_NAME="El Capitan"
[[ $(sw_vers -productVersion | \grep 10.12 | wc -l) -gt 0 ]] && PSEUDO_NAME="Sierra"
VERSION=$(sw_vers -productVersion)
ARCH_NAME="i386"
[[ $(uname -m | \grep 64 | wc -l) -gt 0 ]] && ARCH_NAME="amd64"
# Debian family
elif [[ -f /etc/debian_version ]]; then
local id=$(cat /etc/os-release | \grep "^ID=" | awk -F= '{ print $2 }')
if [[ "$id" == "debian" ]] || [[ "$id" == "raspbian" ]]; then
DIST=$id
PSEUDO_NAME=$(cat /etc/os-release | \grep "^VERSION=" | awk -F= '{ print $2 }' | \grep -oEi '[a-z]+')
VERSION=$(cat /etc/debian_version)
else
DIST=$(cat /etc/lsb-release | \grep '^DISTRIB_ID' | awk -F= '{ print $2 }')
PSEUDO_NAME=$(cat /etc/lsb-release | \grep '^DISTRIB_CODENAME' | awk -F= '{ print $2 }')
VERSION=$(cat /etc/lsb-release | \grep '^DISTRIB_RELEASE' | awk -F= '{ print $2 }')
fi
DIST_BASED_ON="debian"
ARCH_NAME="i386"
[[ $(uname -m | \grep 64 | wc -l) -gt 0 ]] && ARCH_NAME="amd64"
# RedHat family
elif [[ -f /etc/redhat-release ]]; then
DIST=$(cat /etc/redhat-release | sed s/\ release.*// | tr "[A-Z]" "[a-z]")
[[ "$DIST" == *"red"* ]] && DIST="redhat"
[[ "$DIST" == *"centos"* ]] && DIST="centos"
[[ "$DIST" == *"scientific"* ]] && DIST="scientific"
DIST_BASED_ON="redhat"
PSEUDO_NAME=$(cat /etc/redhat-release | sed s/.*\(// | sed s/\)//)
VERSION=$(cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//)
ARCH_NAME="i386"
[[ $(uname -m | \grep 64 | wc -l) -gt 0 ]] && ARCH_NAME="amd64"
# Windows/Cygwin
elif [[ "$OSTYPE" == "cygwin"* ]]; then
OS="windows"
DIST="cygwin"
DIST_BASED_ON="linux"
PSEUDO_NAME="Windows"
VERSION=$(cmd /c ver | \grep -oEi '[0-9]+\.[0-9]+\.[0-9]+')
ARCH_NAME="i386"
[ $(uname -m | \grep 64 | wc -l) -gt 0 ] && ARCH_NAME="amd64"
KERNEL=$(uname -r | \grep -oEi '[0-9]+\.[0-9]+\.[0-9]+')
fi
# containerisation
if cat /proc/1/cgroup 2> /dev/null | grep -i docker > /dev/null 2>&1; then
CONTAINER="docker"
fi
# convert metadata to lower case
export OS=$(echo $OS | tr "[A-Z]" "[a-z]")
export DIST=$(echo $DIST | tr "[A-Z]" "[a-z]")
export DIST_BASED_ON=$(echo $DIST_BASED_ON | tr "[A-Z]" "[a-z]")
export PSEUDO_NAME=$(echo $PSEUDO_NAME | tr "[A-Z]" "[a-z]")
export VERSION=$(echo $VERSION | tr "[A-Z]" "[a-z]")
export ARCH=$(echo $ARCH | tr "[A-Z]" "[a-z]")
export ARCH_NAME=$(echo $ARCH_NAME | tr "[A-Z]" "[a-z]")
export KERNEL=$(echo $KERNEL | tr "[A-Z]" "[a-z]")
[ -n "$CONTAINER" ] && export CONTAINER=$(echo $CONTAINER | tr "[A-Z]" "[a-z]")
return 0
}
function system_info {
echo "OS: $OS"
echo "DIST: $DIST"
echo "DIST BASED ON: $DIST_BASED_ON"
echo "PSEUDO NAME: $PSEUDO_NAME"
echo "VERSION: $VERSION"
echo "ARCH: $ARCH"
echo "ARCH NAME: $ARCH_NAME"
echo "KERNEL: $KERNEL"
[ -n "$CONTAINER" ] && echo "CONTAINER: $CONTAINER"
return 0
}
function system_test {
# get optional parameters
local skip_selected_tests="n"
local show_errors="n"
while [ "$1" != "" ]; do
case $1 in
--skip-selected-tests) skip_selected_tests="y"
;;
--show-errors) show_errors="y"
;;
esac
shift
done
source ~/.bash_asserts
printf "\n"
print_h1 "Run tests...\n"
system_info
printf "\n"
local log=/tmp/dotfiles-test.log
rm -f $log
(
assert_prog_exists "awk"
assert_prog_exists "bc"
assert_prog_exists "cat"
assert_prog_exists "chmod"
assert_prog_exists "cut"
assert_prog_exists "du"
assert_prog_exists "echo"
assert_prog_exists "expect"
assert_prog_exists "expr"
assert_prog_exists "find"
assert_prog_exists "grep"
assert_prog_exists "head"
assert_prog_exists "ln"
assert_prog_exists "ls"
assert_prog_exists "openssl"
assert_prog_exists "pcregrep"
assert_prog_exists "perl"
assert_prog_exists "printf"
assert_prog_exists "pygmentize"
assert_prog_exists "python"
assert_prog_exists "sed"
assert_prog_exists "sort"
assert_prog_exists "strip"
assert_prog_exists "sudo"
assert_prog_exists "test"
assert_prog_exists "tr"
assert_prog_exists "unzip"
assert_prog_exists "wc"
assert_prog_exists "wget"
if [ "$DIST" == "macosx" ]; then
assert_prog_exists "dscl"
assert_prog_exists "dseditgroup"
assert_prog_exists "ruby"
fi
if [ "$OS" == "linux" ]; then
assert_prog_exists "groupadd"
assert_prog_exists "useradd"
assert_prog_exists "userdel"
fi
) 2>&1 | tee $log
local count_programs_found=$(cat $log | removecc | \grep "^program " | \grep "ok$" | wc -l | sed "s/^[ ]*//g")
local count_programs_missing=$(cat $log | removecc | \grep "^program " | \grep -v "ok$" | wc -l | sed "s/^[ ]*//g")
if [ $count_programs_missing -gt 0 ]; then
print_h2 "$count_programs_found programs found, $count_programs_missing missing\n"
else
print_h2 "$count_programs_found programs found\n"
fi
rm -f $log
for file in $(\ls -1 ~/bin/*.test 2> /dev/null); do
if [ $show_errors == "y" ]; then
[ -x $file ] && $file $skip_selected_tests 2>&1 | tee -a $log
else
[ -x $file ] && $file $skip_selected_tests 2> /dev/null | tee -a $log
fi
done
local count_passed=$(cat $log | removecc | \grep "^test " | \grep "ok$" | wc -l | sed "s/^[ ]*//g")
local count_skipped=$(cat $log | removecc | \grep "^test " | \grep " skip" | wc -l | sed "s/^[ ]*//g")
local count_failed=$(cat $log | removecc | \grep "^test " | \grep -v "ok$" | \grep -v " skip" | wc -l | sed "s/^[ ]*//g")
if [ $count_failed -gt 0 ]; then
print_h2 "$count_passed tests passed, $count_skipped skipped, $count_failed failed\n"
else
print_h2 "$count_passed tests passed, $count_skipped skipped\n"
fi
rm -f $log
return 0
}
system_detect