-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·302 lines (266 loc) · 9.07 KB
/
build.sh
File metadata and controls
executable file
·302 lines (266 loc) · 9.07 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
#!/usr/bin/env bash
set -euo pipefail
MEMBERS=$(cat members.json)
WEEK=$(( $(date +%s) / (7 * 86400) ))
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
shuffle() {
echo "$MEMBERS" | jq -c '.[]' | while read -r m; do
id=$(echo "$m" | jq -r '.id')
key=$(echo -n "$WEEK-$id" | sha256sum | awk '{print $1}')
echo "$key $m"
done | sort | cut -d' ' -f2- | jq -s '.'
}
redirect_html() {
cat <<REOF
<!DOCTYPE html>
<html>
<head><meta http-equiv="refresh" content="0; url=$1"></head>
<body>Redirecting to <a href="$1">$2</a></body>
</html>
REOF
}
detect_domain() {
local text="$1"
if [[ "${text,,}" == *"umaring.mkr.cx"* ]]; then echo "mkr.cx"
elif [[ "${text,,}" == *"umaring.github.io"* ]]; then echo "github.io"
else echo "unknown"
fi
}
# outputs: status domain (two lines)
check_member() {
local url="$1" id="$2"
local html errtmp
errtmp=$(mktemp)
local http_code
html=$(curl -sL -A "$UA" --connect-timeout 5 --max-time 10 -w '\n%{http_code}' "$url" 2>"$errtmp")
http_code=$(echo "$html" | tail -1)
html=$(echo "$html" | sed '$d')
case "$http_code" in
2*|3*) ;;
403) rm -f "$errtmp"; printf "html\nunknown"; return ;;
*) echo " $id: HTTP $http_code ($url)" >&2; rm -f "$errtmp"; printf "offline\nunknown"; return ;;
esac
rm -f "$errtmp"
local scripts
scripts=$(echo "$html" | grep -oiE 'src=['"'"'"]?[^'"'"'" >]+' | sed "s/^src=['\"]\\?//")
for src in $scripts; do
if echo "$src" | grep -qiE "umaring\\.(mkr\\.cx|github\\.io)/${id}\\.js"; then
printf "member.js\n%s" "$(detect_domain "$src")"
return
fi
done
for src in $scripts; do
if echo "$src" | grep -qiE 'umaring\.(mkr\.cx|github\.io)/ring\.js'; then
printf "ring.js\n%s" "$(detect_domain "$src")"
return
fi
done
local jstmp=$(mktemp)
for src in $scripts; do
case "$src" in *.js|*.js\?*) ;; *) continue ;; esac
case "$src" in
http*) ;;
//*) src="https:$src" ;;
/*) src="${url%/}$src" ;;
*) src="${url%/}/$src" ;;
esac
curl -fL -A "$UA" --connect-timeout 5 --max-time 10 "$src" -o "$jstmp" 2>"$jstmp.err" || { echo " $id: $(cat "$jstmp.err") ($src)" >&2; rm -f "$jstmp.err"; continue; }
rm -f "$jstmp.err"
if grep -qi "umaring" "$jstmp"; then
printf "js\n%s" "$(detect_domain "$(cat "$jstmp")")"
rm -f "$jstmp"
return
fi
done
rm -f "$jstmp"
if echo "$html" | grep -qi "umaring"; then
printf "html\n%s" "$(detect_domain "$html")"
return
fi
printf "missing\nunknown"
}
index_page() {
local data="$1"
local date=$(date -u '+%Y-%m-%d %H:%M UTC')
cat <<'HEADER'
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>UMass Webring</title>
<style>
body { font-family: monospace; max-width: 640px; margin: 40px auto; padding: 0 20px; background: #111; color: #ccc; }
h1 { font-size: 1.4em; color: #fff; }
h2 { font-size: 1.1em; color: #fff; margin-top: 30px; }
p { line-height: 1.5; }
a { color: #8bf; }
table { border-collapse: collapse; width: 100%; }
td { padding: 4px 8px; }
.ok { color: #6e6; }
.warn { color: #fc6; }
.err { color: #f66; }
.tag { font-size: 0.85em; }
.note { color: #999; font-size: 0.9em; line-height: 1.6; }
.time { color: #666; font-size: 0.85em; margin-top: 20px; }
</style>
</head>
<body>
<h1>UMass Ring</h1>
<p>A webring for UMass Amherst students and alumni.
Want to join? Add yourself to
<a href="https://github.com/umaring/umaring" target="_blank">the repo</a>
and submit a PR.</p>
<h2>Members</h2>
<table>
HEADER
echo "$data" | jq -r '.[] |
def tag(cls; txt): " <span class=\"tag \(cls)\">\(txt)</span>";
def status_tag:
if .status == "ring.js" then tag("warn"; "ring.js")
elif .status == "missing" then tag("err"; "missing")
elif .status == "offline" then tag("err"; "offline")
else ""
end;
def domain_tag:
if .domain == "mkr.cx" then tag("warn"; "mkr.cx")
else ""
end;
"<tr><td><a href=\"\(.url)\" target=\"_blank\">\(.name)</a>\(status_tag)\(domain_tag)</td></tr>"'
cat <<'NOTES'
</table>
<h2>Migration notes</h2>
<p class="note">
<span class="warn">mkr.cx</span> — The <b>umaring.mkr.cx</b> domain is being
retired. Update your URLs to use <b>umaring.github.io</b> instead.<br><br>
<span class="warn">ring.js</span> — The shared <b>ring.js</b> script is
being replaced with per-member scripts. Switch from<br>
<code><script src=".../ring.js?id=you"></script></code> to<br>
<code><script src="https://umaring.github.io/<b>you</b>.js"></script></code><br><br>
Most members will need to do both at once.
</p>
NOTES
cat <<FOOTER
<img src="/umass.png" alt="UMass Ring" style="image-rendering: pixelated;">
<p class="time">Last checked: $date</p>
</body>
</html>
FOOTER
}
write_member_files() {
local OUT="$1" id="$2" member="$3" prev="$4" next="$5"
local prev_url=$(echo "$prev" | jq -r '.url')
local prev_name=$(echo "$prev" | jq -r '.name')
local next_url=$(echo "$next" | jq -r '.url')
local next_name=$(echo "$next" | jq -r '.name')
local data=$(jq -nc --argjson prev "$prev" --argjson member "$member" --argjson next "$next" \
'{prev: $prev, member: $member, next: $next}')
mkdir -p "$OUT/$id/prev" "$OUT/$id/next"
echo "$data" > "$OUT/$id/index.html"
echo "$data" > "$OUT/$id.json"
redirect_html "$prev_url" "$prev_name" > "$OUT/$id/prev/index.html"
redirect_html "$next_url" "$next_name" > "$OUT/$id/next/index.html"
local prev_json=$(echo "$prev" | jq -c '{id, name, url}')
local next_json=$(echo "$next" | jq -c '{id, name, url}')
sed -e "s|PREV_DATA_HERE|$prev_json|" -e "s|NEXT_DATA_HERE|$next_json|" member.js > "$OUT/$id.js"
}
# run check_member in parallel, collect results into $tmpdir/{0,1,...}
parallel_check() {
local shuffled="$1"
local len=$(echo "$shuffled" | jq 'length')
local tmpdir=$(mktemp -d)
for i in $(seq 0 $((len - 1))); do
local url=$(echo "$shuffled" | jq -r ".[$i].url")
local id=$(echo "$shuffled" | jq -r ".[$i].id")
( check_member "$url" "$id" > "$tmpdir/$i" ) &
done
wait
echo "$tmpdir"
}
check() {
local shuffled=$(shuffle)
local len=$(echo "$shuffled" | jq 'length')
local tmpdir=$(parallel_check "$shuffled")
for i in $(seq 0 $((len - 1))); do
local id=$(echo "$shuffled" | jq -r ".[$i].id")
local url=$(echo "$shuffled" | jq -r ".[$i].url")
local status=$(sed -n '1p' "$tmpdir/$i")
printf "%8s: %s (%s)\n" "$status" "$id" "$url"
done
rm -rf "$tmpdir"
}
check_domain() {
local shuffled=$(shuffle)
local len=$(echo "$shuffled" | jq 'length')
local tmpdir=$(parallel_check "$shuffled")
for i in $(seq 0 $((len - 1))); do
local id=$(echo "$shuffled" | jq -r ".[$i].id")
local url=$(echo "$shuffled" | jq -r ".[$i].url")
local domain=$(sed -n '2p' "$tmpdir/$i")
printf "%10s: %s (%s)\n" "$domain" "$id" "$url"
done
rm -rf "$tmpdir"
}
build() {
local OUT="out"
rm -rf "$OUT"
mkdir -p "$OUT"
local shuffled=$(shuffle)
local total=$(echo "$shuffled" | jq 'length')
echo "Checking members..." >&2
local tmpdir=$(parallel_check "$shuffled")
local alive="[]"
local statuses="[]"
for i in $(seq 0 $((total - 1))); do
local m=$(echo "$shuffled" | jq -c ".[$i]")
local url=$(echo "$m" | jq -r '.url')
local id=$(echo "$m" | jq -r '.id')
local name=$(echo "$m" | jq -r '.name')
local status=$(sed -n '1p' "$tmpdir/$i")
local domain=$(sed -n '2p' "$tmpdir/$i")
statuses=$(echo "$statuses" | jq -c --arg s "$status" --arg d "$domain" --arg name "$name" --arg url "$url" \
'. + [{status: $s, domain: $d, name: $name, url: $url}]')
case "$status" in
member.js|ring.js|js|html)
alive=$(echo "$alive" | jq -c --argjson m "$m" '. + [$m]')
;;
*)
echo " $status: $id ($url)" >&2
;;
esac
done
rm -rf "$tmpdir"
local len=$(echo "$alive" | jq 'length')
echo "$len/$total members alive" >&2
local first=$(echo "$alive" | jq -c '.[0]')
local last=$(echo "$alive" | jq -c '.[-1]')
mkdir -p "$OUT/health"
echo -n "OK" > "$OUT/health/index.html"
mkdir -p "$OUT/all"
echo "$alive" | jq -c '.' > "$OUT/all/index.html"
for i in $(seq 0 $((len - 1))); do
local member=$(echo "$alive" | jq -c ".[$i]")
local prev=$(echo "$alive" | jq -c ".[(($i - 1 + $len) % $len)]")
local next=$(echo "$alive" | jq -c ".[(($i + 1) % $len)]")
local id=$(echo "$member" | jq -r '.id')
write_member_files "$OUT" "$id" "$member" "$prev" "$next"
done
for i in $(seq 0 $((total - 1))); do
local m=$(echo "$shuffled" | jq -c ".[$i]")
local id=$(echo "$m" | jq -r '.id')
[ -d "$OUT/$id" ] && continue
write_member_files "$OUT" "$id" "$m" "$last" "$first"
done
local ring_data=$(echo "$alive" | jq -c '[.[] | {id, name, url}]')
sed "s|RING_DATA_HERE|$ring_data|" ring.js > "$OUT/ring.js"
index_page "$statuses" > "$OUT/index.html"
mkdir -p "$OUT/status"
index_page "$statuses" > "$OUT/status/index.html"
cp umass.png "$OUT/umass.png"
echo "Build complete: $OUT/"
}
case "${1:-build}" in
build) build ;;
check) check ;;
check_domain) check_domain ;;
*) echo "Usage: $0 [build|check|check_domain]" >&2; exit 1 ;;
esac