|
| 1 | +use super::run_with_sudo::run_with_sudo; |
| 2 | +use crate::prelude::*; |
| 3 | +use crate::run::check_system::SystemInfo; |
| 4 | +use std::path::Path; |
| 5 | +use std::process::Command; |
| 6 | + |
| 7 | +fn is_system_compatible(system_info: &SystemInfo) -> bool { |
| 8 | + system_info.os == "ubuntu" || system_info.os == "debian" |
| 9 | +} |
| 10 | + |
| 11 | +/// Installs packages with caching support. |
| 12 | +/// |
| 13 | +/// This function provides a common pattern for installing tools on Ubuntu/Debian systems |
| 14 | +/// with automatic caching to speed up subsequent installations (e.g., in CI environments). |
| 15 | +/// |
| 16 | +/// # Arguments |
| 17 | +/// |
| 18 | +/// * `system_info` - System information to determine compatibility |
| 19 | +/// * `setup_cache_dir` - Optional directory to restore from/save to cache |
| 20 | +/// * `is_installed` - Function that checks if the tool is already installed |
| 21 | +/// * `install_packages` - Async closure that: |
| 22 | +/// 1. Performs the installation (e.g., downloads .deb files, calls `apt::install`) |
| 23 | +/// 2. Returns a Vec of package names that should be cached via `dpkg -L` |
| 24 | +/// |
| 25 | +/// # Flow |
| 26 | +/// |
| 27 | +/// 1. Check if already installed - if yes, skip everything |
| 28 | +/// 2. Try to restore from cache (if cache_dir provided) |
| 29 | +/// 3. Check again if installed - if yes, we're done |
| 30 | +/// 4. Run the install closure to install and get package names |
| 31 | +/// 5. Save installed packages to cache (if cache_dir provided) |
| 32 | +/// |
| 33 | +/// # Example |
| 34 | +/// |
| 35 | +/// ```rust,ignore |
| 36 | +/// apt::install_cached( |
| 37 | +/// system_info, |
| 38 | +/// setup_cache_dir, |
| 39 | +/// || Command::new("which").arg("perf").status().is_ok(), |
| 40 | +/// || async { |
| 41 | +/// let packages = vec!["linux-tools-common".to_string()]; |
| 42 | +/// let refs: Vec<&str> = packages.iter().map(|s| s.as_str()).collect(); |
| 43 | +/// apt::install(system_info, &refs)?; |
| 44 | +/// Ok(packages) // Return package names for caching |
| 45 | +/// }, |
| 46 | +/// ).await?; |
| 47 | +/// ``` |
| 48 | +pub async fn install_cached<F, I, Fut>( |
| 49 | + system_info: &SystemInfo, |
| 50 | + setup_cache_dir: Option<&Path>, |
| 51 | + is_installed: F, |
| 52 | + install_packages: I, |
| 53 | +) -> Result<()> |
| 54 | +where |
| 55 | + F: Fn() -> bool, |
| 56 | + I: FnOnce() -> Fut, |
| 57 | + Fut: std::future::Future<Output = Result<Vec<String>>>, |
| 58 | +{ |
| 59 | + if is_installed() { |
| 60 | + info!("Tool already installed, skipping installation"); |
| 61 | + return Ok(()); |
| 62 | + } |
| 63 | + |
| 64 | + // Try to restore from cache first |
| 65 | + if let Some(cache_dir) = setup_cache_dir { |
| 66 | + restore_from_cache(system_info, cache_dir)?; |
| 67 | + |
| 68 | + if is_installed() { |
| 69 | + info!("Tool has been successfully restored from cache"); |
| 70 | + return Ok(()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Install and get the package names for caching |
| 75 | + let cache_packages = install_packages().await?; |
| 76 | + |
| 77 | + info!("Installation completed successfully"); |
| 78 | + |
| 79 | + // Save to cache after successful installation |
| 80 | + if let Some(cache_dir) = setup_cache_dir { |
| 81 | + let cache_refs: Vec<&str> = cache_packages.iter().map(|s| s.as_str()).collect(); |
| 82 | + save_to_cache(system_info, cache_dir, &cache_refs)?; |
| 83 | + } |
| 84 | + |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +pub fn install(system_info: &SystemInfo, packages: &[&str]) -> Result<()> { |
| 89 | + if !is_system_compatible(system_info) { |
| 90 | + bail!( |
| 91 | + "Package installation is not supported on this system, please install necessary packages manually" |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + debug!("Installing packages: {packages:?}"); |
| 96 | + |
| 97 | + run_with_sudo(&["apt-get", "update"])?; |
| 98 | + let mut install_cmd = vec!["apt-get", "install", "-y", "--allow-downgrades"]; |
| 99 | + install_cmd.extend_from_slice(packages); |
| 100 | + run_with_sudo(&install_cmd)?; |
| 101 | + |
| 102 | + debug!("Packages installed successfully"); |
| 103 | + Ok(()) |
| 104 | +} |
| 105 | + |
| 106 | +/// Restore cached tools from the cache directory to the root filesystem |
| 107 | +fn restore_from_cache(system_info: &SystemInfo, cache_dir: &Path) -> Result<()> { |
| 108 | + if !is_system_compatible(system_info) { |
| 109 | + warn!("Cache restore is not supported on this system, skipping"); |
| 110 | + return Ok(()); |
| 111 | + } |
| 112 | + |
| 113 | + if !cache_dir.exists() { |
| 114 | + debug!("Cache directory does not exist: {}", cache_dir.display()); |
| 115 | + return Ok(()); |
| 116 | + } |
| 117 | + |
| 118 | + // Check if the directory has any contents |
| 119 | + let has_contents = std::fs::read_dir(cache_dir) |
| 120 | + .map(|mut entries| entries.next().is_some()) |
| 121 | + .unwrap_or(false); |
| 122 | + |
| 123 | + if !has_contents { |
| 124 | + debug!("Cache directory is empty: {}", cache_dir.display()); |
| 125 | + return Ok(()); |
| 126 | + } |
| 127 | + |
| 128 | + debug!( |
| 129 | + "Restoring tools from cache directory: {}", |
| 130 | + cache_dir.display() |
| 131 | + ); |
| 132 | + |
| 133 | + // Use bash to properly handle glob expansion |
| 134 | + let cache_dir_str = cache_dir |
| 135 | + .to_str() |
| 136 | + .ok_or_else(|| anyhow!("Invalid cache directory path"))?; |
| 137 | + |
| 138 | + let copy_cmd = format!("cp -r {cache_dir_str}/* /"); |
| 139 | + |
| 140 | + run_with_sudo(&["bash", "-c", ©_cmd])?; |
| 141 | + |
| 142 | + debug!("Cache restored successfully"); |
| 143 | + Ok(()) |
| 144 | +} |
| 145 | + |
| 146 | +/// Save installed packages to the cache directory |
| 147 | +fn save_to_cache(system_info: &SystemInfo, cache_dir: &Path, packages: &[&str]) -> Result<()> { |
| 148 | + if !is_system_compatible(system_info) { |
| 149 | + warn!("Caching of installed package is not supported on this system, skipping"); |
| 150 | + return Ok(()); |
| 151 | + } |
| 152 | + |
| 153 | + debug!( |
| 154 | + "Saving installed packages to cache: {}", |
| 155 | + cache_dir.display() |
| 156 | + ); |
| 157 | + |
| 158 | + // Create cache directory if it doesn't exist |
| 159 | + std::fs::create_dir_all(cache_dir).context("Failed to create cache directory")?; |
| 160 | + |
| 161 | + let cache_dir_str = cache_dir |
| 162 | + .to_str() |
| 163 | + .ok_or_else(|| anyhow!("Invalid cache directory path"))?; |
| 164 | + |
| 165 | + // Logic taken from https://stackoverflow.com/a/59277514 |
| 166 | + // This shell command lists all the files outputted by the given packages and copy them to the cache directory |
| 167 | + let packages_str = packages.join(" "); |
| 168 | + let shell_cmd = format!( |
| 169 | + "dpkg -L {packages_str} | while IFS= read -r f; do if test -f \"$f\"; then echo \"$f\"; fi; done | xargs cp --parents --target-directory {cache_dir_str}", |
| 170 | + ); |
| 171 | + |
| 172 | + debug!("Running cache save command: {shell_cmd}"); |
| 173 | + |
| 174 | + let output = Command::new("sh") |
| 175 | + .arg("-c") |
| 176 | + .arg(&shell_cmd) |
| 177 | + .output() |
| 178 | + .context("Failed to execute cache save command")?; |
| 179 | + |
| 180 | + if !output.status.success() { |
| 181 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 182 | + error!("stderr: {stderr}"); |
| 183 | + bail!("Failed to save packages to cache"); |
| 184 | + } |
| 185 | + |
| 186 | + debug!("Packages cached successfully"); |
| 187 | + Ok(()) |
| 188 | +} |
0 commit comments