Repository navigation
Expand file tree
/
Copy pathlocksql
More file actions
executable file
·825 lines (798 loc) · 31.6 KB
/
Copy pathlocksql
File metadata and controls
executable file
·825 lines (798 loc) · 31.6 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
#!/bin/sh
# locksql: the launcher. locksql runs in Docker; this script, installed as
# `locksql` on the host by scripts/install.sh, runs every command in the
# image, each with the account and the volumes it needs (docs/docker.md):
#
# stats, forget as the console account, with the home volume of the
# workspace's locksql lock that `lox run` runs (found as
# the agent commands find its socket; --instance NAME
# picks one of several); without lox run, the volume
# locksql-home of a console started by hand
# (`locksql console`, up to v0.17), when there is one
# init, config as you, with your home: they write your agents' files
# and your user config
# profile as you, so that the files it writes are yours
# doctor --fix as root, which gives .locksql/ to the console account
# mcp, status, tables, describe, plan, run, request, logout, pii, doctor
# as the agent account (agent), with the socket volume of
# the workspace's locksql lock that `lox run` runs (read
# from .lockfamily/up.json and checked with Docker;
# --instance NAME picks one of several): none without
# lox run, but doctor then runs anyway and says why
# anything else as the agent account, with no socket: help...
#
# The console is started by `lox run` only (lockhub): `locksql console`
# says so and runs nothing.
#
# The project (the nearest directory up from here that holds .locksql/, or
# this directory) is mounted at its own path. Every container also mounts
# the company's managed directory, when this machine has one
# (/etc/lockfamily on Linux, /Library/Application Support/lockfamily on
# macOS), read-only on /etc/lockfamily, where lockkit reads managed.toml in
# the image, as lox run mounts it in its locks. `locksql update` updates this
# launcher and its image; `locksql version` prints both versions.
#
# Environment:
# LOCKSQL_IMAGE image to run (default: the release image of this
# launcher's version; `locksql` for a launcher run
# from a clone, the tag of `docker build -t locksql .`)
# LOCKSQL_VAULT=1 the image with the vault CLI (credentials = "vault")
# LOCKSQL_DOCKER_ARGS extra `docker run` arguments, split on spaces
# (e.g. --network shop)
set -eu
# scripts/install.sh and locksql update write the release version here,
# and the digests of its images once their cosign signatures verify: the
# launcher then runs the image by digest, never by a tag that could move.
launcher_version=dev
image_digest=
vault_image_digest=
repo="DavidGodefroid/locksql"
registry="ghcr.io/davidgodefroid/locksql"
say() { printf 'locksql: %s\n' "$*" >&2; }
die() { say "$*"; exit 1; }
image="${LOCKSQL_IMAGE:-}"
if [ -z "$image" ]; then
if [ "$launcher_version" = dev ]; then
image=locksql
else
image="$registry:$launcher_version"
fi
if [ "$launcher_version" != dev ] && [ -n "$image_digest" ]; then
image="$registry@$image_digest"
[ "${LOCKSQL_VAULT:-0}" = 1 ] && image="$registry@$vault_image_digest"
elif [ "${LOCKSQL_VAULT:-0}" = 1 ]; then
case "$image" in
*:*) image="$image-vault" ;;
*) image="$image:vault" ;;
esac
fi
fi
# The project root: the nearest directory up from here that holds .locksql/
# (or --project, below).
here="$(pwd -P)"
project="$here"
d="$here"
while :; do
if [ -d "$d/.locksql" ]; then
project="$d"
break
fi
[ "$d" = / ] && break
d="$(dirname "$d")"
done
# An explicit --project DIR (or --project=DIR) of profile, the command
# that takes it (it takes no positional argument; config's --project is
# a switch, and elsewhere it would be text such as SQL), is the project
# instead: mounted at its physical path, which the command gets in its
# place. "--" ends the options.
take=
explicit=
case "${1:-}" in
profile) scan=1 ;;
*) scan= ;;
esac
for a in "$@"; do
[ -n "$scan" ] || break
if [ -n "$take" ]; then
explicit=$a
take=
continue
fi
case "$a" in
--) break ;;
--project) take=1 ;;
--project=*) explicit=${a#--project=} ;;
esac
done
[ -z "$take" ] || die "--project: name the project directory"
if [ -n "$explicit" ]; then
case "$explicit" in /*) ;; *) explicit="$here/$explicit" ;; esac
project="$(cd "$explicit" 2>/dev/null && pwd -P)" || die "--project $explicit: no such directory"
n=$#
done_opts=
for a in "$@"; do
if [ -n "$done_opts" ]; then
set -- "$@" "$a"
continue
fi
if [ -n "$take" ]; then
a=$project
take=
fi
case "$a" in
--) done_opts=1 ;;
--project) take=1 ;;
--project=*) a="--project=$project" ;;
esac
set -- "$@" "$a"
done
shift "$n"
fi
tty=-i
if [ -t 0 ] && [ -t 1 ]; then tty=-it; fi
# The company-managed directory of this machine (lockkit's
# managed.DefaultPath): managed.toml, its signature bundle, signer.toml and
# lox's managed.toml.verified stamp. Every container mounts it read-only on
# /etc/lockfamily, the path lockkit reads in the (Linux) image, as lox run
# mounts it in its locks (lockhub#179): the commands see the bounds the
# console enforces. lockkit's ownership checks still apply in the
# container: a bind mount keeps the files' owner and mode.
case "$(uname -s)" in
Darwin) managed_dir="/Library/Application Support/lockfamily" ;;
*) managed_dir=/etc/lockfamily ;;
esac
# docker_run ARGS...: docker run with the project mounted at its own path,
# from this directory, then the managed directory, then ARGS (the account,
# the volumes, the image and its command). LOCKSQL_DOCKER_ARGS go before
# ARGS.
docker_run() {
docker_prep
# LOCKSQL_DOCKER_ARGS is split on spaces, never globbed.
set -f
# shellcheck disable=SC2086 # LOCKSQL_DOCKER_ARGS is split on purpose
exec docker run --rm -v "$project:$project" -w "$workdir" ${managed_mount:+-v} ${managed_mount:+"$managed_mount"} ${LOCKSQL_DOCKER_ARGS:-} "$@"
}
# docker_run_then ARGS...: docker_run, back in the launcher afterwards; it
# returns docker's exit status.
docker_run_then() {
docker_prep
set -f
# shellcheck disable=SC2086 # LOCKSQL_DOCKER_ARGS is split on purpose
docker run --rm -v "$project:$project" -w "$workdir" ${managed_mount:+-v} ${managed_mount:+"$managed_mount"} ${LOCKSQL_DOCKER_ARGS:-} "$@"
rc=$?
set +f
return "$rc"
}
# docker_prep: docker is there, the project and here can be mounted,
# $workdir is here when it is in the project, else the project, and
# $managed_mount is the managed directory's -v argument, empty without one.
docker_prep() {
command -v docker >/dev/null 2>&1 || die "docker is required: locksql runs in Docker (docs/docker.md)"
mountable "$project"
mountable "$here"
# No managed directory: no mount, no managed policy. One that is not a
# directory (a file, a dangling link) is refused, as lockkit refuses it
# on the host.
managed_mount=
if [ -e "$managed_dir" ] || [ -L "$managed_dir" ]; then
[ -d "$managed_dir" ] || die "$managed_dir is not a directory: locksql cannot mount the company-managed configuration"
mountable "$managed_dir"
managed_mount="$managed_dir:/etc/lockfamily:ro"
fi
case "$here" in
"$project" | "$project"/*) workdir=$here ;;
*) workdir=$project ;;
esac
}
# mountable PATH: PATH can be a docker -v source and target, which ":"
# separates.
mountable() {
case "$1" in
*:*) die "$1 holds a \":\", which docker -v cannot mount: move or rename it" ;;
esac
}
# has_flag FLAG ARGS...: whether the options in ARGS (up to "--") hold
# FLAG or FLAG=...
has_flag() {
f=$1
shift
for a in "$@"; do
case "$a" in
--) return 1 ;;
"$f" | "$f"=*) return 0 ;;
esac
done
return 1
}
# check_tag TAG: TAG is a release tag (it goes into URLs and into the
# launcher's text).
check_tag() {
printf '%s\n' "$1" | grep -Eqx 'v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.]+)?' ||
die "invalid version $1: a release tag, vX.Y.Z"
}
# verified_digest TAG: the digest of the release image registry:TAG, once
# cosign verifies its signature by the release workflow; empty when it does
# not verify.
verified_digest() {
cosign verify --certificate-identity "$identity" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
"$registry:$1" 2>/dev/null |
sed -n 's/.*"docker-manifest-digest":"\(sha256:[0-9a-f]\{64\}\)".*/\1/p' | sed -n 1p
}
# update [vX.Y.Z]: replace this launcher with a release's (the cosign
# signature of checksums.txt and the archive's checksum checked, as
# scripts/install.sh does), then pull that release's image.
update() {
if [ -n "${1:-}" ]; then
case "$1" in v*) check_tag "$1" ;; *) check_tag "v$1" ;; esac
fi
if command -v curl >/dev/null 2>&1; then
fetch() { curl -fsSL -o "$2" "$1"; }
latest() { curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/$repo/releases/latest"; }
elif command -v wget >/dev/null 2>&1; then
fetch() { wget -q -O "$2" "$1"; }
latest() { wget -q -S --spider "https://github.com/$repo/releases/latest" 2>&1 | sed -n 's/^ *[Ll]ocation: *//p' | tail -n 1 | tr -d '\r'; }
else
die "curl or wget is required"
fi
case "$(uname -s)" in
Linux) os=linux ;;
Darwin) os=darwin ;;
*) die "unsupported OS $(uname -s): Linux and macOS only (WSL 2 on Windows)" ;;
esac
case "$(uname -m)" in
x86_64 | amd64) arch=amd64 ;;
aarch64 | arm64) arch=arm64 ;;
*) die "unsupported architecture $(uname -m): amd64 and arm64 only" ;;
esac
tag="${1:-${LOCKSQL_VERSION:-}}"
if [ -z "$tag" ]; then
tag="$(latest)"
tag="${tag##*/}"
case "$tag" in
v[0-9]*) ;;
*) die "no published release found; name one: locksql update vX.Y.Z" ;;
esac
fi
case "$tag" in v*) ;; *) tag="v$tag" ;; esac
check_tag "$tag"
version="${tag#v}"
# The release workflow run for this very tag, as scripts/install.sh
# checks it: a tag moved to an older release's signed files (archive or
# image) does not verify.
identity="https://github.com/$repo/.github/workflows/release.yml@refs/tags/$tag"
if [ "$version" = "$launcher_version" ]; then
say "the launcher is already $tag"
else
self="$(command -v "$0" 2>/dev/null || printf '%s' "$0")"
archive="locksql_${version}_${os}_${arch}.tar.gz"
base="https://github.com/$repo/releases/download/$tag"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM
say "downloading $archive ($tag)"
fetch "$base/$archive" "$tmp/$archive" || die "download failed: $base/$archive"
fetch "$base/checksums.txt" "$tmp/checksums.txt" || die "download failed: checksums.txt"
if command -v cosign >/dev/null 2>&1; then
fetch "$base/checksums.txt.sigstore.json" "$tmp/checksums.txt.sigstore.json" ||
die "download failed: checksums.txt.sigstore.json"
cosign verify-blob --bundle "$tmp/checksums.txt.sigstore.json" \
--certificate-identity "$identity" \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
"$tmp/checksums.txt" >/dev/null 2>&1 || die "cosign: signature of checksums.txt does not verify"
say "cosign: checksums.txt signature verified"
elif [ "${LOCKSQL_SKIP_COSIGN:-0}" = 1 ]; then
say "WARNING: LOCKSQL_SKIP_COSIGN=1: the signature of checksums.txt is NOT checked; the checksum only detects corruption"
else
die "cosign is required to check the release's signature (https://docs.sigstore.dev/cosign/system_config/installation/); LOCKSQL_SKIP_COSIGN=1 updates without it"
fi
want="$(awk -v f="$archive" '$2 == f { print $1 }' "$tmp/checksums.txt")"
[ -n "$want" ] || die "$archive is not listed in checksums.txt"
if command -v sha256sum >/dev/null 2>&1; then
got="$(sha256sum "$tmp/$archive" | awk '{ print $1 }')"
else
got="$(shasum -a 256 "$tmp/$archive" | awk '{ print $1 }')"
fi
[ "$want" = "$got" ] || die "checksum mismatch for $archive"
tar -xzf "$tmp/$archive" -C "$tmp" scripts/locksql 2>/dev/null ||
die "$archive holds no launcher (scripts/locksql): $tag predates the Docker launcher"
digest= vdigest=
if command -v cosign >/dev/null 2>&1; then
digest="$(verified_digest "$version")"
vdigest="$(verified_digest "$version-vault")"
[ -n "$digest" ] && [ -n "$vdigest" ] && [ "$digest" != "$vdigest" ] ||
die "cosign: the signature of $registry:$version (and -vault) does not verify, or the image is not published yet"
say "cosign: image $registry:$version verified ($digest)"
else
say "WARNING: LOCKSQL_SKIP_COSIGN=1: the image is NOT verified and runs by its tag"
fi
sed -e "s/^launcher_version=dev\$/launcher_version=$version/" \
-e "s/^image_digest=\$/image_digest=$digest/" \
-e "s/^vault_image_digest=\$/vault_image_digest=$vdigest/" \
"$tmp/scripts/locksql" >"$tmp/launcher"
grep -qx "launcher_version=$version" "$tmp/launcher" || die "the launcher of $tag does not take its version"
if [ -w "$self" ]; then
install -m 0755 "$tmp/launcher" "$self"
else
command -v sudo >/dev/null 2>&1 || die "$self is not writable and sudo is missing"
say "replacing $self with sudo"
sudo install -m 0755 "$tmp/launcher" "$self"
fi
say "launcher updated to $tag ($self)"
registry_image="$registry:$version"
[ -n "$digest" ] && registry_image="$registry@$digest"
[ "${LOCKSQL_VAULT:-0}" = 1 ] && registry_image="$registry:$version-vault"
[ "${LOCKSQL_VAULT:-0}" = 1 ] && [ -n "$vdigest" ] && registry_image="$registry@$vdigest"
[ -n "${LOCKSQL_IMAGE:-}" ] && say "LOCKSQL_IMAGE is set ($LOCKSQL_IMAGE): it still overrides $registry_image"
image="$registry_image"
fi
command -v docker >/dev/null 2>&1 || die "docker is required: locksql runs in Docker (docs/docker.md)"
say "pulling $image"
exec docker pull "$image"
}
# Locks started by `lox run` (lockhub's docs/lox-run.md): `lox run` runs each
# lock's console under compose, with its socket in the volume
# lockfamily-<sha256 of the workspace, 8 hex>_<instance>-run, and names it
# in <workspace>/.lockfamily/up.json. The agent commands mount that volume
# read-only on /run/locksql (connecting needs no write), never a fixed one:
# the consoles run hub-attached only, started by `lox run`. The checks are
# those of lea, which mounts the same volumes for its agents: up.json is
# the user's own regular file (no link) that no one else can write, in a
# .lockfamily of the same kind, of this very workspace; then Docker must
# confirm the volume: lox run's labels (project, role socket, instance,
# type, workspace), and every container that mounts it is lox run's (run
# by compose from lox run's own state directory) or mounts it read-only.
# These checks catch mistakes and files others can write; an agent that
# runs as you with Docker access gets past any of them (docs/launcher.md).
# up.json only names the instance:
# what is mounted is what Docker's labels prove, read as JSON, compared
# whole; anything else fails closed.
# json_safe S: S holds nothing JSON escapes (a quote, a backslash, a
# control character, U+2028, U+2029): "S" is then its JSON encoding.
json_safe() {
case "$1" in *'"'* | *'\'* | *"$(printf '\342\200\250')"* | *"$(printf '\342\200\251')"*) return 1 ;; esac
[ "$(printf '%s' "$1" | LC_ALL=C tr -d '\001-\037')" = "$1" ]
}
# private PATH: PATH is no link, is the user's, and neither its group nor
# others may write it.
private() {
[ ! -L "$1" ] && [ "$(ls -nd "$1" | awk '{ print $3 }')" = "$(id -u)" ] &&
[ -z "$(find "$1" -prune \( -perm -0020 -o -perm -0002 \) -print)" ]
}
# sha8 S: the first 8 hex digits of the sha256 of S (lox run's project).
sha8() {
if command -v sha256sum >/dev/null 2>&1; then
printf '%s' "$1" | sha256sum
else
printf '%s' "$1" | shasum -a 256
fi | awk '{ print substr($1, 1, 8) }'
}
# lockup_parse < up.json: lox run's state (version 1, written by Go's
# json.MarshalIndent with a two-space indent): a line Pproject, a line
# Wworkspace, then a line Lname socket_volume client_gid per lock of type
# locksql. Fails on anything else, or on a value it would have to unescape.
lockup_parse() {
awk -v want=locksql '
function str(v) {
if (v !~ /^"[^"\\]*"$/ || v ~ /[\001-\037]/) { bad = 1; return "" }
return substr(v, 2, length(v) - 2)
}
{ line = $0 }
NR == 1 { if (line != "{") bad = 1; next }
{ last = line; match(line, /^ */); ind = RLENGTH; rest = substr(line, ind + 1) }
rest == "}" || rest == "}," {
if (inlocks && ind == 4) cur = ""
else if (inlocks && ind == 2) inlocks = 0
next
}
rest !~ /^"[^"\\]*": / { next }
{
k = substr(rest, 2, index(rest, "\":") - 2)
v = substr(rest, length(k) + 5)
sub(/,$/, "", v)
}
!inlocks && ind == 2 {
if (k == "version") version = v
else if (k == "project") project = str(v)
else if (k == "workspace") workspace = str(v)
else if (k == "locks") { if (v == "{") inlocks = 1; else if (v != "{}") bad = 1; locks = 1 }
next
}
inlocks && ind == 4 {
if (v != "{" || (k in seen)) bad = 1
seen[k] = 1; cur = k; n++; name[n] = k
next
}
inlocks && ind == 6 && cur != "" {
if (k == "type" || k == "socket_volume" || k == "client_gid") f[cur, k] = str(v)
}
END {
if (bad || last != "}" || inlocks || !locks || version != "1") exit 1
print "P" project
print "W" workspace
for (i = 1; i <= n; i++) {
c = name[i]
if (f[c, "type"] != want) continue
if (c !~ /^[a-z0-9][a-z0-9-]*$/ || length(c) > 63 || f[c, "socket_volume"] !~ /^[a-z0-9][a-z0-9_.-]*$/ || f[c, "client_gid"] !~ /^[0-9]*$/) exit 1
print "L" c " " f[c, "socket_volume"] " " f[c, "client_gid"]
}
}'
}
# lockup_volume [INSTANCE]: the socket volume of the locksql lock (INSTANCE,
# the only one without it) that `lox run` runs for the workspace of this
# directory, in $lockup_vol; when there is none the agents can trust, why,
# with what to do, in $lockup_why.
lockup_volume() {
lockup_vol= lockup_why= lockup_none=
lu_inst=${1:-}
# The workspace: the nearest directory up from here whose .lockfamily
# holds up.json (a sub-project's own .lockfamily may hold none).
lu_ws=
lu_d=$here
while :; do
if [ -e "$lu_d/.lockfamily/up.json" ] || [ -L "$lu_d/.lockfamily/up.json" ] || [ -L "$lu_d/.lockfamily" ]; then
lu_ws=$lu_d
break
fi
[ "$lu_d" = / ] && break
lu_d=$(dirname "$lu_d")
done
if [ -z "$lu_ws" ]; then
lockup_why="no .lockfamily/up.json in $here or above it: start the workspace's locks with \`lox run\` (lockhub), then run locksql from the workspace"
lockup_none=1
return 1
fi
# A sub-project with its own .locksql/config.toml below the workspace:
# its clients key their socket on the sub-project, lox run's console on
# the workspace, so none of them would reach the console.
lu_d=$here
while [ "$lu_d" != "$lu_ws" ]; do
if [ -f "$lu_d/.locksql/config.toml" ]; then
lockup_why="$lu_d has its own .locksql/ below the workspace $lu_ws: lox run's console serves the workspace's .locksql only; run locksql from $lu_ws, or make $lu_d a workspace of its own (lox init there)"
return 1
fi
lu_d=$(dirname "$lu_d")
done
lu_lf=$lu_ws/.lockfamily
lu_f=$lu_lf/up.json
if [ -L "$lu_lf" ] || [ ! -d "$lu_lf" ]; then
lockup_why="$lu_lf is not a directory (a link?): it is not lox run's; run \`lox run\`"
return 1
fi
if ! private "$lu_lf"; then
lockup_why="$lu_lf is not yours, or others than you may write it: locksql trusts none of it (chmod go-w it, then run \`lox run\`)"
return 1
fi
if [ -L "$lu_f" ] || [ ! -f "$lu_f" ]; then
lockup_why="$lu_f is not a regular file (a link?): it is not lox run's; run \`lox run\`"
return 1
fi
if ! private "$lu_f"; then
lockup_why="$lu_f is not yours, or others than you may write it: locksql trusts none of it; run \`lox run\`"
return 1
fi
if [ "$(wc -c <"$lu_f")" -gt 1048576 ]; then
lockup_why="$lu_f is larger than 1 MiB: it is not lox run's; run \`lox run\`"
return 1
fi
lu_ino=$(ls -di "$lu_f")
if ! lu_state=$(lockup_parse <"$lu_f") || [ -L "$lu_f" ] || [ "$(ls -di "$lu_f")" != "$lu_ino" ]; then
# Go's json.MarshalIndent writes &, < and > as &, < and
# >, which lockup_parse never unescapes.
if grep -Eq '\\u00(26|3[cCeE])' "$lu_f"; then
lockup_why="the workspace's path holds &, < or >, which lox run's state escapes: locksql cannot check it; move or rename the workspace"
else
lockup_why="$lu_f is not lox run's state (version 1): run \`lox run\`"
fi
return 1
fi
lu_project= lu_wsf= lu_locks=
while IFS= read -r lu_line; do
case "$lu_line" in
P*) lu_project=${lu_line#P} ;;
W*) lu_wsf=${lu_line#W} ;;
L*) lu_locks="$lu_locks${lu_line#L}
" ;;
esac
done <<EOF_STATE
$lu_state
EOF_STATE
lu_want="lockfamily-$(sha8 "$lu_ws")"
if [ "$lu_project" != "$lu_want" ] || [ "$lu_wsf" != "$lu_ws" ]; then
lockup_why="$lu_f: project $lu_project of $lu_wsf, not this workspace's ($lu_want of $lu_ws): run \`lox run\` in $lu_ws"
return 1
fi
if ! json_safe "$lu_ws"; then
lockup_why="the workspace's path $lu_ws holds a quote, a backslash or a control character: locksql cannot check lox run's labels; move or rename it"
return 1
fi
lu_names=$(printf '%s' "$lu_locks" | awk '{ print $1 }' | sort | tr '\n' ' ')
lu_names=${lu_names% }
if [ -n "$lu_inst" ]; then
case "$lu_inst" in
*[!a-z0-9-]* | -*)
lockup_why="--instance: not an instance name (lowercase letters, digits and -)"
return 1
;;
esac
lu_sel=$(printf '%s' "$lu_locks" | awk -v n="$lu_inst" '$1 == n')
if [ -z "$lu_sel" ]; then
lockup_why="lox run runs no locksql lock $lu_inst in $lu_ws (its locksql locks: ${lu_names:-none}): name one with --instance, or add it (lox add locksql $lu_inst) and run \`lox run\`"
return 1
fi
else
case "$lu_names" in
"")
lockup_why="lox run runs no locksql lock in $lu_ws: add one (lox add locksql <name>), then run \`lox run\`"
return 1
;;
*" "*)
lockup_why="lox run runs several locksql locks in $lu_ws ($lu_names): name one with --instance NAME"
lockup_several=$lu_names
return 1
;;
esac
lu_sel=$lu_locks
fi
# shellcheck disable=SC2086 # name, volume and group: checked words
set -- $lu_sel
lu_name=$1 lu_sock=$2 lu_gid=${3:-}
lu_vol=${lu_project}_$lu_name-run
if [ "$lu_sock" != "$lu_vol" ]; then
lockup_why="$lu_f: lock $lu_name: socket volume $lu_sock is not lox run's $lu_vol: run \`lox run\`"
return 1
fi
if [ -n "$lu_gid" ] && [ "$lu_gid" != 10000 ]; then
lockup_why="lock $lu_name: clients group $lu_gid, this image's is 10000: update locksql (locksql update) or the lock's image"
return 1
fi
lu_got=$(docker volume inspect --format '{{json (index .Labels "com.docker.compose.project")}}
{{json (index .Labels "com.docker.compose.volume")}}
{{json (index .Labels "dev.lockfamily.role")}}
{{json (index .Labels "dev.lockfamily.instance")}}
{{json (index .Labels "dev.lockfamily.type")}}
{{json (index .Labels "dev.lockfamily.workspace")}}' "$lu_vol" 2>/dev/null) || {
lockup_why="lock $lu_name: no volume $lu_vol: run \`lox run\`"
return 1
}
if [ "$lu_got" != "\"$lu_project\"
\"$lu_name-run\"
\"socket\"
\"$lu_name\"
\"locksql\"
\"$lu_ws\"" ]; then
lockup_why="lock $lu_name: volume $lu_vol is not lox run's socket volume of it (its labels): locksql trusts none of it; run \`lox run\`"
return 1
fi
# Every container that mounts the volume, running or not, must be lox
# up's own or unable to serve a socket there. lox run's: compose ran it
# from lox run's state directory, with the file on stdin or that
# directory's compose.yaml (compose's project label alone is anyone's: a
# compose file written in the workspace, run by you, would serve the
# socket from an image of its own). Any other container, compose's or
# not, must mount the volume read-only (an agent's, as this one): with
# a read-write mount it could listen on the socket's path.
lu_state_dir=$(printf '%s' "${XDG_STATE_HOME:-${HOME:-}/.local/state}/lockfamily/$lu_project" | sed -e 's#//*#/#g')
if ! json_safe "$lu_state_dir"; then
lockup_why="lox run's state directory $lu_state_dir holds a quote, a backslash or a control character: locksql cannot check lox run's containers"
return 1
fi
lu_ids=$(docker ps -a --no-trunc --filter "volume=$lu_vol" --format '{{.ID}}' 2>/dev/null) || {
lockup_why="lock $lu_name: cannot list the containers of $lu_vol (docker ps)"
return 1
}
if [ -n "$lu_ids" ] && printf '%s\n' "$lu_ids" | grep -vqx '[0-9a-f]\{64\}'; then
lockup_why="lock $lu_name: cannot read the containers of $lu_vol (docker ps)"
return 1
fi
for lu_id in $lu_ids; do
# Compose's project, working directory and files, as JSON; then
# rw or ro for each mount of the volume (its name: checked
# characters).
lu_c=$(docker inspect --type container -f '{{json (index .Config.Labels "com.docker.compose.project")}}
{{json (index .Config.Labels "com.docker.compose.project.working_dir")}}
{{json (index .Config.Labels "com.docker.compose.project.config_files")}}
{{range .Mounts}}{{if eq .Name "'"$lu_vol"'"}}{{if .RW}}rw {{else}}ro {{end}}{{end}}{{end}}' "$lu_id" 2>/dev/null) || {
lockup_why="lock $lu_name: cannot read the containers of $lu_vol (docker inspect $lu_id)"
return 1
}
lu_rw=$(printf '%s\n' "$lu_c" | sed -n 4p)
lu_c=$(printf '%s\n' "$lu_c" | sed -n 1,3p)
if [ "$lu_c" = "\"$lu_project\"
\"$lu_state_dir\"
\"-\"" ] || [ "$lu_c" = "\"$lu_project\"
\"$lu_state_dir\"
\"$lu_state_dir/compose.yaml\"" ]; then
continue # lox run's
fi
# Read-only mounts only (none listed: not one docker shows by name).
case " $lu_rw" in
" " | *" rw "*)
lockup_why="lock $lu_name: volume $lu_vol is mounted read-write by container $lu_id, which lox run did not start (not compose run from $lu_state_dir): locksql trusts none of its socket; remove that container, then run \`lox run\`"
return 1
;;
esac
done
lockup_vol=$lu_vol
}
lockup_vol= lockup_why= lockup_none=
# lockup_home: the home volume of the locksql lock (--instance, the only one
# without it) that `lox run` runs for this workspace, in $home_vol: once
# its socket volume passes lockup_volume's checks, lox run's compose
# volume <project>_<instance>-home, whose compose labels Docker must
# confirm (lox run gives it no others). Without lox run's state at all
# (no .lockfamily/up.json), the volume locksql-home that a console started
# by hand (`locksql console`, up to v0.17) used, when there is one: never a
# new one. Else why, and exit 2.
lockup_home() {
command -v docker >/dev/null 2>&1 || die "docker is required: locksql runs in Docker (docs/docker.md)"
mountable "$project"
mountable "$here"
if ! lockup_volume "$instance"; then
if [ -n "$lockup_none" ] && docker volume inspect locksql-home >/dev/null 2>&1; then
say "no lox run in this workspace: the volume locksql-home of a console started by hand (locksql console, up to v0.17)"
home_vol=locksql-home
return
fi
say "$lockup_why"
exit 2
fi
home_vol=${lockup_vol%-run}-home
lh_name=${home_vol#"${lu_project}_"}
lh_got=$(docker volume inspect --format '{{json (index .Labels "com.docker.compose.project")}}
{{json (index .Labels "com.docker.compose.volume")}}' "$home_vol" 2>/dev/null) || {
say "lock $lu_name: no volume $home_vol: run \`lox run\`"
exit 2
}
if [ "$lh_got" != "\"$lu_project\"
\"$lh_name\"" ]; then
say "lock $lu_name: volume $home_vol is not lox run's home volume of it (its labels): locksql trusts none of it; run \`lox run\`"
exit 2
fi
}
# --instance NAME (or --instance=NAME): the launcher's own option of the
# agent commands, which of lox run's locksql locks when the workspace has
# several (stats and forget too: whose home). Taken out of the command line
# ("--" ends the options).
instance=
case "${1:-}" in
stats | forget | mcp | doctor | status | tables | describe | plan | run | request | logout | pii)
n=$#
take=
done_opts=
for a in "$@"; do
if [ -n "$done_opts" ]; then
set -- "$@" "$a"
continue
fi
if [ -n "$take" ]; then
instance=$a
take=
continue
fi
case "$a" in
--) done_opts=1 ;;
--instance)
take=1
continue
;;
--instance=*)
instance=${a#--instance=}
continue
;;
esac
set -- "$@" "$a"
done
shift "$n"
[ -z "$take" ] || die "--instance: name the lock (lox run's instance)"
;;
esac
# lockup_mount: lox run's socket volumes of this workspace's locksql locks,
# as the agent commands mount them ($mounts, docker run options), or the
# reason there are none (exit 2, as a client finding no console). One lock,
# or the one --instance names, is mounted on /run/locksql. Several and no
# --instance: each, checked as one is, on /run/locksql/<instance>, and
# their names in $instances (LEA_LOCKSQL_INSTANCES), as lea mounts them;
# the client reaches each profile on the lock that serves it.
lockup_mount() {
command -v docker >/dev/null 2>&1 || die "docker is required: locksql runs in Docker (docs/docker.md)"
mountable "$project"
mountable "$here"
lockup_several= instances=
if lockup_volume "$instance"; then
mounts="--mount type=volume,src=$lockup_vol,dst=/run/locksql,readonly"
return
fi
case "$lockup_several" in
"" | *[!a-z0-9\ -]*)
say "$lockup_why"
exit 2
;;
esac
mounts=
lm_names=$lockup_several
for lm_name in $lm_names; do
lockup_volume "$lm_name" || {
say "$lockup_why"
exit 2
}
mounts="$mounts --mount type=volume,src=$lockup_vol,dst=/run/locksql/$lm_name,readonly"
done
instances=$lm_names
}
cmd="${1:-}"
case "$cmd" in
update)
shift
update "$@"
;;
version | --version)
printf 'locksql launcher %s (image %s)\n' "$launcher_version" "$image"
docker_run "$image" version
;;
console)
# No agent command mounts a console's socket but lox run's: a console
# started here would be one no client reaches.
say "the console is started by lox run only (lockhub): run lox run in the workspace"
exit 2
;;
stats | forget)
# The console account's state: the lock's home volume, as lox run
# mounts it on the console.
lockup_home
docker_run "$tty" --user locksql --mount "type=volume,src=$home_vol,dst=/home/locksql" "$image" "$@"
;;
init | config)
# init also writes the agents' own files in your home (~/.codex, ...);
# config exports and imports your user config there.
mountable "$HOME"
docker_run "$tty" --user "$(id -u):$(id -g)" -e HOME="$HOME" -v "$HOME:$HOME" "$image" "$@"
;;
profile)
docker_run "$tty" --user "$(id -u):$(id -g)" -e HOME=/tmp "$image" "$@"
;;
doctor)
# The project's .locksql must be yours, as lox run reads it: doctor
# checks it for you, whichever account it runs as.
me="$(id -u):$(id -g)"
if has_flag --fix "$@"; then
mountable "$HOME"
# Entries of .locksql that are not yours (the console account's,
# given to it by doctor --fix up to v0.13.0): lox run refuses them,
# and only root can give them back. Root makes that repair only,
# asking first unless --yes, in a container of its own, and only
# when there is one; its text goes to stderr.
if [ -e "$project/.locksql" ] && [ -n "$(find "$project/.locksql" ! -user "$(id -u)" -print 2>/dev/null)" ]; then
root_tty=
[ "$tty" != -it ] || root_tty=-it
# shellcheck disable=SC2086 # no argument when empty
docker_run_then $root_tty --user 0 -e LOCKSQL_DOCTOR_FOR="$me" "$image" "$@" >&2 || :
fi
# The checks and the other repairs as you: your agents' wiring in
# your home, .locksql's modes. In the clients group only for the
# socket directory, where no console's socket is mounted: the
# console checks say it is not running.
docker_run "$tty" --user "$me" --group-add locksql-clients -e HOME="$HOME" -e USER="$(id -un 2>/dev/null || id -u)" \
-e LOCKSQL_DOCTOR_FOR="$me" -v "$HOME:$HOME" "$image" "$@"
fi
# The checks run without lox run's socket too: then they say so.
if command -v docker >/dev/null 2>&1 && lockup_volume "$instance"; then
docker_run "$tty" --mount "type=volume,src=$lockup_vol,dst=/run/locksql,readonly" -e LOCKSQL_DOCTOR_FOR="$me" "$image" "$@"
fi
[ -z "$lockup_why" ] || say "$lockup_why"
docker_run "$tty" -e LOCKSQL_DOCTOR_FOR="$me" "$image" "$@"
;;
mcp)
# The agent speaks MCP on stdin and stdout: no terminal.
lockup_mount
# shellcheck disable=SC2086 # $mounts: checked words, split on purpose
docker_run -i $mounts ${instances:+-e "LEA_LOCKSQL_INSTANCES=$instances"} "$image" "$@"
;;
status | tables | describe | plan | run | request | logout | pii)
lockup_mount
# shellcheck disable=SC2086 # $mounts: checked words, split on purpose
docker_run "$tty" $mounts ${instances:+-e "LEA_LOCKSQL_INSTANCES=$instances"} "$image" "$@"
;;
*)
docker_run "$tty" "$image" "$@"
;;
esac