blob: 6fe4da493e484e351d9908e58e3aa6fa86a1386c (
plain)
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
|
#!/bin/sh
# fail on errors
set -e
# if the error output matches this regex, auto retry
# and dont prompt user.
auto_retry_regex="write_to_session_record_port" # Guix does not know how to
# gracefully reconnect TLS...
# run dir
run="${XDG_RUNTIME_DIR:-/tmp}/guix-retry/$$"
mkdir -p "$run"
# delete run dir on exit
function cleanup {
real="$(realpath "$run")"
if [[ ! "$real" = "/" ]]; then
rm -fr "$real"
fi
}
trap cleanup EXIT
# get error stream
stderr="$run/stderr"
mkfifo -m 600 "$stderr"
log="$run/log"
# unset fail on errors
set +e
# retry command forever
# until success or abort
while true; do
# execute command
if [[ ! -z "$GUIX_RETRY_NO_TTY" ]]; then
"$@" 2>"$stderr" &
else
command="$@"
script -q -c "$command" 2>"$stderr" "$log" &
fi
pid="$!"
output="$(cat "$stderr" | tee /dev/stderr)"
wait "$pid"
err="$?"
if [[ -z "$GUIX_RETRY_NO_TTY" ]]; then
err="$(tail -n1 "$log" | awk -F '"' '{ print $2 }')"
fi
if [[ "$err" -eq 0 ]]; then
# command succeeded, break loop
break
fi
if [[ "$output" =~ "$auto_retry_regex" ]]; then
# command errored and is set to auto retry
continue
fi
if [[ ! -z "$GUIX_RETRY_NO_PROMPT" ]]; then
# do not prompt, just fail
exit 1
fi
# check if user wants to retry
read -p "Command failed. Retry? (y/N): " response
if [[ ! "$response" = "y" ]]; then
# command failed and told to abort, break loop
exit 1
fi
done
|