Sub topic 13
Testing patches and rolling them out in stages
This chapter assumes you can run apt and dnf for review and upgrades, read a reboot-required signal, hold a package, and read transaction history. You also need to be comfortable with systemd units and with SSH access to more than one machine. Here you combine those pieces into a routine that limits the damage from a bad update.
Why stage a rollout
A patch can fail in ways the package manager cannot see. The install may succeed while the service behaves differently afterwards. A new library version can change a default. A kernel update can break a driver on one hardware model and not on another. The package manager only reports whether files were unpacked and scripts ran.
Staging reduces this risk. You apply the same set of updates to a small group of machines first, check them, and then widen the group. If the first group fails, most of your fleet is untouched.
Staging works only when the groups are meaningful. A test machine that runs nothing like production tells you little. The aim is a chain of environments where each step is more representative and more important than the last.
Build the stages
A common layout uses three stages.
| Stage | Purpose | Typical contents |
|---|---|---|
| Test | Catch install failures and obvious breakage | Disposable or rebuildable machines that match production's OS and major packages |
| Canary | Catch problems that only real traffic or real data shows | One or a few production machines, ideally one per role |
| Production | Apply to everything else | Remaining machines, in batches |
For a single server, you can still use the idea. Take a snapshot or backup, patch during a quiet window, and verify before you leave. The snapshot plays the role of the test stage.
Keep the machines in each stage at the same major release and from the same repositories. If the test group points at a different mirror or has different third-party repositories, it may not see the same updates.
The core problem: the same updates on every stage
Repositories change over time. If you patch the test group on Monday and production on Friday, production may receive newer packages than the ones you tested. You then have tested one set of updates and deployed another.
There are three ways to handle this.
- Patch close together. Run the stages within a short window, such as a day. This is simple and leaves a small gap, but it does not remove it.
- Record and replay. Save the list of package versions installed on the tested machine and install exactly those on later stages.
- Use a frozen snapshot of the repository. Mirror the repository at a point in time and point every stage at that snapshot. This gives the strongest guarantee and costs the most to run. Tools such as Pulp, Foreman with Katello, and Landscape provide this.
Record and replay needs no extra infrastructure, so it is a good starting point. The next sections show it on both families.
Record what the test machine installed
First, on the test machine, capture the pending updates before you change anything. This is the list you are about to test.
sudo apt update
apt list --upgradable 2>/dev/null | tail -n +2 | tee /tmp/pending-before.txtOn a RHEL-family machine, use dnf:
dnf check-update | tee /tmp/pending-before.txt
echo "exit code: $?"Note that $? after a pipe reports the exit status of tee, not dnf. If you need the exit code of dnf check-update (100 means updates are available), run it without the pipe or use ${PIPESTATUS[0]} in bash.
Apply the updates, then record the resulting versions of every installed package.
# Debian and Ubuntu
dpkg-query -W -f='${Package}=${Version}\n' | sort > /tmp/tested-versions.txt# RHEL family
rpm -qa --qf '%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}\n' | sort > /tmp/tested-versions.txtThis file is the artifact you tested. Keep it with a date in the name, and store it somewhere other than the test machine.
Replay the tested versions on later stages
On Debian and Ubuntu you can install a named version with package=version. You pass the exact versions for only the packages that were pending. This avoids touching packages you did not test.
#!/usr/bin/env bash
# replay-apt.sh: install the tested versions of the packages that are upgradable.
set -euo pipefail
TESTED=/tmp/tested-versions.txt
sudo apt update
# Names of packages that have updates pending on this machine.
mapfile -t pending < <(apt list --upgradable 2>/dev/null | tail -n +2 | cut -d/ -f1)
args=()
for pkg in "${pending[@]}"; do
# Look up the tested pin for this package, if there is one.
line=$(grep -m1 -F "${pkg}=" "$TESTED" || true)
if [ -n "$line" ]; then
args+=("$line")
else
echo "No tested version for $pkg; skipping" >&2
fi
done
if [ "${#args[@]}" -gt 0 ]; then
sudo apt install --only-upgrade "${args[@]}"
fiThe script has two limits worth knowing. If the repository no longer carries the tested version, apt install fails with a message that the version cannot be found. That failure is useful, because it tells you the repositories moved and you must retest. And grep -F "${pkg}=" can match a longer package name that ends with the same text, so for a real fleet you should anchor the match at the start of the line.
On RHEL-family systems the same idea uses dnf upgrade with full package names. Because the file already holds name-version-release.arch strings, you can pass them directly:
sudo dnf upgrade $(cat /tmp/tested-versions.txt | grep -E '^(openssl|nginx)-')Choose the package names yourself, or generate the list from dnf check-update on the target machine. Unlike the apt script, this fails the whole transaction if any one requested build is missing from the enabled repositories, which is the safer behavior.
Controlling timing with the tools you already have
You do not always need replay. Some simpler options give a similar effect.
- Stagger the schedule. If you use
unattended-upgradesordnf-automatic, run the test group on one timer and production on a later one. Usesystemctl edit dnf-automatic.timerto add aRandomizedDelaySecor a differentOnCalendarvalue for each group. - Use holds as a gate. Hold a package on production with
apt-mark holdordnf versionlock addwhile the canary proves it. Release the hold once you are satisfied. Remember to remove holds afterward; a forgotten hold quietly stops security fixes. - Use security-only updates in production. Restrict production to security fixes with
dnf upgrade --security, or to the security origin on Debian and Ubuntu, and apply the wider set of updates to the test group first. This lowers the volume of change where it matters most.
Run a stage and decide whether to go on
A stage has three parts: apply, verify, and decide. Write down the checks before you start, so that a pass or fail is a fact and not an impression.
Use this flow for each stage.
Verification checks should be small and automatic. A reasonable minimum is:
- No systemd units in a failed state.
- The services this machine exists to run are active.
- A real request succeeds, such as an HTTP check against the application.
- No new errors in the journal since the patch started.
- The reboot-required signal is understood and handled.
This script covers the first four on a systemd machine. Adjust the service names and URL for your environment.
#!/usr/bin/env bash
# verify-patch.sh: basic post-patch checks. Exits non-zero on the first failure.
set -u
SERVICES=("nginx")
URL="http://localhost/"
SINCE="${1:-15 minutes ago}"
fail=0
failed=$(systemctl --failed --no-legend | wc -l)
if [ "$failed" -ne 0 ]; then
echo "FAIL: $failed failed unit(s)"
systemctl --failed --no-legend
fail=1
fi
for svc in "${SERVICES[@]}"; do
if ! systemctl is-active --quiet "$svc"; then
echo "FAIL: $svc is not active"
fail=1
fi
done
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "$URL" || true)
if [ "$code" != "200" ]; then
echo "FAIL: $URL returned '$code'"
fail=1
fi
errors=$(journalctl -p err --since "$SINCE" --no-pager -q | wc -l)
if [ "$errors" -ne 0 ]; then
echo "WARN: $errors error line(s) in the journal since $SINCE"
fi
[ "$fail" -eq 0 ] && echo "OK: checks passed"
exit "$fail"Journal errors are reported as a warning because some are harmless noise. Read them before you decide.
Soak time and batch size
A check that passes immediately can still miss slow failures, such as a memory leak, a cron job that runs nightly, or a certificate renewal. Leave time between stages. For a canary, one full business day is a common minimum, and a weekly job means you may want to wait for it.
For the production stage, patch in batches and keep enough capacity serving. If a service runs on six machines behind a load balancer, take out one or two at a time, patch them, verify, and return them to service before the next batch. If you patch the whole pool at once, a bad update takes everything down together.
Reboots belong in this plan too. A kernel update takes effect only after a reboot, so a canary that has not rebooted has not tested the new kernel. Reboot canary machines as part of the stage.
Plan the rollback before you start
Decide how you will undo a bad patch before you apply it, because the options differ.
- On RHEL-family systems,
dnf history undocan reverse a transaction, provided the older package versions are still available in a repository. Test that on the test stage. - On Debian and Ubuntu,
apthas no undo. You can reinstall an older version withapt install package=oldversion, if it is still available in the repositories or in/var/cache/apt/archives. The dependencies may also need downgrading. - For anything that touches data formats, such as a database server, a package downgrade may not be enough because the new version may have already migrated files. A snapshot or backup is the dependable rollback.
For virtual machines and cloud instances, a snapshot before patching is the most reliable plan. Confirm that you can restore from it. An untested restore is only a hope.
Common mistakes
- Treating the test machine as representative when it is not. Different packages, configuration, or data volume hide problems.
- Testing one set of updates and deploying another. Record versions, patch close together, or freeze the repository.
- Skipping the reboot on canaries. Kernel and core-library fixes go untested.
- Verifying only that the host is up. A machine can answer SSH while its application is broken.
- Forgetting holds. A held package stays vulnerable until someone removes the hold.
- No stop rule. Decide in advance which failure halts the rollout and who may decide to continue.
Exercise
You patched your test machine on Monday and recorded its installed versions. On Friday you run apt install --only-upgrade with those exact versions on a production server, and apt reports that one tested version cannot be found. What does this tell you, and what should you do?
Show answer
The repository no longer offers the version you tested, which usually means a newer build replaced it. You have not tested the build that production would now receive. Do not fall back to installing whatever is newest without checking. Re-run the update on the test stage, verify it, record the new versions, and then continue the rollout with that list. A frozen repository snapshot would avoid this problem.
Exercise
Your canary servers passed every check right after patching, but a week later a nightly report job fails on production after the full rollout. Name two changes to your routine that would have been more likely to catch it.
Show answer
First, lengthen the soak time on the canary so that it runs through at least one nightly cycle, or the weekly cycle if the job is weekly, before production starts. Second, add a check that exercises the job, such as running the report once manually on the canary after patching, or choose a canary that runs the same jobs as production. Rebooting the canary and monitoring its logs over the soak period also helps.