Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upgrade --apply #173

Merged
merged 1 commit into from
Nov 3, 2023
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
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();
}
}
Loading