-
Notifications
You must be signed in to change notification settings - Fork 2
/
common
247 lines (209 loc) · 5.21 KB
/
common
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
#!/bin/bash
ABORTAG="[abort]"
ERRTAG="[error]"
WARNTAG="[warning]"
SYSLOGTAG="tingle"
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
TINGLE_ROOT=
if [ "${TINGLE_PREFIX}" != "/usr" ]; then
TINGLE_ROOT="${TINGLE_PREFIX}"
fi
UNIMPORTANT="${TINGLE_ROOT}/etc/tingle/unimportant-packages"
KILLSWITCH="${TINGLE_ROOT}/etc/tingle/noupdate"
BIGPTY="${TINGLE_PREFIX}/lib/tingle/bigpty"
exec 3>&1
exec 4>&2
set -E
trap handle_error ERR
trap handle_interrupt INT QUIT TERM
declare -a ignore_rvs
abort() {
# We could pass -s to logger and let it do the echo out to stderr,
# but I don't want to pass ABORTAG and friends into syslog. That's
# what syslog priorities are for.
log_error "$@"
echo "$ABORTAG" "$@" >&4
exit_clean 2
}
error() {
log_error "$@"
echo "$ERRTAG" "$@" >&4
exit_clean 1
}
warning() {
log_warning "$@"
debug "$WARNTAG" "$@"
}
debug() {
if ! is_interactive ; then
exec 2>/dev/null
fi
echo "$@" >&2
exec 2>&4
}
buffer() {
echo "$TMP_DIR/$(echo "$(date +%s)-$RANDOM" | sed "s/[ \t/-]//g")"
}
# For commands that return non-zero without actually failing.
do_ignore_derp() {
# Since we can't reliably detect failures, assume stderr is useless.
if ! is_interactive ; then
exec 2>/dev/null
fi
"$@" || true
exec 2>&4
}
do_or_die() {
local b i old_opts rv suppress=no
if ! is_interactive ; then
b="$(buffer)"
exec 2>"$b"
fi
old_opts="$-"
set +e
"$@"
rv=$?
if echo "$old_opts" | grep -q e; then
set -e
fi
exec 2>&4
if [ "$rv" -ne 0 ]; then
if [ ! -z "$ignore_rvs" ]; then
for (( i=0 ; i < ${#ignore_rvs[@]} ; i++ )); do
if [ "$rv" -eq "${ignore_rvs[$i]}" ]; then
suppress=yes
fi
done
ignore_rvs=( )
fi
if [ "$suppress" != "yes" ]; then
if ! is_interactive ; then
cat "$b" >&2
echo >&2
fi
error "\`$*' returned non-zero"
else
return $rv
fi
fi
}
execd() {
local b_err b_out d="$1" i rv
if [ ! -d "$d" ] ; then
warning "$d is not a valid hook scripts directory"
else
debug "Scanning $d for hook scripts"
find -L "${d}" -mindepth 1 -maxdepth 1 -type f | while read i; do
if [ -x "$i" ]; then
debug "Executing $i"
if ! is_interactive ; then
b_out="$(buffer)"
b_err="$(buffer)"
exec 1>"$b_out"
exec 2>"$b_err"
fi
set +e
"$i"
rv=$?
set -e
exec 1>&3
exec 2>&4
if [ $rv -ne 0 ]; then
if ! is_interactive ; then
cat "$b_out" >&1
cat "$b_err" >&2
echo >&2
fi
error "\`$i' returned non-zero"
fi
else
warning "$i is not executable"
fi
done
fi
}
exit_clean() {
local STATUS
STATUS=0
[ -n "$1" ] && STATUS="$1"
nuke_tmp
exit "$STATUS"
}
handle_error() {
local rv=$?
# A trap set on ERR is still hot even when set -e is not in effect.
# I don't really care about failures outside of set -e sections.
if echo "$-" | grep -q e; then
exit_clean "$rv"
fi
}
handle_interrupt() {
exit_clean 1
}
invoke_os_flavour() {
local BASE="$1" FLAVA SUFFIX old_opts rv
case "$(os_flavour)" in
CentOS|RedHat)
SUFFIX="rpm"
;;
Debian)
SUFFIX="dpkg"
;;
*)
error "tingle does not recognise your operating system"
;;
esac
FLAVA="${TINGLE_PREFIX}/lib/tingle/${BASE}-${SUFFIX}"
if [ -x "$FLAVA" ]; then
debug "Running ${FLAVA##*/}"
old_opts="$-"
set +e
do_or_die "$FLAVA"
rv=$?
if echo "$old_opts" | grep -q e; then
set -e
fi
return "$rv"
else
error "Could not execute $FLAVA"
fi
}
is_interactive() {
tty -s && test "${TINGLE_NONINTERACTIVE:+x}" != x && \
return 0
return 1
}
log_error() {
logger -p user.err -t "$SYSLOGTAG" -- "$@" || true
}
log_info() {
logger -t "$SYSLOGTAG" -- "$@" || true
}
log_warning() {
logger -p user.warn -t "$SYSLOGTAG" -- "$@" || true
}
nuke_tmp() {
rm -fr "$TMP_DIR"
}
os_flavour() {
if [ -e /etc/redhat-release ]; then
if grep -qi centos /etc/redhat-release ; then
echo "CentOS"
elif grep -qi scientific /etc/redhat-release ; then
echo "Scientific"
else
echo "RedHat"
fi
elif [ -e /etc/debian_version ]; then
# You're one of those Ubuntu users, are ye? See bug #19353.
echo "Debian"
fi
}
abort_on_killswitch() {
# All potentially disastrous automated processes need a killswitch
# within easy reach.
if [ -f "$KILLSWITCH" ]; then
abort "Aborting on killswitch. Remove $KILLSWITCH and try again."
fi
}
TMP_DIR="$(mktemp -d -t tingle.XXXXXX)"