|
| 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 | +pub fn install(system_info: &SystemInfo, packages: &[&str]) -> Result<()> { |
| 12 | + if !is_system_compatible(system_info) { |
| 13 | + bail!( |
| 14 | + "Package installation is not supported on this system, please install necessary packages manually" |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + debug!("Installing packages: {packages:?}"); |
| 19 | + |
| 20 | + run_with_sudo(&["apt-get", "update"])?; |
| 21 | + let mut install_cmd = vec![ |
| 22 | + "apt-get", |
| 23 | + "install", |
| 24 | + "-y", |
| 25 | + "--allow-downgrades", |
| 26 | + "--reinstall", |
| 27 | + ]; |
| 28 | + install_cmd.extend_from_slice(packages); |
| 29 | + run_with_sudo(&install_cmd)?; |
| 30 | + |
| 31 | + info!("Packages installed successfully"); |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +/// Restore cached tools from the cache directory to the root filesystem |
| 36 | +pub fn restore_from_cache(system_info: &SystemInfo, cache_dir: &Path) -> Result<()> { |
| 37 | + if !is_system_compatible(system_info) { |
| 38 | + warn!("Cache restore is not supported on this system, skipping"); |
| 39 | + return Ok(()); |
| 40 | + } |
| 41 | + |
| 42 | + if !cache_dir.exists() { |
| 43 | + debug!("Cache directory does not exist: {}", cache_dir.display()); |
| 44 | + return Ok(()); |
| 45 | + } |
| 46 | + |
| 47 | + // Check if the directory has any contents |
| 48 | + let has_contents = std::fs::read_dir(cache_dir) |
| 49 | + .map(|mut entries| entries.next().is_some()) |
| 50 | + .unwrap_or(false); |
| 51 | + |
| 52 | + if !has_contents { |
| 53 | + debug!("Cache directory is empty: {}", cache_dir.display()); |
| 54 | + return Ok(()); |
| 55 | + } |
| 56 | + |
| 57 | + info!( |
| 58 | + "Restoring tools from cache directory: {}", |
| 59 | + cache_dir.display() |
| 60 | + ); |
| 61 | + |
| 62 | + // Use bash to properly handle glob expansion |
| 63 | + let cache_dir_str = cache_dir |
| 64 | + .to_str() |
| 65 | + .ok_or_else(|| anyhow!("Invalid cache directory path"))?; |
| 66 | + |
| 67 | + let copy_cmd = format!("cp -r {cache_dir_str}/* /"); |
| 68 | + |
| 69 | + let output = Command::new("sudo") |
| 70 | + .args(["bash", "-c", ©_cmd]) |
| 71 | + .output() |
| 72 | + .context("Failed to run cache restore command")?; |
| 73 | + |
| 74 | + if !output.status.success() { |
| 75 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 76 | + error!("stderr: {stderr}"); |
| 77 | + bail!("Failed to restore cache"); |
| 78 | + } |
| 79 | + |
| 80 | + info!("Cache restored successfully"); |
| 81 | + Ok(()) |
| 82 | +} |
| 83 | + |
| 84 | +/// Save installed packages to the cache directory |
| 85 | +pub fn save_to_cache(system_info: &SystemInfo, cache_dir: &Path, packages: &[&str]) -> Result<()> { |
| 86 | + if !is_system_compatible(system_info) { |
| 87 | + warn!("Caching of installed package is not supported on this system, skipping"); |
| 88 | + return Ok(()); |
| 89 | + } |
| 90 | + |
| 91 | + info!( |
| 92 | + "Saving installed packages to cache: {}", |
| 93 | + cache_dir.display() |
| 94 | + ); |
| 95 | + |
| 96 | + // Create cache directory if it doesn't exist |
| 97 | + std::fs::create_dir_all(cache_dir).context("Failed to create cache directory")?; |
| 98 | + |
| 99 | + let cache_dir_str = cache_dir |
| 100 | + .to_str() |
| 101 | + .ok_or_else(|| anyhow!("Invalid cache directory path"))?; |
| 102 | + |
| 103 | + // Logic taken from https://stackoverflow.com/a/59277514 |
| 104 | + // This shell command lists all the files outputted by the given packages and copy them to the cache directory |
| 105 | + let packages_str = packages.join(" "); |
| 106 | + let shell_cmd = format!( |
| 107 | + "sudo 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}", |
| 108 | + ); |
| 109 | + |
| 110 | + debug!("Running cache save command: {shell_cmd}"); |
| 111 | + |
| 112 | + let output = Command::new("sh") |
| 113 | + .arg("-c") |
| 114 | + .arg(&shell_cmd) |
| 115 | + .output() |
| 116 | + .context("Failed to execute cache save command")?; |
| 117 | + |
| 118 | + if !output.status.success() { |
| 119 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 120 | + error!("stderr: {stderr}"); |
| 121 | + bail!("Failed to save packages to cache"); |
| 122 | + } |
| 123 | + |
| 124 | + info!("Packages cached successfully"); |
| 125 | + Ok(()) |
| 126 | +} |
0 commit comments