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
13 hours, 36 minutes ago
History
droplet.sh · 6.7 KB · 93 lines
| 1 | #!/bin/sh |
| 2 | # Day-to-day control of your desk and the computer it is attached to. |
| 3 | # infra/droplet.sh up [size] sit down: a computer of any size, desk attached, provisioned |
| 4 | # infra/droplet.sh down get up: destroy the computer and its DNS record; the desk stays |
| 5 | # infra/droplet.sh status computer, services, desk usage |
| 6 | # infra/droplet.sh ssh shell on the computer |
| 7 | # infra/droplet.sh creds address, login and PIN (kept on the desk) |
| 8 | # infra/droplet.sh logs follow nc-host and nc-rendezvous |
| 9 | # infra/droplet.sh update git pull + rebuild + restart the nc services |
| 10 | # infra/droplet.sh provision re-copy provision/ and re-run apply.sh |
| 11 | # infra/droplet.sh dns <fqdn> point an A record at this computer |
| 12 | # infra/droplet.sh desk the desk volume: size, region, attachment |
| 13 | # infra/droplet.sh snapshot-desk back up the desk (volume snapshot, ~$0.06/GB-month) |
| 14 | # infra/droplet.sh off | on power off (still billed) / on |
| 15 | # infra/droplet.sh idle how long since the last device disconnected |
| 16 | # infra/droplet.sh autostop check N 'down' if idle N minutes (for the controller, object.dbbasic.com) |
| 17 | # Defaults (domain, desk, user, region, size) come from the profile in NC_DESK_ENV: |
| 18 | # infra/desk.env unless people.sh picked a person's (infra/people/NAME.env). |
| 19 | set -e |
| 20 | DIR=$(cd "$(dirname "$0")" && pwd); ROOT=$(cd "$DIR/.." && pwd) |
| 21 | ENVF=${NC_DESK_ENV:-$DIR/desk.env} |
| 22 | [ -f "$ENVF" ] && . "$ENVF" |
| 23 | export NC_DESK_ENV=$ENVF |
| 24 | cd "$DIR" |
| 25 | # the computer: NC_DROPLET, else the profile's host name, else the droplet tagged "nc" |
| 26 | NAME=${NC_DROPLET:-$NC_HOST_NAME} |
| 27 | NAME=${NAME:-$(doctl compute droplet list --tag-name nc --format Name --no-header 2>/dev/null | head -1)} |
| 28 | NAME=${NAME:-nc} |
| 29 | VOL=desk-${NC_DESK:-${NC_HOST_NAME:-nc}} |
| 30 | |
| 31 | ip() { doctl compute droplet list --format Name,PublicIPv4 --no-header | awk -v n="$NAME" '$1==n{print $2}'; } |
| 32 | id() { doctl compute droplet list --format Name,ID --no-header | awk -v n="$NAME" '$1==n{print $2}'; } |
| 33 | volid() { doctl compute volume list --format ID,Name --no-header | awk -v n="$VOL" '$2==n{print $1; exit}'; } |
| 34 | |
| 35 | case "$1" in |
| 36 | up|create) |
| 37 | [ -z "$(id)" ] || { echo "$NAME is already up at $(ip)"; exit 0; } |
| 38 | ./create-droplet.sh "${NC_HOST_NAME:-nc}" "${NC_REGION:-nyc3}" "${2:-${NC_SIZE:-s-2vcpu-4gb}}" ;; |
| 39 | down|destroy) |
| 40 | DID=$(id); [ -n "$DID" ] || { echo "no computer is up; desk $VOL is untouched"; exit 0; } |
| 41 | IP=$(ip) |
| 42 | DOMAIN=${NC_DOMAIN:-$(ssh -o ConnectTimeout=5 root@"$IP" 'sed -n "s/^NC_DOMAIN=//p" /etc/nc/env' 2>/dev/null || true)} |
| 43 | # park DNS first, so the name never points at an IP that is about to be released |
| 44 | [ -z "$DOMAIN" ] || sh ./dns-park.sh "$DOMAIN" "$IP" |
| 45 | doctl compute droplet delete "$DID" --force |
| 46 | echo "$NAME destroyed (billing stopped); desk $VOL kept" ;; |
| 47 | status) |
| 48 | doctl compute droplet list --format Name,PublicIPv4,Status,Size,Region --no-header | awk -v n="$NAME" '$1==n' || true |
| 49 | IP=$(ip); [ -n "$IP" ] && ssh -o ConnectTimeout=5 root@"$IP" ' |
| 50 | echo "services: $(systemctl is-active nc-xorg nc-desktop nc-audio nc-rendezvous nc-host | paste -sd" " -)" |
| 51 | . /etc/nc/env; echo "hosts: $(curl -s -u "nc:$NC_PASSWORD" localhost:8765/hosts)" |
| 52 | mountpoint -q /desk && echo "desk: $(df -h /desk | awk "NR==2{print \$3\" used of \"\$2\" (\"\$5\")\"}")" || echo "desk: not mounted"' 2>/dev/null || true ;; |
| 53 | ssh) ssh root@"$(ip)" ;; |
| 54 | creds) ssh root@"$(ip)" '. /etc/nc/env; if [ -n "$NC_DOMAIN" ]; then echo "address: https://$NC_DOMAIN"; else echo "address: http://$NC_PUBLIC_IP:8765"; fi |
| 55 | echo "user: nc"; echo "password: $NC_PASSWORD"; echo "host: $NC_HOST_NAME"; echo "PIN: $NC_PIN"' ;; |
| 56 | logs) ssh root@"$(ip)" journalctl -f -u nc-host -u nc-rendezvous ;; |
| 57 | update) ssh root@"$(ip)" 'cd /opt/network-computer && git pull -q && export PATH=$PATH:/usr/local/go/bin && go build -buildvcs=false -o /usr/local/bin/ ./cmd/... && systemctl restart nc-rendezvous nc-host && echo updated' ;; |
| 58 | provision) IP=$(ip); ssh root@"$IP" 'mkdir -p /root/provision'; scp -q -r "$ROOT/provision/." root@"$IP":/root/provision/ |
| 59 | # the profile's personal settings (keys too) go as a file, never on a command line |
| 60 | [ -f "$ENVF" ] && { scp -q "$ENVF" root@"$IP":/root/provision/person.env; ssh root@"$IP" 'chmod 600 /root/provision/person.env'; } |
| 61 | SIZE=$(doctl compute droplet get "$(id)" -o json | grep -o '"size_slug": *"[^"]*"' | head -1 | cut -d'"' -f4) |
| 62 | PRICE=$(doctl compute size list --format Slug,PriceHourly --no-header | awk -v s="$SIZE" '$1==s{print $2}') |
| 63 | ssh root@"$IP" "NC_PUBLIC_IP=$IP NC_HOST_NAME=$NC_HOST_NAME NC_DOMAIN=$NC_DOMAIN NC_DESK_USER=$NC_DESK_USER NC_KEYBOARD=$NC_KEYBOARD NC_SIZE=$SIZE NC_PRICE_HOURLY=$PRICE sh /root/provision/apply.sh" ;; |
| 64 | dns) sh ./dns-point.sh "${2:?usage: droplet.sh dns <name.domain>}" "$(ip)" ;; |
| 65 | desk) doctl compute volume list --format Name,Size,Region,DropletIDs,Tags --no-header | awk -v n="$VOL" '$1==n' | grep . || echo "no desk named $VOL yet (it is created on the first 'up')" ;; |
| 66 | snapshot-desk) |
| 67 | VID=$(volid); [ -n "$VID" ] || { echo "no desk named $VOL"; exit 1; } |
| 68 | doctl compute volume snapshot "$VID" --snapshot-name "$VOL-$(date +%Y%m%d-%H%M)" --tag nc-desk >/dev/null |
| 69 | echo "snapshot of $VOL taken" ;; |
| 70 | off) doctl compute droplet-action power-off "$(id)" --wait >/dev/null && echo "$NAME powered off (still billed; 'down' stops billing)" ;; |
| 71 | on) doctl compute droplet-action power-on "$(id)" --wait >/dev/null && echo "$NAME powered on at $(ip)" ;; |
| 72 | idle) IP=$(ip); [ -n "$IP" ] || { echo "no computer is up"; exit 0; } |
| 73 | ssh -o ConnectTimeout=8 root@"$IP" 'cat /run/nc-host/idle 2>/dev/null' | awk -v now="$(date +%s)" ' |
| 74 | $1=="busy" {print $2 " device(s) connected"; exit} |
| 75 | $1=="idle-since" {printf "idle for %d min\n", (now-$2)/60; exit} |
| 76 | {print "unknown"}' ;; |
| 77 | autostop) |
| 78 | # 'autostop check N': run 'down' if nobody has been connected for N minutes. Meant for |
| 79 | # the controller (object.dbbasic.com), which holds the DigitalOcean token: never the |
| 80 | # desk itself, and not a Mac (it sleeps, and macOS keeps background jobs off external drives). |
| 81 | case "$2" in |
| 82 | check) |
| 83 | N=${3:?minutes}; IP=$(ip); [ -n "$IP" ] || exit 0 |
| 84 | S=$(ssh -o ConnectTimeout=8 -o BatchMode=yes root@"$IP" 'cat /run/nc-host/idle 2>/dev/null') || exit 0 |
| 85 | set -- $S |
| 86 | if [ "$1" = idle-since ] && [ $(( ($(date +%s) - $2) / 60 )) -ge "$N" ]; then |
| 87 | echo "$(date '+%F %T') $NAME idle $(( ($(date +%s) - $2) / 60 )) min >= $N: down" |
| 88 | "$DIR/droplet.sh" down |
| 89 | fi ;; |
| 90 | *) echo "usage: droplet.sh autostop check MINUTES (run by the controller; see docs/DESKS.md)"; exit 1 ;; |
| 91 | esac ;; |
| 92 | *) sed -n "2,14p" "$DIR/droplet.sh" ;; |
| 93 | esac |