-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcommon_functions.sh
More file actions
executable file
·272 lines (233 loc) · 5.71 KB
/
common_functions.sh
File metadata and controls
executable file
·272 lines (233 loc) · 5.71 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
#!/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"
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
}
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_hem_project() {
local RESULT=1
if [ -f /app/tools/hem/config.yaml ] || [ -f /app/tools/hobo/config.yaml ]; then
RESULT=0
fi
convert_exit_code_to_string "$RESULT"
}
is_app_mountpoint() {
grep -q -E "/app (nfs|vboxsf|fuse\\.osxfs)" /proc/mounts
local RESULT="$?"
convert_exit_code_to_string "$RESULT"
}
is_chown_forbidden() {
# Determine if the app directory is an NFS mountpoint, which doesn't allow chowning.
grep -q -E "/app (nfs|vboxsf)" /proc/mounts
local RESULT="$?"
convert_exit_code_to_string "$RESULT"
}
is_vboxsf_mountpoint() {
grep -q "/app vboxsf" /proc/mounts
local RESULT="$?"
convert_exit_code_to_string "$RESULT"
}
alias_function() {
local -r ORIG_FUNC=$(declare -f "$1")
local -r NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
eval "$NEWNAME_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
set -x
}
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 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
set -x
}
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 do_ownership() {
local OWNERSHIP_PATH=($1)
local USER="$2"
local GROUP="$3"
local ALLOW_GROUP_WRITE="${4:-true}"
if [ "$IS_CHOWN_FORBIDDEN" != 'true' ]; then
find "${OWNERSHIP_PATH[@]}" \( ! -user "${USER}" -or ! -group "${GROUP}" \) -exec chown "${USER}:${GROUP}" {} +
fi
do_read_write_permissions "${OWNERSHIP_PATH[@]}" "${ALLOW_GROUP_WRITE}"
}
function do_read_write_permissions() {
local OWNERSHIP_PATH=($1)
local ALLOW_GROUP_WRITE="${2:-true}"
if [ "$IS_CHOWN_FORBIDDEN" != 'true' ]; then
if [ "$ALLOW_GROUP_WRITE" == 'true' ]; then
find "${OWNERSHIP_PATH[@]}" \( ! -perm /u=w -or ! -perm /g=w -or -perm /o=w \) -exec chmod ug+rw,o-w {} +
else
find "${OWNERSHIP_PATH[@]}" \( ! -perm /u=w -or -perm /g=w -or -perm /o=w \) -exec chmod u+rw,go-w {} +
fi
else
find "${OWNERSHIP_PATH[@]}" \( ! -perm /u=w -or ! -perm /g=w -or ! -perm /o=w \) -exec chmod a+rw {} +
fi
}
function do_read_permissions() {
local OWNERSHIP_PATH=($1)
find "${OWNERSHIP_PATH[@]}" \( ! -perm /u=r -or ! -perm /g=r -or ! -perm /o=r \) -exec chmod a+r {} +
}
function do_remove_other_permissions() {
local OWNERSHIP_PATH=($1)
find "${OWNERSHIP_PATH[@]}" \( -perm /o=r -or -perm /o=w -or -perm /o=x \) -exec chmod o-rwx {} +
}