Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ target/
# Local secrets and run output
.env
.hetzner/
.deploy/
**/.terraform/
*.tfstate
*.tfstate.*
*.tfvars
*.retry
logs/
inspect-logs/

Expand Down
93 changes: 50 additions & 43 deletions docs/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ variable, and the UCloud session is a separate file. Apply changes with
`/work/ucloud-sandboxes/gateway-venv`, creates missing credentials and the
SSH bootstrap key (registering it with UCloud), installs config and units,
and runs `gateway-reconcile`. Rerunning it keeps existing credentials.
5. **Install ops tooling** on the gateway:

```bash
sudo scripts/ops/install_ucloud_ops.sh --backups <dir under /work/data> \
--public-gateway <gateway-url> --public-relay <relay-url>
sudo scripts/ops/install_ucloud_observability_stack.sh \
--private-bind-ip 0.0.0.0 --endpoint-host <gateway-host>
```
5. **Converge ops tooling and observability** on the gateway with Ansible
(`infra/ansible/site.yml` using an inventory based on
`infra/ansible/inventories/ucloud.example/`).

6. **Create the store node**, if the chunk store uses one: steps 1, 3 and 4
of [Store node lost](#store-node-lost), without the restore.
Expand All @@ -134,9 +129,32 @@ sudo python -m sandboxes.systemd gateway-reconcile \
```

The one convergence routine for every provider: enables timers and optional
services by configuration, restarts everything in order and waits for health.
Run it after any change to the wheel, units or `deployment.json`. Hosts never
upgrade OS packages unattended; package updates are a deliberate task.
services by configuration (including `ucloud-sandbox-gatewayd.service` when
`/usr/local/libexec/ucloud-gatewayd` and its unit are installed), restarts
everything in order and waits for health. Run it after any change to the wheel,
units or `deployment.json`. Hosts never upgrade OS packages unattended; package
updates are a deliberate task.

## Declarative infrastructure

Permanent host infrastructure is split into two layers under `infra/`:

- **Cloud resources (`infra/hetzner/`):** OpenTofu module managing the Hetzner
private network, egress route, SSH key, firewalls, protected Primary IPv4s,
persistent chunk-store volume, and Gateway/Store VMs (default `cpx32`).
UCloud continues provisioning VMs with `sandboxes submit-vm`; future backends
add either an OpenTofu module or existing SSH hosts.
- **Host convergence (`infra/ansible/`):** Provider-agnostic Ansible playbooks
(`site.yml`, `upgrade.yml`) and roles (`common`, `gateway`, `store`,
`observability`, `ops`, plus optional `gateway_nat` and `gateway_ingress`
overlays toggled via inventory vars). Uses only `ansible.builtin.*` modules
and keeps environment inventories outside Git under `.deploy/<env>/`.
- **Release builds (`scripts/release/build_release.py`):** Builds the Python
wheel, `ucloud-gatewayd`, and `ucloud-chunk-serve` into one digest-pinned
directory consumed by `ansible-playbook upgrade.yml -e release_dir=...`.

See `infra/hetzner/README.md` and `infra/ansible/README.md` for usage and
example inventories (`hetzner.example`, `ucloud.example`).

## Releases

Expand Down Expand Up @@ -222,13 +240,13 @@ replay. Never repair a node by hand-installing a different artifact.

## Backups

`install_ucloud_ops.sh` installs two timers (it refuses a backup directory
outside `/work`):
The `ops` Ansible role (`infra/ansible/roles/ops/`) installs two timers with
configurable retention (`ops_backup_gateway_keep`, `ops_backup_chunk_index_keep`):

| Timer | Contents | Kept |
| Timer | Contents | Default kept |
| --- | --- | --- |
| `ucloud-sandbox-backup-gateway` (hourly) | `postgres.dump`, `state/` (online SQLite copies, tokens, SSH and producer keys, UCloud session), `etc/`, chunk-index tokens, `manifest.json` with SHA-256s | 48 |
| `ucloud-sandbox-backup-chunk-index` (6 h) | The store node's index, `quick_check`ed and gzipped | 8 |
| `ucloud-sandbox-backup-gateway` (hourly) | `postgres.dump`, `state/` (online SQLite copies, tokens, SSH and producer keys, UCloud session), `etc/`, chunk-index tokens, `manifest.json` with SHA-256s | 4 (Hetzner) / 24 (UCloud) |
| `ucloud-sandbox-backup-chunk-index` (6 h) | The store node's index, `quick_check`ed and gzipped | 2 (Hetzner) / 4 (UCloud) |

Run either by hand: `backup_gateway_state.py gateway|chunk-index --dest <dir> --keep <n>`
as root on the gateway. To verify a snapshot: check the digests in
Expand All @@ -250,7 +268,7 @@ recovery. Alerts go to `journalctl -u ucloud-sandbox-ops-watch`; set
`ALERT_WEBHOOK_URL=` in `/etc/ucloud-sandboxes/alerts.env` (mode 0600) to post
them to a Slack-compatible webhook. Thresholds are `ops_watch.py` options.

**Telemetry.** `install_ucloud_observability_stack.sh` runs an OTLP
**Telemetry.** The `observability` Ansible role (`infra/ansible/roles/observability/`) runs an OTLP
collector (port 4318), Tempo (72 h), VictoriaMetrics (14 days) and Grafana 13
on the gateway's local disk (never on `/work`); losing the gateway loses it.
Grafana and the collector keep running when Tempo or VictoriaMetrics restarts.
Expand All @@ -268,9 +286,8 @@ Operation counts (`ucloud_platform_operation_count_total`) carry a `status`:
exists; the client retries) or `conflict` (a routing transaction rolled back
by a serialization conflict and retried). Only `error` is a failure.

Grafana is public at `https://<public-host>/grafana/` with `--public-url` on
the installer and `--grafana-port 3000` on `configure_hetzner_sdk_ingress.sh`;
otherwise tunnel `-L 3000:127.0.0.1:3000`. Its table actions (drain, evict or
Grafana is public at `https://<public-host>/grafana/` when `gateway_ingress_mode: nginx_tls`
and `observability_enabled: true` in Ansible; otherwise tunnel `-L 3000:127.0.0.1:3000`. Its table actions (drain, evict or
undrain a worker; cancel queued creates and builds) call `/grafana/ops/`,
which nginx forwards to the gateway's operator routes with the gateway token,
only for a signed-in Grafana session sending `X-UCloud-Ops: 1`.
Expand Down Expand Up @@ -354,16 +371,9 @@ workers re-register on their next heartbeat.
1. Create a VM like the old gateway: private network, project drive, both
public links, the same hostname if possible.
2. `apt-get install ca-certificates curl docker.io nftables openssl python3-venv postgresql-18`
3. Stage the installer's inputs from the newest backup in `$STAGE` (mode 0700):
`state.tar` (`tar -cf state.tar state`), `ucloud.pgdump` (from
`postgres.dump`), `deployment.json`, `postgres.dsn` and `s3.env` (from
`etc/`), `ucloud-session.json` (from `state/`), `release/` with the live
kit's two bundles, and the live kit's wheel.
4. `sudo scripts/ops/install_ucloud_gateway.sh`. It restores PostgreSQL and the
state directory (refusing existing ones), installs venv, units and
drop-ins, and stops before starting services. Copy the chunk-index tokens
from the backup to `/var/lib/ucloud-chunk-index/`.
5. **Only if the hostname changed**, rehome the registry. As the service user,
3. Restore PostgreSQL (`pg_restore` from `postgres.dump`), `state/`, `etc/`,
and `/var/lib/ucloud-chunk-index/{read,write}.token` from the newest backup.
4. **Only if the hostname changed**, rehome the registry. As the service user,
with gateway services stopped (review the counts, then add `--execute`):

```bash
Expand All @@ -374,8 +384,9 @@ workers re-register on their next heartbeat.
It rewrites the registry host in every stored image reference. Then update
the DNS-named keys listed under [Machines](#machines-and-their-state);
clients must change `guest_relay_url`.
6. Run `gateway-reconcile`.
7. Rerun `install_ucloud_ops.sh` and `install_ucloud_observability_stack.sh`.
5. Run `ansible-playbook -i <inventory> infra/ansible/site.yml -e release_dir=<release>`
(which installs the wheel, `ucloud-gatewayd`, units, ops timers, observability,
and runs `gateway-reconcile`).

## Credentials

Expand Down Expand Up @@ -426,17 +437,13 @@ Required keys: `scope_id`, `network_id`, `sandbox_image`, `builder_image`
(default `HETZNER_API_KEY`). Private-only workers need
`enable_private_egress: true`, with the gateway NATing their traffic.

1. `scripts/ops/setup_hetzner_foundation.py --egress-gateway-ip <ip> --execute`
(network, SSH key, firewalls).
2. Prepare PostgreSQL as in UCloud step 3.
3. Stage the wheel, `deployment.json`, both bundles, the SSH key pair, the
Hetzner env file and `configure_hetzner_sdk_ingress.sh` in `/tmp`; run
`scripts/ops/install_hetzner_gateway.sh --public-ip <ipv4>`.
4. `scripts/ops/configure_hetzner_sdk_ingress.sh --public-host <dns-or-ip>` (Nginx
and Let's Encrypt; relay under `/relay/`).
5. `install_ucloud_observability_stack.sh --private-bind-ip 10.42.0.2
--public-url https://<public-host>/grafana/`, then rerun step 4 with
`--grafana-port 3000`.
1. Provision network, firewalls, SSH key, protected Primary IPv4s, store volume,
and permanent Gateway/Store VMs with OpenTofu (`infra/hetzner/`).
2. Build a release directory (`uv run python scripts/release/build_release.py --out dist/releases/<tag>`).
3. Converge Gateway and Store hosts with Ansible (`infra/ansible/site.yml`,
using an inventory based on `infra/ansible/inventories/hetzner.example/`),
which configures PostgreSQL 18, mounts, `ucloud-gatewayd`, private worker NAT,
Nginx + Let's Encrypt TLS ingress, observability, and backups.

Hetzner caps dedicated cores per project; past the cap creates fail with
`resource_limit_exceeded` (the autoscaler logs `Create failed:`) and the
Expand Down
46 changes: 46 additions & 0 deletions infra/ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Provider-Agnostic Host Convergence (Ansible)

Converges the permanent **gateway** and **store** hosts across Hetzner Cloud,
UCloud, or any SSH-reachable Ubuntu VM using built-in Ansible modules only (no
external Galaxy collections required).

Ephemeral sandbox workers and builders remain managed by the gateway autoscaler
via snapshot + digest-pinned bundles.

## Quick Start

1. Copy the matching example inventory into a gitignored `.deploy/<env>/inventory/` directory:

```bash
cp -r infra/ansible/inventories/hetzner.example .deploy/prod/inventory
# Edit .deploy/prod/inventory/hosts.yml and group_vars/all.yml
```

2. Dry-run with full diff before touching production:

```bash
cd infra/ansible
ansible-playbook -i ../../.deploy/prod/inventory/hosts.yml site.yml --check --diff
```

3. Apply convergence:

```bash
ansible-playbook -i ../../.deploy/prod/inventory/hosts.yml site.yml
```

## Fast Application Upgrades

Build a versioned release directory (wheel + `ucloud-gatewayd` + `ucloud-chunk-serve`):

```bash
uv run python scripts/release/build_release.py --out dist/releases/candidate
```

Then roll out to Gateway and Store in one command:

```bash
cd infra/ansible
ansible-playbook -i ../../.deploy/prod/inventory/hosts.yml upgrade.yml \
-e release_dir=../../dist/releases/candidate
```
11 changes: 11 additions & 0 deletions infra/ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[defaults]
roles_path = roles
host_key_checking = True
retry_files_enabled = False
interpreter_python = auto_silent
stdout_callback = default
result_format = yaml

[ssh_connection]
pipelining = True
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
59 changes: 59 additions & 0 deletions infra/ansible/inventories/hetzner.example/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
# Hetzner Cloud overlay for cpx32 (4c / 8 GiB / 160 GiB NVMe) gateway + store.
# Copy to .deploy/<env>/inventory/group_vars/all.yml and fill in your values.

gateway_nat_enabled: true
gateway_private_ip: 10.42.0.2
gateway_private_subnet_cidr: 10.42.0.0/24

gateway_ingress_mode: nginx_tls
gateway_public_host: 203.0.113.10
gateway_public_url: "https://203.0.113.10"
gateway_public_relay_url: "https://203.0.113.10/relay"

gateway_provider_env_file: /etc/ucloud-sandboxes/hetzner.env
gateway_provider_dropin_name: hetzner.conf

# Local NVMe bind mounts on gateway (avoids separate Hetzner registry volume).
gateway_registry_require_mount: true
gateway_mounts:
- src: /var/lib/ucloud-registry-local
path: /mnt/ucloud-registry
fstype: none
opts: bind
dump: 0
passno: 0
- src: /mnt/ucloud-registry/backups
path: /work/backups
fstype: none
opts: "bind,nofail,x-systemd.requires-mounts-for=/mnt/ucloud-registry"
dump: 0
passno: 0

# Persistent 1 TB Hetzner volume + cache/index bind mounts on store.
# Replace <VOLUME_ID> with `tofu output -raw chunk_store_volume_id`.
store_mounts:
- src: /dev/disk/by-id/scsi-0HC_Volume_<VOLUME_ID>
path: /mnt/store-replica
fstype: ext4
opts: "defaults,discard,nofail"
dump: 0
passno: 2
- src: /mnt/store-replica/cache
path: /var/lib/ucloud-chunk-store/cache
fstype: none
opts: "bind,nofail,x-systemd.requires-mounts-for=/mnt/store-replica"
dump: 0
passno: 0
- src: /mnt/store-replica/index
path: /var/lib/ucloud-chunk-index
fstype: none
opts: "bind,nofail,x-systemd.requires-mounts-for=/mnt/store-replica"
dump: 0
passno: 0

ops_backup_dir: /work/backups
ops_backup_requires_mounts_for: /work/backups
ops_backup_gateway_keep: 4
ops_backup_chunk_index_keep: 2
ops_backup_chunk_index_ssh_user: root
18 changes: 18 additions & 0 deletions infra/ansible/inventories/hetzner.example/hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copy this directory to a gitignored path such as .deploy/prod/inventory/
# and replace placeholder addresses, volume device IDs, and SSH key paths.
all:
children:
gateway:
hosts:
sandboxes-gateway:
ansible_host: 203.0.113.10
ansible_user: root
ansible_ssh_private_key_file: /path/to/gateway-init
store:
hosts:
sandboxes-store:
ansible_host: 10.42.0.200
ansible_user: root
ansible_ssh_private_key_file: /path/to/gateway-init
ansible_ssh_common_args: >-
-o ProxyCommand="ssh -i /path/to/gateway-init -W %h:%p root@203.0.113.10"
26 changes: 26 additions & 0 deletions infra/ansible/inventories/ucloud.example/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# UCloud provider overlay (VMs created via `sandboxes submit-vm`).
# UCloud handles private worker egress and public TLS links natively.

gateway_nat_enabled: false
gateway_ingress_mode: none
gateway_public_host: ""
gateway_public_url: "https://gateway.example.cloud"
gateway_public_relay_url: "https://relay.example.cloud"

# Project drive mounted by UCloud at /work/data holds registry + backups.
gateway_registry_mount: /work/data
gateway_registry_require_mount: true
gateway_mounts: []
store_mounts: []

# UCloud gateway typically has more RAM; tune PG & observability as desired.
gateway_postgres_shared_buffers: 2GB
gateway_postgres_effective_cache_size: 8GB
observability_private_bind_ip: 0.0.0.0

ops_backup_dir: /work/data/backups
ops_backup_requires_mounts_for: /work/data
ops_backup_gateway_keep: 24
ops_backup_chunk_index_keep: 4
ops_backup_chunk_index_ssh_user: ucloud
16 changes: 16 additions & 0 deletions infra/ansible/inventories/ucloud.example/hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copy this directory to a gitignored path such as .deploy/ucloud/inventory/
# and replace hostnames with your UCloud gateway and store VM hostnames.
all:
children:
gateway:
hosts:
ucloud-gateway:
ansible_host: gateway.example.internal
ansible_user: ucloud
ansible_become: true
store:
hosts:
ucloud-store:
ansible_host: store.example.internal
ansible_user: ucloud
ansible_become: true
28 changes: 28 additions & 0 deletions infra/ansible/roles/common/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
ucloud_user: ucloud
ucloud_group: ucloud
ucloud_install_root: /work/ucloud-sandboxes
ucloud_venv_dir: "{{ ucloud_install_root }}/gateway-venv"
ucloud_data_parent: /var/lib/ucloud-sandboxes
ucloud_data_root: "{{ ucloud_data_parent }}/state"
ucloud_etc_dir: /etc/ucloud-sandboxes
ucloud_libexec_dir: /usr/local/libexec/ucloud-sandboxes
ucloud_chunk_tokens_dir: /var/lib/ucloud-chunk-index

disable_unattended_upgrades: true
gateway_journald_max_use: 1G
store_journald_max_use: 512M
gateway_nat_enabled: false
gateway_ingress_mode: none
observability_enabled: true
ops_backups_enabled: true
ops_watch_enabled: true

gateway_sysctl:
90-ucloud-sandboxes-gateway.conf:
net.ipv4.ip_forward: "{{ 1 if (gateway_nat_enabled | bool) else 0 }}"
store_sysctl:
90-ucloud-chunk-store.conf:
net.core.somaxconn: 4096

store_ucloud_home: /home/ucloud
9 changes: 9 additions & 0 deletions infra/ansible/roles/common/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Restart systemd-journald
ansible.builtin.systemd_service:
name: systemd-journald
state: restarted

- name: Reload sysctl
ansible.builtin.command: sysctl --system
changed_when: false
Loading
Loading