Skip to content

Commit 4413f71

Browse files
authored
Merge pull request #173 from cgwalters/add-apply-option
2 parents daeb900 + 061393c commit 4413f71

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

lib/src/cli.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@ pub(crate) struct UpgradeOpts {
3333
#[clap(long)]
3434
pub(crate) touch_if_changed: Option<Utf8PathBuf>,
3535

36-
/// Check if an update is available without applying it
37-
#[clap(long)]
36+
/// Check if an update is available without applying it.
37+
#[clap(long, conflicts_with = "apply")]
3838
pub(crate) check: bool,
39+
40+
/// Restart or reboot into the new target image.
41+
///
42+
/// Currently, this option always reboots. In the future this command
43+
/// will detect the case where no kernel changes are queued, and perform
44+
/// a userspace-only restart.
45+
#[clap(long, conflicts_with = "check")]
46+
pub(crate) apply: bool,
3947
}
4048

4149
/// Perform an switch operation
@@ -364,6 +372,11 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
364372
if let Some(path) = opts.touch_if_changed {
365373
std::fs::write(&path, "").with_context(|| format!("Writing {path}"))?;
366374
}
375+
if opts.apply {
376+
crate::reboot::reboot()?;
377+
}
378+
} else {
379+
tracing::debug!("No changes");
367380
}
368381

369382
Ok(())

lib/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
pub mod cli;
1717
pub(crate) mod deploy;
1818
mod lsm;
19+
mod reboot;
1920
mod reexec;
2021
mod status;
2122
mod utils;

lib/src/reboot.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//! Handling of system restarts/reboot
2+
3+
use std::io::Write;
4+
use std::process::Command;
5+
6+
use fn_error_context::context;
7+
8+
/// Initiate a system reboot.
9+
/// This function will only return in case of error.
10+
#[context("Initiating reboot")]
11+
pub(crate) fn reboot() -> anyhow::Result<()> {
12+
// Flush output streams
13+
let _ = std::io::stdout().flush();
14+
let _ = std::io::stderr().flush();
15+
let st = Command::new("reboot").status()?;
16+
if !st.success() {
17+
anyhow::bail!("Failed to reboot: {st:?}");
18+
}
19+
tracing::debug!("Initiated reboot, sleeping forever...");
20+
loop {
21+
std::thread::park();
22+
}
23+
}

0 commit comments

Comments
 (0)