dagger-rs/crates/dagger-core/src/downloader.rs

242 lines
6.5 KiB
Rust
Raw Normal View History

2023-01-27 08:31:09 +01:00
use std::{
fs::File,
2023-02-25 01:55:24 +01:00
io::{copy, Write},
2023-01-27 12:21:33 +01:00
os::unix::prelude::PermissionsExt,
2023-01-27 08:31:09 +01:00
path::PathBuf,
};
use eyre::Context;
2023-01-27 12:21:33 +01:00
use flate2::read::GzDecoder;
2023-01-27 08:31:09 +01:00
use platform_info::Uname;
2023-01-27 12:21:33 +01:00
use sha2::Digest;
use tar::Archive;
2023-01-27 21:57:39 +01:00
use tempfile::tempfile;
2023-01-27 08:31:09 +01:00
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
#[derive(Clone)]
pub struct Platform {
pub os: String,
pub arch: String,
}
impl Platform {
pub fn from_system() -> eyre::Result<Self> {
let platform = platform_info::PlatformInfo::new()?;
let os_name = platform.sysname();
let arch = platform.machine().to_lowercase();
let normalize_arch = match arch.as_str() {
"x86_64" => "amd64",
"aarch" => "arm64",
2023-02-25 01:39:03 +01:00
"aarch64" => "arm64",
2023-01-27 08:31:09 +01:00
arch => arch,
};
Ok(Self {
os: os_name.to_lowercase(),
arch: normalize_arch.into(),
})
}
}
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
pub struct TempFile {
prefix: String,
directory: PathBuf,
file: File,
}
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
impl TempFile {
pub fn new(prefix: &str, directory: &PathBuf) -> eyre::Result<Self> {
let prefix = prefix.to_string();
let directory = directory.clone();
let file = tempfile()?;
Ok(Self {
prefix,
directory,
file,
})
}
}
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
pub type CliVersion = String;
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
pub struct Downloader {
version: CliVersion,
platform: Platform,
}
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
const DEFAULT_CLI_HOST: &str = "dl.dagger.io";
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
const CLI_BIN_PREFIX: &str = "dagger-";
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
const CLI_BASE_URL: &str = "https://dl.dagger.io/dagger/releases";
2023-01-27 08:33:46 +01:00
#[allow(dead_code)]
2023-01-27 08:31:09 +01:00
impl Downloader {
pub fn new(version: CliVersion) -> eyre::Result<Self> {
Ok(Self {
version,
platform: Platform::from_system()?,
})
}
pub fn archive_url(&self) -> String {
let ext = match self.platform.os.as_str() {
"windows" => "zip",
_ => "tar.gz",
};
let version = &self.version;
let os = &self.platform.os;
let arch = &self.platform.arch;
format!("{CLI_BASE_URL}/{version}/dagger_v{version}_{os}_{arch}.{ext}")
}
pub fn checksum_url(&self) -> String {
let version = &self.version;
format!("{CLI_BASE_URL}/{version}/checksums.txt")
}
pub fn cache_dir(&self) -> eyre::Result<PathBuf> {
let env = std::env::var("XDG_CACHE_HOME").unwrap_or("".into());
let env = env.trim();
let mut path = match env {
"" => dirs::cache_dir().ok_or(eyre::anyhow!(
"could not find cache_dir, either in env or XDG_CACHE_HOME"
))?,
path => PathBuf::from(path),
};
path.push("dagger");
std::fs::create_dir_all(&path)?;
Ok(path)
}
pub async fn get_cli(&self) -> eyre::Result<PathBuf> {
2023-01-27 08:31:09 +01:00
let version = &self.version;
let mut cli_bin_path = self.cache_dir()?;
cli_bin_path.push(format!("{CLI_BIN_PREFIX}{version}"));
if self.platform.os == "windows" {
cli_bin_path = cli_bin_path.with_extension("exe")
}
if !cli_bin_path.exists() {
cli_bin_path = self
.download(cli_bin_path)
.await
2023-01-27 08:31:09 +01:00
.context("failed to download CLI from archive")?;
}
Ok(cli_bin_path)
}
async fn download(&self, path: PathBuf) -> eyre::Result<PathBuf> {
let expected_checksum = self.expected_checksum().await?;
2023-01-27 08:31:09 +01:00
2023-01-27 12:21:33 +01:00
let mut bytes = vec![];
2023-02-20 10:47:58 +01:00
let actual_hash = self.extract_cli_archive(&mut bytes).await?;
2023-01-27 08:31:09 +01:00
if expected_checksum != actual_hash {
eyre::bail!("downloaded CLI binary checksum doesn't match checksum from checksums.txt")
}
2023-01-27 12:21:33 +01:00
let mut file = std::fs::File::create(&path)?;
let meta = file.metadata()?;
let mut perm = meta.permissions();
perm.set_mode(0o700);
file.set_permissions(perm)?;
file.write_all(bytes.as_slice())?;
Ok(path)
2023-01-27 08:31:09 +01:00
}
async fn expected_checksum(&self) -> eyre::Result<String> {
2023-01-27 08:31:09 +01:00
let archive_url = &self.archive_url();
let archive_path = PathBuf::from(&archive_url);
let archive_name = archive_path
.file_name()
.ok_or(eyre::anyhow!("could not get file_name from archive_url"))?;
let resp = reqwest::get(self.checksum_url()).await?;
2023-01-27 08:31:09 +01:00
let resp = resp.error_for_status()?;
for line in resp.text().await?.lines() {
2023-01-27 08:31:09 +01:00
let mut content = line.split_whitespace();
let checksum = content
.next()
.ok_or(eyre::anyhow!("could not find checksum in checksums.txt"))?;
let file_name = content
.next()
.ok_or(eyre::anyhow!("could not find file_name in checksums.txt"))?;
if file_name == archive_name {
return Ok(checksum.to_string());
}
}
eyre::bail!("could not find a matching version or binary in checksums.txt")
}
2023-02-20 10:47:58 +01:00
pub async fn extract_cli_archive(&self, dest: &mut Vec<u8>) -> eyre::Result<String> {
2023-01-27 08:31:09 +01:00
let archive_url = self.archive_url();
2023-02-20 10:47:58 +01:00
let resp = reqwest::get(&archive_url).await?;
let resp = resp.error_for_status()?;
let bytes = resp.bytes().await?;
2023-01-27 12:21:33 +01:00
let mut hasher = sha2::Sha256::new();
2023-02-20 10:47:58 +01:00
hasher.update(&bytes);
2023-01-27 12:21:33 +01:00
let res = hasher.finalize();
2023-01-27 08:31:09 +01:00
if archive_url.ends_with(".zip") {
// TODO: Nothing for now
2023-01-27 12:21:33 +01:00
todo!()
2023-01-27 08:31:09 +01:00
} else {
2023-02-20 10:47:58 +01:00
self.extract_from_tar(&bytes, dest)?;
2023-01-27 12:21:33 +01:00
}
Ok(hex::encode(res))
}
fn extract_from_tar(&self, temp: &[u8], output: &mut Vec<u8>) -> eyre::Result<()> {
let decompressed_temp = GzDecoder::new(temp);
let mut archive = Archive::new(decompressed_temp);
for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;
if path.ends_with("dagger") {
copy(&mut entry, output)?;
return Ok(());
}
2023-01-27 08:31:09 +01:00
}
2023-01-27 12:21:33 +01:00
eyre::bail!("could not find a matching file")
2023-01-27 08:31:09 +01:00
}
}
#[cfg(test)]
mod test {
use super::Downloader;
#[tokio::test]
async fn download() {
let cli_path = Downloader::new("0.3.10".into())
.unwrap()
.get_cli()
.await
.unwrap();
2023-01-27 08:31:09 +01:00
2023-01-27 12:21:33 +01:00
assert_eq!(
Some("dagger-0.3.10"),
cli_path.file_name().and_then(|s| s.to_str())
)
2023-01-27 08:31:09 +01:00
}
}