#cloud-config
#
# A Jetbridge Provider on a new Hetzner Cloud server. It starts closed, so it
# admits only you.
#
# 1. Fill in the three values under write_files, below.
# 2. In Hetzner's Cloud Console, create a server from the Docker CE app. Any
#    type will do; cax11 is plenty. Choose your SSH key, and paste this file
#    into Cloud config. Or, with the hcloud CLI:
#
#      hcloud server create --name jetbridge --image docker-ce --type cax11 \
#        --ssh-key <your key> --user-data-from-file provider.cloud-config.yaml
#
# 3. Point your API hostname's A record at the new server's IPv4 address, and
#    its AAAA record, if it has one, at the server's IPv6 address. The Provider
#    publishes both. Setup waits for the A record, then finishes by itself.
#
# To watch it, ssh root@<the server's address> and run:
#
#   cloud-init status --wait
#   tail -f /var/log/cloud-init-output.log
#   docker logs -f jetbridge-provider
#
# Back up /etc/jetbridge and /var/lib/jetbridge-provider. The first holds
# provider.toml, with the clients and hostnames you denied, and your policy
# files. The second's pds/ directory holds the Provider's signing key, which is
# its identity. The docs say how, and how to run it:
# https://jetbridge.link/docs/provider/

write_files:
  # Fill in each value after its =
  - path: /opt/jetbridge/setup.conf
    content: |
      # The hostname for your Provider's API, such as provider.example.com
      api_host=
      # An email address or URL for abuse reports, which you must read and answer
      abuse_contact=
      # Your atproto handle or DID. The Provider admits only this account.
      operator=

  - path: /etc/unbound/unbound.conf.d/jetbridge.conf
    content: |
      server:
        cache-max-negative-ttl: 30

  - path: /opt/jetbridge/compose.yaml
    content: |
      services:
        provider:
          image: seamark.cr/byjp.me/jetbridge-provider
          container_name: jetbridge-provider
          command: run
          restart: unless-stopped
          network_mode: host
          volumes:
            - /etc/jetbridge:/etc/jetbridge
            - /var/lib/jetbridge-provider:/var/lib/jetbridge-provider

ssh_pwauth: false

packages:
  - unbound
  - dnsutils
  - ufw

runcmd:
  - |
    set -eu
    say() { echo "jetbridge: $*"; }
    # Values are read as text and only ever quoted, never run.
    conf() { sed -n "s/^$1=//p" /opt/jetbridge/setup.conf; }
    host=$(conf api_host)
    contact=$(conf abuse_contact)
    account=$(conf operator)
    if [ -z "$host" ] || [ -z "$contact" ] || [ -z "$account" ]; then
      say "api_host, abuse_contact and operator must all be filled in"
      exit 1
    fi

    # Ubuntu's unbound validates DNSSEC and listens on 127.0.0.1.
    systemctl restart unbound
    if ! dig @127.0.0.1 dnssec-failed.org | grep -q 'status: SERVFAIL'; then
      say "the resolver on 127.0.0.1 doesn't validate DNSSEC"
      exit 1
    fi

    ufw default deny incoming
    for port in 22 80 443; do ufw allow "$port/tcp"; done
    ufw --force enable

    install -d -o 65532 -g 65532 /etc/jetbridge
    install -d -o 65532 -g 65532 -m 0700 /var/lib/jetbridge-provider

    dev=$(ip -4 route show default | sed -n '1s/.* dev \([^ ]*\).*/\1/p')
    ip=$(ip -4 -o addr show dev "$dev" scope global | awk '{ sub("/.*", "", $4); print $4; exit }')
    if [ -z "$ip" ]; then
      say "this server has no IPv4 address"
      exit 1
    fi
    until dig @127.0.0.1 +short -t A -q "$host" | grep -qxF "$ip"; do
      say "point $host's A record at $ip; checking again in 30 seconds"
      sleep 30
    done

    docker run --rm --network host \
      -v /etc/jetbridge:/etc/jetbridge \
      -v /var/lib/jetbridge-provider:/var/lib/jetbridge-provider \
      seamark.cr/byjp.me/jetbridge-provider init \
      --api-host="$host" --abuse-contact="$contact" --operator="$account"
    cd /opt/jetbridge
    docker compose up -d
    say "started; follow it with: docker logs -f jetbridge-provider"
