OILS / soil / web-worker.sh View on Github | oils.pub

414 lines, 187 significant
1#!/usr/bin/env bash
2#
3# Functions to invoke soil/web remotely.
4#
5# soil/web is deployed manually, and then this runs at HEAD in the repo. Every
6# CI run has an up-to-date copy.
7#
8# Usage:
9# soil/web-worker.sh <function name>
10
11set -o nounset
12set -o pipefail
13set -o errexit
14
15REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
16
17source soil/common.sh
18source test/tsv-lib.sh # tsv2html
19source web/table/html.sh # table-sort-{begin,end}
20
21# ~/
22# soil-web/ # executable files
23# doctools/
24# html_head.py
25# soil/
26# web.py
27# web.sh
28# travis-ci.oilshell.org/ # served over HTTP
29# index.html
30# web/
31# base.css
32# soil.css
33# github-jobs/
34# index.html
35# 3619/ # $GITHUB_RUN_NUMBER
36# dev-minimal.wwz
37# cpp-small.wwz
38# sourcehut-jobs/
39# index.html
40# 22/ # $JOB_ID
41# dev-minimal.wwz
42# 23 # $JOB_ID
43# cpp-small.wwz
44
45sshq() {
46 # Don't need commands module as I said here!
47 # https://www.oilshell.org/blog/2017/01/31.html
48 #
49 # This is Bernstein chaining through ssh.
50
51 my-ssh $SOIL_USER_HOST "$(printf '%q ' "$@")"
52}
53
54remote-rewrite-jobs-index() {
55 sshq soil-web/soil/web.sh rewrite-jobs-index "$@"
56}
57
58remote-cleanup-jobs-index() {
59 local prefix=$1
60 # clean it up for real!
61 sshq soil-web/soil/web.sh cleanup-jobs-index "$prefix" false
62}
63
64remote-cleanup-status-api() {
65 #sshq soil-web/soil/web.sh cleanup-status-api false
66 # 2024-07 - work around bug by doing dry_run only.
67 #
68 # TODO: Fix the logic in soil/web.sh
69
70 if false; then
71 sshq soil-web/soil/web.sh cleanup-status-api true
72 else
73 curl --include --fail-with-body \
74 --form 'run-hook=soil-cleanup-status-api' \
75 --form 'arg1=true' \
76 $WWUP_URL
77 fi
78}
79
80my-scp() {
81 scp -o StrictHostKeyChecking=no "$@"
82}
83
84my-ssh() {
85 ssh -o StrictHostKeyChecking=no "$@"
86}
87
88scp-status-api() {
89 local run_id=${1:-TEST2-github-run-id}
90 local job_name=$2
91
92 local status_file="_soil-jobs/$job_name.status.txt"
93 local remote_path="$SOIL_REMOTE_DIR/status-api/github/$run_id/$job_name"
94
95 # We could make this one invocation of something like:
96 # cat $status_file | sshq soil/web.sh PUT $remote_path
97
98 if false; then
99 my-ssh $SOIL_USER_HOST "mkdir -p $(dirname $remote_path)"
100
101 # the consumer should check if these are all zero
102 # note: the file gets RENAMED
103 my-scp $status_file "$SOIL_USER_HOST:$remote_path"
104 else
105 # Note: we don't need to change the name of the file, because we just glob
106 # the dir
107 curl --include --fail-with-body \
108 --form 'payload-type=status-api' \
109 --form "subdir=github/$run_id" \
110 --form "file1=@$status_file" \
111 $WWUP_URL
112 fi
113}
114
115scp-results() {
116 # could also use Travis known_hosts addon?
117 local prefix=$1 # sourcehut- or ''
118 shift
119
120 my-scp "$@" "$SOIL_USER_HOST:$SOIL_REMOTE_DIR/${prefix}jobs/"
121}
122
123# Dummy that doesn't depend on results
124deploy-test-wwz() {
125 set -x
126 local out_name="$(date +%Y-%m-%d__%H-%M-%S)_test"
127
128 local wwz=$out_name.wwz
129
130 cat >index.html <<EOF
131<a href="build/oil-manifest.txt">build/oil-manifest.txt</a> <br/>
132<a href="build/opy-manifest.txt">build/opy-manifest.txt</a> <br/>
133<a href="env.txt">env.txt</a> <br/>
134EOF
135
136 dump-env > env.txt
137
138 zip -q $wwz env.txt index.html build/*.txt
139
140 scp-results '' $wwz
141}
142
143format-wwz-index() {
144 ### What's displayed in $ID.wwz/index.html
145
146 local job_id=$1
147 local tsv=${2:-_tmp/soil/INDEX.tsv}
148
149 soil-html-head "$job_id.wwz" /uuu/web
150
151 cat <<EOF
152 <body class="width40">
153 <p id="home-link">
154 <a href="..">Up</a>
155 | <a href="/">Home</a>
156 | <a href="//oils.pub/">oils.pub</a>
157 </p>
158
159 <h1>$job_id.wwz</h1>
160EOF
161
162 echo '<ul>'
163 cat <<EOF
164 <li>
165 <a href="_tmp/soil/INDEX.tsv">_tmp/soil/INDEX.tsv</a>, also copied to
166 <a href="../$job_id.tsv">../$job_id.tsv</a>.
167 </li>
168 <li>
169 <a href="../$job_id.json">../$job_id.json</a>
170 </li>
171EOF
172
173 if test -f _tmp/soil/image.html; then
174 echo '
175 <li>
176 <a href="_tmp/soil/image.html">Container Image Stats</a>
177 </li>
178 '
179 fi
180
181 echo '</ul>'
182}
183
184format-image-stats() {
185 local soil_dir=${1:-_tmp/soil}
186 local web_base_url=${2:-'/web'} # for production
187
188 table-sort-html-head "Image Stats" $web_base_url
189
190 # prints <body>; make it wide for the shell commands
191 table-sort-begin "width60"
192
193 # TODO:
194 # - Format the TSV as an HTML table
195 # - Save the name and tag and show it
196
197 cat <<EOF
198 <p id="home-link">
199 <a href="/">Home</a>
200 | <a href="//oils.pub/">oils.pub</a>
201 </p>
202
203 <h1>Images Tagged</h1>
204
205 <a href="images-tagged.txt">images-tagged.txt</a> <br/>
206
207 <h1>Image Layers</h1>
208EOF
209
210 tsv2html3 $soil_dir/image-layers.tsv
211
212 # First column is number of bytes; ignore header
213 local total_bytes=$(awk '
214 { sum += $1 }
215 END { printf("%.1f", sum / 1000000) }
216 ' $soil_dir/image-layers.tsv)
217
218 echo "<p>Total Size: <b>$total_bytes MB</b></p>"
219
220
221 cat <<EOF
222 <h2>Raw Data</h2>
223
224 <a href="image-layers.txt">image-layers.txt</a> <br/>
225 <a href="image-layers.tsv">image-layers.tsv</a> <br/>
226EOF
227
228 table-sort-end image-layers
229}
230
231make-job-wwz() {
232 local job_id=${1:-test-job}
233
234 local wwz=$job_id.wwz
235
236 # Doesn't exist when we're not using a container
237 if test -f _tmp/soil/image-layers.tsv; then
238 format-image-stats _tmp/soil > _tmp/soil/image.html
239 fi
240
241 format-wwz-index $job_id > index.html
242
243 # _tmp/soil: Logs are in _tmp, see soil/worker.sh
244 # web/ : spec test HTML references this.
245 # Note that that index references /web/{base,soil}.css, outside the .wwz
246 # osh-summary.html uses table-sort.js and ajax.js
247 #
248 # TODO:
249 # - Could move _tmp/{spec,stateful,syscall} etc. to _test
250 # - Create _tmp/benchmarks/{compute,gc,gc-cachegrind,osh-parser,mycpp-examples,...}
251 # - would require release/$VERSION/pub/benchmarks.wwz, like we have
252 # pub/metrics.wwz, for consistent links
253
254 zip -q -r $wwz \
255 index.html \
256 _build/wedge/logs \
257 _gen/mycpp/examples \
258 _test \
259 _tmp/{soil,spec,src-tree-www,wild-www,stateful,process-table,syscall,benchmark-data,metrics,mycpp-examples,compute,gc,gc-cachegrind,perf,vm-baseline,osh-runtime,osh-parser,host-id,shell-id} \
260 _tmp/uftrace/{index.html,stage2} \
261 web/{base,src-tree,spec-tests,spec-cpp,line-counts,benchmarks,wild}.css web/ajax.js \
262 web/table/table-sort.{css,js} \
263 _release/oil*.tar _release/*.xshar _release/VERSION/
264}
265
266test-collect-json() {
267 soil/collect_json.py _tmp/soil PATH
268}
269
270deploy-job-results() {
271 ### Copy .wwz, .tsv, and .json to a new dir
272
273 local prefix=$1 # e.g. github- for example.com/github-jobs/
274 local run_dir=$2 # e.g. 1234 # make this dir
275 local job_name=$3 # e.g. cpp-small for example.com/github-jobs/1234/cpp-small.wwz
276 shift 2
277 # rest of args are more env vars
278
279 # writes $job_name.wwz
280 make-job-wwz $job_name
281
282 # Debug permissions. When using docker rather than podman, these dirs can be
283 # owned by root and we can't write into them.
284 ls -l -d _tmp/soil
285 ls -l _tmp/soil
286
287 date +%s > _tmp/soil/task-deploy-start-time.txt
288
289 soil/collect_json.py _tmp/soil "$@" > $job_name.json
290
291 # So we don't have to unzip it
292 cp _tmp/soil/INDEX.tsv $job_name.tsv
293
294 if false; then
295 local remote_dest_dir="$SOIL_REMOTE_DIR/${prefix}jobs/$run_dir"
296 my-ssh $SOIL_USER_HOST "mkdir -p $remote_dest_dir"
297
298 # Do JSON last because that's what 'list-json' looks for
299 my-scp $job_name.{wwz,tsv,json} "$SOIL_USER_HOST:$remote_dest_dir"
300 else
301 curl --include --fail-with-body \
302 --form "payload-type=${prefix}jobs" \
303 --form "subdir=$run_dir" \
304 --form "file1=@${job_name}.wwz" \
305 --form "file2=@${job_name}.tsv" \
306 --form "file3=@${job_name}.json" \
307 $WWUP_URL
308 fi
309
310 log ''
311 log 'View CI results here:'
312 log ''
313 log "https://$SOIL_HOST/uuu/${prefix}jobs/$run_dir/"
314 log "https://$SOIL_HOST/uuu/${prefix}jobs/$run_dir/$job_name.wwz/"
315 log ''
316}
317
318publish-cpp-tarball() {
319 local prefix=${1:-'github-'} # e.g. example.com/github-jobs/
320
321 # Example of dir structure we need to cleanup:
322 #
323 # sourcehut-jobs/
324 # git-$hash/
325 # index.html
326 # oils-for-unix.tar
327 # github-jobs/
328 # git-$hash/
329 # oils-for-unix.tar
330 #
331 # Algorithm
332 # 1. List all JSON, finding commit date and commit hash
333 # 2. Get the OLDEST commit dates, e.g. all except for 50
334 # 3. Delete all commit hash dirs not associated with them
335
336 if false; then
337 # Note: don't upload code without auth
338 # TODO: Move it to a different dir.
339
340 local commit_hash
341 commit_hash=$(cat _tmp/soil/commit-hash.txt)
342
343 local tar=_release/oils-for-unix.tar
344 curl --include --fail-with-body \
345 --form 'payload-type=github-jobs' \
346 --form "subdir=git-$commit_hash" \
347 --form "file1=@$tar" \
348 $WWUP_URL
349
350 log 'Tarball:'
351 log ''
352 log "https://$SOIL_HOST/code/github-jobs/git-$commit_hash/"
353
354 else
355 # Fix subtle problem here !!!
356 shopt -s inherit_errexit
357
358 local git_commit_dir
359 git_commit_dir=$(git-commit-dir "$prefix")
360
361 my-ssh $SOIL_USER_HOST "mkdir -p $git_commit_dir"
362
363 # Do JSON last because that's what 'list-json' looks for
364
365 local tar=_release/oils-for-unix.tar
366
367 # Permission denied because of host/guest issue
368 #local tar_gz=$tar.gz
369 #gzip -c $tar > $tar_gz
370
371 # Avoid race condition
372 # Crappy UUID: seconds since epoch, plus PID
373 local timestamp
374 timestamp=$(date +%s)
375
376 local temp_name="tmp-$timestamp-$$.tar"
377
378 my-scp $tar "$SOIL_USER_HOST:$git_commit_dir/$temp_name"
379
380 my-ssh $SOIL_USER_HOST \
381 "mv -v $git_commit_dir/$temp_name $git_commit_dir/oils-for-unix.tar"
382
383 log 'Tarball:'
384 log ''
385 log "https://$git_commit_dir"
386 fi
387
388}
389
390remote-event-job-done() {
391 ### "Client side" handler: a job calls this when it's done
392
393 local prefix=$1 # 'github-' or 'sourcehut-'
394 local run_id=$2 # $GITHUB_RUN_NUMBER or git-$hash
395
396 log "remote-event-job-done $prefix $run_id"
397
398 # Deployed code dir
399 if false; then
400 sshq soil-web/soil/web.sh event-job-done "$@"
401 else
402 # Note: I think curl does URL escaping of arg1= arg2= ?
403 curl --include --fail-with-body \
404 --form 'run-hook=soil-event-job-done' \
405 --form "arg1=$prefix" \
406 --form "arg2=$run_id" \
407 $WWUP_URL
408 fi
409}
410
411filename=$(basename $0)
412if test $filename = 'web-worker.sh'; then
413 "$@"
414fi