-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcommon_functions.sh
More file actions
executable file
·386 lines (330 loc) · 7.64 KB
/
common_functions.sh
File metadata and controls
executable file
·386 lines (330 loc) · 7.64 KB
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
#!/bin/bash
escape_shell_args() {
printf "%q " "$@"
}
resolve_path() {
local -r PATH="$1"
local -r WORKING_PATH="$2"
case "${PATH}" in
/*)
echo "${PATH}"
;;
*)
echo "${WORKING_PATH}/${PATH}"
;;
esac
}
load_env() {
set +x
shopt -s nullglob
set -- /usr/local/share/env/*
if [ "$#" -gt 0 ]; then
for file in "$@"; do
# shellcheck source=/dev/null
source "${file}"
done
fi
}
get_user_home_directory() {
local USER="$1"
if [ -z "$USER" ]; then
return 1
fi
getent passwd "$USER" | cut -d: -f 6
}
as_user() (
set +x
local COMMAND="$1"
local WORKING_DIR="$2"
if [ "true" = "$NON_PRIVILEGED_USER" ]; then
set -x
/bin/bash -c "cd '$WORKING_DIR'; $COMMAND"
return "$?"
fi
local USER="$3"
if [ -z "$COMMAND" ]; then
return 1;
fi
if [ -z "$WORKING_DIR" ]; then
WORKING_DIR='/app';
fi
if [ -z "$USER" ]; then
USER='build';
fi
USER_HOME="$(get_user_home_directory "$USER")"
set -x
sudo -u "$USER" -E HOME="$USER_HOME" /bin/bash -c "cd '$WORKING_DIR'; $COMMAND"
)
as_build() (
set +x
as_user "$1" "$2" 'build'
)
as_code_owner() (
set +x
as_user "$1" "$2" "$CODE_OWNER"
)
as_app_user() (
set +x
as_user "$1" "$2" "$APP_USER"
)
convert_exit_code_to_string() {
if [ "$1" -eq 0 ]; then
echo 'true';
else
echo 'false';
fi
}
run_return_boolean() {
if "$@"; then
echo 'true'
else
echo 'false'
fi
}
convert_to_boolean_string() {
if [ "$1" == '1' ] || [ "$1" == "true" ]; then
echo 'true';
else
echo 'false';
fi
}
convert_to_boolean_string_zero_is_true() {
if [ "$1" == '0' ]; then
echo 'true';
elif [ "$1" == '1' ]; then
echo 'false';
else
convert_to_boolean_string "$1"
fi
}
is_true() {
case "$1" in
true|1)
return 0;
;;
esac
return 1;
}
is_false() {
case "$1" in
true|1)
return 1;
;;
esac
return 0;
}
is_hem_project() {
[ -f /app/tools/hem/config.yaml ] || [ -f /app/tools/hobo/config.yaml ]
return "$?"
}
get_filesystem_for_work_directory() (
set +e
grep "$WORK_DIRECTORY" /proc/mounts | awk '{ print $3 }'
)
is_app_mountpoint() {
local FILESYSTEM=''
FILESYSTEM="$(get_filesystem_for_work_directory)"
echo "$FILESYSTEM" | grep -q -E "(nfs|vboxsf|fuse\\.osxfs)"
return "$?"
}
is_chown_forbidden() {
# Determine if the app directory is an NFS mountpoint, which doesn't allow chowning.
local FILESYSTEM=''
FILESYSTEM="$(get_filesystem_for_work_directory)"
echo "$FILESYSTEM" | grep -q -E "(nfs|vboxsf)"
return "$?"
}
is_vboxsf_mountpoint() {
local FILESYSTEM=''
FILESYSTEM="$(get_filesystem_for_work_directory)"
echo "$FILESYSTEM" | grep -q "vboxsf"
return "$?"
}
alias_function() {
local -r ORIG_FUNC=$(declare -f "$1")
local -r NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
eval "$NEWNAME_FUNC"
}
before() {
local -r ORIG_FUNC=$(declare -f "$1")
local -r PREPENDED_FUNC="${ORIG_FUNC/\{/\{ $2;}"
eval "$PREPENDED_FUNC"
}
after() {
local -r ORIG_FUNC=$(declare -f "$1")
local -r APPENDED_FUNC="${ORIG_FUNC/%\}/$2; \}}"
eval "$APPENDED_FUNC"
}
replace() {
local -r ORIG_FUNC=$(declare -f "$1")
local -r REPLACED_FUNC="$1() { $2; }"
eval "$REPLACED_FUNC"
}
do_build() {
:
}
do_start() {
:
}
do_user_ssh_keys() (
set +x
local SSH_USER="$1"
if [ -z "$SSH_USER" ]; then
return 1;
fi
local SSH_FILENAME="$2"
local SSH_PRIVATE_KEY="$3"
local SSH_PUBLIC_KEY="$4"
local SSH_KNOWN_HOSTS="$5"
local SSH_USER_HOME
SSH_USER_HOME=$(get_user_home_directory "$SSH_USER")
if [ -n "$SSH_PRIVATE_KEY" ]; then
echo "Setting up SSH keys for the $SSH_USER user"
(
umask 0077
mkdir -p "$SSH_USER_HOME/.ssh/"
echo "$SSH_PRIVATE_KEY" | base64 --decode > "$SSH_USER_HOME/.ssh/$SSH_FILENAME"
)
if [ -n "$SSH_PUBLIC_KEY" ]; then
echo "$SSH_PUBLIC_KEY" | base64 --decode > "$SSH_USER_HOME/.ssh/$SSH_FILENAME.pub"
fi
if [ -n "$SSH_KNOWN_HOSTS" ]; then
echo "$SSH_KNOWN_HOSTS" | base64 --decode > "$SSH_USER_HOME/.ssh/known_hosts"
fi
chown -R "$SSH_USER" "$SSH_USER_HOME/.ssh/"
unset SSH_PRIVATE_KEY
unset SSH_PUBLIC_KEY
unset SSH_KNOWN_HOSTS
fi
)
function do_enable_tracing() {
export PS4='$(date "+%s.%N ($LINENO) + ")'
}
function do_apt_get_update() {
apt-get update -qq
}
function do_install_security_updates() {
do_apt_get_update
DEBIAN_FRONTEND=noninteractive apt-get -s dist-upgrade | grep "^Inst" | \
grep -i securi | awk -F " " '{print $2}' | \
xargs apt-get -qq -y --no-install-recommends install
do_clear_apt_caches
}
function do_clear_apt_caches() {
apt-get auto-remove -qq -y
apt-get clean
rm -rf /var/lib/apt/lists/*
}
function canonical_port() {
local PORT="$1"
if [[ $PORT =~ tcp://[^:]+:([0-9]+) ]]; then
PORT="${BASH_REMATCH[1]}"
fi
if [[ "$PORT" =~ [^0-9] ]] || [[ "$PORT" -lt 1 ]] || [[ "$PORT" -gt 65535 ]]; then
echo "Invalid Port specified for $VAR_NAME: '$PORT'"
exit 1
fi
echo "$PORT"
}
function has_acl() {
local FILESYSTEM=''
FILESYSTEM="$(get_filesystem_for_work_directory)"
case "$FILESYSTEM" in
fuse.osx)
return 1
;;
*)
return 0
;;
esac
}
function permission_mode() {
if [ "$IS_CHOWN_FORBIDDEN" == "true" ]; then
echo "chmod"
else
echo "stickybit"
fi
}
function set_path_permissions() {
local -a READABLE_USERS=()
IFS=" " read -r -a READABLE_USERS <<< "$1"
local -a WRITEABLE_USERS=()
IFS=" " read -r -a WRITEABLE_USERS <<< "$2"
local -a PATHS=()
IFS=" " read -r -a PATHS <<< "${@:3}"
case "$PERMISSION_MODE" in
facl)
PERMISSIONS=()
for user in "${WRITEABLE_USERS[@]}"; do
PERMISSIONS+=(-m "$(printf -- 'user:%s:rwX' "$user")" -m "$(printf -- 'default:user:%s:rwX' "$user")")
done
for user in "${READABLE_USERS[@]}"; do
PERMISSIONS+=(-m "$(printf -- 'user:%s:rX' "$user")" -m "$(printf -- 'default:user:%s:rX' "$user")")
done
setfacl -R "${PERMISSIONS[@]}" "${PATHS[@]}"
find "${PATHS[@]}" ! -perm /660 -exec chmod ug+rw,o-rwx {} +
;;
stickybit)
GROUP="$(printf '%s' "${WRITEABLE_USERS[@]}")"
if ! getent group "$GROUP" >/dev/null; then
groupadd "$GROUP"
fi
for USER in "${WRITEABLE_USERS[@]}"; do
usermod -a -G "$GROUP" "$USER"
done
find "${PATHS[@]}" ! -group "$GROUP" -exec chgrp "$GROUP" {} +
find "${PATHS[@]}" -type d ! -perm -2070 -exec chmod g+ws {} +
find "${PATHS[@]}" -type f ! -perm -0060 -exec chmod g+w {} +
;;
chmod)
find "${PATHS[@]}" ! -perm -0666 -exec chmod a+rw {} +
;;
*)
echo "unsupported permission mode '$PERMISSION_MODE'" >&2
;;
esac
}
function wait_for_remote_ports() (
set +x
local -r TIMEOUT=$1
local -r INTERVAL=0.5
local -r CHECK_TOTAL=$((TIMEOUT*2))
local COUNT
shift
COUNT=0
until (test_remote_ports "$@")
do
((COUNT++)) || true
if [ "${COUNT}" -gt "${CHECK_TOTAL}" ]
then
echo "One of the services [$*] didn't become ready in time"
exit 1
fi
sleep "${INTERVAL}"
done
)
function test_remote_ports() {
local SERVICE
local SERVICE_PARAMS
for SERVICE in "$@"; do
IFS=':'
# shellcheck disable=SC2206
SERVICE_PARAMS=($SERVICE)
unset IFS
timeout 1 bash -c "cat < /dev/null > /dev/tcp/${SERVICE_PARAMS[0]}/${SERVICE_PARAMS[1]}" 2>/dev/null || return 1
done
}
function deprecate_env_var() {
local -r DEPRECATED_ENV_VAR="$1"
local -r NEW_ENV_VAR="$2"
if [ -n "${!DEPRECATED_ENV_VAR:-}" ]; then
echo "deprecated: $DEPRECATED_ENV_VAR is deprecated, please use $NEW_ENV_VAR instead" >&1
eval "export $NEW_ENV_VAR=${!DEPRECATED_ENV_VAR}"
fi
}
function do_list_functions() {
compgen -A function -X '!do_**' | sed 's/^do_//'
}
function do_shell() {
bash "$@"
}