crates/edge/cli/src/bundle.rs 3.5 KiB 111 lines Raw
1 //! The bundle half of `doctor`.2 //!3 //! A bundle is a directory, so the check walks the deploy file's plugin4 //! directories and checks every directory that has a manifest. The shape check5 //! comes before the digest because a missing part is a reason to stop an6 //! install even when the files that remain hash correctly. The check runs7 //! before the socket check for the same reason.8 9 use std::path::{Path, PathBuf};10 11 use inf3r_bundle::{Bundle, MANIFEST};12 13 use crate::http::CliError;14 15 /// The line that `doctor` prints when the deploy file is absent.16 pub const NO_DEPLOY: &str = "bundles: no deploy file";17 18 /// The result of the bundle check.19 #[derive(Clone, PartialEq, Eq, Debug)]20 pub struct Check {21     /// True when every bundle was complete and verified.22     pub ok: bool,23     /// One line per problem, in path order.24     pub lines: Vec<String>,25 }26 27 /// Checks every bundle below the deploy file's bundle directories.28 ///29 /// A missing deploy file is not a failed check: a host without bindings has no30 /// bundles to check. A directory without a manifest is not a bundle and is31 /// skipped.32 pub fn bundles(deploy: &Path) -> Result<Check, CliError> {33     if !deploy.is_file() {34         return Ok(Check {35             ok: true,36             lines: vec![NO_DEPLOY.to_string()],37         });38     }39     let deploy = match inf3r_deploy::Deploy::read(deploy) {40         Ok(deploy) => deploy,41         Err(why) => {42             return Ok(Check {43                 ok: false,44                 lines: vec![format!("deploy {}: {why}", deploy.display())],45             });46         }47     };48     let mut dirs = Vec::new();49     for root in &deploy.node.bundle_dirs {50         find_bundles(root, &mut dirs)?;51     }52     dirs.sort();53 54     let mut check = Check {55         ok: true,56         lines: Vec::new(),57     };58     for dir in dirs {59         let bundle = Bundle::read(&dir);60         let id = match &bundle {61             Ok(bundle) => bundle.id.clone(),62             Err(_) => dir.display().to_string(),63         };64         match bundle {65             Ok(bundle) => {66                 let mut complete = true;67                 for problem in bundle.anatomy(&dir) {68                     complete = false;69                     check.ok = false;70                     check.lines.push(format!("bundle {id}: {problem}"));71                 }72                 match Bundle::verify(&dir) {73                     Ok(()) if complete => check.lines.push(format!("bundle {id}: ok")),74                     Ok(()) => {}75                     Err(why) => {76                         check.ok = false;77                         check.lines.push(format!("bundle {id}: {why}"));78                     }79                 }80             }81             Err(why) => {82                 check.ok = false;83                 check.lines.push(format!("bundle {id}: {why}"));84             }85         }86     }87     Ok(check)88 }89 90 fn find_bundles(root: &Path, found: &mut Vec<PathBuf>) -> Result<(), CliError> {91     let entries = match std::fs::read_dir(root) {92         Ok(entries) => entries,93         Err(why) if why.kind() == std::io::ErrorKind::NotFound => return Ok(()),94         Err(why) => return Err(CliError::Io(why.to_string())),95     };96     for entry in entries {97         let entry = entry.map_err(|why| CliError::Io(why.to_string()))?;98         let kind = entry99             .file_type()100             .map_err(|why| CliError::Io(why.to_string()))?;101         if !kind.is_dir() {102             continue;103         }104         let dir = entry.path();105         if dir.join(MANIFEST).is_file() {106             found.push(dir.clone());107         }108         find_bundles(&dir, found)?;109     }110     Ok(())111 }