use crate::node_config::NodeConfig;
use crate::schema::structs::NetworkEnvironment;
use crate::util::cmd::run_cmd;
use crate::util::lang_util::remove_whitespace;
use log::{error, info};
use std::time::Duration;
use reqwest::ClientBuilder;
use tokio::time;
use redgold_schema::ErrorInfoContext;
use redgold_schema::structs::ErrorInfo;
const S3_PREFIX_URL: &str = "https://redgold-public.s3.us-west-1.amazonaws.com/release/";
fn get_s3_sha256_path(network_type: NetworkEnvironment) -> String {
format!(
"{}{}/redgold_linux_sha256_checksum",
S3_PREFIX_URL,
network_type.to_std_string()
)
}
fn get_s3_linux_binary_path(network_type: NetworkEnvironment) -> String {
format!(
"{}{}/redgold_linux",
S3_PREFIX_URL,
network_type.to_std_string()
)
}
fn get_s3_mac_binary_path(network_type: NetworkEnvironment) -> String {
format!(
"{}{}/redgold_mac",
S3_PREFIX_URL,
network_type.to_std_string()
)
}
pub async fn pull_sha256_hash(
network_type: NetworkEnvironment,
) -> Result<String, ErrorInfo> {
let client = reqwest::Client::new();
let res = client
.get(get_s3_sha256_path(network_type))
.send()
.await.error_info("Send request failure")?
.text()
.await.error_info("decoding failure of text")?;
Ok(res)
}
pub async fn get_s3_sha256_release_hash(
network_type: NetworkEnvironment, timeout: Option<Duration>
) -> Result<String, ErrorInfo> {
let client = ClientBuilder::new().timeout(timeout.unwrap_or(Duration::from_secs(2))).build()
.error_info("Client build failure")?;
let res = client
.get(get_s3_sha256_path(network_type))
.send()
.await.error_info("Send failure")?
.text()
.await.error_info("text decoding failure")?;
let res = res.replace("\n", "").trim().to_string();
Ok(res)
}
pub async fn get_s3_sha256_release_hash_short_id(
network_type: NetworkEnvironment, timeout: Option<Duration>
) -> Result<String, ErrorInfo> {
get_s3_sha256_release_hash(network_type, timeout).await.map(|s| {
let len = s.len();
let start = len - 9;
s[start..len].to_string()
})
}
#[ignore]
#[tokio::test]
async fn checksum_poll() {
let hash = pull_sha256_hash(NetworkEnvironment::Dev).await.expect("hash missing");
println!("hash: {}", hash);
}
pub async fn poll_update(
_network_type: NetworkEnvironment,
current_hash: String,
duration: Duration
) {
let mut interval = time::interval(duration);
loop {
interval.tick().await;
let result = pull_sha256_hash(_network_type).await;
match result {
Ok(s) => {
if remove_whitespace(&*s) != remove_whitespace(&*current_hash) {
info!("Found new sha256 hash current: {} new: {}", current_hash, s);
if std::env::var("REDGOLD_DOCKER").is_ok() {
} else {
let path_exec = std::env::current_exe().expect("Can't find the current exe");
let exec_name = path_exec.file_name().expect("filename access failure").to_str()
.expect("Filename missing").to_string();
if exec_name.contains("linux") {
let out1 = run_cmd(
"wget",
vec![
&*get_s3_linux_binary_path(_network_type),
"-O",
"redgold_linux_updated",
],
);
let out2 = run_cmd("chmod", vec!["+x", "/root/redgold_linux_updated"]);
info!("Download output: {} {}", out1.0, out1.1);
info!("Chmod output: {} {}", out2.0, out2.1);
} else if exec_name.contains("mac") {
let out1 = run_cmd(
"wget",
vec![
&*get_s3_mac_binary_path(_network_type),
"-O",
"redgold_mac_updated",
],
);
let out2 = run_cmd("chmod", vec!["+x", "/root/redgold_linux_updated"]);
info!("Download output: {} {}", out1.0, out1.1);
info!("Chmod output: {} {}", out2.0, out2.1);
}
}
}
}
Err(e) => {
use crate::schema::json_or;
error!("Error querying s3 for updated checksum check, {}", json_or(&e))
}
}
}
}
pub fn auto_update_enabled(network_env: NetworkEnvironment, disabled: bool) -> bool {
let disable_auto_update_types: Vec<NetworkEnvironment> =
vec![NetworkEnvironment::Debug, NetworkEnvironment::Local];
!disable_auto_update_types.contains(&network_env) || !disabled
}
#[test]
fn verify_auto_update_start() {
assert!(auto_update_enabled(NetworkEnvironment::Dev, false));
}
pub async fn from_node_config(node_config: NodeConfig) {
if let Some(hash) = node_config.executable_checksum {
if auto_update_enabled(node_config.network, node_config.disable_auto_update)
{
info!(
"auto update disabled but would be Starting auto update process from CURRENT_EXE_SHA256={}",
hash
);
} else {
info!("Auto update disabled for network type: {} with config disable_auto_update {:?}",
node_config.network.to_std_string(), node_config.disable_auto_update);
}
}
}
#[tokio::test]
async fn try_query() {
let result = pull_sha256_hash(NetworkEnvironment::Test).await;
println!("{:?}", result);
}
#[test]
fn debug() {
use std::env;
println!("{}", env::consts::OS); }