Repository navigation
fix(ami): clean --command run state before CreateImage; run --command once per instance (#1057) - #1240
Merged
Conversation
Contributor
Author
|
Follow-up for the per-instance state this PR leaves out of scope: #1241 (Refs, not closing). |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
… once per instance (#1057) spawn ami create runs launcher.ImageStateCleanupScript over SSM before CreateImage, removing the completion file (tag or default), /tmp/SPAWN_*, the rendered and embedded command, the sweep env and the gate list; it refuses to image (unless --force) when the clean cannot run or fails. The bootstrap writes /var/lib/spawn/command-ran-<instance-id> before starting --command and skips the command when it exists, so the CreateImage reboot after a cloud-init clean does not re-run it, while an instance launched from the image runs its own.
scttfrdmn
force-pushed
the
fix/ami-clean-1057
branch
from
October 20, 2026 17:40
71567db to
e386fec
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1057
What changes
spawn ami create/spawn create-amiclean the instance's run state before CreateImage.Before calling CreateImage, spawn runs
launcher.ImageStateCleanupScripton thesource instance over SSM (
AWS-RunShellScript, as root). The script removes:spawn:completion-filetag, otherwise the default, and/tmp/SPAWN_COMPLETEin both cases;/tmp/SPAWN_*file, which includes the exit code;/tmp/spawn-command.shand/etc/spawn/command. The embedded command was alsobeing baked in, so an instance launched from the image with no
--commandwould have run the old command;
/etc/profile.d/spawn-params.shand/run/spawn/required-gates;command-ran-*markers inherited from a parent image. The source instance's ownmarker is kept.
The script also checks that every one of those paths is gone, and prints an ok
line. It refuses with exit 3 while
--commandis still running, meaning the markerexists but there is no exit code and no completion file yet.
Refusal. spawn does not create the image if any of these happen:
The error names
--force, which images anyway and prints a warning. Windowsinstances are skipped because the Windows bootstrap runs no
--command(#1022).Behaviour change: imaging a stopped Linux instance now needs
--force.The bootstrap runs
--commandonce per instance. Inside the backgroundedsubshell, after the Spot-replacement check, it now:
/var/lib/spawn/command-ran-$INSTANCE_IDand, if the file exists, skips the command and writes no completion file;Two effects:
cloud-init clean, the CreateImage reboot no longer re-runs the command on the source instance.--command.To re-run a command on purpose, delete the marker. The log message says this.
Gates, each watched failing against a revert that compiles
TestImageStateCleanupRemovesEveryDiscoveredRunStatePathdiscovers the run-state set from the rendered bootstrap rather than from a list. It takes every file written under/tmp, the completion file, and every file the bootstrap both writes and tests with[ -s|-f|-e ]. It fails if discovery finds nothing./data/out dir/done.json,/etc/profile.d/spawn-params.sh,/etc/spawn/command,/run/spawn/required-gates,/tmp/SPAWN_EXITCODE,/tmp/spawn-command.shand/var/lib/spawn/command-ran-$INSTANCE_ID.sh. Then it asserts that everything is removed except this instance's marker and the command log./etc/spawn/commandfrom the script, and when I removed the keep-own-marker line.TestCommandRunsOncePerInstanceIDruns the real--commandblock three times in the existing bash sandbox: same instance ID, same ID again, then a new ID. The workload ran once, was skipped, then ran again. With the marker check replaced byif false, the workload ran twice on the same instance.TestAMICreate_CleansRunStateOverSSMBeforeCreateImageuses a fake SSM runner and a fake CreateImage that record call order. It asserts SSM runs before CreateImage, and that the script isImageStateCleanupScript(id, tag completion file).TestAMICreate_RefusesWhenTheCleanFailscovers six failure modes. Each one must not call CreateImage, and--forcemust create the image. It failed when I disabled the refusal.TestRunCreateAMIGoesThroughTheCleaningImageris an AST check thatrunCreateAMIcallscreateFromInstanceand neverCreateAMIdirectly. It failed when I restored the direct call.Checks
check-build-tags, which fails identically on unmodified main on this machine.Not done here
--commandrun. Other per-instance state also gets baked in:/etc/spawn/plugins.json, which spored loads at every startup,/etc/profile.d/job-array.shand the peers files. That is filed separately (see the follow-up issue linked in a comment).