lemano revert
Put a target’s files and kernel parameters back to how a generation recorded them
Description
Put the files and kernel parameters (sysctl) of a target back to how
generation SEQ recorded them. This is the only command that writes on a
server; discover, diff and the others only read.
With paths or --sysctl keys, only those are reverted and nothing else is
considered (they are a filter, and so for --delete-added: it deletes only
the added files among the paths you named); with neither, every file and
kernel parameter that differs between generation SEQ and the live host.
Other kinds (packages, users, groups, services, firewall rules, mounts, ...)
are not reverted by this command: the preview lists those that differ, and
a name that is not a file or a kernel parameter is refused with that said.
The revert takes the target's lock and runs a fresh pass, recorded as
discover records it: the preview is the difference between that live state
and generation SEQ, in the direction of the revert ("-" what the host has
now, "+" what it will have). It shows each file's content diff with the
rules of lemano diff (a file that only some users can read, one that
holds sealed text or one that looks like it holds secrets, only with
--show-secrets), its owner, group and mode,
and for a kernel parameter its live value and the value set at boot as two
lines: the live value is set with sysctl -w, the value at boot by reverting
the files that set it.
- a file that generation SEQ had and the host no longer has is created
again, with its owner, group and mode;
- a file the host has and generation SEQ did not is listed, and deleted
only with --delete-added;
- a file whose content generation SEQ did not store (larger than the
content limit, excluded, not read) is listed with the reason; the
others are reverted if you approve.
Then it asks: "Do you want to revert these changes? Only 'yes' will be accepted."
Any other answer cancels (exit code 1). --dry-run shows the preview and
stops. Every plan carries a digest, printed on the "Plan digest" line and
as planDigest in -o json and -o yaml: a keyed hash of what this revert
would do on the host (the target, the generation, the options that change
the writes, and for each change its path or key, its action, and the keyed
hashes, mode and owner of the file before and after), which says nothing
about the content of a file and does not move when something else changes
on the host.
Without a terminal to ask on, or with -o json or -o yaml, and without
--expect-plan or --auto-approve, the command shows the same preview as
--dry-run, writes nothing on the server and records no revert, and fails
with exit code 9 (ConfirmationRequired); its error holds the command that
approves the plan shown: yours plus --expect-plan DIGEST.
--expect-plan DIGEST is the approval of a plan you were shown: no question
is asked. The command reads the server again and builds its plan; when that
plan has the digest, it is applied, and the check before each write still
refuses a file that changed during the run. When it has another digest
(something the revert touches changed since), nothing is written, the new
plan is shown and the command fails with exit code 9, with the command that
approves the new plan. --auto-approve accepts whatever plan the run builds,
whether or not it is the one you were shown; it cannot be combined with
--expect-plan.
Each file is written to a temporary file beside it, with its final mode
from the start, checked (sshd -t for sshd's configuration, visudo -c for
sudoers), then renamed over the live one. Just before the rename the host
checks that the live file still has the content the preview showed; a file
changed since then is refused, reported as "changed since the preview", and
the others are written. No backup is left on the host: the version being
replaced is in the store, from the fresh pass.
Nothing is restarted. The preview names the services that probably read a
file; --reload NAME (repeatable) reloads one after a successful write,
named as you know it: --reload sshd reloads ssh.service on the Debian
family.
A revert that writes a file controlling access to the host (sshd's
configuration, sudoers, /etc/pam.d, /etc/security, firewall rules,
authorized_keys, /etc/passwd, /etc/shadow, /etc/group, /etc/gshadow,
/etc/nsswitch.conf, /etc/hosts.allow, /etc/hosts.deny) runs as a trial, as
does any revert with --trial: before writing, the host keeps a copy of
each file and a timer that puts them back when the window ends (5m by
default, 1m to 60m). After the write Lemano logs in again over a new
connection (tried again for up to 20s while sshd reloads); if that works
it asks "Keep these changes? Only 'yes' will be accepted."
(--auto-confirm answers yes once the login worked), and on yes the host
drops the timer and the copies. On any other answer, or when the fresh
login fails, the change is undone at once, through the fresh connection or
the one that wrote; the host's timer is the fallback. The timer does not
survive a reboot, so with systemd the host also has a unit that undoes the
trial at boot, before SSH starts; without systemd the undo is an at job,
which runs once atd starts after the boot. When nothing undid it (no atd,
or the boot undo failed) the next discover reports the condition
UnfinishedTrial, to be finished with lemano target end-trial.
--no-trial writes without a trial.
The revert is recorded as a new generation, "reverted to SEQ" in lemano
history, with how it was approved (typed yes, or --auto-approve; for a
trial, typed yes or --auto-confirm) and the result of each change
(applied, skipped or failed, with the reason); a pass then records the
state it left. History moves forward: no generation is removed.
Writing needs root through sudo -n. On a target with privilege: user, only
files the login owns are reverted, and kernel parameters are not; the rest
is listed with the reason.
Exit codes: 0 when every change was applied (or with --dry-run, or when
nothing differs); 1 when the revert is cancelled or a trial is not kept; 3
when the fresh login of a trial failed (the change is undone); 7 when a
change was not applied (a file changed since the preview, a failed
check); 9 when there is nobody to approve it, or the plan is not the one
given to --expect-plan (see above). The questions are asked only on the
text output, at a terminal.Synopsis
lemano revert NAME --to SEQ [PATH...] [--sysctl KEY]...Examples
lemano revert web-07 --to 12 --dry-run
lemano revert web-07 --to 12 /etc/nginx/nginx.conf
lemano revert web-07 --to 12 --sysctl net.ipv4.ip_forward
lemano revert web-07 --to 12 /etc/ssh/sshd_config --reload sshd
lemano revert web-07 --to 12 --expect-plan hmac-sha256:3f0c...e9a1 --no-trial
lemano revert web-07 --to 12 --delete-added --auto-approve --no-trialFlags
| Flag | Type | Default | Description |
|---|---|---|---|
--auto-approve | write without asking, whatever plan the run builds | ||
--auto-confirm | keep a trial’s changes once the fresh login worked, without asking | ||
--delete-added | delete the files that the generation did not have (only among the named paths, if any) | ||
--dry-run | show the preview and write nothing | ||
--expect-plan | string | approve the plan with this digest, as a preview printed it: write only if the plan is unchanged | |
-f, --filename | string | target file or directory of target files (default the current directory) | |
--no-content | do not show what changes in files, only that their content changes | ||
--no-trial | write without a trial, even a file that controls access | ||
-o, --output | string | output format: json or yaml (default: a table for people) | |
--reload | stringArray | a service to reload after a successful write, such as sshd (repeatable) | |
--show-secrets | also show the content of files that only some users can read or that look like they hold secrets | ||
--sysctl | stringArray | a kernel parameter to revert, live and at boot (repeatable) | |
--to | int64 | 0 | the generation to go back to (required) |
--trial | string | write as a trial that the host undoes after DURATION unless confirmed (default 5m, from 1m to 60m) |
Flags inherited from parent commands
| Flag | Type | Default | Description |
|---|---|---|---|
--log-level | string | warn | log level on standard error: debug, info, warn or error |
--no-color | never colour the text (colour is also off when $NO_COLOR is set, when $TERM is dumb and when the output is not a terminal; $FORCE_COLOR turns it on, over $NO_COLOR) | ||
--password-prompt | ask on the terminal for the login password of each target whose file names none | ||
--ssh-config | string | ssh configuration file that targets’ connection settings are read from, or none to read none (default $LEMANO_SSH_CONFIG, else the files ssh reads) | |
--store | string | path of the store (default $LEMANO_STORE, else $XDG_DATA_HOME/lemano/lemano.db) | |
--sudo-password-prompt | ask on the terminal for the sudo password of each target whose file names none |
Exit codes
See exit codes.
See also
- lemano: the parent command