-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcin Nowak-Liebiediew
committed
Aug 7, 2023
1 parent
c99773a
commit 0a6de4f
Showing
22 changed files
with
224 additions
and
175 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use anyhow::anyhow; | ||
use fn_error_context::context; | ||
use std::{ | ||
ffi::OsStr, | ||
path::Path, | ||
process::{self, Command}, | ||
}; | ||
|
||
/// Calls a binary that was delivered with an extension tarball. | ||
/// | ||
/// # Returns | ||
/// - On success, returns stdout as a string. | ||
/// - On error, returns an error message including stdout and stderr. | ||
#[context("Calling {} CLI failed, or, it returned an error.", binary_name)] | ||
pub fn call_extension_bundled_binary<S, I>( | ||
dfx_cache_path: &Path, | ||
binary_name: &str, | ||
args: I, | ||
) -> anyhow::Result<String> | ||
where | ||
I: IntoIterator<Item = S>, | ||
S: AsRef<OsStr>, | ||
{ | ||
let extension_binary_path = | ||
std::env::current_exe().map_err(|e| anyhow::anyhow!("Failed to get current exe: {}", e))?; | ||
let extension_dir_path = extension_binary_path.parent().ok_or_else(|| { | ||
anyhow::anyhow!( | ||
"Failed to locate parent of dir of executable: {}", | ||
extension_binary_path.display() | ||
) | ||
})?; | ||
let binary_to_call = extension_dir_path.join(binary_name); | ||
// TODO | ||
dbg!(&binary_to_call); | ||
std::fs::remove_file(binary_to_call.clone()).unwrap(); | ||
panic!("trying to prettify command output, but something is not working the way I expect."); | ||
let mut command = Command::new(&binary_to_call); | ||
// If extension's dependency calls dfx; it should call dfx in this dir. | ||
command.env("PATH", dfx_cache_path.join("dfx")); | ||
command.args(args); | ||
command | ||
.stdin(process::Stdio::null()) | ||
.output() | ||
.map_err(anyhow::Error::from) | ||
.and_then(|output| -> Result<String, anyhow::Error> { | ||
if output.status.success() { | ||
Ok(String::from_utf8_lossy(&output.stdout).into_owned()) | ||
} else { | ||
let args: Vec<_> = command | ||
.get_args() | ||
.into_iter() | ||
.map(OsStr::to_string_lossy) | ||
.collect(); | ||
Err(anyhow!( | ||
"Call failed:\n{:?} {}\nStdout:\n{}\n\nStderr:\n{}", | ||
command.get_program(), | ||
args.join(" "), | ||
String::from_utf8_lossy(&output.stdout), | ||
String::from_utf8_lossy(&output.stderr) | ||
)) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use backoff::future::retry; | ||
use backoff::ExponentialBackoffBuilder; | ||
use flate2::read::GzDecoder; | ||
use std::path::Path; | ||
use std::time::Duration; | ||
use std::{fs, io::copy}; | ||
use tokio::runtime::Runtime; | ||
|
||
pub fn download_ic_binary(replica_rev: &str, binary_name: &str, destination_path: &Path) { | ||
let arch = match std::env::consts::ARCH { | ||
"x86_64" => "x86_64", | ||
"aarch64" => "x86_64", // let's rely on rosetta2 for now, since ic binaiers are not available for arm64 | ||
_ => panic!("Unsupported architecture"), | ||
}; | ||
let os = match std::env::consts::OS { | ||
"macos" => "darwin", | ||
"linux" => "linux", | ||
// "windows" => "windows", // unsupported till dfx supports windows | ||
_ => panic!("Unsupported OS"), | ||
}; | ||
|
||
let url = format!( | ||
"https://download.dfinity.systems/ic/{replica_rev}/openssl-static-binaries/{arch}-{os}/{binary_name}.gz", | ||
arch = arch, | ||
os = os, | ||
binary_name = binary_name, | ||
); | ||
println!("Downloading {}", url); | ||
|
||
let bytes = Runtime::new().unwrap().block_on(download_bytes(&url)); | ||
let mut d = GzDecoder::new(&*bytes); | ||
let tempdir = tempfile::tempdir().expect("Failed to create temp dir"); | ||
let temp_file = tempdir.path().join(binary_name); | ||
let mut temp = fs::File::create(&temp_file).expect("Failed to create the file"); | ||
copy(&mut d, &mut temp).expect("Failed to copy content"); | ||
|
||
fs::rename(temp_file, &destination_path).expect("Failed to move extension"); | ||
#[cfg(unix)] | ||
{ | ||
use std::os::unix::fs::PermissionsExt; | ||
dfx_core::fs::set_permissions(&destination_path, std::fs::Permissions::from_mode(0o500)) | ||
.expect("Failed to set permissions"); | ||
} | ||
} | ||
|
||
async fn download_bytes(url: &str) -> Vec<u8> { | ||
let retry_policy = ExponentialBackoffBuilder::new() | ||
.with_initial_interval(Duration::from_secs(1)) | ||
.with_max_interval(Duration::from_secs(16)) | ||
.with_multiplier(2.0) | ||
.with_max_elapsed_time(Some(Duration::from_secs(300))) | ||
.build(); | ||
let resp = retry(retry_policy, || async { | ||
match reqwest::get(url).await { | ||
Ok(response) => Ok(response), | ||
Err(err) => Err(backoff::Error::transient(err)), | ||
} | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
let bytes = resp.bytes().await.expect("Failed to read response"); | ||
bytes.to_vec() | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub mod call; | ||
pub mod dfx; | ||
pub mod download_ic_binaries; | ||
pub mod download_wasms; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.