Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c36294b
The stack playbook's test deploys from an environment's file
davidjalbers Dec 6, 2026
65f09e5
Index: append PLX-Stack/PreparedHost, which the stack playbook's head…
davidjalbers Dec 6, 2026
2fa626a
The stack playbook deploys from an environment's file
davidjalbers Dec 6, 2026
a6b4836
The image of a prepared host is shared by the tests of both playbooks
davidjalbers Dec 6, 2026
a76519c
The stack playbook's test deploys environments with a hostname onto a…
davidjalbers Dec 6, 2026
10b0365
The stack playbook claims an environment's port and writes its route
davidjalbers Dec 6, 2026
692c30e
Merge remote-tracking branch 'origin/stacks-2' into davidjalbers/plx-…
davidjalbers Dec 6, 2026
2fe7d02
The doctor's test no longer cites the removed requirement PathPrefix …
davidjalbers Dec 6, 2026
c37e345
The doctor no longer lists PLX-Stack/PathPrefix as a removed requirement
davidjalbers Dec 6, 2026
62972fe
The deploy verb's test deploys the stacks that have the environment's…
davidjalbers Dec 6, 2026
2381d41
The deploy job's setup test: the deploy key comes from a repository s…
davidjalbers Dec 6, 2026
2a3eef2
The workflows' tests: a deploy job checks out its own repo alone and …
davidjalbers Dec 6, 2026
8d1ad88
plx deploy deploys the stacks that have the environment's file, to th…
davidjalbers Dec 6, 2026
73971c5
plx-ci setup deploy holds the deploy key from a repository secret, re…
davidjalbers Dec 6, 2026
1f87667
The deploy jobs check out their own repo alone and hold the deploy ke…
davidjalbers Dec 6, 2026
35a727c
The stack template's test: a file for each environment the branches d…
davidjalbers Dec 6, 2026
6dfa69c
The newborn operations repo's deploy job deploys its stacks from thei…
davidjalbers Dec 6, 2026
ee909cc
The newborn repo's test lists the environment files of its stack, whi…
davidjalbers Dec 6, 2026
7867126
The template stack renders a file for each environment the branches d…
davidjalbers Dec 6, 2026
40200ab
The operations repo's marker says plx deploy takes each stack's envir…
davidjalbers Dec 6, 2026
9bdbb68
The newborn operations repo's stacks name a hostname in both environm…
davidjalbers Dec 6, 2026
d177671
The stack playbook's test drops every secret, and reads one that is e…
davidjalbers Dec 6, 2026
2da1794
The stack playbook writes an empty .env.secret, refuses an empty secr…
davidjalbers Dec 6, 2026
8ab9006
The stack playbook's test narrows a run, and ends a host or hostname …
davidjalbers Dec 6, 2026
a4fde3c
The stack playbook refuses a narrowed run, and a host or hostname end…
davidjalbers Dec 6, 2026
7ffa5d5
The stack playbook's test claims a port something listens on, and a h…
davidjalbers Dec 6, 2026
59731ef
The route script skips a port something listens on, and says why the …
davidjalbers Dec 6, 2026
917c85a
The deploy root is written once, and the stack playbook's header says…
davidjalbers Dec 6, 2026
0a925d9
plx deploy's test: the variables go as one JSON object, from a checko…
davidjalbers Dec 6, 2026
5ae3a69
plx deploy passes the playbook's variables as one JSON object, words …
davidjalbers Dec 6, 2026
b0cf0a2
The doctor's test: a deviation under a removed requirement is told th…
davidjalbers Dec 6, 2026
2795317
A deviation under PLX-Stack/PathPrefix or PLX-Ingress/PrefixInRecord …
davidjalbers Dec 6, 2026
674c326
merge's test: a birth-only path is written at a birth and never by an…
davidjalbers Dec 6, 2026
4b51e19
The engine's policy birth-only: a path written at a birth and never a…
davidjalbers Dec 6, 2026
9a8b66f
The stack's tests: an update adds no environment file to a stack, a b…
davidjalbers Dec 6, 2026
687675f
The template stack's environment files are birth-only: an update adds…
davidjalbers Dec 6, 2026
9f84c5d
The update newborn test: a stack of 0.138.0 gets no environment file …
davidjalbers Dec 6, 2026
2942d1d
Merge branch 'davidjalbers/plx-298-the-stack-playbook-applies-an' int…
davidjalbers Dec 6, 2026
4bc60e9
plx deploy's test: a run that exits 0 and prints no Result line is a …
davidjalbers Dec 6, 2026
f63ba76
plx deploy's test: a path holding {{ or {% is refused before anything…
davidjalbers Dec 6, 2026
52862a6
plx deploy refuses a path Ansible would template; the doctor's remove…
davidjalbers Dec 6, 2026
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
177 changes: 120 additions & 57 deletions ansible/platform/playbooks/stack.yml
Original file line number Diff line number Diff line change
@@ -1,81 +1,144 @@
---
# The stack playbook: deploys one environment of one stack onto one host.
# It runs where the deploy starts, a CI job or a person's machine, connects
# as the deploy user, and is never installed on the host. The deploy itself
# is the role plexus.platform.stack; the platform playbook
# (plexus.platform.playbook) lays the environment out before its first deploy.
# The stack playbook: deploys one environment of one stack onto its host. It
# runs where the deploy starts, a CI job or a person's machine, reads there the
# environment's file and the rendered compose file, connects to the host the
# file names as the deploy user, and is never installed on the host. The
# deploy is the role plexus.platform.stack.
#
# ansible-playbook -i deploy@app-1.example.com, plexus.platform.stack \
# -e stack_name=shop -e stack_environment=staging -e stack_compose_file=./compose.yaml
# ansible-playbook plexus.platform.stack -e stack_name=shop \
# -e stack_environment_file=stack/staging.yaml -e stack_compose_file=./compose.yaml
#
# Variables, documented in the role's defaults: stack_name, stack_environment
# and stack_compose_file, given on every run; stack_root (/opt/stacks) and
# stack_wait_timeout (60 seconds).
# Variables, documented in the role's defaults: stack_name,
# stack_environment_file and stack_compose_file, given on every run; the
# environment is named after its file (staging.yaml is staging). Further
# stack_root (the deploy root, /opt/stacks), stack_port_range (20000-20999),
# stack_secret_timeout (30 seconds) and stack_wait_timeout (60 seconds).
#
# What it expects on the host: the directory <stack_root>/<stack>-<environment>,
# which the user it connects as may write, holding the .env (and .env.secret
# where the environment has secrets) that the platform playbook writes; Docker
# What the caller passes, and what not. No inventory (-i) and no --limit: the
# one host is the file's `host`, [user@]address[:port], reached as the user it
# names, else as plexus_deploy_user (the role deploy_user's default, plexus,
# whatever an inventory says), on the port it names, else 22; an inventory
# only lends the connection variables of a host of the same name. No --tags
# or --skip-tags. It refuses a --limit, --tags or --skip-tags it sees, but a
# --limit that leaves out localhost, or --skip-tags always, makes
# ansible-playbook run none of its tasks and exit 0 without a Result line.
# Arguments for the connection, such as --ssh-common-args or --private-key,
# apply. PLEXUS_TAG and PLEXUS_COMMIT_SHA are the caller's: it renders them
# into the compose file it hands over, each app's image pinned by its digest in
# place of ${PLEXUS_TAG} and the commit in place of ${PLEXUS_COMMIT_SHA}; the
# playbook writes neither, and the compose file is otherwise handed over
# uninterpolated, since the host interpolates it from .env.
#
# What it expects where it runs: the vault's CLI, op, with a session or a
# token, where the environment has secrets. On the host, as the platform
# playbook prepares it (PLX-Stack/PreparedHost): the deploy user, owner of
# /opt/stacks; where the environment has a hostname, the directory of route
# fragments the deploy user writes and the sudo rule by which it reloads the
# proxy (both as the header of the ingress role's tasks names them); Docker
# with its compose plugin, which that user may run (the docker group); and
# Python. It uses no privilege escalation. The compose project is
# <stack>-<environment>, the name the platform playbook writes into .env.
# Python. It uses no privilege escalation but that rule.
#
# In order:
# 1. Checks its variables, that the inventory holds one host, and that the
# directory and its .env are there.
# 2. Compares the compose file with the compose.yaml in place. Where they are
# the same, 3 to 5 are left out.
# 3. Keeps the compose.yaml in place as compose.yaml.prev and writes the new one.
# 4. Pulls the images, so that an image that cannot be had fails the deploy
# before migrate runs, and a tag that moved since the last deploy is
# fetched, which run and up, pulling only what is missing, would not do.
# Where the file is the one in place, nothing is pulled, so a tag that
# moved is fetched only by a deploy that changes the file.
# 5. Runs the service `migrate` once, where the file has one, and removes its
# container; where it fails, the line says its exit code and the last line
# it wrote.
# 6. Brings up every service Compose starts by itself, `migrate` excepted, with
# In order, where it runs:
# 1. Checks its arguments and variables, and reads the environment's file and
# checks that it can write it: a map of `host`, `hostname`, a DNS name,
# `env`, names to strings, and `secrets`, names to op:// pointers; plx
# doctor checks the file whole.
# 2. Reads each pointer under `secrets` with `op read`, also in a dry run,
# refusing a value that is empty in the vault, and adds the host the file
# names.
# On the host, into /opt/stacks/<stack>-<environment>, the compose project of
# that name:
# 3. Where the environment has a hostname, claims its loopback port and
# writes its route (the role's files/route.sh says how).
# 4. Creates the directory where it is missing, mode 0750.
# 5. Renders .env from `env`, with COMPOSE_PROJECT_NAME and, where the
# environment has a hostname, PLEXUS_LOOPBACK_PORT; .env.secret, mode
# 0600, from the secrets' values, where the environment has secrets or
# the directory holds one already, which ends empty once the environment
# has none; each value double-quoted and escaped (templates/dotenv.j2), so
# that Compose reads it back as written. Compares them, and the compose
# file, with the files in place: a file that changes is kept as
# <file>.prev, all of them before the first is written, and then written,
# .env, .env.secret, compose.yaml. .env.secret.prev, 0600, keeps the
# previous secrets until .env.secret changes again.
# 6. Pulls the images, where compose.yaml changed, so that an image that
# cannot be had fails the deploy before migrate runs, and a tag that moved
# since the last deploy is fetched, which run and up, pulling only what is
# missing, would not do.
# 7. Runs the service `migrate` once, where compose.yaml changed and has
# one, and removes its container; where it fails, the line says its exit
# code and the last line it wrote.
# 8. Brings up every service Compose starts by itself, `migrate` excepted, with
# --no-deps, so that a service that depends on migrate's completion does
# not run it again, and --remove-orphans, so that a service the file no
# longer has goes. Then waits for every service that declares a
# healthcheck to turn healthy; a service without one counts as soon as it
# runs. stack_wait_timeout bounds the whole wait, and a healthcheck's
# start_period does not extend it: a service that needs longer to turn
# healthy needs the variable raised. Compose re-creates a container only
# where its service changed: an image pinned by digest that did not change
# leaves its line, and its container, as they were. Where no service is
# left to start, a stack of `migrate` alone or of none, it starts nothing
# and takes down whatever the project still runs; a take-down of something
# it ran counts as deployed, as a start does.
# A failure after 3 restores compose.yaml.prev and runs 6 again. After a first
# deploy, where there is no previous file, it removes the stack's containers
# and networks and the compose.yaml; the volumes stay. Where the file was the
# one in place, a failure undoes nothing; so a run after a rollback, with the
# same file, deploys it again and fails the same way. .env and .env.secret are
# the platform playbook's and are neither written nor restored here.
# where its service changed, its image or the files it loads: an image
# pinned by digest that did not change, and values that did not, leave
# the container as it was. Where no service is left to start, a stack of
# `migrate` alone or of none, it starts nothing and takes down whatever
# the project still runs; a take-down of something it ran counts as
# deployed, as a start does.
# A failure after the first write of 5 restores the files that run replaced,
# each from its .prev, removes a file it created, and runs 8 again; the route
# is not rolled back, since it joins the hostname to a port that does not
# change. Where compose.yaml was not there before, a first deploy, it removes
# the stack's containers and networks and the compose.yaml instead; the
# volumes stay, and .env and .env.secret stay with the port the route names.
# Where no file changed, a failure undoes nothing; so a run after a rollback,
# with the same files, deploys them again and fails the same way. A run in
# which nothing changed changes no file, reloads nothing and re-creates no
# container.
#
# Nothing locks an environment as a whole: two runs for the same environment
# at the same time are not supported, and can both fail, one undoing what the
# other made. What keeps them apart is the pipeline's own order, one deploy job
# per branch at a time.
#
# Nothing locks an environment: two runs for the same environment at the same
# time are not supported, and can both fail, one undoing what the other made.
# What keeps them apart is the pipeline's own order, one deploy job per branch
# at a time.
# No secret's value appears in the run's output, with -v or --diff either:
# every task that holds one prints nothing of it.
#
# The last task, Result, prints one line as its `msg`:
# <stack>-<environment>: deployed the file changed, or a container was started or
# taken down; healthy
# The last task, Result, prints one line as its `msg`. Every run prints it, and
# exits 0 only where it says deployed, unchanged or would deploy, but for the
# two runs of no task above and a host that cannot be reached, which Ansible's
# own error ends, with exit code 4:
# <stack>-<environment>: deployed a file or the route changed, or a container was
# started or taken down; healthy
# <stack>-<environment>: unchanged nothing changed, and healthy
# <stack>-<environment>: would deploy under --check, where compose.yaml would change
# <stack>-<environment>: rolled back: <why> the previous compose.yaml is up and healthy again
# <stack>-<environment>: would deploy under --check, where a file or the route would change
# <stack>-<environment>: rolled back: <why> the previous files are up and healthy again
# <stack>-<environment>: failed: <why> nothing was rolled back, or the undo failed too
# <why> names the step that failed and the first line of its error, as in
# `'Bring the stack up and wait for health' failed (rc 1): General error:
# container shop-staging-web-1 is unhealthy`. Where the check of the
# variables fails, the line begins `the stack:`. The task, and with it the
# run, fails unless the line says deployed, unchanged or would deploy.
# arguments or the variables fails, the line begins `the stack:`.
#
# --check, with --diff, runs 1 and 2 and shows whether compose.yaml would
# change, and how. It pulls, migrates and starts nothing, so it says nothing
# about the images, migrate or health.
# --check, with --diff, runs 1 to 5 and shows whether .env, .env.secret,
# compose.yaml and the route would change, and how, but for .env.secret. It
# writes nothing, takes no lock, and pulls, migrates and starts nothing, so it
# says nothing about the images, migrate or health.
- name: Read the environment
hosts: localhost
connection: local
gather_facts: false
vars_files:
- ../roles/deploy_user/defaults/main.yml # plexus_deploy_user
tasks:
- name: Read the environment's file and its secrets
ansible.builtin.include_role:
name: plexus.platform.stack
tasks_from: environment.yml
apply: { tags: [always] }
tags: [always]

