Skip to content

7f. Move stacks to another host (0.46.0)

pull config and push config (7d) move a host’s configuration and leave its deployments behind. Two commands move the rest, from host A (the one being retired) to host B:

  • pstack forward makes A pass its deploys, and the visitors it no longer serves, to B. CI and visitors keep working whichever host their DNS reaches.
  • pstack migrate copies A’s deployments to B and brings each up in the state it had on A.

The blocks in this section need two hosts, so they are illustrative, not captured.

Terminal window
# against A
export PSTACK_API_URL=https://api.preview.example.com
export PSTACK_TOKEN=… # admin on A to set or turn off; maintainer to read
export PSTACK_FORWARD_TOKEN=… # a user token on B, developer or higher
pstack forward set https://api.preview-b.example.com
pstack forward
Forwarding to https://api.preview-b.example.com · last ok 3m ago

pstack forward off prints Not forwarding. The token comes from PSTACK_FORWARD_TOKEN only, never a flag: argv is readable through ps. No read returns it.

While it is set, A passes two kinds of request to B:

  • Every request under /api/deployments and /api/jobs. A checks the caller at the route’s own tier first, so a viewer cannot deploy through A. Then it swaps the caller’s credential for B’s token and passes the request on. B’s answer comes back unchanged: status, body, job id, log stream. Deploys run on B only, and CI’s up → poll → stream against A reaches B’s job. A’s own UI lists B’s deployments; A’s records go stale.
  • A visitor request for a preview hostname A does not serve. Previews still running on A keep serving from A. A stack asleep on A is forwarded, not woken: waking it would start a stale copy on the host being retired. One-label hostnames only, like the wake catch-all.

A serves these itself:

  • a request carrying X-Pstack-Forwarded-By. A adds that header to everything it forwards, so A→B and B→A both on still runs one deploy, on B;
  • a share link’s requests;
  • a job A itself holds, such as a loki-apply. The job list, GET /api/jobs, is B’s;
  • the control hostnames (control., api., loki., grafana.) and every other route.

Share links are per host. POST …/share is forwarded, so a link minted through a forwarding A is B’s: signed with B’s PSTACK_TOKEN, it verifies only on B, and opened on A before DNS moves it answers 401. A link A minted earlier verifies only on A.

forward set checks B before it saves anything:

  1. GET /api/auth/me on B with the token answers 200;
  2. the token is an account’s, developer or higher, and not B’s root PSTACK_TOKEN: one host’s root credential must not sit on another;
  3. without --via, the URL is not A’s own API.

Each failure is a 400 naming the check, e.g.

the token on api.preview-b.example.com is viewer: developer or higher

The URL is https://, or plain http:// to a loopback or private address. A redirect from B is a refusal.

--via <ip[:port]> is where A connects. The URL’s host stays the Host header and the TLS server name, so B’s certificate is checked for it. It is for a URL that resolves back to A, which api.<A's domain> does until DNS moves. With --via the own-URL check is skipped: the loop guard and B’s /api/auth/me catch a misconfiguration. Under HTTP-01, B has no certificate for api.<A's domain> until DNS moves, so use B’s own domain’s API URL.

When B does not answer. A forwarded API request that never reached B (connection refused, a DNS failure) is tried 3 times in all, 2 s then 5 s apart. Then A answers 502 forward: api.preview-b.example.com unreachable after 3 attempts and emits forward.failed. A failure after the connection, a TLS error included, is not retried, because B may already have started a job: 502 forward: <host>: <error>. Whatever B answers, a 401, a 409, a 500, reaches the caller as B’s answer.

A forwarded visitor request is never retried: a retry would hold the visitor’s request open. Any failure answers A’s Down page (503, retry-after: 5), which the browser retries, and logs one line. last ok is the last time B answered, written at most once a minute.

What B must have. The preview’s domain must be one B answers on: its primary, or one added with PUT /api/domains or --extra-domain. Under DNS-01, B’s wildcard covers it at once. Under HTTP-01, B gets a hostname’s certificate only once public DNS points at B; until then a forwarded visitor gets A’s Down page.

The setting stays on A. pull config does not carry it, because applied to B it would make B forward to itself; the export names it in skipped. Over the API it is GET/PUT/DELETE /api/forward (reference) or pstack api forward …. The Control page shows the same line as pstack forward.

Terminal window
export PSTACK_FROM_TOKEN=… # a token on A
export PSTACK_TO_TOKEN=… # a token on B
pstack migrate --from https://api.preview.example.com --to https://api.preview-b.example.com
pr-101 running copied, up
pr-102 asleep copied, up, slept
pr-103 down copied
pr-104 running unchanged

It runs from a laptop or CI against both APIs; the hosts never call each other for the copy. For each deployment on the source, or each <id> given:

  1. Copy its spec, compose and stored variables, unchanged, when the target lacks it or the source’s updatedAt is newer. A named spec stays named, so the target must have it already (push config copies named specs).
  2. Match the source’s state. Running → up. Asleep → up, then sleep, so it wakes on request as before. Down → nothing. A deployment this run did not copy is only ever brought up to the source’s state: a newer target that is running stays running.
  3. Wait for the jobs, then print one line each: id, source state, what was done.

Exit 1 when anything failed: a copy, an up (a seed failing, an image that will not pull), or a source row docker did not answer for (unknown skipped (docker did not answer)).

Run it again to catch up. A second run copies only what changed since the first. With forwarding on before the first run there is nothing to catch up. Volumes and databases never move: each stack on the target runs its own seed.

Without --sleep-source it never changes the source. --sleep-source is a sleep pass: it copies nothing and changes nothing on the target. It puts each running source copy the target holds to sleep, so visitors still reaching A are forwarded to B. A deployment the target no longer holds (CI deleted it through the forwarding A) is listed as not on target and left as it is; a re-copy would run its up hooks again for a closed pull request. Every source call carries X-Pstack-Forwarded-By: migrate, so a forwarding A answers for itself. A stack sleep refuses stays running and is listed, not failed:

pr-105 running source slept
pr-106 running left running (shared)
pr-107 running not on target

Never down A’s copies. down runs each axis’s down hook, and those delete external resources keyed by the stack name, such as a database branch or a DNS record, that B’s copy uses too. Sleep removes containers, keeps volumes and runs no hooks. Destroying A’s machine runs no hooks either.

  1. pstack pull config on A, pstack push config on B: accounts, registries, routing, settings. B must answer on A’s domain.
  2. pstack forward set <B's API URL> on A. Every deploy from here runs on B. Use B’s own domain’s API URL; --via <B's IP> only with a URL that resolves to A and a certificate B already has (DNS-01).
  3. pstack migrate --from <A> --to <B>.
  4. Switch DNS.
  5. pstack migrate --from <A> --to <B> --sleep-source. It copies nothing, leaves B alone and sleeps A’s copies that B holds, so visitors still reaching A are forwarded to B. Previews CI closed on B since step 3 stay closed. It runs after DNS because under HTTP-01 B gets its certificates only then.
  6. When nothing reaches A any more, retire it. Never down A’s copies.