From 45269c64dc1e69a152374c44e7a682724e6454ad Mon Sep 17 00:00:00 2001 From: IBRAHIM KHALIL ATTEIB YACOUB Date: Fri, 20 Nov 2026 11:15:01 +0300 Subject: [PATCH] feat: add stash service role Installs the Stash backup agent from its release binary and registers the machine to one of the customer's S3 buckets. - The release and its SHA-256 are pinned together in the role (Stash 3.2.4); a download that does not match is never put in place. - Registration only runs while the machine has no Stash config, with a one-time token the platform adds to the role config. The token is passed through the environment and the result is never logged. - A failure in the role never fails the boot playbook. Replaces the placeholder in agents/backup-agent/README.md. Co-Authored-By: Claude Fable 5.2 --- README.md | 1 + agents/backup-agent/README.md | 34 +++- capabilities/service-roles/stash/defaults.yml | 6 + capabilities/service-roles/stash/linux.yml | 147 ++++++++++++++++++ capabilities/service-roles/stash/meta.yml | 6 + checksums.sha256 | 5 +- manifest.json | 7 +- 7 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 capabilities/service-roles/stash/defaults.yml create mode 100644 capabilities/service-roles/stash/linux.yml create mode 100644 capabilities/service-roles/stash/meta.yml diff --git a/README.md b/README.md index b6704a5..6e837e4 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Optional daemons a VM can opt into via `service_roles..config` — see `ca | `traefik` | Installs Traefik as a systemd service from its official release binary, optionally enabling the dashboard and Let's Encrypt via ACME. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux | | `docker` | Installs Docker Engine from its official repo, optionally adding users to the `docker` group. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux | | `tailscale` | Installs Tailscale, optionally joining your tailnet automatically when an auth key is provided. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux | +| `stash` | Installs the Stash backup agent from its checksum-pinned release binary and registers the machine to one of the customer's S3 buckets. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux (x86_64 only) | | `fail2ban` | Installs and enables Fail2ban, optionally overriding the default ban time and retry threshold. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux | | `ufw` | Installs and enables UFW, applying default incoming/outgoing policies and a port allowlist before enabling. | Ubuntu 22.04/24.04, Debian 12 (not packaged for RedHat family) | | `grafana` | Installs and enables Grafana from its official repo, optionally setting the admin password and root URL. | Ubuntu 22.04/24.04, Debian 12, AlmaLinux | diff --git a/agents/backup-agent/README.md b/agents/backup-agent/README.md index b81508d..2ef3293 100644 --- a/agents/backup-agent/README.md +++ b/agents/backup-agent/README.md @@ -1,3 +1,33 @@ -# backup-agent installers (placeholder) +# backup-agent (Stash) -No install scripts exist yet — today the registration flow (`docs/backup.agent/registration.md` in `NextDeveloper/S3`) only documents a bare CLI command the customer runs manually. When a real installer is built for Linux/Windows/macOS, it belongs here, following the same pattern as `agents/vm-service`. +Stash is the PlusClouds backup agent ([plusclouds/backup.agent](https://github.com/plusclouds/backup.agent)). + +## Linux virtual machines + +Stash is installed through the `stash` service role, not through a script in this directory: see `capabilities/service-roles/stash/`. + +What the role does on each boot: + +1. Downloads the `stash.linux` release pinned in the role to `/usr/local/bin/stash`, unless that exact build is already there. The download is verified against the SHA-256 pinned next to the version. +2. If the machine has no `/etc/plusclouds/stash.yaml` yet and the platform supplied a registration token, runs `stash register`. Registration writes the config and installs and starts the `stash` systemd unit. +3. Does nothing else on a machine that is already registered, apart from upgrading the binary when the pinned release changes. + +A failure in the role never fails the boot playbook. + +### Role config + +| Key | Set by | Meaning | +| --- | --- | --- | +| `bucket` | customer | Name of the S3 bucket in their account to back up to. Empty = install only. | +| `registration_token` | platform | One-time, short-lived token created when the machine's metadata is built. Not a customer key, not listed in `defaults.yml`. | +| `register_endpoint` | platform | API base URL passed to `stash register --endpoint`. | + +The platform resolves `bucket` inside the customer's own account and only then adds the token. The S3 secret never travels through the metadata: Stash receives it from the registration call. + +### Moving to a newer Stash release + +Change `stash_version` and `stash_sha256` together in `capabilities/service-roles/stash/linux.yml`, taking the checksum of `stash.linux` from the release page, then regenerate the manifest. + +## Windows and macOS + +Not covered yet. Stash is installed and registered by hand there (`stash register --endpoint --token `). diff --git a/capabilities/service-roles/stash/defaults.yml b/capabilities/service-roles/stash/defaults.yml new file mode 100644 index 0000000..bfefb37 --- /dev/null +++ b/capabilities/service-roles/stash/defaults.yml @@ -0,0 +1,6 @@ +bucket: + default: "" + description: >- + Name of the S3 bucket in your account that this machine backs up to. + Empty = Stash is installed but left unregistered (register it later + with `stash register`). diff --git a/capabilities/service-roles/stash/linux.yml b/capabilities/service-roles/stash/linux.yml new file mode 100644 index 0000000..f4df239 --- /dev/null +++ b/capabilities/service-roles/stash/linux.yml @@ -0,0 +1,147 @@ +--- +# Installs the Stash backup agent (plusclouds/backup.agent) and registers the +# machine to an S3 bucket. +# +# This file is included on every boot, so every task has to be safe to repeat: +# the binary is only downloaded when it is missing or is not the pinned build, +# and registration only happens while the machine has no Stash config yet. +# +# Two values in the role config are not customer keys and are therefore not +# listed in defaults.yml: `registration_token` and `register_endpoint`. The +# platform adds them when it builds this machine's metadata, after resolving +# the customer's `bucket` inside their own account. The token is one-time and +# short-lived. Without them Stash is installed and left unregistered. +# +# Nothing here may fail the boot playbook: the rest of first boot (agent +# deployment, post-boot script) runs after the service roles. Any failure ends +# up in the rescue section at the bottom. +- name: Set Stash role defaults + set_fact: + # The release and its SHA-256 are pinned together. To move to a newer + # Stash, change both, taking the checksum of `stash.linux` from the + # release page. + stash_version: "3.2.4" + stash_sha256: "92bc9f59010be887c99cbc8ce1838da270c661af2e348007773a51dc56606774" + stash_bucket: "{{ service_roles.stash.config.bucket | default('') }}" + stash_registration_token: "{{ service_roles.stash.config.registration_token | default('') }}" + stash_register_endpoint: "{{ service_roles.stash.config.register_endpoint | default('') }}" + stash_registered_now: false + no_log: true + +- name: Install and register Stash + block: + # Stash publishes a single Linux build, for amd64. + - name: Stop on architectures Stash has no build for + debug: + msg: >- + Stash is not installed: it is only built for x86_64 and this + machine is {{ ansible_facts.architecture }}. + when: ansible_facts.architecture != "x86_64" + + - name: Install Stash on a supported architecture + when: ansible_facts.architecture == "x86_64" + block: + - name: Check whether this machine is already registered + stat: + path: /etc/plusclouds/stash.yaml + register: stash_config_before + + # get_url leaves an existing file alone when it already matches the + # checksum, and never puts a download in place that does not match. + - name: Download the pinned Stash binary + get_url: + url: "https://github.com/plusclouds/backup.agent/releases/download/v{{ stash_version }}/stash.linux" + dest: /usr/local/bin/stash + checksum: "sha256:{{ stash_sha256 }}" + owner: root + group: root + mode: "0755" + timeout: 120 + register: stash_binary + + # A freshly written file under /usr/local/bin can carry the wrong + # SELinux context, in which case systemd cannot execute it. + - name: Restore SELinux context for the Stash binary (RedHat family) + command: restorecon -v /usr/local/bin/stash + when: + - ansible_facts.os_family == "RedHat" + - stash_binary.changed + changed_when: true + + - name: Check for the Stash systemd unit + stat: + path: /etc/systemd/system/stash.service + register: stash_unit + + - name: Restart Stash after a binary upgrade + systemd: + name: stash + state: restarted + when: + - stash_binary.changed + - stash_config_before.stat.exists + - stash_unit.stat.exists + + # The token goes in through the environment, not the command line, + # which every local user could read. On success `stash register` + # prints the agent API key and the S3 secret key, so the result is + # never logged. It also installs and starts the systemd unit itself. + - name: Register this machine with the platform + command: + argv: + - /usr/local/bin/stash + - register + - --endpoint + - "{{ stash_register_endpoint }}" + environment: + STASH_REGISTER_TOKEN: "{{ stash_registration_token }}" + register: stash_register + when: + - not stash_config_before.stat.exists + - stash_registration_token | length > 0 + - stash_register_endpoint | length > 0 + changed_when: stash_register.rc == 0 + failed_when: false + no_log: true + + - name: Record whether registration succeeded + set_fact: + stash_registered_now: "{{ (stash_register.rc | default(1)) == 0 }}" + + # Covers a machine that was registered by hand, or before Stash + # installed its own service. + - name: Install the Stash service on an already registered machine + command: /usr/local/bin/stash service install + when: + - stash_config_before.stat.exists + - not stash_unit.stat.exists + changed_when: true + + - name: Debug - Stash status + debug: + msg: >- + {%- if stash_config_before.stat.exists -%} + Stash {{ stash_version }} is installed and this machine was already registered. + {%- elif stash_registered_now | bool -%} + Stash {{ stash_version }} installed and registered{{ ' to bucket ' ~ stash_bucket if stash_bucket | length > 0 else '' }}. + {%- elif stash_registration_token | length > 0 -%} + Stash {{ stash_version }} installed, but registration failed. The + registration token is one-time and short-lived: restart the machine + from the panel to get a new one. + {%- elif stash_bucket | length > 0 -%} + Stash {{ stash_version }} installed, but not registered: the platform + supplied no registration token for bucket {{ stash_bucket }}. Check + that the bucket exists in this account. + {%- else -%} + Stash {{ stash_version }} installed, but not registered: no bucket was + set in the role config. Register by hand with `stash register`. + {%- endif -%} + + rescue: + # Only the task name is shown: a failed task's result can hold the + # registration token. + - name: Debug - Stash role failed + debug: + msg: >- + The Stash role did not complete (failed at: {{ ansible_failed_task.name | default('unknown task') }}). + The rest of the boot configuration continues. diff --git a/capabilities/service-roles/stash/meta.yml b/capabilities/service-roles/stash/meta.yml new file mode 100644 index 0000000..256f3ce --- /dev/null +++ b/capabilities/service-roles/stash/meta.yml @@ -0,0 +1,6 @@ +description: "Installs the Stash backup agent and registers this machine to one of your S3 buckets, so its backups can be scheduled from the panel." +supported_os: + - "Ubuntu 22.04" + - "Ubuntu 24.04" + - "Debian 12" + - "AlmaLinux" diff --git a/checksums.sha256 b/checksums.sha256 index 9c0feec..a127f8e 100644 --- a/checksums.sha256 +++ b/checksums.sha256 @@ -1,4 +1,4 @@ -458dddd48ebfe444988be472725f4f040272b667d2ddc109b4150f53179de6aa agents/backup-agent/README.md +18e6c912255647aa52fa804ff6d6c2a7eba278da4910b3ddefa137fda4c79fc1 agents/backup-agent/README.md daec070d647ec6cd3df8737d227c968dc54dab4a7fc725ae182f604b24081eb3 agents/storaged/README.md 59e6e7806dd415e0af3c767d5ed3c664a5f6177dbe5a0e408cd9bbf095140cd1 agents/vm-service/agent.yaml 17e55e6851ad47b57e3ce1df834e9edcaa1f8141d78e22862f95bb7e22164cda agents/vm-service/cleanup.sh @@ -121,6 +121,9 @@ a77c002180aef7e8311ccc9d868cacd219a14452a689ea056d6ac6a803fab3a7 capabilities/s baa771754d48ad67c1844550c66542fb86c3a2ec8f7acb1f32c2f3ba015e6085 capabilities/service-roles/redis/defaults.yml 83bc1792f8a198456b8568be6dd783453e00647501df1584df288fd820c157b3 capabilities/service-roles/redis/linux.yml 427e449622ffc8cfd71b13d05ffb78a17b7ab11ca1f3eb567bb57948cca30f37 capabilities/service-roles/redis/meta.yml +fefb04422f2d673e55b5a973599ef0b59b44e57333bd16b37c7a427c90bb42eb capabilities/service-roles/stash/defaults.yml +0a5a462d751d2ecf27cfe10b27c7f8baba29f026d068b8710e5fb12b7176ebd2 capabilities/service-roles/stash/linux.yml +d2992fef02d82b8cfea095c2757b26b7a902a680a402b27c67b67cf41757b73a capabilities/service-roles/stash/meta.yml 5815ac7c97ccdb35ce1cddcb0598fffb87e1051ecf41f9d706096cf8aeb2f5cb capabilities/service-roles/tailscale/defaults.yml 3708f6c79ea11e95679570dce0c1b2db7d50a7c3b7d379b999324ca39fc55aa8 capabilities/service-roles/tailscale/linux.yml 83b497c159928099e78fe26926768c69eb8aa993638de59542ac604a31ab0547 capabilities/service-roles/tailscale/meta.yml diff --git a/manifest.json b/manifest.json index 060f32a..3acbefe 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { - "generated_at": "2026-10-10T19:53:45Z", + "generated_at": "2026-11-20T07:31:06Z", "files": { - "agents/backup-agent/README.md": {"sha256": "458dddd48ebfe444988be472725f4f040272b667d2ddc109b4150f53179de6aa", "size": 348}, + "agents/backup-agent/README.md": {"sha256": "18e6c912255647aa52fa804ff6d6c2a7eba278da4910b3ddefa137fda4c79fc1", "size": 1901}, "agents/storaged/README.md": {"sha256": "daec070d647ec6cd3df8737d227c968dc54dab4a7fc725ae182f604b24081eb3", "size": 395}, "agents/vm-service/agent.yaml": {"sha256": "59e6e7806dd415e0af3c767d5ed3c664a5f6177dbe5a0e408cd9bbf095140cd1", "size": 2726}, "agents/vm-service/cleanup.sh": {"sha256": "17e55e6851ad47b57e3ce1df834e9edcaa1f8141d78e22862f95bb7e22164cda", "size": 352}, @@ -124,6 +124,9 @@ "capabilities/service-roles/redis/defaults.yml": {"sha256": "baa771754d48ad67c1844550c66542fb86c3a2ec8f7acb1f32c2f3ba015e6085", "size": 367}, "capabilities/service-roles/redis/linux.yml": {"sha256": "83bc1792f8a198456b8568be6dd783453e00647501df1584df288fd820c157b3", "size": 2152}, "capabilities/service-roles/redis/meta.yml": {"sha256": "427e449622ffc8cfd71b13d05ffb78a17b7ab11ca1f3eb567bb57948cca30f37", "size": 229}, + "capabilities/service-roles/stash/defaults.yml": {"sha256": "fefb04422f2d673e55b5a973599ef0b59b44e57333bd16b37c7a427c90bb42eb", "size": 213}, + "capabilities/service-roles/stash/linux.yml": {"sha256": "0a5a462d751d2ecf27cfe10b27c7f8baba29f026d068b8710e5fb12b7176ebd2", "size": 6550}, + "capabilities/service-roles/stash/meta.yml": {"sha256": "d2992fef02d82b8cfea095c2757b26b7a902a680a402b27c67b67cf41757b73a", "size": 233}, "capabilities/service-roles/tailscale/defaults.yml": {"sha256": "5815ac7c97ccdb35ce1cddcb0598fffb87e1051ecf41f9d706096cf8aeb2f5cb", "size": 770}, "capabilities/service-roles/tailscale/linux.yml": {"sha256": "3708f6c79ea11e95679570dce0c1b2db7d50a7c3b7d379b999324ca39fc55aa8", "size": 2191}, "capabilities/service-roles/tailscale/meta.yml": {"sha256": "83b497c159928099e78fe26926768c69eb8aa993638de59542ac604a31ab0547", "size": 207},