demo / network-computer
Public
Copy of github.com/askrobots/network-computer (MIT): a desk you reach from anything. The original lives on GitHub.
GitStable demo
README: say this is a copy and where the original is
cde82c2
14 hours, 3 minutes ago
History
nc-object-bootstrap · 6.0 KB · 114 lines
| 1 | #!/bin/sh |
| 2 | # Make the desk's object server ready for voice: the desk user exists, their |
| 3 | # AI keys (from the desk's secrets file) are stored as the user's service keys, |
| 4 | # and the app packages are installed. Run by apply.sh as root; safe to repeat. |
| 5 | # Key values are read from the file and passed on stdin, never printed. |
| 6 | set -e |
| 7 | . /etc/nc/env |
| 8 | . /etc/nc/object-server.env |
| 9 | OS=/opt/dbbasic-object-server; PY=$OS/.venv/bin/python |
| 10 | U=$NC_DESK_USER |
| 11 | asuser() { runuser -u "$U" -- env DBBASIC_DATA_DIR="$DBBASIC_DATA_DIR" DBBASIC_OBJECTS_DIR="$DBBASIC_OBJECTS_DIR" "$@"; } |
| 12 | cd "$OS" |
| 13 | for i in $(seq 30); do curl -sf http://127.0.0.1:8001/health >/dev/null && break; sleep 1; done |
| 14 | |
| 15 | if ! asuser "$PY" object_identity_cli.py list-users --json 2>/dev/null | grep -q "\"$U\""; then |
| 16 | head -c 24 /dev/urandom | base64 | asuser "$PY" object_identity_cli.py create-superuser --user-id "$U" --display-name "$U" --password-stdin >/dev/null |
| 17 | echo " object server user $U created" |
| 18 | fi |
| 19 | |
| 20 | SECRETS="$(getent passwd "$U" | cut -d: -f6)/.config/dbbasic/ai.env" # the dbbasic apps' key file |
| 21 | if [ -r "$SECRETS" ]; then |
| 22 | asuser "$PY" - "$U" "$SECRETS" <<'PYEOF' |
| 23 | import sys, object_service_keys, os |
| 24 | user, path = sys.argv[1], sys.argv[2] |
| 25 | names = {"ANTHROPIC_API_KEY": "anthropic", "OPENAI_API_KEY": "openai"} |
| 26 | done = [] |
| 27 | for line in open(path): |
| 28 | k, _, v = line.strip().partition("=") |
| 29 | if k in names and v.strip(): |
| 30 | object_service_keys.set_service_key(user, names[k], v.strip().strip('"\''), |
| 31 | base_dir=os.environ["DBBASIC_DATA_DIR"]) |
| 32 | done.append(names[k]) |
| 33 | print(" keys stored for:", ", ".join(done) or "none") |
| 34 | PYEOF |
| 35 | fi |
| 36 | |
| 37 | for p in app-theme app-views app-nav app-shell app-files app-projects app-collab app-notes app-tasks app-notify app-settings app-sites; do |
| 38 | code=$(curl -s -o /dev/null -w '%{http_code}' -X POST -H "Authorization: Token $DBBASIC_ADMIN_TOKEN" "http://127.0.0.1:8001/packages/$p/install") |
| 39 | echo " package $p: $code" |
| 40 | done |
| 41 | |
| 42 | # the key ~/Objects signs in with (nc-object-files): minted once and kept |
| 43 | # root-only, on the desk volume when there is one so a new computer reuses it; |
| 44 | # minted again only when it stops working (revoked, or a fresh data dir). |
| 45 | # Passed to curl on stdin, never on a command line or printed. |
| 46 | DAVSEC=/etc/nc/davfs2.secrets; DAVURL=http://127.0.0.1:8001/dav/files/ |
| 47 | if [ -d /desk/nc ]; then |
| 48 | [ -e /desk/nc/davfs2.secrets ] || { [ -f $DAVSEC ] && [ ! -L $DAVSEC ] && mv $DAVSEC /desk/nc/davfs2.secrets; } || true |
| 49 | ln -sfn /desk/nc/davfs2.secrets $DAVSEC |
| 50 | fi |
| 51 | davok() { |
| 52 | [ -s $DAVSEC ] || return 1 |
| 53 | code=$(awk -v u="$U" '{printf "user = \"%s:%s\"\n", u, $3}' $DAVSEC | |
| 54 | curl -s -o /dev/null -w '%{http_code}' -K - -X PROPFIND -H 'Depth: 0' $DAVURL) |
| 55 | [ "$code" = 207 ] |
| 56 | } |
| 57 | if ! davok; then |
| 58 | KEY=$(curl -s -X POST -H "Authorization: Token $DBBASIC_ADMIN_TOKEN" -H 'Content-Type: application/json' \ |
| 59 | -d '{"name":"desk folder (~/Objects)"}' "http://127.0.0.1:8001/identity/users/$U/api-keys" | |
| 60 | "$PY" -c 'import json,sys; print(json.load(sys.stdin).get("token",""))') |
| 61 | if [ -n "$KEY" ]; then |
| 62 | (umask 077; printf '%s %s %s\n' "$DAVURL" "$U" "$KEY" > "$(readlink -f $DAVSEC)") |
| 63 | davok && echo " ~/Objects key minted" || echo " !! ~/Objects key minted but refused (is DBBASIC_ENABLE_WEBDAV on?)" |
| 64 | else |
| 65 | echo " !! could not mint the ~/Objects key" |
| 66 | fi |
| 67 | unset KEY |
| 68 | fi |
| 69 | |
| 70 | # the object server for the desk user's dbbasic apps (Webmaster publishes |
| 71 | # there): ~/.config/dbbasic/object-server.env, 0600, the user's; its own API key, |
| 72 | # apart from ~/Objects' root-only one, minted again only if it stops working |
| 73 | UH=$(getent passwd "$U" | cut -d: -f6); OSF="$UH/.config/dbbasic/object-server.env" |
| 74 | oskey=$(sed -n 's/^DBBASIC_OBJECT_SERVER_KEY=//p' "$OSF" 2>/dev/null || true) |
| 75 | code=none # (not "$( [ -n ] && ...)": under set -e an empty key would end the script) |
| 76 | if [ -n "$oskey" ]; then |
| 77 | code=$(printf 'header = "Authorization: Bearer %s"\n' "$oskey" | |
| 78 | curl -s -o /dev/null -w '%{http_code}' -K - -X PROPFIND -H 'Depth: 0' $DAVURL) |
| 79 | fi |
| 80 | if [ "$code" != 207 ]; then |
| 81 | KEY=$(curl -s -X POST -H "Authorization: Token $DBBASIC_ADMIN_TOKEN" -H 'Content-Type: application/json' \ |
| 82 | -d '{"name":"desk apps (~/.config/dbbasic/object-server.env)"}' "http://127.0.0.1:8001/identity/users/$U/api-keys" | |
| 83 | "$PY" -c 'import json,sys; print(json.load(sys.stdin).get("token",""))') |
| 84 | if [ -n "$KEY" ]; then |
| 85 | runuser -u "$U" -- mkdir -p "$UH/.config/dbbasic" |
| 86 | (umask 077; printf '%s\n' "# the object server the dbbasic apps use (Webmaster publishes here)" \ |
| 87 | "DBBASIC_OBJECT_SERVER_URL=http://127.0.0.1:8001" "DBBASIC_OBJECT_SERVER_USER=$U" \ |
| 88 | "DBBASIC_OBJECT_SERVER_KEY=$KEY" > "$OSF") |
| 89 | chown "$U:$U" "$OSF"; chmod 600 "$OSF" |
| 90 | echo " ~/.config/dbbasic/object-server.env written" |
| 91 | fi |
| 92 | unset KEY |
| 93 | fi |
| 94 | |
| 95 | # where visitors reach published sites (the rendezvous serves /s/ from the |
| 96 | # object server, -sites): Webmaster reports this address after publishing |
| 97 | if [ -f "$OSF" ] && [ -n "${NC_DOMAIN:-}" ]; then |
| 98 | sed -i '/^DBBASIC_OBJECT_SERVER_PUBLIC_URL=/d' "$OSF" |
| 99 | echo "DBBASIC_OBJECT_SERVER_PUBLIC_URL=https://$NC_DOMAIN" >> "$OSF" |
| 100 | fi |
| 101 | |
| 102 | # the voice style from the profile, only if the user hasn't set one |
| 103 | if [ -s /etc/nc/voice-style.initial ]; then |
| 104 | T=$(curl -s -X POST -H "Authorization: Token $DBBASIC_ADMIN_TOKEN" -H "Content-Type: application/json" \ |
| 105 | -d "{\"user_id\":\"$U\",\"ttl_seconds\":300,\"label\":\"bootstrap\"}" http://127.0.0.1:8001/identity/sessions | |
| 106 | "$PY" -c 'import json,sys; print(json.load(sys.stdin).get("token",""))') |
| 107 | cur=$(curl -s -H "Authorization: Bearer $T" http://127.0.0.1:8001/prefs/voice_style | "$PY" -c 'import json,sys; print(json.load(sys.stdin).get("value",""))' 2>/dev/null) |
| 108 | if [ -z "$cur" ]; then |
| 109 | "$PY" -c 'import json,sys; print(json.dumps({"value": open("/etc/nc/voice-style.initial").read()}))' | |
| 110 | curl -s -o /dev/null -X PUT -H "Authorization: Bearer $T" -H "Content-Type: application/json" --data-binary @- \ |
| 111 | http://127.0.0.1:8001/prefs/voice_style && echo " voice style set" |
| 112 | fi |
| 113 | rm -f /etc/nc/voice-style.initial |
| 114 | fi |