diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-10-24 21:40:53 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-10-24 21:40:53 +0200 |
commit | 152272df6e28af35b13d393ec30b8fd3a333bccf (patch) | |
tree | e8e89790a858d303b80c0815391acb9bf7ea173a | |
parent | 4604ddfb40c10d8f33fa2993c2c3c84bbe993e55 (diff) | |
download | rebel-152272df6e28af35b13d393ec30b8fd3a333bccf.tar rebel-152272df6e28af35b13d393ec30b8fd3a333bccf.zip |
Move definition of Dependency into runner module
runner::TaskInput does not contain the dependency hashes anymore, this
is handled internally in the runner module now. Unfortunately this also
means that we can't use this struct directly for the InputHash anymore,
but overall this reduces the complexity of the interface between runner
and executor.
-rw-r--r-- | src/executor.rs | 11 | ||||
-rw-r--r-- | src/runner/container/task.rs | 33 | ||||
-rw-r--r-- | src/runner/mod.rs | 11 | ||||
-rw-r--r-- | src/types.rs | 17 |
4 files changed, 43 insertions, 29 deletions
diff --git a/src/executor.rs b/src/executor.rs index 39e262d..10bb57f 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -66,13 +66,13 @@ impl<'ctx> Executor<'ctx> { .all(|dep| self.tasks_done.contains_key(&dep)) } - fn fetch_deps(&self, task: &TaskRef<'ctx>) -> Result<Vec<Dependency>> { + fn fetch_deps(&self, task: &TaskRef<'ctx>) -> Result<Vec<runner::Dependency>> { let task_def = &self.ctx[task.id]; task_def .fetch .iter() .map(|Fetch { name, sha256 }| { - Ok(Dependency::Fetch { + Ok(runner::Dependency::Fetch { name: self.tpl.eval_raw(name, &task.args).with_context(|| { format!("Failed to evaluate fetch filename for task {}", task) })?, @@ -82,7 +82,7 @@ impl<'ctx> Executor<'ctx> { .collect() } - fn task_deps(&self, task: &TaskRef<'ctx>) -> Result<HashMap<DependencyHash, Dependency>> { + fn task_deps(&self, task: &TaskRef<'ctx>) -> Result<HashSet<runner::Dependency>> { Ok(self .fetch_deps(task)? .into_iter() @@ -96,7 +96,7 @@ impl<'ctx> Executor<'ctx> { .expect("invalid runtime depends of build_depends") .into_iter() .filter_map(|dep| self.tasks_done[&dep.task].outputs.get(dep.output)) - .map(|&output| Dependency::Task { + .map(|&output| runner::Dependency::Task { output, path: "".to_string(), }), @@ -111,12 +111,11 @@ impl<'ctx> Executor<'ctx> { .expect("invalid runtime depends of host_depends") .into_iter() .filter_map(|dep| self.tasks_done[&dep.task].outputs.get(dep.output)) - .map(|&output| Dependency::Task { + .map(|&output| runner::Dependency::Task { output, path: paths::abs(paths::TASK_SYSROOT), }), ) - .map(|dep| (dep.dependency_hash(), dep)) .collect()) } diff --git a/src/runner/container/task.rs b/src/runner/container/task.rs index 06e542f..bc75fd1 100644 --- a/src/runner/container/task.rs +++ b/src/runner/container/task.rs @@ -14,6 +14,7 @@ use nix::{ sys::wait, unistd::{self, Gid, Uid}, }; +use serde::Serialize; use tee_readwrite::TeeWriter; use crate::{ @@ -27,9 +28,33 @@ use super::{jobserver::Jobserver, ns, tar}; pub const BUILD_UID: Uid = Uid::from_raw(1000); pub const BUILD_GID: Gid = Gid::from_raw(1000); +fn dependency_hash(dep: &runner::Dependency) -> DependencyHash { + DependencyHash(StringHash( + cjson::digest::<DependencyHasher, _>(dep).unwrap().into(), + )) +} + fn input_hash(task: &runner::TaskInput) -> InputHash { + #[derive(Debug, Serialize)] + struct HashInput<'a> { + pub command: &'a str, + pub inherit: &'a [LayerHash], + pub depends: HashMap<DependencyHash, &'a runner::Dependency>, + pub outputs: &'a HashMap<String, String>, + } + let input = HashInput { + command: &task.command, + inherit: &task.inherit, + depends: task + .depends + .iter() + .map(|dep| (dependency_hash(dep), dep)) + .collect(), + outputs: &task.outputs, + }; + InputHash(StringHash( - cjson::digest::<InputHasher, _>(task).unwrap().into(), + cjson::digest::<InputHasher, _>(&input).unwrap().into(), )) } @@ -143,15 +168,15 @@ fn unpack_dependencies(input_hash: &InputHash, task: &runner::Task) -> Result<() fs::mkdir(&downloads_dir)?; fs::mkdir(&depends_dir)?; - for dep in task.input.depends.values() { + for dep in &task.input.depends { match dep { - Dependency::Fetch { name, .. } => { + runner::Dependency::Fetch { name, .. } => { fs::copy( paths::join(&[paths::DOWNLOADS_DIR, name]), paths::join(&[&downloads_dir, name]), )?; } - Dependency::Task { output, path } => { + runner::Dependency::Task { output, path } => { unpack_dependency( paths::archive_filename(output), paths::join(&[&depends_dir, path]), diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 02e7884..92f28f0 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -2,15 +2,22 @@ pub mod container; use ipc_channel::ipc; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use crate::{types::*, util::error::*}; +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq, Hash)] +#[serde(rename_all = "snake_case")] +pub enum Dependency { + Fetch { name: String, sha256: StringHash }, + Task { output: ArchiveHash, path: String }, +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct TaskInput { pub command: String, pub inherit: Vec<LayerHash>, - pub depends: HashMap<DependencyHash, Dependency>, + pub depends: HashSet<Dependency>, pub outputs: HashMap<String, String>, } diff --git a/src/types.rs b/src/types.rs index fe57540..338d608 100644 --- a/src/types.rs +++ b/src/types.rs @@ -2,8 +2,6 @@ use std::fmt::Display; use serde::{Deserialize, Serialize}; -use crate::util::cjson; - #[derive(Clone, Copy, PartialEq, Eq, Hash)] pub struct StringHash(pub [u8; 32]); @@ -72,18 +70,3 @@ impl Display for TaskID { write!(f, "{}:{}", self.recipe, self.task) } } - -#[derive(Clone, Debug, Deserialize, Serialize)] -#[serde(rename_all = "snake_case")] -pub enum Dependency { - Fetch { name: String, sha256: StringHash }, - Task { output: ArchiveHash, path: String }, -} - -impl Dependency { - pub fn dependency_hash(&self) -> DependencyHash { - DependencyHash(StringHash( - cjson::digest::<DependencyHasher, _>(self).unwrap().into(), - )) - } -} |