feat: Autoinstall docker
All checks were successful
Run Check Script / check (pull_request) Successful in 1m25s
All checks were successful
Run Check Script / check (pull_request) Successful in 1m25s
This commit is contained in:
25
harmony_tools/Cargo.toml
Normal file
25
harmony_tools/Cargo.toml
Normal file
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "harmony_tools"
|
||||
description = "Install tools such as k3d, docker and more"
|
||||
edition = "2021"
|
||||
version.workspace = true
|
||||
readme.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
log = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
octocrab = "0.44.0"
|
||||
regex = "1.11.1"
|
||||
reqwest = { version = "0.12", features = ["stream", "rustls-tls", "http2"], default-features = false }
|
||||
url.workspace = true
|
||||
sha2 = "0.10.8"
|
||||
futures-util = "0.3.31"
|
||||
kube.workspace = true
|
||||
inquire.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
env_logger = { workspace = true }
|
||||
httptest = "0.16.3"
|
||||
pretty_assertions = "1.4.1"
|
||||
300
harmony_tools/src/docker.rs
Normal file
300
harmony_tools/src/docker.rs
Normal file
@@ -0,0 +1,300 @@
|
||||
use crate::downloadable_asset::DownloadableAsset;
|
||||
use inquire::Select;
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
use url::Url;
|
||||
|
||||
pub struct Docker {
|
||||
base_dir: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum DockerVariant {
|
||||
Standard,
|
||||
Rootless,
|
||||
Manual,
|
||||
}
|
||||
|
||||
impl fmt::Display for DockerVariant {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
DockerVariant::Standard => write!(f, "Standard Docker (requires sudo)"),
|
||||
DockerVariant::Rootless => write!(f, "Rootless Docker (no sudo required)"),
|
||||
DockerVariant::Manual => {
|
||||
write!(f, "Exit and install manually (Docker or podman-docker)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Docker {
|
||||
pub fn new(base_dir: PathBuf) -> Self {
|
||||
Self { base_dir }
|
||||
}
|
||||
|
||||
/// Provides the DOCKER_HOST and DOCKER_SOCK env vars for local usage.
|
||||
///
|
||||
/// If a rootless Docker installation is detected in the user's home directory,
|
||||
/// it returns the appropriate `DOCKER_HOST` pointing to the user's Docker socket.
|
||||
/// Otherwise, it returns an empty HashMap, assuming the standard system-wide
|
||||
/// Docker installation is used.
|
||||
pub fn get_docker_env(&self) -> HashMap<String, String> {
|
||||
let mut env = HashMap::new();
|
||||
|
||||
if let Ok(home) = std::env::var("HOME") {
|
||||
let rootless_sock = PathBuf::from(&home).join(".docker/run/docker.sock");
|
||||
let rootless_bin = PathBuf::from(&home).join("bin/docker");
|
||||
|
||||
if rootless_bin.exists() && rootless_sock.exists() {
|
||||
let docker_host = format!("unix://{}", rootless_sock.display());
|
||||
debug!(
|
||||
"Detected rootless Docker, setting DOCKER_HOST={}",
|
||||
docker_host
|
||||
);
|
||||
env.insert("DOCKER_HOST".to_string(), docker_host);
|
||||
}
|
||||
}
|
||||
|
||||
env
|
||||
}
|
||||
|
||||
/// Gets the path to the docker binary
|
||||
pub fn get_bin_path(&self) -> PathBuf {
|
||||
// Check standard PATH first
|
||||
if let Ok(path) = std::process::Command::new("which")
|
||||
.arg("docker")
|
||||
.output()
|
||||
.map(|o| PathBuf::from(String::from_utf8_lossy(&o.stdout).trim()))
|
||||
{
|
||||
if path.exists() {
|
||||
debug!("Found Docker in PATH: {:?}", path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
// Check common rootless location
|
||||
if let Ok(home) = std::env::var("HOME") {
|
||||
let rootless_path = PathBuf::from(home).join("bin/docker");
|
||||
if rootless_path.exists() {
|
||||
debug!("Found rootless Docker at: {:?}", rootless_path);
|
||||
return rootless_path;
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Docker not found in PATH or rootless location, using 'docker' from PATH");
|
||||
PathBuf::from("docker")
|
||||
}
|
||||
|
||||
/// Checks if docker is installed and available in the PATH
|
||||
pub fn is_installed(&self) -> bool {
|
||||
let bin_path = self.get_bin_path();
|
||||
trace!("Checking if Docker is installed at: {:?}", bin_path);
|
||||
|
||||
std::process::Command::new(&bin_path)
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map(|output| {
|
||||
if output.status.success() {
|
||||
trace!("Docker version check successful");
|
||||
true
|
||||
} else {
|
||||
trace!(
|
||||
"Docker version check failed with status: {:?}",
|
||||
output.status
|
||||
);
|
||||
false
|
||||
}
|
||||
})
|
||||
.map_err(|e| {
|
||||
trace!("Failed to execute Docker version check: {}", e);
|
||||
e
|
||||
})
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Prompts the user to choose an installation method
|
||||
fn prompt_for_installation(&self) -> DockerVariant {
|
||||
let options = vec![
|
||||
DockerVariant::Standard,
|
||||
DockerVariant::Rootless,
|
||||
DockerVariant::Manual,
|
||||
];
|
||||
|
||||
Select::new(
|
||||
"Docker binary was not found. How would you like to proceed?",
|
||||
options,
|
||||
)
|
||||
.with_help_message("Standard requires sudo. Rootless runs in user space.")
|
||||
.prompt()
|
||||
.unwrap_or(DockerVariant::Manual)
|
||||
}
|
||||
|
||||
/// Installs docker using the official shell script
|
||||
pub async fn install(&self, variant: DockerVariant) -> Result<(), String> {
|
||||
let (script_url, script_name, use_sudo) = match variant {
|
||||
DockerVariant::Standard => ("https://get.docker.com", "get-docker.sh", true),
|
||||
DockerVariant::Rootless => (
|
||||
"https://get.docker.com/rootless",
|
||||
"get-docker-rootless.sh",
|
||||
false,
|
||||
),
|
||||
DockerVariant::Manual => return Err("Manual installation selected".to_string()),
|
||||
};
|
||||
|
||||
info!("Installing {}...", variant);
|
||||
debug!("Downloading installation script from: {}", script_url);
|
||||
|
||||
// Download the installation script
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(script_url).map_err(|e| {
|
||||
error!("Failed to parse installation script URL: {}", e);
|
||||
format!("Failed to parse installation script URL: {}", e)
|
||||
})?,
|
||||
file_name: script_name.to_string(),
|
||||
checksum: Some(String::new()), // Skip checksum verification for official scripts
|
||||
};
|
||||
|
||||
let downloaded_script = asset
|
||||
.download_to_path(self.base_dir.join("scripts"))
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!("Failed to download installation script: {}", e);
|
||||
format!("Failed to download installation script: {}", e)
|
||||
})?;
|
||||
|
||||
debug!("Installation script downloaded to: {:?}", downloaded_script);
|
||||
|
||||
// Execute the installation script
|
||||
let mut cmd = std::process::Command::new("sh");
|
||||
if use_sudo {
|
||||
cmd.arg("sudo").arg("sh");
|
||||
}
|
||||
cmd.arg(&downloaded_script);
|
||||
|
||||
debug!("Executing installation command: {:?}", cmd);
|
||||
|
||||
let status = cmd.status().map_err(|e| {
|
||||
error!("Failed to execute docker installation script: {}", e);
|
||||
format!("Failed to execute docker installation script: {}", e)
|
||||
})?;
|
||||
|
||||
if status.success() {
|
||||
info!("{} installed successfully", variant);
|
||||
if variant == DockerVariant::Rootless {
|
||||
warn!("Please follow the instructions above to finish rootless setup (environment variables).");
|
||||
}
|
||||
|
||||
// Validate the installation by running hello-world
|
||||
self.validate_installation()?;
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
error!(
|
||||
"{} installation script failed with exit code: {:?}",
|
||||
variant,
|
||||
status.code()
|
||||
);
|
||||
Err(format!("{} installation script failed", variant))
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates the Docker installation by running a test container.
|
||||
///
|
||||
/// This method runs `docker run --rm hello-world` to verify that Docker
|
||||
/// is properly installed and functional.
|
||||
fn validate_installation(&self) -> Result<(), String> {
|
||||
info!("Validating Docker installation by running hello-world container...");
|
||||
|
||||
let output = self
|
||||
.command()
|
||||
.args(["run", "--rm", "hello-world"])
|
||||
.output()
|
||||
.map_err(|e| {
|
||||
error!("Failed to execute hello-world validation: {}", e);
|
||||
format!("Failed to execute hello-world validation: {}", e)
|
||||
})?;
|
||||
|
||||
if output.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
if stdout.contains("Hello from Docker!") {
|
||||
info!("Docker installation validated successfully");
|
||||
trace!("Validation output: {}", stdout);
|
||||
Ok(())
|
||||
} else {
|
||||
warn!("Hello-world container ran but expected output not found");
|
||||
debug!("Output was: {}", stdout);
|
||||
Err("Docker validation failed: unexpected output from hello-world".to_string())
|
||||
}
|
||||
} else {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
error!(
|
||||
"Hello-world validation failed with exit code: {:?}",
|
||||
output.status.code()
|
||||
);
|
||||
debug!("Validation stderr: {}", stderr);
|
||||
if !stderr.is_empty() {
|
||||
Err(format!("Docker validation failed: {}", stderr.trim()))
|
||||
} else {
|
||||
Err(
|
||||
"Docker validation failed: hello-world container did not run successfully"
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Ensures docker is installed, prompting if necessary
|
||||
pub async fn ensure_installed(&self) -> Result<(), String> {
|
||||
if self.is_installed() {
|
||||
debug!("Docker is already installed at: {:?}", self.get_bin_path());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
debug!("Docker is not installed, prompting for installation method");
|
||||
match self.prompt_for_installation() {
|
||||
DockerVariant::Manual => {
|
||||
info!("User chose manual installation");
|
||||
Err("Docker installation cancelled by user. Please install docker or podman-docker manually.".to_string())
|
||||
}
|
||||
variant => self.install(variant).await,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a pre-configured Command for running Docker commands.
|
||||
///
|
||||
/// The returned Command is set up with:
|
||||
/// - The correct Docker binary path (handles rootless installations)
|
||||
/// - Appropriate environment variables (e.g., DOCKER_HOST for rootless)
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use harmony_tools::Docker;
|
||||
/// # use std::path::PathBuf;
|
||||
/// # let docker = Docker::new(PathBuf::from("."));
|
||||
/// let mut cmd = docker.command();
|
||||
/// cmd.args(["ps", "-a"]);
|
||||
/// // Now cmd is ready to be executed
|
||||
/// ```
|
||||
pub fn command(&self) -> std::process::Command {
|
||||
let bin_path = self.get_bin_path();
|
||||
trace!("Creating Docker command with binary: {:?}", bin_path);
|
||||
|
||||
let mut cmd = std::process::Command::new(&bin_path);
|
||||
|
||||
// Add Docker-specific environment variables
|
||||
let env = self.get_docker_env();
|
||||
if !env.is_empty() {
|
||||
trace!("Setting Docker environment variables: {:?}", env);
|
||||
for (key, value) in env {
|
||||
cmd.env(key, value);
|
||||
}
|
||||
} else {
|
||||
trace!("No Docker-specific environment variables to set");
|
||||
}
|
||||
|
||||
cmd
|
||||
}
|
||||
}
|
||||
360
harmony_tools/src/downloadable_asset.rs
Normal file
360
harmony_tools/src/downloadable_asset.rs
Normal file
@@ -0,0 +1,360 @@
|
||||
use futures_util::StreamExt;
|
||||
use log::{debug, warn};
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use url::Url;
|
||||
|
||||
const CHECKSUM_FAILED_MSG: &str = "Downloaded file failed checksum verification";
|
||||
|
||||
/// Represents an asset that can be downloaded from a URL with checksum verification.
|
||||
///
|
||||
/// This struct facilitates secure downloading of files from remote URLs by
|
||||
/// verifying the integrity of the downloaded content using SHA-256 checksums.
|
||||
/// It handles downloading the file, saving it to disk, and verifying the checksum matches
|
||||
/// the expected value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # use url::Url;
|
||||
/// # use std::path::PathBuf;
|
||||
///
|
||||
/// # async fn example() -> Result<(), String> {
|
||||
/// let asset = DownloadableAsset {
|
||||
/// url: Url::parse("https://example.com/file.zip").unwrap(),
|
||||
/// file_name: "file.zip".to_string(),
|
||||
/// checksum: "a1b2c3d4e5f6...".to_string(),
|
||||
/// };
|
||||
///
|
||||
/// let download_dir = PathBuf::from("/tmp/downloads");
|
||||
/// let file_path = asset.download_to_path(download_dir).await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct DownloadableAsset {
|
||||
pub(crate) url: Url,
|
||||
pub(crate) file_name: String,
|
||||
pub(crate) checksum: Option<String>,
|
||||
}
|
||||
|
||||
impl DownloadableAsset {
|
||||
fn verify_checksum(&self, file: PathBuf) -> bool {
|
||||
// Skip verification if no checksum is provided
|
||||
let expected_checksum = match &self.checksum {
|
||||
Some(checksum) => checksum,
|
||||
None => {
|
||||
debug!("No checksum provided, skipping verification");
|
||||
return file.exists();
|
||||
}
|
||||
};
|
||||
|
||||
if !file.exists() {
|
||||
debug!("File does not exist: {:?}", file);
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut file = match std::fs::File::open(&file) {
|
||||
Ok(file) => file,
|
||||
Err(e) => {
|
||||
warn!("Failed to open file for checksum verification: {:?}", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buffer = [0; 1024 * 1024]; // 1MB buffer
|
||||
|
||||
loop {
|
||||
let bytes_read = match file.read(&mut buffer) {
|
||||
Ok(0) => break,
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
warn!("Error reading file for checksum: {:?}", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
hasher.update(&buffer[..bytes_read]);
|
||||
}
|
||||
|
||||
let result = hasher.finalize();
|
||||
let calculated_hash = format!("{:x}", result);
|
||||
|
||||
debug!("Expected checksum: {}", expected_checksum);
|
||||
debug!("Calculated checksum: {}", calculated_hash);
|
||||
|
||||
calculated_hash == *expected_checksum
|
||||
}
|
||||
|
||||
/// Downloads the asset to the specified directory, verifying its checksum.
|
||||
///
|
||||
/// This function will:
|
||||
/// 1. Create the target directory if it doesn't exist
|
||||
/// 2. Check if the file already exists with the correct checksum
|
||||
/// 3. If not, download the file from the URL
|
||||
/// 4. Verify the downloaded file's checksum matches the expected value
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `folder` - The directory path where the file should be saved
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Ok(PathBuf)` - The path to the downloaded file on success
|
||||
/// * `Err(String)` - A descriptive error message if the download or verification fails
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an error if:
|
||||
/// - The network request fails
|
||||
/// - The server responds with a non-success status code
|
||||
/// - Writing to disk fails
|
||||
/// - The checksum verification fails
|
||||
pub(crate) async fn download_to_path(&self, folder: PathBuf) -> Result<PathBuf, String> {
|
||||
if !folder.exists() {
|
||||
fs::create_dir_all(&folder)
|
||||
.await
|
||||
.expect("Failed to create download directory");
|
||||
}
|
||||
|
||||
let target_file_path = folder.join(&self.file_name);
|
||||
debug!("Downloading to path: {:?}", target_file_path);
|
||||
|
||||
if self.verify_checksum(target_file_path.clone()) {
|
||||
debug!("File already exists with correct checksum, skipping download");
|
||||
return Ok(target_file_path);
|
||||
}
|
||||
|
||||
debug!("Downloading from URL: {}", self.url);
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(self.url.clone())
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to download file: {e}"))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!(
|
||||
"Failed to download file, status: {}",
|
||||
response.status()
|
||||
));
|
||||
}
|
||||
|
||||
let mut file = File::create(&target_file_path)
|
||||
.await
|
||||
.expect("Failed to create target file");
|
||||
|
||||
let mut stream = response.bytes_stream();
|
||||
while let Some(chunk_result) = stream.next().await {
|
||||
let chunk = chunk_result.expect("Error while downloading file");
|
||||
file.write_all(&chunk)
|
||||
.await
|
||||
.expect("Failed to write data to file");
|
||||
}
|
||||
|
||||
file.flush().await.expect("Failed to flush file");
|
||||
drop(file);
|
||||
|
||||
// Only verify checksum if one was provided
|
||||
if self.checksum.is_some() && !self.verify_checksum(target_file_path.clone()) {
|
||||
return Err(CHECKSUM_FAILED_MSG.to_string());
|
||||
}
|
||||
|
||||
debug!(
|
||||
"File downloaded and verified successfully: {}",
|
||||
target_file_path.to_string_lossy()
|
||||
);
|
||||
Ok(target_file_path)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use httptest::{
|
||||
matchers::{self, request},
|
||||
responders, Expectation, Server,
|
||||
};
|
||||
|
||||
const BASE_TEST_PATH: &str = "/tmp/harmony-test-k3d-download";
|
||||
const TEST_CONTENT: &str = "This is a test file.";
|
||||
const TEST_CONTENT_HASH: &str =
|
||||
"f29bc64a9d3732b4b9035125fdb3285f5b6455778edca72414671e0ca3b2e0de";
|
||||
|
||||
fn setup_test() -> (PathBuf, Server) {
|
||||
let _ = env_logger::builder().try_init();
|
||||
|
||||
// Create unique test directory
|
||||
let test_id = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis();
|
||||
let download_path = format!("{}/test_{}", BASE_TEST_PATH, test_id);
|
||||
std::fs::create_dir_all(&download_path).unwrap();
|
||||
|
||||
(PathBuf::from(download_path), Server::run())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_to_path_success() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(request::method_path("GET", "/test.txt"))
|
||||
.respond_with(responders::status_code(200).body(TEST_CONTENT)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: Some(TEST_CONTENT_HASH.to_string()),
|
||||
};
|
||||
|
||||
let result = asset
|
||||
.download_to_path(folder.join("success"))
|
||||
.await
|
||||
.unwrap();
|
||||
let downloaded_content = std::fs::read_to_string(result).unwrap();
|
||||
assert_eq!(downloaded_content, TEST_CONTENT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_to_path_already_exists() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(matchers::any())
|
||||
.times(0)
|
||||
.respond_with(responders::status_code(200).body(TEST_CONTENT)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: Some(TEST_CONTENT_HASH.to_string()),
|
||||
};
|
||||
|
||||
let target_file_path = folder.join(&asset.file_name);
|
||||
std::fs::write(&target_file_path, TEST_CONTENT).unwrap();
|
||||
|
||||
let result = asset.download_to_path(folder).await.unwrap();
|
||||
let content = std::fs::read_to_string(result).unwrap();
|
||||
assert_eq!(content, TEST_CONTENT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_to_path_server_error() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(matchers::any()).respond_with(responders::status_code(404)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: Some(TEST_CONTENT_HASH.to_string()),
|
||||
};
|
||||
|
||||
let result = asset.download_to_path(folder.join("error")).await;
|
||||
assert!(result.is_err());
|
||||
assert!(result.unwrap_err().contains("status: 404"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_to_path_checksum_failure() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
let invalid_content = "This is NOT the expected content";
|
||||
server.expect(
|
||||
Expectation::matching(matchers::any())
|
||||
.respond_with(responders::status_code(200).body(invalid_content)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: Some(TEST_CONTENT_HASH.to_string()),
|
||||
};
|
||||
|
||||
let join_handle =
|
||||
tokio::spawn(async move { asset.download_to_path(folder.join("failure")).await });
|
||||
|
||||
assert_eq!(
|
||||
join_handle.await.unwrap().err().unwrap(),
|
||||
CHECKSUM_FAILED_MSG
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_with_specific_path_matcher() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(matchers::request::path("/specific/path.txt"))
|
||||
.respond_with(responders::status_code(200).body(TEST_CONTENT)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/specific/path.txt").to_string()).unwrap(),
|
||||
file_name: "path.txt".to_string(),
|
||||
checksum: Some(TEST_CONTENT_HASH.to_string()),
|
||||
};
|
||||
|
||||
let result = asset.download_to_path(folder).await.unwrap();
|
||||
let downloaded_content = std::fs::read_to_string(result).unwrap();
|
||||
assert_eq!(downloaded_content, TEST_CONTENT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_without_checksum() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(matchers::any())
|
||||
.respond_with(responders::status_code(200).body(TEST_CONTENT)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: None,
|
||||
};
|
||||
|
||||
let result = asset
|
||||
.download_to_path(folder.join("no_checksum"))
|
||||
.await
|
||||
.unwrap();
|
||||
let downloaded_content = std::fs::read_to_string(result).unwrap();
|
||||
assert_eq!(downloaded_content, TEST_CONTENT);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_download_without_checksum_already_exists() {
|
||||
let (folder, server) = setup_test();
|
||||
|
||||
server.expect(
|
||||
Expectation::matching(matchers::any())
|
||||
.times(0)
|
||||
.respond_with(responders::status_code(200).body(TEST_CONTENT)),
|
||||
);
|
||||
|
||||
let asset = DownloadableAsset {
|
||||
url: Url::parse(&server.url("/test.txt").to_string()).unwrap(),
|
||||
file_name: "test.txt".to_string(),
|
||||
checksum: None,
|
||||
};
|
||||
|
||||
let target_file_path = folder.join(&asset.file_name);
|
||||
std::fs::write(&target_file_path, TEST_CONTENT).unwrap();
|
||||
|
||||
let result = asset.download_to_path(folder).await.unwrap();
|
||||
let content = std::fs::read_to_string(result).unwrap();
|
||||
assert_eq!(content, TEST_CONTENT);
|
||||
}
|
||||
}
|
||||
447
harmony_tools/src/k3d.rs
Normal file
447
harmony_tools/src/k3d.rs
Normal file
@@ -0,0 +1,447 @@
|
||||
use kube::Client;
|
||||
use log::{debug, info};
|
||||
use std::{ffi::OsStr, path::PathBuf};
|
||||
|
||||
use crate::downloadable_asset::DownloadableAsset;
|
||||
|
||||
const K3D_BIN_FILE_NAME: &str = "k3d";
|
||||
|
||||
pub struct K3d {
|
||||
base_dir: PathBuf,
|
||||
cluster_name: Option<String>,
|
||||
}
|
||||
|
||||
impl K3d {
|
||||
pub fn new(base_dir: PathBuf, cluster_name: Option<String>) -> Self {
|
||||
Self {
|
||||
base_dir,
|
||||
cluster_name,
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_binary_for_current_platform(
|
||||
&self,
|
||||
latest_release: octocrab::models::repos::Release,
|
||||
) -> DownloadableAsset {
|
||||
let os = std::env::consts::OS;
|
||||
let arch = std::env::consts::ARCH;
|
||||
|
||||
debug!("Detecting platform: OS={}, ARCH={}", os, arch);
|
||||
|
||||
let binary_pattern = match (os, arch) {
|
||||
("linux", "x86") => "k3d-linux-386",
|
||||
("linux", "x86_64") => "k3d-linux-amd64",
|
||||
("linux", "arm") => "k3d-linux-arm",
|
||||
("linux", "aarch64") => "k3d-linux-arm64",
|
||||
("windows", "x86_64") => "k3d-windows-amd64.exe",
|
||||
("macos", "x86_64") => "k3d-darwin-amd64",
|
||||
("macos", "aarch64") => "k3d-darwin-arm64",
|
||||
_ => panic!("Unsupported platform: {}-{}", os, arch),
|
||||
};
|
||||
|
||||
debug!("Looking for binary matching pattern: {}", binary_pattern);
|
||||
|
||||
let binary_asset = latest_release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == binary_pattern)
|
||||
.unwrap_or_else(|| panic!("No matching binary found for {}", binary_pattern));
|
||||
|
||||
let binary_url = binary_asset.browser_download_url.clone();
|
||||
|
||||
let checksums_asset = latest_release
|
||||
.assets
|
||||
.iter()
|
||||
.find(|asset| asset.name == "checksums.txt")
|
||||
.expect("Checksums file not found in release assets");
|
||||
|
||||
let checksums_url = checksums_asset.browser_download_url.clone();
|
||||
|
||||
let body = reqwest::get(checksums_url)
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let checksum = body
|
||||
.lines()
|
||||
.find_map(|line| {
|
||||
if line.ends_with(&binary_pattern) {
|
||||
Some(line.split_whitespace().next().unwrap_or("").to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| panic!("Checksum not found for {}", binary_pattern));
|
||||
|
||||
debug!("Found binary at {} with checksum {}", binary_url, checksum);
|
||||
|
||||
let checksum = Some(checksum);
|
||||
DownloadableAsset {
|
||||
url: binary_url,
|
||||
file_name: K3D_BIN_FILE_NAME.to_string(),
|
||||
checksum,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_latest_release(&self) -> Result<PathBuf, String> {
|
||||
let latest_release = self.get_latest_release_tag().await.unwrap();
|
||||
|
||||
let release_binary = self.get_binary_for_current_platform(latest_release).await;
|
||||
debug!("Foudn K3d binary to install : {release_binary:#?}");
|
||||
release_binary.download_to_path(self.base_dir.clone()).await
|
||||
}
|
||||
|
||||
// TODO : Make sure this will only find actual released versions, no prereleases or test
|
||||
// builds
|
||||
pub async fn get_latest_release_tag(&self) -> Result<octocrab::models::repos::Release, String> {
|
||||
let octo = octocrab::instance();
|
||||
let latest_release = octo
|
||||
.repos("k3d-io", "k3d")
|
||||
.releases()
|
||||
.get_latest()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
debug!("Got k3d releases {latest_release:#?}");
|
||||
|
||||
Ok(latest_release)
|
||||
}
|
||||
|
||||
/// Checks if k3d binary exists and is executable
|
||||
///
|
||||
/// Verifies that:
|
||||
/// 1. The k3d binary exists in the base directory
|
||||
/// 2. It has proper executable permissions (on Unix systems)
|
||||
/// 3. It responds correctly to a simple command (`k3d --version`)
|
||||
pub fn is_installed(&self) -> bool {
|
||||
let binary_path = self.get_k3d_binary_path();
|
||||
|
||||
if !binary_path.exists() {
|
||||
debug!("K3d binary not found at {:?}", binary_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
if !self.ensure_binary_executable(&binary_path) {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.can_execute_binary_check(&binary_path)
|
||||
}
|
||||
|
||||
/// Verifies if the specified cluster is already created
|
||||
///
|
||||
/// Executes `k3d cluster list <cluster_name>` and checks for a successful response,
|
||||
/// indicating that the cluster exists and is registered with k3d.
|
||||
pub fn is_cluster_initialized(&self) -> bool {
|
||||
let cluster_name = match self.get_cluster_name() {
|
||||
Ok(name) => name,
|
||||
Err(_) => {
|
||||
debug!("Could not get cluster name, can't verify if cluster is initialized");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
let binary_path = self.base_dir.join(K3D_BIN_FILE_NAME);
|
||||
if !binary_path.exists() {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.verify_cluster_exists(&binary_path, cluster_name)
|
||||
}
|
||||
|
||||
fn get_cluster_name(&self) -> Result<&String, String> {
|
||||
match &self.cluster_name {
|
||||
Some(name) => Ok(name),
|
||||
None => Err("No cluster name available".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a new k3d cluster with the specified name
|
||||
///
|
||||
/// This method:
|
||||
/// 1. Creates a new k3d cluster using `k3d cluster create <cluster_name>`
|
||||
/// 2. Waits for the cluster to initialize
|
||||
/// 3. Returns a configured Kubernetes client connected to the cluster
|
||||
///
|
||||
/// # Returns
|
||||
/// - `Ok(Client)` - Successfully created cluster and connected client
|
||||
/// - `Err(String)` - Error message detailing what went wrong
|
||||
pub async fn initialize_cluster(&self) -> Result<Client, String> {
|
||||
let cluster_name = match self.get_cluster_name() {
|
||||
Ok(name) => name,
|
||||
Err(_) => return Err("Could not get cluster_name, cannot initialize".to_string()),
|
||||
};
|
||||
|
||||
debug!("Initializing k3d cluster '{}'", cluster_name);
|
||||
|
||||
self.create_cluster(cluster_name)?;
|
||||
self.create_kubernetes_client().await
|
||||
}
|
||||
|
||||
fn get_k3d_binary_path(&self) -> PathBuf {
|
||||
self.base_dir.join(K3D_BIN_FILE_NAME)
|
||||
}
|
||||
|
||||
fn get_k3d_binary(&self) -> Result<PathBuf, String> {
|
||||
let path = self.get_k3d_binary_path();
|
||||
if !path.exists() {
|
||||
return Err(format!("K3d binary not found at {:?}", path));
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Ensures k3d is installed and the cluster is initialized
|
||||
///
|
||||
/// This method provides a complete setup flow:
|
||||
/// 1. Checks if k3d is installed, downloads and installs it if needed
|
||||
/// 2. Verifies if the specified cluster exists, creates it if not
|
||||
/// 3. Returns a Kubernetes client connected to the cluster
|
||||
///
|
||||
/// # Returns
|
||||
/// - `Ok(Client)` - Successfully ensured k3d and cluster are ready
|
||||
/// - `Err(String)` - Error message if any step failed
|
||||
pub async fn ensure_installed(&self) -> Result<Client, String> {
|
||||
if !self.is_installed() {
|
||||
debug!("K3d is not installed, downloading latest release");
|
||||
self.download_latest_release()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to download k3d: {}", e))?;
|
||||
|
||||
if !self.is_installed() {
|
||||
return Err("Failed to install k3d properly".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let client;
|
||||
if !self.is_cluster_initialized() {
|
||||
debug!("Cluster is not initialized, initializing now");
|
||||
client = self.initialize_cluster().await?;
|
||||
} else {
|
||||
self.start_cluster().await?;
|
||||
|
||||
debug!("K3d and cluster are already properly set up");
|
||||
client = self.create_kubernetes_client().await?;
|
||||
}
|
||||
|
||||
self.ensure_k3d_config_is_default(self.get_cluster_name()?)?;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
// Private helper methods
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
fn ensure_binary_executable(&self, binary_path: &PathBuf) -> bool {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let mut perms = match std::fs::metadata(binary_path) {
|
||||
Ok(metadata) => metadata.permissions(),
|
||||
Err(e) => {
|
||||
debug!("Failed to get binary metadata: {}", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
perms.set_mode(0o755);
|
||||
|
||||
if let Err(e) = std::fs::set_permissions(binary_path, perms) {
|
||||
debug!("Failed to set executable permissions on k3d binary: {}", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
fn ensure_binary_executable(&self, _binary_path: &PathBuf) -> bool {
|
||||
// Windows doesn't use executable file permissions
|
||||
true
|
||||
}
|
||||
|
||||
fn can_execute_binary_check(&self, binary_path: &PathBuf) -> bool {
|
||||
match std::process::Command::new(binary_path)
|
||||
.arg("--version")
|
||||
.output()
|
||||
{
|
||||
Ok(output) => {
|
||||
if output.status.success() {
|
||||
debug!("K3d binary is installed and working");
|
||||
true
|
||||
} else {
|
||||
debug!("K3d binary check failed: {:?}", output);
|
||||
false
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to execute K3d binary: {}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn verify_cluster_exists(&self, binary_path: &PathBuf, cluster_name: &str) -> bool {
|
||||
match std::process::Command::new(binary_path)
|
||||
.args(["cluster", "list", cluster_name, "--no-headers"])
|
||||
.output()
|
||||
{
|
||||
Ok(output) => {
|
||||
if output.status.success() && !output.stdout.is_empty() {
|
||||
debug!("Cluster '{}' is initialized", cluster_name);
|
||||
true
|
||||
} else {
|
||||
debug!("Cluster '{}' is not initialized", cluster_name);
|
||||
false
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!("Failed to check cluster initialization: {}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_k3d_command<I, S>(&self, args: I) -> Result<std::process::Output, String>
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: AsRef<std::ffi::OsStr>,
|
||||
{
|
||||
let binary_path = self.get_k3d_binary()?;
|
||||
self.run_command(binary_path, args)
|
||||
}
|
||||
|
||||
pub fn run_command<I, S, C>(&self, cmd: C, args: I) -> Result<std::process::Output, String>
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: AsRef<std::ffi::OsStr>,
|
||||
C: AsRef<OsStr>,
|
||||
{
|
||||
let output = std::process::Command::new(cmd).args(args).output();
|
||||
match output {
|
||||
Ok(output) => {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
debug!("stderr : {}", stderr);
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
debug!("stdout : {}", stdout);
|
||||
Ok(output)
|
||||
}
|
||||
Err(e) => Err(format!("Failed to execute command: {}", e)),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_cluster(&self, cluster_name: &str) -> Result<(), String> {
|
||||
let output = self.run_k3d_command(["cluster", "create", cluster_name])?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("Failed to create cluster: {}", stderr));
|
||||
}
|
||||
|
||||
info!("Successfully created k3d cluster '{}'", cluster_name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_k3d_config_is_default(&self, cluster_name: &str) -> Result<(), String> {
|
||||
let output = self.run_k3d_command(["kubeconfig", "merge", "-d", cluster_name])?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("Failed to setup k3d kubeconfig : {}", stderr));
|
||||
}
|
||||
|
||||
let output = self.run_command(
|
||||
"kubectl",
|
||||
["config", "use-context", &format!("k3d-{cluster_name}")],
|
||||
)?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!(
|
||||
"Failed to switch kubectl context to k3d : {}",
|
||||
stderr
|
||||
));
|
||||
}
|
||||
info!(
|
||||
"kubectl is now using 'k3d-{}' as default context",
|
||||
cluster_name
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_kubernetes_client(&self) -> Result<Client, String> {
|
||||
Client::try_default()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to create Kubernetes client: {}", e))
|
||||
}
|
||||
|
||||
pub async fn get_client(&self) -> Result<Client, String> {
|
||||
match self.is_cluster_initialized() {
|
||||
true => Ok(self.create_kubernetes_client().await?),
|
||||
false => Err("Cannot get client! Cluster not initialized yet".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
async fn start_cluster(&self) -> Result<(), String> {
|
||||
let cluster_name = self.get_cluster_name()?;
|
||||
let output = self.run_k3d_command(["cluster", "start", cluster_name])?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("Failed to start cluster: {}", stderr));
|
||||
}
|
||||
|
||||
debug!("Successfully started k3d cluster '{}'", cluster_name);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use regex::Regex;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{k3d::K3D_BIN_FILE_NAME, K3d};
|
||||
|
||||
#[tokio::test]
|
||||
async fn k3d_latest_release_should_get_latest() {
|
||||
let dir = get_clean_test_directory();
|
||||
|
||||
assert!(!dir.join(K3D_BIN_FILE_NAME).exists());
|
||||
|
||||
let k3d = K3d::new(dir.clone(), None);
|
||||
let latest_release = k3d.get_latest_release_tag().await.unwrap();
|
||||
|
||||
let tag_regex = Regex::new(r"^v\d+\.\d+\.\d+$").unwrap();
|
||||
assert!(tag_regex.is_match(&latest_release.tag_name));
|
||||
assert!(!latest_release.tag_name.is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn k3d_download_latest_release_should_get_latest_bin() {
|
||||
let dir = get_clean_test_directory();
|
||||
|
||||
assert!(!dir.join(K3D_BIN_FILE_NAME).exists());
|
||||
|
||||
let k3d = K3d::new(dir.clone(), None);
|
||||
let bin_file_path = k3d.download_latest_release().await.unwrap();
|
||||
assert_eq!(bin_file_path, dir.join(K3D_BIN_FILE_NAME));
|
||||
assert!(dir.join(K3D_BIN_FILE_NAME).exists());
|
||||
}
|
||||
|
||||
fn get_clean_test_directory() -> PathBuf {
|
||||
let dir = PathBuf::from("/tmp/harmony-k3d-test-dir");
|
||||
|
||||
if dir.exists() {
|
||||
if let Err(e) = std::fs::remove_dir_all(&dir) {
|
||||
// TODO sometimes this fails because of the race when running multiple tests at
|
||||
// once
|
||||
panic!("Failed to clean up test directory: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = std::fs::create_dir_all(&dir) {
|
||||
panic!("Failed to create test directory: {}", e);
|
||||
}
|
||||
|
||||
dir
|
||||
}
|
||||
}
|
||||
6
harmony_tools/src/lib.rs
Normal file
6
harmony_tools/src/lib.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
mod docker;
|
||||
mod downloadable_asset;
|
||||
mod k3d;
|
||||
pub use docker::*;
|
||||
use downloadable_asset::*;
|
||||
pub use k3d::*;
|
||||
Reference in New Issue
Block a user