Skip to content

Commit

Permalink
Merge pull request #173 from cgwalters/add-apply-option
Browse files Browse the repository at this point in the history
  • Loading branch information
jlebon authored Nov 3, 2023
2 parents daeb900 + 061393c commit 4413f71
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ pub(crate) struct UpgradeOpts {
#[clap(long)]
pub(crate) touch_if_changed: Option<Utf8PathBuf>,

/// Check if an update is available without applying it
#[clap(long)]
/// Check if an update is available without applying it.
#[clap(long, conflicts_with = "apply")]
pub(crate) check: bool,

/// Restart or reboot into the new target image.
///
/// Currently, this option always reboots. In the future this command
/// will detect the case where no kernel changes are queued, and perform
/// a userspace-only restart.
#[clap(long, conflicts_with = "check")]
pub(crate) apply: bool,
}

/// Perform an switch operation
Expand Down Expand Up @@ -364,6 +372,11 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
if let Some(path) = opts.touch_if_changed {
std::fs::write(&path, "").with_context(|| format!("Writing {path}"))?;
}
if opts.apply {
crate::reboot::reboot()?;
}
} else {
tracing::debug!("No changes");
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
pub mod cli;
pub(crate) mod deploy;
mod lsm;
mod reboot;
mod reexec;
mod status;
mod utils;
Expand Down
23 changes: 23 additions & 0 deletions lib/src/reboot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Handling of system restarts/reboot
use std::io::Write;
use std::process::Command;

use fn_error_context::context;

/// Initiate a system reboot.
/// This function will only return in case of error.
#[context("Initiating reboot")]
pub(crate) fn reboot() -> anyhow::Result<()> {
// Flush output streams
let _ = std::io::stdout().flush();
let _ = std::io::stderr().flush();
let st = Command::new("reboot").status()?;
if !st.success() {
anyhow::bail!("Failed to reboot: {st:?}");
}
tracing::debug!("Initiated reboot, sleeping forever...");
loop {
std::thread::park();
}
}

0 comments on commit 4413f71

Please sign in to comment.