- name: Deploy one environment of a stack
hosts: all
hosts: stack_host
gather_facts: false
vars_files:
- ../roles/deploy_user/vars/main.yml # deploy_user_root
- ../roles/ingress/defaults/main.yml # ingress_fragments_dir
roles:
- { role: plexus.platform.stack }
- { role: plexus.platform.stack, tags: [always] }
13 changes: 8 additions & 5 deletions ansible/platform/roles/deploy_user/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
---
# The unprivileged user that owns the deploy root and as which the stack
# playbook deploys over SSH; the role creates both, and changes nothing below
# the root. The deploy root is /opt/stacks and no variable: it is the path the
# stack contract names for every environment (PLX-Stack/HostDirectory).
# the root. The deploy root is no setting: vars/main.yml names it.
#
# The plexus_ variables are shared with other roles (plexus.host.docker adds
# the user to the docker group), so they keep that prefix and have no default
# here; set them in the operations repo's group variables:
# plexus_deploy_user the user's name
# the user to the docker group), so they keep that prefix; set them in the
# operations repo's group variables:
# plexus_deploy_pubkey the public key the deployer connects with; unset or
# empty: no key is authorized
# plexus_ghcr_user the registry login the user pulls the stacks' images
# plexus_ghcr_token with, renewed on every run; an unset or empty token:
# no login, the images are public or an earlier login stands

