From 24512a6eb737676ea75a0d3f8283bce6b109f856 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Fri, 3 May 2024 15:21:49 +0530 Subject: [PATCH] feat(manager): reuse downloaded binaries --- sn_node_manager/src/config.rs | 27 ++++++++++++++++++--------- sn_node_manager/src/helpers.rs | 27 ++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/sn_node_manager/src/config.rs b/sn_node_manager/src/config.rs index 06f0b540cb..5766647561 100644 --- a/sn_node_manager/src/config.rs +++ b/sn_node_manager/src/config.rs @@ -38,6 +38,15 @@ pub fn get_node_manager_path() -> Result { Ok(path.to_path_buf()) } +#[cfg(windows)] +pub fn get_node_manager_path() -> Result { + let path = Path::new("C:\\ProgramData\\safenode-manager"); + if !path.exists() { + std::fs::create_dir_all(path)?; + } + Ok(path.to_path_buf()) +} + #[cfg(unix)] pub fn get_node_registry_path() -> Result { use std::os::unix::fs::PermissionsExt; @@ -65,6 +74,15 @@ pub fn get_node_registry_path() -> Result { Ok(node_registry_path) } +#[cfg(windows)] +pub fn get_node_registry_path() -> Result { + let path = Path::new("C:\\ProgramData\\safenode-manager"); + if !path.exists() { + std::fs::create_dir_all(path)?; + } + Ok(path.join("node_registry.json")) +} + #[cfg(unix)] pub fn get_service_data_dir_path(custom_path: Option, owner: &str) -> Result { let path = match custom_path { @@ -139,15 +157,6 @@ pub fn create_owned_dir(path: PathBuf, _owner: &str) -> Result<()> { Ok(()) } -#[cfg(windows)] -pub fn get_node_registry_path() -> Result { - let path = Path::new("C:\\ProgramData\\safenode-manager"); - if !path.exists() { - std::fs::create_dir_all(path)?; - } - Ok(path.join("node_registry.json")) -} - #[cfg(unix)] pub fn is_running_as_root() -> bool { users::get_effective_uid() == 0 diff --git a/sn_node_manager/src/helpers.rs b/sn_node_manager/src/helpers.rs index 77c668a05f..53d785e743 100644 --- a/sn_node_manager/src/helpers.rs +++ b/sn_node_manager/src/helpers.rs @@ -14,13 +14,14 @@ use indicatif::{ProgressBar, ProgressStyle}; use semver::Version; use sn_releases::{get_running_platform, ArchiveType, ReleaseType, SafeReleaseRepoActions}; use std::{ + fs::create_dir_all, io::Read, path::PathBuf, process::{Command, Stdio}, sync::Arc, }; -use crate::VerbosityLevel; +use crate::{config, VerbosityLevel}; const MAX_DOWNLOAD_RETRIES: u8 = 3; @@ -54,7 +55,7 @@ pub async fn download_and_extract_release( callback }; - let temp_dir_path = create_temp_dir()?; + let mut download_dir_path = create_temp_dir()?; let mut download_attempts = 1; let archive_path = loop { @@ -67,7 +68,7 @@ pub async fn download_and_extract_release( println!("Retrieving {release_type} from {url}"); } match release_repo - .download_release(url, &temp_dir_path, &callback) + .download_release(url, &download_dir_path, &callback) .await { Ok(archive_path) => break archive_path, @@ -82,6 +83,8 @@ pub async fn download_and_extract_release( } } } else { + download_dir_path = config::get_node_manager_path()?.join("downloads"); + create_dir_all(&download_dir_path)?; let version = if let Some(version) = version.clone() { Version::parse(&version)? } else { @@ -91,6 +94,20 @@ pub async fn download_and_extract_release( release_repo.get_latest_version(&release_type).await? }; + let archive_name = format!( + "{}-{}-{}.{}", + release_type.to_string().to_lowercase(), + version, + &get_running_platform()?, + &ArchiveType::TarGz + ); + let archive_path = download_dir_path.join(&archive_name); + + // return if the file has been downloaded already + if archive_path.exists() { + break archive_path; + } + if verbosity != VerbosityLevel::Minimal { println!("Downloading {release_type} version {version}..."); } @@ -100,7 +117,7 @@ pub async fn download_and_extract_release( &version, &get_running_platform()?, &ArchiveType::TarGz, - &temp_dir_path, + &download_dir_path, &callback, ) .await @@ -123,7 +140,7 @@ pub async fn download_and_extract_release( } let safenode_download_path = - release_repo.extract_release_archive(&archive_path, &temp_dir_path)?; + release_repo.extract_release_archive(&archive_path, &download_dir_path)?; if verbosity != VerbosityLevel::Minimal { println!("Download completed: {}", &safenode_download_path.display());