Staging deployment ================== A staging deployment is a third clone of this repository, built from the repository alone onto the smallest server Hetzner sells, seeded with synthetic data, and destroyed again when the round is over. It exists for one reason: **nothing is switched on a live deployment without a full rehearsal on staging.** A new role, a replaced service, a migration of the directory or of the mail stack -- each is deployed on staging first, run through the checklist at the end of this chapter, and only then on a live host. Rollback is rehearsed there too, because a rollback that has never been run is a plan, not a procedure. The rules that follow from that purpose: * The smallest server (``cax11``, 2 vCPU, 4 GB, arm64), no storage box, no Hetzner snapshots. Staging holds nothing worth paying to keep. * Synthetic data only. No member directory, no mail archive and no backup of a live host is ever restored onto it; ``seed-test-data.yml`` creates what the checklist needs. * Destroyed between rounds. A staging host that lives on becomes a second production host nobody looks after. What survives a round is the clone on the control machine with its tfvars and ``host_vars``. * Nothing of it is committed. Everything that makes the clone a staging deployment is in files the repository already ignores. A project and a zone of its own ------------------------------- A second deployment cannot share the Hetzner project of the first. ``tf/main.tf`` names the server ``compact-1``, the firewalls ``icmp``, ``ssh``, ``mail`` and ``http+https``, and the SSH keys after the files in ``tf/ssh-keys/``; ``tf/dns.tf`` owns the zone. All of these names are unique per project, so a second apply inside the same project collides with the first on every one of them -- or, worse, with a shared state file, adopts and rebuilds the live server. Staging therefore gets its own Hetzner project (``ae-staging``, say), its own API token, its own state file and its own zone. The zone is a child of the live deployment's zone, ``staging.example.com`` below ``example.com``, and the parent has to delegate it: an NS record set at the label ``staging`` in the parent zone tells every resolver to ask the child zone's name servers for everything below it. That is what the ``delegated_subzones`` variable in ``tf/variables.tf`` of the *parent* deployment is for (see the section "Delegated sub-zones" of the :doc:`Terraform chapter `). The values are the name servers Hetzner assigned to the child zone, and those exist only once the child zone does, so the order is: apply staging first, read its name servers, then apply the parent. Before the first apply, look in the parent zone for a record that was made by hand at the label ``staging`` -- an old test site, for instance. Terraform does not know about it, the plan looks clean, and an NS set next to an A record at the same name is nothing to find out about on the live zone. Delete or move it first. The staging clone ----------------- Next to the clones of the live deployments: .. code-block:: sh cd ~/projects/ae git clone ../windsbraut staging # origin = the reference clone, ff only cd staging/tf mkdir ssh-keys && cp ../../windsbraut/tf/ssh-keys/*.pub ssh-keys/ Replace ``windsbraut`` with whatever your reference clone is called. The staging clone never gets commits of its own; it is fast-forwarded to the branch under test and otherwise left alone. ``tf/secrets.auto.tfvars`` holds the token of the new project, created in the Console under "Security", "API tokens", with read and write permissions: .. code-block:: terraform hcloud_token = "..." ``tf/config.auto.tfvars`` describes the machine and the zone: .. code-block:: terraform domain_name = "staging.example.com" server_type = "cax11" server_location = "fsn1" server_backups = false server_image = "debian-13" dmarc_policy = "v=DMARC1; p=none;" service_hosts = { mattermost = null } ``server_backups`` turns the daily snapshots off (20% of the server price, for a host with nothing on it). ``server_image`` is ``debian-13`` because a fresh deployment has no reason to build from bookworm and dist-upgrade; the live hosts have to keep ``debian-12`` because the attribute is immutable, as the variable explains. ``dmarc_policy`` stays at ``p=none`` so that a test message which fails alignment is reported, not dropped, while the setup is being debugged. ``service_hosts`` drops the names of roles staging does not run, ``mattermost = null`` as on the live deployments. A name that stays published for a role in ``ae_disabled_roles`` -- ``office.cloud`` for collabora, say -- only resolves to a server that has no vhost for it, which is harmless; add ``collabora = null`` if the zone should say what runs. Both ``*.auto.tfvars`` files are gitignored. Then, in ``tf/``: .. code-block:: sh tofu init tofu apply The apply creates the zone, the server, the firewalls, the records, the reverse DNS entries and ``ansible/hosts.ini``. Now read the name servers of the new zone. They are in the state: .. code-block:: sh tofu state show hcloud_zone.zone prints an ``authoritative_nameservers`` block whose ``assigned`` list is the set to delegate to; the Console shows the same three names on the zone's overview page. Put them, fully qualified with the trailing dot, into ``delegated_subzones`` in the **parent** clone's ``tf/config.auto.tfvars``: .. code-block:: terraform delegated_subzones = { staging = [ "hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com.", "helium.ns.hetzner.de.", ] } and ``tofu apply`` there -- the plan contains exactly one new NS record set and nothing else. Check both sides afterwards: .. code-block:: sh dig NS staging.example.com dig A mail.staging.example.com The first has to return the three names, the second the server's address. Do not run the playbook before both do: Caddy requests a Let's Encrypt certificate for every vhost the moment it starts, and a name that does not resolve yet fails the challenge and is retried against a rate limit (see :ref:`the-order-of-the-first-run`). Port 25 on first boot ~~~~~~~~~~~~~~~~~~~~~ Hetzner blocks outbound port 25 on new *accounts* until the block is lifted on request; a new *project* inside an established account is not supposed to be affected, but that is a thing to know before the mail stack is deployed, not after the first test message sits in the queue. Log in once the server is up and try a real MX: .. code-block:: sh ssh root@mail.staging.example.com nc -zv -w5 gmail-smtp-in.l.google.com 25 ``succeeded`` or ``open`` means outbound mail will work. A timeout means the block is in place; ask Hetzner support to lift it for the project before going on, since half of the mail checklist depends on it. Should the image ship without ``nc``, bash alone does the same: .. code-block:: sh timeout 5 bash -c 'cat /dev/tcp/gmail-smtp-in.l.google.com/25' && echo open host_vars ~~~~~~~~~ ``ansible/host_vars/compact-1.yml`` of the staging clone (the host is called ``compact-1`` in every deployment, which is why nothing host-specific may ever be copied between clones): .. code-block:: yaml ae_caddy_serve_apex: false ae_disabled_roles: - backup - mounts - mattermost - website - collabora # re-enable only for a round that tests the office - blackbox-exporter ae_swap_size: "4G" ae_lldap_mail_operators: - "test1" # a seed person; see below for when it takes effect ``ae_caddy_serve_apex: false`` because nothing points at the bare ``staging.example.com`` and a placeholder site there would only cost a certificate. ``backup`` and ``mounts`` need a storage box staging does not have; ``mattermost`` and ``website`` are not part of every deployment and have nothing to do with the switches being rehearsed; ``collabora`` is memory this server does not have; ``blackbox-exporter`` probes names from the outside that a throwaway host has no business alerting about. ``ae_swap_size`` is what makes the full stack fit into 4 GB: the ``swap`` role does nothing unless a host sets it, and with it creates and enables ``/swapfile`` before the first container starts. ``ae_lldap_mail_operators`` has to name somebody because the lldap role refuses an empty list: a mail domain without a postmaster is not one. On a fresh host, though, no person exists when the role first runs -- ``setup.yml`` comes before ``seed-test-data.yml`` and creates only service accounts -- and the role adds the uid to the operator groups without checking LLDAP's answer, so a uid that does not exist yet is silently not added and ``postmaster@``, ``abuse@`` and the other operator aliases end up without a member. Name a seed person (``test1`` is the one the seed makes an application administrator anyway) and, once the seed has run, run the lldap role once more so that the membership is actually made: .. code-block:: sh ansible-playbook -i hosts.ini setup.yml --tags lldap