askrobots / network-computer
Public
Your phone is the terminal. Stream a Linux/Mac desktop to an iPhone over WebRTC through NAT, with audio and input. Open source, single binaries.
Dan Quellhorst
Firefox policy: no WebRTC mDNS (a page using WebRTC made Firefox listen on UDP 5353)
a1d92f3
1 month, 3 weeks ago
History
people.sh · 6.2 KB · 108 lines
| 1 | #!/bin/sh |
| 2 | # Desks for other people: one machine and one desk volume each, set up for them. |
| 3 | # infra/people.sh new NAME a profile from the template: infra/people/NAME.env (edit it) |
| 4 | # infra/people.sh up NAME create their desk: volume, machine, DNS, provisioning, their things |
| 5 | # infra/people.sh welcome NAME what to send them: address, login, PIN, how to connect |
| 6 | # infra/people.sh list everyone, and whose machine is up |
| 7 | # infra/people.sh health [NAME|all] check services; restart any that stopped (and power on if off) |
| 8 | # infra/people.sh update [NAME|all] bring them to the latest version (re-provision) |
| 9 | # infra/people.sh remove NAME [--delete-desk NAME] machine down, DNS name deleted; the desk |
| 10 | # volume (their files) only with the name given twice |
| 11 | # infra/people.sh NAME <droplet.sh command> anything else: status, ssh, logs, idle, down, creds... |
| 12 | # Profiles are gitignored (they can hold keys). Their desk volumes are never deleted by any script. |
| 13 | set -e |
| 14 | DIR=$(cd "$(dirname "$0")" && pwd); P=$DIR/people |
| 15 | profile() { f=$P/$1.env; [ -f "$f" ] || { echo "no profile $f (infra/people.sh new $1)"; exit 1; }; echo "$f"; } |
| 16 | people() { for f in "$P"/*.env; do n=$(basename "$f" .env); [ "$n" = example ] || echo "$n"; done; } |
| 17 | run() { NC_DESK_ENV=$(profile "$1") sh "$DIR/droplet.sh" "$2" ${3:+"$3"}; } |
| 18 | |
| 19 | case "$1" in |
| 20 | new) |
| 21 | N=${2:?usage: people.sh new NAME} |
| 22 | echo "$N" | grep -Eq '^[a-z][a-z0-9-]{1,30}$' || { echo "NAME: lowercase letters, digits, dashes"; exit 1; } |
| 23 | f=$P/$N.env; [ ! -e "$f" ] || { echo "$f exists"; exit 1; } |
| 24 | BASE=$( [ -f "$DIR/desk.env" ] && . "$DIR/desk.env"; echo "${NC_DOMAIN#*.}" ) |
| 25 | sed -e "s/alice\\.example\\.com/$N.${BASE:-example.com}/" -e "s/=alice /=$N /; s/=alice$/=$N/" \ |
| 26 | -e "s/\"Alice\"/\"$N\"/" -e "s/Call me Alice/Call me $N/" "$P/example.env" > "$f" |
| 27 | chmod 600 "$f" |
| 28 | echo "profile: $f (edit it: name, time zone, apps, voice style, keys), then: infra/people.sh up $N" ;; |
| 29 | up) |
| 30 | f=$(profile "${2:?usage: people.sh up NAME}") |
| 31 | NC_DESK_ENV=$f sh "$DIR/create-droplet.sh" |
| 32 | echo; sh "$0" welcome "$2" ;; |
| 33 | welcome) |
| 34 | f=$(profile "${2:?usage: people.sh welcome NAME}") |
| 35 | ( . "$f"; IP=$(doctl compute droplet list --format Name,PublicIPv4 --no-header | awk -v n="$NC_HOST_NAME" '$1==n{print $2}') |
| 36 | [ -n "$IP" ] || { echo "$NC_HOST_NAME is not up"; exit 1; } |
| 37 | ssh -o ConnectTimeout=8 root@"$IP" '. /etc/nc/env; printf "%s\n%s\n%s\n" "$NC_PASSWORD" "$NC_PIN" "$NC_HOST_NAME"' | { |
| 38 | read -r PASS; read -r PIN; read -r HOST |
| 39 | ADDR=${NC_DOMAIN:+https://$NC_DOMAIN}; ADDR=${ADDR:-http://$IP:8765} |
| 40 | cat <<EOT |
| 41 | Hi ${NC_WELCOME_NAME:-$2}, |
| 42 | |
| 43 | Your computer is ready. Open it from any browser (or the Network Computer app): |
| 44 | |
| 45 | Address $ADDR |
| 46 | User nc |
| 47 | Password $PASS |
| 48 | Computer $HOST |
| 49 | PIN $PIN (asked once per device) |
| 50 | |
| 51 | Click the picture to start using it; Shift+Esc gives the mouse back. There is a |
| 52 | Welcome note on its Desktop with the basics (search, voice, copy and paste, files). |
| 53 | EOT |
| 54 | } ) |
| 55 | ;; |
| 56 | list) |
| 57 | UP=$(doctl compute droplet list --format Name,PublicIPv4,Size,Status --no-header) |
| 58 | printf "%-14s %-26s %-14s %s\n" PERSON ADDRESS SIZE MACHINE |
| 59 | for n in $(people); do |
| 60 | ( . "$P/$n.env"; m=$(echo "$UP" | awk -v h="$NC_HOST_NAME" '$1==h{print $4" "$2}') |
| 61 | printf "%-14s %-26s %-14s %s\n" "$n" "${NC_DOMAIN:-(ip only)}" "${NC_SIZE:-?}" "${m:-down (desk kept)}" ) |
| 62 | done ;; |
| 63 | health) |
| 64 | [ -n "$(people)" ] || { echo "$(date '+%F %T') no people yet"; exit 0; } |
| 65 | for n in $( [ -z "$2" ] || [ "$2" = all ] && people || echo "$2" ); do |
| 66 | ( . "$(profile "$n")" |
| 67 | ID=$(doctl compute droplet list --format Name,ID,Status --no-header | awk -v h="$NC_HOST_NAME" '$1==h{print $2" "$3}') |
| 68 | [ -n "$ID" ] || { echo "$n: down (not created)"; exit 0; } |
| 69 | set -- $ID |
| 70 | if [ "$2" = off ] && [ "${NC_KEEP_RUNNING:-yes}" = yes ]; then |
| 71 | doctl compute droplet-action power-on "$1" --wait >/dev/null && echo "$n: was off, powered on"; fi |
| 72 | IP=$(doctl compute droplet get "$1" --format PublicIPv4 --no-header) |
| 73 | ssh -o ConnectTimeout=10 -o BatchMode=yes root@"$IP" ' |
| 74 | bad=""; for s in nc-xorg nc-desktop nc-audio nc-rendezvous nc-host nc-object-server nc-object-daemon nc-voice; do |
| 75 | systemctl is-active --quiet $s || { systemctl restart $s; bad="$bad $s"; }; done |
| 76 | echo "${bad:+restarted:$bad}${bad:-all services up}; desk $(df -h /desk 2>/dev/null | awk "NR==2{print \$5}") used"' 2>/dev/null | |
| 77 | sed "s/^/$n: /" || echo "$n: unreachable over ssh" ) |
| 78 | done ;; |
| 79 | update) |
| 80 | for n in $( [ -z "$2" ] || [ "$2" = all ] && people || echo "$2" ); do |
| 81 | echo "== $n"; run "$n" provision | grep -E "FAIL|ok, |!!" || true |
| 82 | done ;; |
| 83 | remove) |
| 84 | N=${2:?usage: people.sh remove NAME [--delete-desk NAME]}; f=$(profile "$N") |
| 85 | ( . "$f" |
| 86 | NC_DESK_ENV=$f sh "$DIR/droplet.sh" down |
| 87 | if [ -n "$NC_DOMAIN" ]; then # down parks the name at 127.0.0.1; a person who's gone needs none |
| 88 | ZONE=${NC_DOMAIN#*.}; REC=${NC_DOMAIN%%.*} |
| 89 | for id in $(doctl compute domain records list "$ZONE" --format ID,Type,Name --no-header | awk -v r="$REC" '$2=="A" && $3==r{print $1}'); do |
| 90 | doctl compute domain records delete "$ZONE" "$id" --force && echo "DNS $NC_DOMAIN deleted" |
| 91 | done |
| 92 | fi |
| 93 | if [ "$3" = --delete-desk ] && [ "$4" = "$N" ]; then |
| 94 | VOL=desk-${NC_DESK:-$N} |
| 95 | VID=$(doctl compute volume list --format ID,Name --no-header | awk -v v="$VOL" '$2==v{print $1}') |
| 96 | for i in 1 2 3 4 5 6; do [ -z "$VID" ] && break; doctl compute volume delete "$VID" --force 2>/dev/null && { echo "desk $VOL deleted (their files are gone)"; break; }; sleep 10; done |
| 97 | rm -f "$f"; echo "profile removed" |
| 98 | else |
| 99 | echo "desk desk-${NC_DESK:-$N} kept (their files); to delete it too: people.sh remove $N --delete-desk $N" |
| 100 | fi ) |
| 101 | ;; |
| 102 | keepalive) |
| 103 | echo "Each desk keeps its own services running (nc-health, every 5 minutes)." |
| 104 | echo "Scheduled work that needs the DigitalOcean API (power on, auto-stop, health rounds)" |
| 105 | echo "belongs on the controller (object.dbbasic.com), not this Mac: see docs/DESKS.md." ;; |
| 106 | ''|-h|--help) sed -n 2,13p "$0" ;; |
| 107 | *) run "$1" "${2:-status}" "$3" ;; |
| 108 | esac |