-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·163 lines (117 loc) · 4.12 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·163 lines (117 loc) · 4.12 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
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
source scripts/helpers.sh
run_started_at=$(get_ms_timestamp)
if [[ -d "is-currently-running" ]]; then
previous_run_started_seconds_ago=$(( $(date +%s) - $(get_file_value "is-currently-running/started-running-at") ))
if [[ $previous_run_started_seconds_ago -gt 120 ]]; then
echo "Warning: Removing stale lock (older than 2 minutes)" >&2
rm is-currently-running/started-running-at
rmdir "is-currently-running"
else
echo "Error: already running (lock exists: is-currently-running)" >&2
echo "If this is wrong, remove the lock using \"rm -rf is-currently-running\"" >&2
exit 1
fi
fi
on_exit() {
script_status_code=$?
if [[ -f "is-currently-running/started-running-at" ]]; then
rm "is-currently-running/started-running-at"
fi
if [[ -d "is-currently-running" ]]; then
rmdir "is-currently-running"
fi
if [[ -f "current-run.tar.gz" ]]; then
rm current-run.tar.gz
fi
if [[ -f "current-run.tar.zst" ]]; then
rm current-run.tar.zst
fi
if [[ -d "current-run" ]]; then
rm -rf current-run
fi
# Exit this trap with the original status code.
exit "$script_status_code"
}
trap on_exit EXIT INT TERM HUP
if ! mkdir "is-currently-running" 2>/dev/null; then
exit 1
fi
date +%s > "is-currently-running/started-running-at"
if [[ -f "data/watchtower-is-unavailable" ]]; then
log_info "Watchtower might be unavailable"
if ! scripts/check-if-watchtower-is-available.sh; then
exit 1
fi
log_info "Watchtower is available again, continuing"
fi
if [[ -d "current-run" ]]; then
rm -rf current-run/
fi
mkdir current-run/
if [[ ! -d data ]]; then
mkdir data
fi
if [[ -f "data/run-number" ]]; then
current_run_number=$(( $(get_file_value "data/run-number") + 1 ))
else
current_run_number=0
fi
echo "$current_run_number" > "data/run-number"
# The client runs when it is installed. The first run after that is triggered by the cron. That might
# happen a few seconds after installation, so skip that first run.
if [[ $current_run_number -eq 1 ]]; then
log_info "Watchtower was just installed. Skipping first run."
exit 0
fi
log_info "Starting run $current_run_number"
if [[ ! -d "data/applications" ]]; then
mkdir -p data/applications
fi
if ! scripts/run-for-server.sh "$current_run_number"; then
log_error "Failed to process for server"
fi
for app_dir in data/applications/*/; do
if [[ ! -d "$app_dir" ]]; then
continue
fi
application_uuid=$(basename "$app_dir")
if [[ ! "$application_uuid" =~ ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ ]]; then
log_error "Invalid application UUID: $application_uuid" && exit 1
fi
if [[ -f "$app_dir/ignore-because-still-setting-up" ]]; then
log_info "Skipping application $application_uuid (still setting up)"
continue
fi
if ! scripts/run-for-application.sh "$current_run_number" "$application_uuid"; then
log_error "Application $application_uuid failed to process"
fi
done
run_duration_in_ms=$(( $(get_ms_timestamp) - run_started_at ))
sending_started_at=$(get_ms_timestamp)
should_self_update="no"
if scripts/send-to-watchtower.sh; then
should_self_update="no"
else
exit_code=$?
if [[ $exit_code -eq 42 ]]; then
should_self_update="yes"
else
log_error "Failed to send to Watchtower" && exit 1
fi
fi
sending_duration_in_ms=$(( $(get_ms_timestamp) - sending_started_at ))
finished_message="Finished run $current_run_number (collecting took ${run_duration_in_ms}ms, sending took ${sending_duration_in_ms}ms)"
rotate_log_file "logs/run-durations.log"
echo "[$(get_human_timestamp)] $finished_message" >> logs/run-durations.log
log_info "$finished_message"
# The self-updater has to run as the last step in this file. Depending on which
# files are updated, self updating can cause the script to not continue running "run.sh".
if [[ "$should_self_update" == "yes" ]]; then
if ! scripts/self-update.sh; then
log_error "Self-update failed" && exit 1
fi
fi
exit 0