From ee6f2a5bb0a20fb618c852cff708ae28cdd0fbe2 Mon Sep 17 00:00:00 2001 From: Devdutt Shenoi Date: Tue, 20 Feb 2024 12:08:06 +0530 Subject: [PATCH] Revert "feat: precondition checks as a feature (#322)" This reverts commit 50181309a8814d06d3ec6c5ad49023f7c67e186f. --- uplink/src/collector/mod.rs | 1 - uplink/src/collector/preconditions.rs | 84 --------------------------- uplink/src/config.rs | 7 --- uplink/src/lib.rs | 9 +-- 4 files changed, 1 insertion(+), 100 deletions(-) delete mode 100644 uplink/src/collector/preconditions.rs diff --git a/uplink/src/collector/mod.rs b/uplink/src/collector/mod.rs index 903663c3..3e2742bd 100644 --- a/uplink/src/collector/mod.rs +++ b/uplink/src/collector/mod.rs @@ -5,7 +5,6 @@ pub mod installer; pub mod journalctl; #[cfg(target_os = "android")] pub mod logcat; -pub mod preconditions; pub mod process; pub mod script_runner; pub mod simulator; diff --git a/uplink/src/collector/preconditions.rs b/uplink/src/collector/preconditions.rs deleted file mode 100644 index e83c6e0b..00000000 --- a/uplink/src/collector/preconditions.rs +++ /dev/null @@ -1,84 +0,0 @@ -use std::{fs::metadata, os::unix::fs::MetadataExt, sync::Arc}; - -use flume::Receiver; -use human_bytes::human_bytes; -use log::debug; -use serde::Deserialize; - -use crate::{base::bridge::BridgeTx, Action, ActionResponse, Config}; - -#[derive(thiserror::Error, Debug)] -pub enum Error { - #[error("File io Error: {0}")] - Io(#[from] std::io::Error), - #[error("Disk space is insufficient: {0}")] - InsufficientDisk(String), -} - -#[derive(Deserialize, Clone, PartialEq, Eq, Debug)] -pub struct Preconditions { - #[serde(alias = "content-length")] - content_length: usize, - #[serde(alias = "uncompressed-size")] - uncompressed_length: usize, -} - -pub struct PreconditionChecker { - config: Arc, - actions_rx: Receiver, - bridge_tx: BridgeTx, -} - -impl PreconditionChecker { - pub fn new(config: Arc, actions_rx: Receiver, bridge_tx: BridgeTx) -> Self { - Self { config, actions_rx, bridge_tx } - } - - #[tokio::main] - pub async fn start(self) { - while let Ok(action) = self.actions_rx.recv() { - let preconditions: Preconditions = match serde_json::from_str(&action.payload) { - Ok(c) => c, - Err(e) => { - let response = ActionResponse::failure(&action.action_id, e.to_string()); - self.bridge_tx.send_action_response(response).await; - continue; - } - }; - - if let Err(e) = self.check_disk_size(preconditions) { - let response = ActionResponse::failure(&action.action_id, e.to_string()); - self.bridge_tx.send_action_response(response).await; - continue; - } - - let response = ActionResponse::progress(&action.action_id, "Checked OK", 100); - self.bridge_tx.send_action_response(response).await; - } - } - - // Fails if there isn't enough space to download and/or install update - // NOTE: both download and installation could happen in the same partition - fn check_disk_size(&self, preconditions: Preconditions) -> Result<(), Error> { - let disk_free_space = - fs2::free_space(&self.config.precondition_checks.as_ref().unwrap().path)? as usize; - let mut required_free_space = preconditions.uncompressed_length; - - // Check if download and installation paths are on the same partition, if yes add download file size to required - if metadata(&self.config.downloader.path)?.dev() - == metadata(&self.config.precondition_checks.as_ref().unwrap().path)?.dev() - { - required_free_space += preconditions.content_length; - } - - let req_size = human_bytes(required_free_space as f64); - let free_size = human_bytes(disk_free_space as f64); - debug!("The installation requires {req_size}; Disk free space is {free_size}"); - - if required_free_space > disk_free_space { - return Err(Error::InsufficientDisk(free_size)); - } - - Ok(()) - } -} diff --git a/uplink/src/config.rs b/uplink/src/config.rs index d7718a97..beddf361 100644 --- a/uplink/src/config.rs +++ b/uplink/src/config.rs @@ -233,12 +233,6 @@ impl Default for DeviceShadowConfig { } } -#[derive(Debug, Clone, Deserialize)] -pub struct PreconditionCheckerConfig { - pub path: PathBuf, - pub actions: Vec, -} - #[derive(Debug, Clone, Deserialize, Default)] pub struct Config { pub project_id: String, @@ -280,5 +274,4 @@ pub struct Config { pub logging: Option, #[cfg(target_os = "android")] pub logging: Option, - pub precondition_checks: Option, } diff --git a/uplink/src/lib.rs b/uplink/src/lib.rs index 85959456..e69185e1 100644 --- a/uplink/src/lib.rs +++ b/uplink/src/lib.rs @@ -67,7 +67,6 @@ use collector::installer::OTAInstaller; use collector::journalctl::JournalCtl; #[cfg(target_os = "android")] use collector::logcat::Logcat; -use collector::preconditions::PreconditionChecker; use collector::process::ProcessHandler; use collector::script_runner::ScriptRunner; use collector::systemstats::StatCollector; @@ -295,7 +294,7 @@ impl Uplink { if !self.config.script_runner.is_empty() { let actions_rx = bridge.register_action_routes(&self.config.script_runner)?; - let script_runner = ScriptRunner::new(actions_rx, bridge_tx.clone()); + let script_runner = ScriptRunner::new(actions_rx, bridge_tx); spawn_named_thread("Script Runner", || { if let Err(e) = script_runner.start() { error!("Script runner stopped!! Error = {:?}", e); @@ -303,12 +302,6 @@ impl Uplink { }); } - if let Some(checker_config) = &self.config.precondition_checks { - let actions_rx = bridge.register_action_routes(&checker_config.actions)?; - let checker = PreconditionChecker::new(self.config.clone(), actions_rx, bridge_tx); - spawn_named_thread("Logger", || checker.start()); - } - Ok(()) }