8000 fix(min): print only status, warnings and next steps from finalize-install by minimal-backporter[bot] · Pull Request #4068 · gominimal/minimal · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
174 changes: 139 additions & 35 deletions crates/minimal/src/cmd/finalize_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,22 @@ impl Plan {
Some(script)
}

/// The summary: a ✓/✗ line per item, what a missing one costs today
/// and what the step changes, and the items no script can fix under
/// their own heading. [`FINISHED`] alone on a finished host.
/// The summary `--show` prints: a ✓/✗ line per item, what a missing one
/// costs today and what the step changes, and the items no script can
/// fix under their own heading. [`FINISHED`] alone on a finished host.
pub(crate) fn summary(&self) -> String {
self.render(true)
}

/// The summary a run prints before it elevates, and `--show --script`
/// prints beside the script: [`Plan::summary`] without what each step
/// changes. `--show` and the script itself say that.
pub(crate) fn brief(&self) -> String {
self.render(false)
}

fn render(&self, detail: bool) -> String {
let today_label = if detail { "today: " } else { "" };
if self.finished() {
return format!("{FINISHED}\n");
}
Expand All @@ -354,10 +366,12 @@ impl Plan {
_ => {
out.push_str(&format!(" ✗ {}\n", item.label));
match (&item.today, item.today_list.as_slice()) {
(Some(today), []) => out.push_str(&format!(" today: {today}\n")),
(Some(today), []) => {
out.push_str(&format!(" {today_label}{today}\n"));
}
(Some(_), list) => {
out.push_str(&format!(
" today: {} thing{} to fix:\n",
" {today_label}{} thing{} to fix:\n",
list.len(),
if list.len() == 1 { "" } else { "s" }
));
Expand All @@ -367,7 +381,7 @@ impl Plan {
}
(None, _) => {}
}
if let Some(step) = &item.step {
if let Some(step) = item.step.as_ref().filter(|_| detail) {
out.push_str(&format!(" this step: {step}\n"));
}
}
Expand Down Expand Up @@ -464,7 +478,7 @@ pub async fn cmd_finalize_install(
}
if args.show {
if args.script {
eprint!("{}", plan.summary());
eprint!("{}", plan.brief());
if let Some(script) = plan.script() {
print!("{script}");
}
Expand All @@ -473,7 +487,7 @@ pub async fn cmd_finalize_install(
}
return Ok(());
}
print!("{}", plan.summary());
print!("{}", plan.brief());
let Some(script) = plan.script() else {
// Nothing a script can do here: a finished host (exit 0), or one
// whose every open item is blocked or waiting (exit 1).
Expand Down Expand Up @@ -1037,6 +1051,12 @@ pub(crate) mod linux {
/// installed the socket unit that places the restarted daemon.
pub(crate) const PLACE_NO_HOLDER: &str = "no process holds ";

/// The `case` pattern for the lines the step's `--place-listener` prints
/// about a holder it did not place. The classifier block prints these
/// lines from a placement that succeeded, and drops the progress lines.
pub(crate) const PLACE_UNPLACED_LINES: &str =
"'left '*' alone: '* | 'could not place '* | 'every holder of '*";

pub( 10BC8 crate) const USERNS_ID: &str = "userns-profile";
const USERNS_LABEL: &str = "the private sandbox every box runs in";
pub(crate) const CLASSIFIER_ID: &str = "classifier";
Expand Down Expand Up @@ -1613,7 +1633,7 @@ pub(crate) mod linux {
[ ! -e {CLASSIFIER_PROGRAM_PATH} ] || cp -p {CLASSIFIER_PROGRAM_PATH} \
{CLASSIFIER_PROGRAM_PATH}.prev\n\
mv -f {CLASSIFIER_PROGRAM_PATH}.new {CLASSIFIER_PROGRAM_PATH}\n\
if {CLASSIFIER_PROGRAM_PATH} --user '{operator}'{mask}; then\n\
if {CLASSIFIER_QUIET} {CLASSIFIER_PROGRAM_PATH} --user '{operator}'{mask}; then\n\
\x20 rm -f {CLASSIFIER_PROGRAM_PATH}.prev\n\
else\n\
\x20 classifier_status=$?\n\
Expand Down Expand Up @@ -1686,21 +1706,29 @@ pub(crate) mod linux {
chmod 0644 {CLASSIFIER_UNIT_SERVICE_PATH} {PLACE_UNIT_SERVICE_PATH} \
{PLACE_UNIT_SOCKET_PATH}\n\
systemctl daemon-reload\n\
systemctl enable {CLASSIFIER_SYSTEMD_UNIT}.service\n\
systemctl enable --now {PLACE_SYSTEMD_UNIT}.socket\n\
systemctl enable --quiet {CLASSIFIER_SYSTEMD_UNIT}.service\n\
systemctl enable --quiet --now {PLACE_SYSTEMD_UNIT}.socket\n\
place_units=yes\n\
fi\n\
# The daemon that may already be listening is placed now; later starts ask\n\
# the socket unit. A daemon that was running read this machine before the\n\
# step installed, so the run says how to make it read the machine again: a\n\
# restart, which the socket unit places. Without the units a restarted\n\
# daemon stands outside its leaf, so the run does not advise one. A placement\n\
# that fails still prints what it said, and the run exits with its status.\n\
# that fails prints what it said, and the run exits with its status. One\n\
# that succeeds prints only the lines about a holder it did not place.\n\
place_status=0\n\
place_out=$({CLASSIFIER_PROGRAM_PATH} --user '{operator}' \
--place-listener '{socket}') || place_status=$?\n\
printf '%s\\n' \"$place_out\"\n\
[ \"$place_status\" -eq 0 ] || exit \"$place_status\"\n\
if [ \"$place_status\" -ne 0 ]; then\n\
\x20 printf '%s\\n' \"$place_out\"\n\
\x20 exit \"$place_status\"\n\
fi\n\
printf '%s\\n' \"$place_out\" | while IFS= read -r place_line; do\n\
\x20 case \"$place_line\" in\n\
\x20 {PLACE_UNPLACED_LINES}) printf '%s\\n' \"$place_line\" ;;\n\
\x20 esac\n\
done\n\
case \"$place_units:$place_out\" in\n\
no:*) ;;\n\
*'{PLACE_NO_HOLDER}'*) ;;\n\
Expand Down Expand Up @@ -1827,6 +1855,11 @@ pub(crate) mod linux {
}
}

/// The setting the script runs the classifier step under, so the step
/// prints its warnings and errors and not its progress. The placement
/// call runs without it, because the script reads what that call prints.
const CLASSIFIER_QUIET: &str = "MINIMAL_CLASSIFIER_QUIET=1";

/// The QUIC block helper's block: the classifier step's helper-only
/// mode, from a staged copy of the bytes this binary carries. The copy
/// the boot unit runs is left alone, and the staged copy is removed
Expand All @@ -1844,7 +1877,7 @@ pub(crate) mod linux {
{CLASSIFIER_SCRIPT_HEREDOC}\n\
chmod 0755 {staged}\n\
quic443_status=0\n\
{staged} --user '{operator}' --quic443-helper-only || quic443_status=$?\n\
{CLASSIFIER_QUIET} {staged} --user '{operator}' --quic443-helper-only || quic443_status=$?\n\
rm -f {staged}\n"
)
}
Expand Down Expand Up @@ -3178,6 +3211,29 @@ mod tests {
);
}

/// A run, and `--show --script`, print the short summary: a ✓ or ✗ per
/// item and what a missing one costs today, without what the step
/// changes.
#[test]
fn finalize_install_run_prints_the_summary_without_the_steps() {
let plan = Plan {
items: vec![
done("userns-profile"),
missing("names", "names"),
Item::waiting("kvm-group", "box names", "no daemon".to_string()),
],
};
assert_eq!(
plan.brief(),
"install status on this machine:\n ✓ a done item\n ✗ a missing item\n \
what it costs today\n ✗ box names — waiting on a daemon (no daemon)\n"
);
let finished = Plan {
items: vec![done("names")],
};
assert_eq!(finished.brief(), finished.summary());
}

/// NET-122: `--show` names what is missing — a ✓ or ✗ per item, what a
/// missing one costs today and what the step changes — and nothing in
/// it is a script, a prompt or an elevation.
Expand Down Expand Up @@ -3847,7 +3903,7 @@ mod tests {
};
let run = plan.script().expect("a script while items are missing");
let program = format!(
"if {} --user 'alice'; then\n",
"if MINIMAL_CLASSIFIER_QUIET=1 {} --user 'alice'; then\n",
crate::resolver::CLASSIFIER_PROGRAM_PATH
);
assert!(
Expand Down Expand Up @@ -3918,7 +3974,7 @@ mod tests {
.expect("the helper's own block runs the helper-only mode");
let classifier_at = script
.find(&format!(
"\nif {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"
"\nif MINIMAL_CLASSIFIER_QUIET=1 {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"
))
.expect("the classifier step");
assert!(
Expand Down Expand Up @@ -4023,7 +4079,7 @@ mod tests {
assert!(
helper_at
< at(&format!(
"\nif {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"
"\nif MINIMAL_CLASSIFIER_QUIET=1 {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"
)),
"the helper's block runs ahead of the classifier step: {script}"
);
Expand Down Expand Up @@ -4439,7 +4495,8 @@ mod tests {
let script = stale.script.expect("a stale copy carries the block");
assert!(
script.contains(&format!(
"\nif {CLASSIFIER_PROGRAM_PATH} --user 'alice' --ct-mark-mask 0x0c000000; then\n"
"\nif MINIMAL_CLASSIFIER_QUIET=1 {CLASSIFIER_PROGRAM_PATH} --user 'alice' \
--ct-mark-mask 0x0c000000; then\n"
)),
"the refresh runs the step with the recorded mask: {script}"
);
Expand Down Expand Up @@ -4790,7 +4847,7 @@ mod tests {
};
use linux::{
Cgroup2Mount, ClassifierFacts, DAEMON_RUNNING_NOTE, PLACE_NO_HOLDER,
classifier_item_over,
PLACE_UNPLACED_LINES, classifier_item_over,
};
use paths::PLACE_REQUEST_SOCKET;
let sock = "/home/alice/.local/state/minimal/providers/local-minimald0/ssh.sock";
Expand All @@ -4816,7 +4873,9 @@ mod tests {
format!("{CLASSIFIER_SCRIPT}{CLASSIFIER_SCRIPT_HEREDOC}\n"),
format!("chmod 0755 {CLASSIFIER_PROGRAM_PATH}.new\n"),
format!("mv -f {CLASSIFIER_PROGRAM_PATH}.new {CLASSIFIER_PROGRAM_PATH}\n"),
format!("if {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"),
format!(
"if MINIMAL_CLASSIFIER_QUIET=1 {CLASSIFIER_PROGRAM_PATH} --user 'alice'; then\n"
),
format!(": > {CLASSIFIER_TREE_RECORD}\n"),
format!("cat > {CLASSIFIER_UNIT_SERVICE_PATH} <<"),
format!("ExecStart={CLASSIFIER_PROGRAM_PATH} --user 'alice'\n"),
Expand All @@ -4838,8 +4897,9 @@ mod tests {
"WantedBy=sockets.target\n".to_string(),
"WantedBy=multi-user.target\n".to_string(),
"systemctl daemon-reload\n".to_string(),
"systemctl enable minimald-classifier.service\n".to_string(),
"systemctl enable --now minimald-place.socket\nplace_units=yes\nfi\n".to_string(),
"systemctl enable --quiet minimald-classifier.service\n".to_string(),
"systemctl enable --quiet --now minimald-place.socket\nplace_units=yes\nfi\n"
.to_string(),
"if ! command -v systemctl >/dev/null 2>&1 ; then\n".to_string(),
" place_units=no\nelse\n".to_string(),
] {
Expand Down Expand Up @@ -4867,7 +4927,7 @@ mod tests {
// to restart it when the socket unit will place the restarted daemon
// (gominimal/inbox#1054).
let (_, after_units) = unit_part
.rsplit_once("\nfi\n")
.split_once("place_units=yes\nfi\n")
.expect("the unit guard closes");
assert!(
after_units.starts_with(&format!(
Expand All @@ -4876,13 +4936,21 @@ mod tests {
# step installed, so the run says how to make it read the machine again: a\n\
# restart, which the socket unit places. Without the units a restarted\n\
# daemon stands outside its leaf, so the run does not advise one. A placement\n\
# that fails still prints what it said, and the run exits with its status.\n\
# that fails prints what it said, and the run exits with its status. One\n\
# that succeeds prints only the lines about a holder it did not place.\n\
place_status=0\n\
place_out=$({CLASSIFIER_PROGRAM_PATH} --user 'alice' --place-listener '{sock}') \
|| place_status=$?\n"
)) && after_units.ends_with(&format!(
"printf '%s\\n' \"$place_out\"\n\
[ \"$place_status\" -eq 0 ] || exit \"$place_status\"\n\
"if [ \"$place_status\" -ne 0 ]; then\n\
\x20 printf '%s\\n' \"$place_out\"\n\
\x20 exit \"$place_status\"\n\
fi\n\
printf '%s\\n' \"$place_out\" | while IFS= read -r place_line; do\n\
\x20 case \"$place_line\" in\n\
\x20 {PLACE_UNPLACED_LINES}) printf '%s\\n' \"$place_line\" ;;\n\
\x20 esac\n\
done\n\
case \"$place_units:$place_out\" in\nno:*) ;;\n\
*'{PLACE_NO_HOLDER}'*) ;;\n*) echo '{DAEMON_RUNNING_NOTE}' ;;\nesac\n"
)),
Expand Down Expand Up @@ -4946,7 +5014,7 @@ mod tests {
use crate::resolver::{ANSWERER_PROGRAM_DIR, CLASSIFIER_SCRIPT};
use linux::{
Cgroup2Mount, ClassifierFacts, DAEMON_RUNNING_NOTE, PLACE_NO_HOLDER,
classifier_item_over,
PLACE_UNPLACED_LINES, classifier_item_over,
};
use std::os::unix::fs::PermissionsExt;

Expand Down Expand Up @@ -5093,9 +5161,9 @@ mod tests {
echo \"placed 4242 in the daemon leaf (it holds $4)\"\nexit 0\n";
let (root, code, copy, units, printed) = run_block(installing, true, true);
assert!(
printed.contains("placed 4242 in the daemon leaf")
!printed.contains("placed 4242 in the daemon leaf")
&& printed.contains(DAEMON_RUNNING_NOTE),
"the placement's own lines print, then the restart note: {printed}"
"the restart note prints, and the placement's progress line does not: {printed}"
);
assert_eq!(code, Some(0));
assert_eq!(
Expand All @@ -5114,18 +5182,38 @@ mod tests {
assert!(root.path().join("var/finalize-install-classifier").exists());
let log = std::fs::read_to_string(root.path().join("systemctl.log"))
.expect("the units are enabled");
assert!(log.contains("enable --now minimald-place.socket"), "{log}");
assert!(
log.contains("enable --quiet --now minimald-place.socket"),
"{log}"
);

// No daemon holds the socket: the placement says so, and the run
// has no daemon to restart.
// No daemon holds the socket: the run has no daemon to restart, and
// prints nothing about the placement.
let nobody = "#!/bin/sh\n[ \"${3:-}\" != --place-listener ] || \
echo \"no process holds $4: nothing to place\"\nexit 0\n";
let (_nobody, code, _, _, printed) = run_block(nobody, true, true);
assert_eq!(code, Some(0));
assert!(
printed.contains(PLACE_NO_HOLDER) && !printed.contains(DAEMON_RUNNING_NOTE),
!printed.contains(PLACE_NO_HOLDER) && !printed.contains(DAEMON_RUNNING_NOTE),
"no daemon, no restart note: {printed}"
);
// A placement that succeeds and leaves a holder where it stands: the
// lines about that holder print, and the progress line does not.
let leaving = "#!/bin/sh\n[ \"${3:-}\" != --place-listener ] || {\n\
echo \"no holder of $4 was found on the first pass; scanned again\"\n\
echo \"left 4243 alone: it holds $4 but its cgroup could not be read\"\n\
echo \"could not place 4244 in the leaf: it has gone\"\n\
echo \"every holder of $4 was left where it stands: nothing placed\"\n\
}\nexit 0\n";
let (_leaving, code, _, _, printed) = run_block(leaving, true, true);
assert_eq!(code, Some(0));
assert!(
printed.contains("left 4243 alone: ")
&& printed.contains("could not place 4244 ")
&& printed.contains("every holder of ")
&& !printed.contains("scanned again"),
"a holder left unplaced is reported: {printed}"
);
// A placement that fails: its own lines still print, the run exits
// with its status, and there is no restart note.
let failing = "#!/bin/sh\n[ \"${3:-}\" != --place-listener ] || \
Expand All @@ -5143,7 +5231,7 @@ mod tests {
assert_eq!(code, Some(0));
assert!(
printed.contains("the placement socket is not installed")
&& printed.contains("placed 4242 in the daemon leaf")
&& !printed.contains("placed 4242 in the daemon leaf")
&& !printed.contains(DAEMON_RUNNING_NOTE),
"no units, no restart note: {printed}"
);
Expand All @@ -5154,6 +5242,22 @@ mod tests {
)),
"the step still says {PLACE_NO_HOLDER:?} when no process holds the socket"
);
// The lines the block prints from a placement are the ones the step
// prints about a holder it did not place.
assert_eq!(
PLACE_UNPLACED_LINES,
"'left '*' alone: '* | 'could not place '* | 'every holder of '*"
);
for line in [
"warn \"left $holder_pid alone: it holds $listener but ",
"warn \"could not place $holder_pid in $procs: ",
"warn \"every holder of $listener was left where it stands, ",
] {
assert!(
CLASSIFIER_SCRIPT.contains(line),
"the step still prints {line:?}"
);
}
// The note names the one command that stops the running boxes and then the daemon,
// and the block can echo it whole.
assert!(
Expand Down
Loading
Loading
0