Skip to content

Commit

Permalink
feat(manager): reuse downloaded binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed May 3, 2024
1 parent 2815489 commit 24512a6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
27 changes: 18 additions & 9 deletions sn_node_manager/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ pub fn get_node_manager_path() -> Result<PathBuf> {
Ok(path.to_path_buf())
}

#[cfg(windows)]
pub fn get_node_manager_path() -> Result<PathBuf> {
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<PathBuf> {
use std::os::unix::fs::PermissionsExt;
Expand Down Expand Up @@ -65,6 +74,15 @@ pub fn get_node_registry_path() -> Result<PathBuf> {
Ok(node_registry_path)
}

#[cfg(windows)]
pub fn get_node_registry_path() -> Result<PathBuf> {
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<PathBuf>, owner: &str) -> Result<PathBuf> {
let path = match custom_path {
Expand Down Expand Up @@ -139,15 +157,6 @@ pub fn create_owned_dir(path: PathBuf, _owner: &str) -> Result<()> {
Ok(())
}

#[cfg(windows)]
pub fn get_node_registry_path() -> Result<PathBuf> {
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
Expand Down
27 changes: 22 additions & 5 deletions sn_node_manager/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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 {
Expand All @@ -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}...");
}
Expand All @@ -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
Expand All @@ -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());
Expand Down

0 comments on commit 24512a6

Please sign in to comment.