Skip to content

Commit

Permalink
fix:doc,code refactoring and errors
Browse files Browse the repository at this point in the history
Signed-off-by: Sayan Paul <[email protected]>
  • Loading branch information
say-paul committed Apr 19, 2024
1 parent c33bac4 commit ee8bd08
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
28 changes: 13 additions & 15 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/// This module contains most of the low-level commands
/// and grub variable modifications
use anyhow::{bail, Result};
use anyhow::{bail, Ok, Result};

use std::process::Command;
use std::str;

/// reboots the system if boot_counter is greater than 0 or can be forced too
pub fn handle_reboot(force: bool) -> Result<()> {
if !force {
if let Some(t) = get_boot_counter()? {
if t <= 0 {
bail!("countdown ended, check greenboot-rollback status")
};
}
let boot_counter = get_boot_counter()?;
if boot_counter <= Some(0) {
bail!("countdown ended, check greenboot-rollback status")
};
}
log::info!("restarting the system");
Command::new("systemctl").arg("reboot").status()?;
Expand All @@ -21,21 +20,21 @@ pub fn handle_reboot(force: bool) -> Result<()> {

/// rollback to previous ostree deployment if boot counter is less than 0
pub fn handle_rollback() -> Result<()> {
if let Some(boot_counter) = get_boot_counter()? {
if boot_counter <= 0 {
let boot_counter = get_boot_counter()?;
if boot_counter <= Some(0) {
log::info!("Greenboot will now attempt to rollback");
Command::new("rpm-ostree").arg("rollback").status()?;
return Ok(());
}
}
bail!("Rollback not initiated");
}

/// sets grub variable boot_counter if not set
pub fn set_boot_counter(reboot_count: u16) -> Result<()> {
match get_boot_counter() {
Ok(Some(current_counter)) => {
log::info!("boot_counter={current_counter}");
let current_counter= get_boot_counter()?;
match current_counter{
Some(counter) => {
log::info!("boot_counter={counter}");
bail!("counter already set");
}
_ => {
Expand All @@ -62,10 +61,9 @@ pub fn set_boot_status(success: bool) -> Result<()> {
if success {
set_grub_var("boot_success", 1)?;
unset_boot_counter()?;
} else {
set_grub_var("boot_success", 0)?;
return Ok(());
}
Ok(())
set_grub_var("boot_success", 0)
}

/// writes greenboot status to motd.d/boot-status
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Cli {
command: Commands,
}
#[derive(Debug, Deserialize)]
///config params for greenboot
/// config params for greenboot
struct GreenbootConfig {
max_reboot: u16,
}
Expand Down Expand Up @@ -128,7 +128,7 @@ fn run_diagnostics() -> Result<()> {
}
}
if !path_exists {
bail!("required.d not found");
bail!("cannot find any required.d folder");
}
for path in GREENBOOT_INSTALL_PATHS {
let greenboot_wanted_path = format!("{path}/check/wanted.d/");
Expand Down Expand Up @@ -281,12 +281,12 @@ mod tests {

use super::*;

///validate when the required folder is not found
/// validate when the required folder is not found
#[test]
fn missing_required_folder() {
assert_eq!(
run_diagnostics().unwrap_err().to_string(),
String::from("required.d not found")
String::from("cannot find any required.d folder")
);
}

Expand Down

0 comments on commit ee8bd08

Please sign in to comment.