Skip to content

Commit

Permalink
chore: some code refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Sayan Paul <paul.sayan@gmail.com>
  • Loading branch information
say-paul committed Sep 14, 2023
1 parent c81d510 commit ce3a2ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
25 changes: 10 additions & 15 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,30 @@ pub fn handle_reboot(force: bool) -> Result<(), Error> {

/// rollback to previous ostree deployment if boot counter is les than 0
pub fn handle_rollback() -> Result<(), Error> {
match get_boot_counter() {
Some(t) if t <= 0 => {
if let Some(t) = get_boot_counter() {
if t <= 0 {
log::info!("Greenboot will now attempt rollback");
let status = Command::new("rpm-ostree").arg("rollback").status()?;
if status.success() {
return Ok(());
}
bail!(status.to_string());
}
_ => log::info!("Rollback not initiated as boot_counter is either unset or not equal to 0"),
}
log::info!("Rollback not initiated as boot_counter is either unset or not equal to 0");
Ok(())
}

/// sets grub variable boot_counter if not set
pub fn set_boot_counter(reboot_count: i32) -> Result<()> {
match get_boot_counter() {
Some(counter) => {
log::info!("boot_counter={counter}");
Ok(())
}
None => {
if set_grub_var("boot_counter", reboot_count) {
log::info!("boot_counter={reboot_count}");
return Ok(());
}
bail!("grub returned error");
}
if let Some(current_counter) = get_boot_counter() {
log::info!("boot_counter={current_counter}");

} else if set_grub_var("boot_counter", reboot_count) {
log::info!("boot_counter={}", reboot_count);
return Ok(());
}
bail!("Failed to set GRUB variable: boot_counter");
}

/// resets grub variable boot_counter
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fn run_green() -> Result<(), Error> {
fn health_check() -> Result<()> {
let config = GreenbootConfig::get_config();
log::info!("{config:?}");
handle_motd("healthcheck is in progress").ok();
handle_motd("healthcheck is in progress")?;
let run_status = run_diagnostics();
match run_status {
Ok(()) => {
Expand All @@ -205,7 +205,7 @@ fn health_check() -> Result<()> {
.unwrap_or_else(|e| log::error!("cannot set boot_counter as: {}", e.to_string()));
handle_reboot(false)
.unwrap_or_else(|e| log::error!("cannot reboot as: {}", e.to_string()));
bail!(e);
Err(e)
}
}
}
Expand Down

0 comments on commit ce3a2ef

Please sign in to comment.