Write a script that is not idempotent, then make it so
Task
Automation gets run twice -- by a retry, by two people, by an overlapping schedule. Write one that breaks when it happens, observe the damage, and then fix it properly rather than by adding a lock.
Steps
- Write
lab/auto/setup-v1.shdoing three things with no existence checks: create a user entry in a file, append a configuration line, and start a container with a fixed name. - Run it once and confirm the result. Run it a SECOND time and record exactly what happened to each of the three in
lab/auto/rerun.md-- one will error, one will duplicate, one may silently corrupt. - Write
lab/auto/setup-v2.shmaking each action idempotent: check before creating, ensure-a-line rather than append, and handle the container already existing. - Run v2 five times in a row and confirm the end state is identical after each. Record the evidence.
- Add fail-loudly behaviour: check every result, exit non-zero on any failure, and prove it by breaking one step deliberately and capturing the non-zero exit in
lab/auto/failure.txt.
Verify
grep -Eci 'duplicate|twice|error' lab/auto/rerun.md
bash lab/auto/setup-v2.sh >/dev/null && bash lab/auto/setup-v2.sh >/dev/null && echo "v2 re-runnable"
test -s lab/auto/failure.txt && grep -Ec 'exit|non-zero|[1-9]' lab/auto/failure.txt
v2 must run twice cleanly and the failure capture must show a non-zero exit. Automation that fails silently is worse than none, because everyone believes the task was done.
This is an independent study companion for CompTIA Cloud+ CV0-004 and is not produced by or endorsed by CompTIA.