# The user's name, by convention the same on every host: the stack playbook
# reads it from this file and connects as it where an environment's host names
# no user.
plexus_deploy_user: plexus # noqa: var-naming[no-role-prefix]

# Further groups for the deploy user, e.g. the group an image's sshd policy
# admits logins from.
deploy_user_groups: []
2 changes: 1 addition & 1 deletion ansible/platform/roles/deploy_user/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Not recursive: what lies below is the stack playbook's.
- name: Give the deploy user the deploy root
ansible.builtin.file:
path: /opt/stacks
path: "{{ deploy_user_root }}"
state: directory
owner: "{{ plexus_deploy_user }}"
group: "{{ plexus_deploy_user }}"
Expand Down
6 changes: 6 additions & 0 deletions ansible/platform/roles/deploy_user/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
# The deploy root, the one place it is written: the path the stack contract
# names for every environment (PLX-Stack/HostDirectory), so no setting, and in
# the role's vars, which no inventory overrides. The stack playbook reads this
# file for its stack_root.
deploy_user_root: /opt/stacks
24 changes: 18 additions & 6 deletions ansible/platform/roles/stack/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
# plexus.platform.stack mounts; its header says what it does, in order.
#
# Given on every run, with no default (`-e`, as `plx deploy` passes them):
# stack_name the stack's name, as in `shop`
# stack_environment the environment's name, one lowercase word, as in `staging`
# stack_compose_file the stack's rendered compose file on the machine that
# runs the playbook, every app's image pinned by digest
# stack_name the stack's name, as in `shop`
# stack_environment_file the environment's file on the machine that runs
# the playbook, as in `stack/staging.yaml`; the
# environment is named after it
# stack_compose_file the stack's rendered compose file on that machine,
# every app's image pinned by digest

# Where the environments live on the host, each in <stack_root>/<stack>-<environment>.
stack_root: /opt/stacks
# Where the environments live on the host, each in <stack_root>/<stack>-<environment>:
# the deploy root, which the playbook reads from the role deploy_user's vars.
stack_root: "{{ deploy_user_root }}"

# The ports an environment's loopback port is taken from, <first>-<last>: the
# lowest that no other environment on the host holds, taken once and kept.
stack_port_range: 20000-20999

# How long, in seconds, each `op read` may take where the playbook runs
# before the deploy fails; a read that hangs waits mostly for an unlock
# nobody answers.
stack_secret_timeout: 30

# How long, in seconds, the deploy waits for every service that declares a
# healthcheck to turn healthy: the whole wait, which a healthcheck's
Expand Down
Loading