This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
run
executable file
·330 lines (269 loc) · 8.52 KB
/
run
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
#!/bin/bash
err_tag="[error]"
info_tag="[info]"
log_error() {
>&2 echo "$err_tag $1"
}
log() {
echo "$info_tag $1"
}
exit_with_error() {
local msg="$1"
local exit_code=${2:-1}
log_error "$msg (exit code $exit_code)"
exit $2
}
exit_if_error() {
if [ $? -eq 0 ]; then
return
fi
exit_with_error "${1:-Command failed}" $?
}
check_executables() {
for exe in "${executables[@]}"; do
if !type "$exe" &>/dev/null; then
exit_with_error "Executable is missing: $exe"
fi
done
}
check_app_runtime_executables() {
if [ -e ~/.cargo/env ]; then
. ~/.cargo/env
fi
executables=(
rustup cargo git bash yarn tmux ionice sudo nice wc cut logger sleep tail
)
check_executables
}
remote_repo_name="bench-bot"
remote_repo="https://github.com/paritytech/$remote_repo_name"
benchbot_user="benchbot"
benchbot_session="/tmp/bench-bot"
install_location="/home/$benchbot_user/bench-bot"
log_file="$install_location/log.txt"
print_help_and_exit() {
echo "
Usage: run [command]
Commands:
bootstrap:
Bootstrap the bot to its predefined location ($install_location).
start, stop, restart:
Execute the relevant subcommand for the bot's process.
update [ref]:
Pull a ref (branch or pull request) from $remote_repo, install it and
restart the bot.
For pull requests:
update pull/number/head:branch (e.g. pull/1/head:master)
For branches:
update branch
help:
Print this message and exit
"
exit $1
}
current_user="${USER:-$(whoami 2>/dev/null)}"
if [ "$current_user" != "$benchbot_user" ]; then
as_benchbot="sudo -u $benchbot_user"
fi
install_deps() {
# needed to detect rustup if it's installed
if [ -e ~/.cargo/env ]; then
. ~/.cargo/env
fi
if [ "${2:-}" == "--force" ] || ! which rustup &>/dev/null; then
log "Installing Rust"
curl https://sh.rustup.rs -sSf | sh -s -- -y
exit_if_error "Failed to install rustup"
# For ensuring consistency, it's _required_ that the default toolchain is
# the same that the release team's Substrate benchmark scripts for Substrate
# uses. It's _good_ to have the toolchain's versions also match.
rustup default stable
rustup toolchain install nightly
exit_if_error "Failed to install nightly toolchain"
rustup target add wasm32-unknown-unknown --toolchain nightly
exit_if_error "Failed to add wasm target"
fi
}
create_bot_user() {
if id "$benchbot_user" &>/dev/null; then
return
fi
log "Creating $benchbot_user current_user"
sudo useradd "$benchbot_user"
exit_if_error "Failed to create current_user $benchbot"
sudo mkhomedir_helper "$benchbot_user"
exit_if_error "Failed to create home directory for $benchbot"
}
install_repo() {
if [ "${2:-}" != "--force" ] && [ -e "$install_location" ]; then
return
fi
mkdir -p "$install_location"
exit_if_error "Failed to create $install_parent"
git clone "$remote_repo" "$install_location"
exit_if_error "Failed to clone $remote_repo to $install_location"
cd "$install_location" && yarn
exit_if_error "Failed to install dependencies in $install_location"
}
install_ref() {
local ref="${1:-}"
if [ ! "$ref" ]; then
log_error "Ref needs to be supplied"
print_help_and_exit 1
fi
cd "$install_location"
exit_if_error "Failed to cd into $install_location"
local detached_head="$(git rev-parse HEAD)"
exit_if_error "Failed to get current HEAD sha"
git checkout "$detached_head" >/dev/null
exit_if_error "Failed to checkout to current HEAD sha"
# NOTE: it's NECESSARY to checkout to the detached HEAD since the branch's ref
# will be deleted in the following step, which might be the current checked-out
# ref; deleting the currently checked-out ref might put the git tree in a
# unrecoverable state.
# Parse pull requests with pull/ID/head:BRANCHNAME as specified by Github
if [[ "$ref" =~ ^pull/[[:digit:]]+/head:(.*) ]]; then
local branch="${BASH_REMATCH[1]}"
else
local branch="$ref"
ref="$ref:$ref"
fi
local branch_ref="refs/heads/$branch"
git update-ref -d "$branch_ref"
exit_if_error "Failed to clean up ref $branch_ref before fetching the branch $branch"
# NOTE: be sure to check the step above since proceeding without it working
# might potentially put the git tree in a unrecoverable state.
# This depends on the git tree being in a detached HEAD state as done by `git
# checkout "$detached_head"` above.
git fetch origin "$ref"
exit_if_error "Failed to fetch $ref from remote"
while IFS= read -r line; do
if [[ "$line" =~ ^[[:space:]]*([^[:space:]]+)[[:space:]]+refs/heads/(.*) ]] &&
[ "${BASH_REMATCH[2]}" == "$branch" ]; then
local ref_commit="${BASH_REMATCH[1]}"
break
fi
done < <(git show-ref)
if [ ! "${ref_commit:-}" ]; then
exit_with_error "Failed to find commit reference for $ref (branch $branch)"
fi
git branch -D "$branch"
git checkout "$ref_commit" >/dev/null
exit_if_error "Failed to checkout commit $ref_commit (ref $ref, branch $branch)"
git switch -c "$branch"
exit_if_error "Failed to switch from detached head to branch $branch (ref $ref, commit $ref_commit)"
local head_sha="$(git rev-parse HEAD)"
exit_if_error "Failed to parse the HEAD commit SHA for $branch (ref $ref, commit $ref_commit)"
log "Installed branch '$branch' at $head_sha"
}
handle_exec() {
local cmd="$1"
shift
case "$cmd" in
start)
if pgrep -u benchbot &>/dev/null; then
exit_with_error "the $benchbot_user user is already running a process"
fi
if [ -e "$log_file" ]; then
local start_from_line="$(wc -l "$log_file" | cut -d ' ' -f1)"
exit_if_error "Failed to count the lines in $log_file"
start_from_line=$(( start_from_line + 1 ))
else
echo "" > "$log_file"
unset start_from_line
fi
unset env_vars
case "${1:-}" in
debug)
local env_vars="DEBUG=true"
;;
esac
# don't want to spam errors in case something is broken; at least
# wait 1 second between attempts in the "while true" loop below
sudo ionice -c 1 -n 0 sudo nice -n -19 sudo -u $benchbot_user \
tmux new-session -d bash -c "
. ~/.cargo/env &&
cd \"$install_location\" &&
git config --local user.name 'Parity Bot' &&
git config --local user.email [email protected] &&
git config --local advice.detachedHead false &&
yarn &&
while true; do \\
${env_vars:-} 2>&1 yarn start | while IFS= read -r line; do \\
echo \"\$(date --rfc-3339=seconds): \$line\" >> \"$log_file\"; \\
logger \"bb: \$line\"; \\
done; \\
sleep 1; \\
done
"
exit_if_error "Failed to create tmux session for user $benchbot_user"
echo -e "\nNote: the command will still be running after quitting this terminal. Use \"run stop\" for stopping it.\n"
tail "--lines=+${start_from_line:-0}" -f "$log_file"
;;
stop)
if pgrep -u benchbot &>/dev/null; then
sudo pkill -u benchbot
fi
;;
restart)
handle_exec stop
handle_exec start "$@"
;;
*)
exit_with_error "Unknown handle_exec command $cmd"
;;
esac
}
main() {
local cmd="$1"
shift
# Initial checks before running the actual commands
case "$cmd" in
start|stop|restart|update)
$as_benchbot bash -c "'${BASH_SOURCE[0]}' check_app_runtime_executables"
exit_if_error
;;
esac
case "$cmd" in
start|stop|restart)
handle_exec "$cmd" "$@"
local exit_code=$?
echo "Exit code: $exit_code"
exit $exit_code
;;
update)
local ref="${1:-}"
if [ ! "$ref" ]; then
log_error "Ref needs to be supplied"
print_help_and_exit 1
fi
shift
handle_exec stop
$as_benchbot bash -c "'${BASH_SOURCE[0]}' install_ref '$ref'"
exit_if_error "Failed to install ref '$ref'"
bash -c "'${BASH_SOURCE[0]}' start $@"
exit_if_error "Failed to start"
;;
install_repo | \
install_ref | \
install_deps | \
check_app_runtime_executables)
"$cmd" "$@"
;;
bootstrap)
create_bot_user
$as_benchbot bash -c "'${BASH_SOURCE[0]}' install_deps"
exit_if_error "Failed to install dependencies"
$as_benchbot bash -c "'${BASH_SOURCE[0]}' install_repo"
exit_if_error "Failed to install repository"
;;
help)
print_help_and_exit 0
;;
*)
log_error "Invalid command $cmd"
print_help_and_exit 1
;;
esac
}
main "$@"