-
Notifications
You must be signed in to change notification settings - Fork 6
/
sh-utils
173 lines (143 loc) · 3.03 KB
/
sh-utils
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
#! /bin/bash
source "$DGL_CONF_HOME/dgl-manage.conf"
DGL_CONF_LOCK_DIR="$DGL_CONF_HOME/locks"
declare -a DGL_LOCKS
DGL_LOCKS=()
PROMPTS_ENABLED=
GIVE_HELP=
help() {
if [[ -n "$GIVE_HELP" ]]; then
cat
echo
exit 0
fi
}
dgl-run() {
"$0" "$@"
}
say() {
echo ";;;; $@"
}
say-do() {
echo "[EXEC] $@"
"$@"
}
running-as-root() {
[[ "$UID" == "0" ]]
}
assert-running-as-root() {
if ! running-as-root; then
abort-saying "$SCRIPT_NAME needs to be run as root"
fi
}
not-running-as-root() {
[[ "$UID" != "0" ]]
}
each-do() {
local cmd="$1"
shift
for arg in "$@"; do
$cmd "$arg"
done
}
announce() {
echo
echo "---------------------------------------------------------------------"
echo "$@"
echo "$@" >>$ANNOUNCEMENTS_FILE
}
abort-saying() {
echo -e "$@, aborting."
exit 1
}
# Call with any argument to disable prompts
enable-prompts() {
local opt
while getopts p opt; do
PROMPTS_ENABLED=1
done
shift $((OPTIND - 1))
# Would be nice to be able to modify the caller's arguments...
}
prompts-enabled() {
[[ -n "$PROMPTS_ENABLED" && -t 0 ]]
}
prompt() {
if prompts-enabled; then
echo "-- Press RETURN to $@ --"
read
else
say "$@"
fi
}
unlock-dgl-locks() {
if (( "${#DGL_LOCKS[@]}" > 0 )); then
rm -f ${DGL_LOCKS[@]}
fi
}
unlock-on-error() {
unlock-dgl-locks
exit 1
}
trap-lock-hook-cleanup() {
if [[ -z "$DGL_TRAP_LOCK_CLEANUP_HOOKED" ]]; then
trap unlock-on-error INT TERM
trap unlock-dgl-locks EXIT
DGL_TRAP_LOCK_CLEANUP_HOOKED=1
fi
}
lock-or-die() {
local lock_name=$1
mkdir -p "$DGL_CONF_LOCK_DIR"
local lock_file="$DGL_CONF_LOCK_DIR/${lock_name}.lock"
shift || true
if ( set -o noclobber; echo "$$" > "$lock_file" ) 2>/dev/null; then
DGL_LOCKS=(${DGL_LOCKS[@]} "$lock_file")
trap-lock-hook-cleanup
else
echo "Could not lock $lock_file: $@"
exit 1
fi
}
dgl-today() {
date +%y%m%d-%H%M
}
cat-error() {
cat >&2
}
assert-chroot-exists() {
if [[ ! -d "$DGL_CHROOT" ]]; then
cat-error <<ERROR
DGL chroot $DGL_CHROOT doesn't exist.
Please create it, or edit $DGL_CONF_HOME/dgl-manage.conf to
point at the real chroot.
ERROR
exit 1
fi
}
assert-login-db-exists() {
if [[ ! -f "$LOGIN_DB" ]]; then
cat-error <<EOF
DGL login db $LOGIN_DB is missing; please check that the chroot
is correctly setup, and that $DGL_CONF_HOME/dgl-manage.conf points
at the correct chroot path.
EOF
exit 1
fi
}
login-query() {
sqlite3 "$LOGIN_DB"
}
dgl-user-exists() {
local user="$1"
local found="$(echo "SELECT username FROM dglusers WHERE username='$user';" |
login-query)"
[[ -n "$found" ]]
}
dgl-user-is-admin() {
local user="$1"
local found="$(echo "SELECT username FROM dglusers
WHERE username='$user' AND (flags & 1) = 1;" |
login-query)"
[[ -n "$found" ]]
}