Author:
Hash:
Timestamp:
+531 -358 +/-17 browse
Kevin Schoon [me@kevinschoon.com]
6bbee5c52d22ea17c7326a0d32cc5ec304845eb4
Tue, 08 Sep 2026 21:58:49 +0000 (2 months ago)
| 1 | diff --git a/ayllu-build/src/config.rs b/ayllu-build/src/config.rs |
| 2 | index b16ccda..7094483 100644 |
| 3 | --- a/ayllu-build/src/config.rs |
| 4 | +++ b/ayllu-build/src/config.rs |
| 5 | @@ -8,6 +8,8 @@ use ayllu_config::{Configurable, Database, Error, Reader, data_dir}; |
| 6 | |
| 7 | use crate::runtime::oci::DEFAULT_INTERRUPT_TIMEOUT_SECS; |
| 8 | |
| 9 | + pub const DEFAULT_BUILD_CONFIG_PATH: &str = ".ayllu-build.json"; |
| 10 | + |
| 11 | /// Optional pre-processor command which will pipe a source file through some |
| 12 | /// external program. The external program must return a valid manifest JSON |
| 13 | /// object. |
| 14 | @@ -66,6 +68,8 @@ impl Default for Image { |
| 15 | pub struct Build { |
| 16 | #[serde(default = "Build::default_work_dir")] |
| 17 | pub work_dir: PathBuf, |
| 18 | + #[serde(default = "Build::default_build_config_path")] |
| 19 | + pub default_build_config_path: PathBuf, |
| 20 | pub pre_processor: Option<PreProcessor>, |
| 21 | #[serde(default = "Build::default_oci_binary")] |
| 22 | pub oci_binary: PathBuf, |
| 23 | @@ -90,6 +94,10 @@ impl Build { |
| 24 | PathBuf::from("/usr/bin/crun") |
| 25 | } |
| 26 | |
| 27 | + fn default_build_config_path() -> PathBuf { |
| 28 | + PathBuf::from(DEFAULT_BUILD_CONFIG_PATH) |
| 29 | + } |
| 30 | + |
| 31 | fn default_interrupt_timeout() -> u64 { |
| 32 | DEFAULT_INTERRUPT_TIMEOUT_SECS |
| 33 | } |
| 34 | @@ -118,6 +126,7 @@ impl Default for Build { |
| 35 | init_script: Build::default_init_script(), |
| 36 | runtime_dir: Build::default_runtime_dir(), |
| 37 | sample_rate_secs: Build::default_sample_rate_secs(), |
| 38 | + default_build_config_path: Build::default_build_config_path(), |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | diff --git a/ayllu-build/src/error.rs b/ayllu-build/src/error.rs |
| 43 | index 8cac31b..205ebf8 100644 |
| 44 | --- a/ayllu-build/src/error.rs |
| 45 | +++ b/ayllu-build/src/error.rs |
| 46 | @@ -65,7 +65,7 @@ pub enum Error { |
| 47 | }, |
| 48 | #[error("Invalid workspace: {0:?}")] |
| 49 | InvalidWorkspace(PathBuf), |
| 50 | - #[error("Bad source, should have three parts .e.g $collection:$name:[$refname], got: {0}")] |
| 51 | + #[error("Bad source, should have three parts .e.g $collection/$name:[$refname], got: {0}")] |
| 52 | InvalidSource(String), |
| 53 | #[error("Invalid CGroup specifier: {0}")] |
| 54 | InvalidCGroupSpecifier(String), |
| 55 | diff --git a/ayllu-build/src/executor.rs b/ayllu-build/src/executor.rs |
| 56 | index c4835c5..f7ca1da 100644 |
| 57 | --- a/ayllu-build/src/executor.rs |
| 58 | +++ b/ayllu-build/src/executor.rs |
| 59 | @@ -13,7 +13,7 @@ use crate::config::Config; |
| 60 | use crate::error::Error; |
| 61 | use crate::graph::Graph; |
| 62 | use crate::runtime::{Handle, Runtime, Update}; |
| 63 | - use crate::source::{Resolved, Source}; |
| 64 | + use crate::source::Source; |
| 65 | |
| 66 | use ayllu_database::{Blob, Ptr, Wrapper as Database}; |
| 67 | |
| 68 | @@ -68,15 +68,11 @@ impl Executor { |
| 69 | } |
| 70 | |
| 71 | /// Allocate the manifest in the database along with it's DAG |
| 72 | - fn allocate(&self, db: &mut Database, source: &Source) -> Result<(Resolved, Graph), Error> { |
| 73 | - let resolved = &source.read( |
| 74 | - self.config.collections.as_slice(), |
| 75 | - self.config.build.pre_processor.as_ref(), |
| 76 | - )?; |
| 77 | + fn allocate(&self, db: &mut Database, source: &Source) -> Result<Graph, Error> { |
| 78 | Ok(db.with(move |tx| { |
| 79 | - let g = Graph::new(resolved, &mut crate::graph::Allocator::Database(tx)) |
| 80 | + let g = Graph::new(source, &mut crate::graph::Allocator::Database(tx)) |
| 81 | .map_err(|e| ayllu_database::Error::Rollback(Box::new(e)))?; |
| 82 | - Ok((resolved.clone(), g)) |
| 83 | + Ok(g) |
| 84 | })?) |
| 85 | } |
| 86 | |
| 87 | @@ -87,14 +83,14 @@ impl Executor { |
| 88 | { |
| 89 | let mut db = Database::new(&self.config.database.path)?; |
| 90 | let mut blob = Blob::new(&self.config.database.path)?; |
| 91 | - let (resolved, graph) = self.allocate(&mut db, source)?; |
| 92 | + let graph = self.allocate(&mut db, source)?; |
| 93 | tracing::info!("evaluating DAG [{}]:\n{}", graph.id(), graph.dot()); |
| 94 | let mut conn = db.call(); |
| 95 | let manifest_id = graph.id(); |
| 96 | |
| 97 | let units = graph.units(); |
| 98 | let mut units = units.iter(); |
| 99 | - runtime.initalize(&resolved, &graph)?; |
| 100 | + runtime.initalize(source, &graph)?; |
| 101 | |
| 102 | conn.manifest_start(manifest_id)?; |
| 103 | conn.manifest_set_dag_content(manifest_id, &graph.as_json())?; |
| 104 | @@ -256,19 +252,16 @@ impl Executor { |
| 105 | |
| 106 | #[cfg(test)] |
| 107 | mod test { |
| 108 | - use std::{cell::RefCell, collections::HashMap, time::Duration}; |
| 109 | + use std::collections::HashMap; |
| 110 | |
| 111 | use ayllu_database::build::State; |
| 112 | |
| 113 | use crate::{ |
| 114 | config::Image, |
| 115 | - error::Error, |
| 116 | executor::ExecutorBuilder, |
| 117 | - runtime::{ |
| 118 | - Handle, Runtime, Sha256Sum, Update, |
| 119 | - test_utils::{Action, FakeRuntime}, |
| 120 | - }, |
| 121 | - source::{Resolved, Source}, |
| 122 | + manifest::Manifest, |
| 123 | + runtime::test_utils::{Action, FakeRuntime}, |
| 124 | + source::Source, |
| 125 | }; |
| 126 | |
| 127 | const TEST_MANIFEST: &str = r#" |
| 128 | @@ -291,9 +284,8 @@ mod test { |
| 129 | let manifest_source_file = tmp_file.path().join("manifest.json"); |
| 130 | std::fs::write(&manifest_source_file, TEST_MANIFEST).unwrap(); |
| 131 | ayllu_database::migrate(&test_db_file).unwrap(); |
| 132 | - let source = Source::Path { |
| 133 | - path: Some(manifest_source_file), |
| 134 | - }; |
| 135 | + let manifest = serde_json::from_str::<Manifest>(TEST_MANIFEST).unwrap(); |
| 136 | + let source = Source::Anonymous { manifest }; |
| 137 | let executor = ExecutorBuilder::default() |
| 138 | .config(crate::config::Config { |
| 139 | database: ayllu_config::Database { |
| 140 | @@ -344,9 +336,8 @@ mod test { |
| 141 | let manifest_source_file = tmp_file.path().join("manifest.json"); |
| 142 | std::fs::write(&manifest_source_file, TEST_MANIFEST).unwrap(); |
| 143 | ayllu_database::migrate(&test_db_file).unwrap(); |
| 144 | - let source = Source::Path { |
| 145 | - path: Some(manifest_source_file), |
| 146 | - }; |
| 147 | + let manifest = serde_json::from_str::<Manifest>(TEST_MANIFEST).unwrap(); |
| 148 | + let source = Source::Anonymous { manifest }; |
| 149 | let executor = ExecutorBuilder::default() |
| 150 | .config(crate::config::Config { |
| 151 | database: ayllu_config::Database { |
| 152 | diff --git a/ayllu-build/src/graph.rs b/ayllu-build/src/graph.rs |
| 153 | index e0301f5..2893420 100644 |
| 154 | --- a/ayllu-build/src/graph.rs |
| 155 | +++ b/ayllu-build/src/graph.rs |
| 156 | @@ -12,7 +12,7 @@ use petgraph::{ |
| 157 | use crate::{ |
| 158 | error::Error, |
| 159 | manifest::{Cache, Output, Step, Workflow}, |
| 160 | - source::Resolved, |
| 161 | + source::Source, |
| 162 | }; |
| 163 | |
| 164 | /// An allocator returns a unique identifier which associatied with each |
| 165 | @@ -24,16 +24,25 @@ pub enum Allocator<'a> { |
| 166 | } |
| 167 | |
| 168 | impl Allocator<'_> { |
| 169 | - pub fn manifest_id(&mut self, resolved: &Resolved) -> Result<i32, Error> { |
| 170 | + pub fn manifest_id(&mut self, source: &Source) -> Result<i32, Error> { |
| 171 | match self { |
| 172 | - Allocator::Database(ptr) => ptr |
| 173 | - .manifest_create( |
| 174 | - &resolved.manifest, |
| 175 | - resolved.collection.as_deref(), |
| 176 | - resolved.name.as_deref(), |
| 177 | - resolved.git_hash.as_deref(), |
| 178 | - ) |
| 179 | - .map_err(Error::Database), |
| 180 | + Allocator::Database(ptr) => match source { |
| 181 | + Source::Anonymous { manifest } => ptr |
| 182 | + .manifest_create(manifest, None, None, None) |
| 183 | + .map_err(Error::Database), |
| 184 | + Source::Repository { |
| 185 | + manifest, |
| 186 | + collection, |
| 187 | + repository, |
| 188 | + } => ptr |
| 189 | + .manifest_create( |
| 190 | + manifest, |
| 191 | + Some(&collection.name), |
| 192 | + Some(&repository.name), |
| 193 | + repository.reference.as_deref(), |
| 194 | + ) |
| 195 | + .map_err(Error::Database), |
| 196 | + }, |
| 197 | Allocator::Fixed(id) => { |
| 198 | let id = *id; |
| 199 | *self = Self::Fixed(id + 1); |
| 200 | @@ -160,9 +169,12 @@ impl Graph { |
| 201 | ) |
| 202 | } |
| 203 | |
| 204 | - pub fn new(resolved: &Resolved, allocator: &mut Allocator) -> Result<Self, Error> { |
| 205 | - let manifest_id = allocator.manifest_id(resolved)?; |
| 206 | - let workflows = &resolved.manifest.workflows; |
| 207 | + pub fn new(source: &Source, allocator: &mut Allocator) -> Result<Self, Error> { |
| 208 | + let manifest_id = allocator.manifest_id(source)?; |
| 209 | + let workflows = match source { |
| 210 | + Source::Anonymous { manifest } => &manifest.workflows, |
| 211 | + Source::Repository { manifest, .. } => &manifest.workflows, |
| 212 | + }; |
| 213 | if let Some(wf) = workflows.iter().find(|wf| wf.steps.is_empty()) { |
| 214 | return Err(Error::EmptyWorkflow { |
| 215 | name: wf.name.clone(), |
| 216 | @@ -391,26 +403,25 @@ mod test { |
| 217 | |
| 218 | macro_rules! source { |
| 219 | ( $( $workflows:expr ),* ) => { |
| 220 | - Resolved{ |
| 221 | + Source::Anonymous{ |
| 222 | manifest: Manifest { |
| 223 | workflows: vec![$($workflows),*] |
| 224 | }, |
| 225 | - ..Default::default() |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | struct TestCase { |
| 231 | - pub input: Resolved, |
| 232 | + pub input: Source, |
| 233 | pub expected: Result<Vec<Id>, Error>, |
| 234 | } |
| 235 | |
| 236 | impl TestCase { |
| 237 | fn n_units(&self) -> usize { |
| 238 | - self.input.manifest.workflows.len() |
| 239 | + self.input.manifest().workflows.len() |
| 240 | + self |
| 241 | .input |
| 242 | - .manifest |
| 243 | + .manifest() |
| 244 | .workflows |
| 245 | .iter() |
| 246 | .fold(0, |n, wf| n + wf.steps.len()) |
| 247 | @@ -550,9 +561,8 @@ mod test { |
| 248 | }) |
| 249 | .collect(); |
| 250 | run_test_case(TestCase { |
| 251 | - input: Resolved { |
| 252 | + input: Source::Anonymous { |
| 253 | manifest: Manifest { workflows }, |
| 254 | - ..Default::default() |
| 255 | }, |
| 256 | expected: Ok(vec![ |
| 257 | Id::workflow("F"), |
| 258 | diff --git a/ayllu-build/src/main.rs b/ayllu-build/src/main.rs |
| 259 | index f367e10..19fe167 100644 |
| 260 | --- a/ayllu-build/src/main.rs |
| 261 | +++ b/ayllu-build/src/main.rs |
| 262 | @@ -10,7 +10,7 @@ use crate::{ |
| 263 | Runtime, |
| 264 | oci::{GcBuilder, OciRuntimeBuilder, TemplateBuilder, debug_shell}, |
| 265 | }, |
| 266 | - source::Source, |
| 267 | + source::{Source, SourceArgs}, |
| 268 | }; |
| 269 | |
| 270 | mod config; |
| 271 | @@ -45,9 +45,17 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 272 | |
| 273 | match cli.command { |
| 274 | Commands::Check { source } => { |
| 275 | - let source = Source::from_arg(None, source.as_deref())?; |
| 276 | - let resolved = source.read(&cfg.collections, cfg.build.pre_processor.as_ref())?; |
| 277 | - let (n_workflows, n_steps) = resolved.manifest.workflows.iter().fold( |
| 278 | + let source = Source::new( |
| 279 | + &SourceArgs { |
| 280 | + source: source.as_deref().unwrap_or("."), |
| 281 | + collections: &cfg.collections, |
| 282 | + pre_processor: cfg.build.pre_processor.as_ref(), |
| 283 | + config_source: &cfg.build.default_build_config_path, |
| 284 | + anonymous: false, |
| 285 | + }, |
| 286 | + Some(&mut std::io::stdin()), |
| 287 | + )?; |
| 288 | + let (n_workflows, n_steps) = source.manifest().workflows.iter().fold( |
| 289 | (0, 0), |
| 290 | |(n_workflows, n_steps), workflow| { |
| 291 | (n_workflows + 1_usize, n_steps + workflow.steps.len()) |
| 292 | @@ -74,12 +82,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 293 | .object_id |
| 294 | .expect("Object ID should reference a commit"); |
| 295 | let resolved = conn.commit_resolve(commit_id)?; |
| 296 | - let source = Source::Named { |
| 297 | - manifest: None, |
| 298 | - collection: resolved.collection.unwrap(), |
| 299 | - name: resolved.repository.unwrap(), |
| 300 | - reference: Some(resolved.commit_hash), |
| 301 | - }; |
| 302 | + let (collection, repo_name, commit_id) = ( |
| 303 | + resolved.collection.unwrap(), |
| 304 | + resolved.repository.unwrap(), |
| 305 | + resolved.commit_id, |
| 306 | + ); |
| 307 | + let source = Source::new( |
| 308 | + &SourceArgs { |
| 309 | + source: &format!("{collection}/{repo_name}:{commit_id}"), |
| 310 | + collections: &cfg.collections, |
| 311 | + pre_processor: cfg.build.pre_processor.as_ref(), |
| 312 | + config_source: &cfg.build.default_build_config_path, |
| 313 | + anonymous: false, |
| 314 | + }, |
| 315 | + None, |
| 316 | + )?; |
| 317 | let executor = ExecutorBuilder::default() |
| 318 | .config(cfg.clone()) |
| 319 | .debug(false) |
| 320 | @@ -96,12 +113,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 321 | } |
| 322 | Commands::Evaluate { |
| 323 | source, |
| 324 | - name, |
| 325 | - manifest, |
| 326 | forward, |
| 327 | debug, |
| 328 | + anonymous, |
| 329 | } => { |
| 330 | - let source = Source::from_arg(manifest.as_deref(), source.as_deref())?; |
| 331 | + let source = Source::new( |
| 332 | + &SourceArgs { |
| 333 | + source: source.as_deref().unwrap_or("."), |
| 334 | + collections: &cfg.collections, |
| 335 | + pre_processor: cfg.build.pre_processor.as_ref(), |
| 336 | + config_source: &cfg.build.default_build_config_path, |
| 337 | + anonymous, |
| 338 | + }, |
| 339 | + Some(&mut std::io::stdin()), |
| 340 | + )?; |
| 341 | let executor = ExecutorBuilder::default() |
| 342 | .config(cfg.clone()) |
| 343 | .debug(debug) |
| 344 | diff --git a/ayllu-build/src/manifest.rs b/ayllu-build/src/manifest.rs |
| 345 | index 541133e..35379c8 100644 |
| 346 | --- a/ayllu-build/src/manifest.rs |
| 347 | +++ b/ayllu-build/src/manifest.rs |
| 348 | @@ -69,7 +69,7 @@ impl Display for Workflow { |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | - #[derive(Serialize, Deserialize, Debug, Clone, Default)] |
| 353 | + #[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 354 | pub struct Manifest { |
| 355 | pub workflows: Vec<Workflow>, |
| 356 | } |
| 357 | diff --git a/ayllu-build/src/runtime/mod.rs b/ayllu-build/src/runtime/mod.rs |
| 358 | index 3e1bb0e..6b6639a 100644 |
| 359 | --- a/ayllu-build/src/runtime/mod.rs |
| 360 | +++ b/ayllu-build/src/runtime/mod.rs |
| 361 | @@ -4,7 +4,7 @@ use std::{ |
| 362 | time::Duration, |
| 363 | }; |
| 364 | |
| 365 | - use crate::{error::Error, source::Resolved}; |
| 366 | + use crate::{error::Error, source::Source}; |
| 367 | |
| 368 | pub mod oci; |
| 369 | |
| 370 | @@ -81,7 +81,7 @@ pub trait Handle { |
| 371 | |
| 372 | pub trait Runtime: Sized { |
| 373 | type Handle: Handle; |
| 374 | - fn initalize(&mut self, resolved: &Resolved, graph: &crate::graph::Graph) -> Result<(), Error>; |
| 375 | + fn initalize(&mut self, source: &Source, graph: &crate::graph::Graph) -> Result<(), Error>; |
| 376 | fn start(&self, manifest_id: i32, workflow_id: i32) -> Result<Self::Handle, Error>; |
| 377 | fn execute<F>( |
| 378 | &self, |
| 379 | @@ -116,7 +116,7 @@ pub(crate) mod test_utils { |
| 380 | use crate::{ |
| 381 | error::Error, |
| 382 | runtime::{Handle, Runtime, Sha256Sum, Update, WorkflowState}, |
| 383 | - source::Resolved, |
| 384 | + source::Source, |
| 385 | }; |
| 386 | |
| 387 | #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
| 388 | @@ -197,7 +197,7 @@ pub(crate) mod test_utils { |
| 389 | |
| 390 | fn initalize( |
| 391 | &mut self, |
| 392 | - _resolved: &Resolved, |
| 393 | + _source: &Source, |
| 394 | _graph: &crate::graph::Graph, |
| 395 | ) -> Result<(), Error> { |
| 396 | Ok(()) |
| 397 | diff --git a/ayllu-build/src/runtime/oci/debug.rs b/ayllu-build/src/runtime/oci/debug.rs |
| 398 | index 68f7dc7..955d509 100644 |
| 399 | --- a/ayllu-build/src/runtime/oci/debug.rs |
| 400 | +++ b/ayllu-build/src/runtime/oci/debug.rs |
| 401 | @@ -13,7 +13,7 @@ use crate::{ |
| 402 | Handle, Runtime, |
| 403 | oci::{OciRuntimeBuilder, TemplateBuilder}, |
| 404 | }, |
| 405 | - source::Resolved, |
| 406 | + source::Source, |
| 407 | }; |
| 408 | |
| 409 | const LEFTOVER_DIRS: &[&str] = &["oci-state/.empty-directory", "oci-state", "builds"]; |
| 410 | @@ -26,7 +26,7 @@ pub fn debug_shell( |
| 411 | gid: u32, |
| 412 | cleanup: bool, |
| 413 | ) -> Result<(), Error> { |
| 414 | - let resolved = Resolved { |
| 415 | + let resolved = Source::Anonymous { |
| 416 | manifest: Manifest { |
| 417 | workflows: vec![Workflow { |
| 418 | image: String::from("noop"), |
| 419 | @@ -34,7 +34,6 @@ pub fn debug_shell( |
| 420 | ..Default::default() |
| 421 | }], |
| 422 | }, |
| 423 | - ..Default::default() |
| 424 | }; |
| 425 | let graph = Graph::new(&resolved, &mut crate::graph::Allocator::Fixed(1))?; |
| 426 | |
| 427 | diff --git a/ayllu-build/src/runtime/oci/gc.rs b/ayllu-build/src/runtime/oci/gc.rs |
| 428 | index 178c88d..bbd12ca 100644 |
| 429 | --- a/ayllu-build/src/runtime/oci/gc.rs |
| 430 | +++ b/ayllu-build/src/runtime/oci/gc.rs |
| 431 | @@ -204,10 +204,7 @@ mod test { |
| 432 | let manifest = Manifest { |
| 433 | workflows: workflows.to_vec(), |
| 434 | }; |
| 435 | - let source = crate::source::Resolved { |
| 436 | - manifest, |
| 437 | - ..Default::default() |
| 438 | - }; |
| 439 | + let source = crate::source::Source::Anonymous { manifest }; |
| 440 | let images = |
| 441 | HashMap::<String, Image>::from_iter(vec![(String::from("noop"), Image::default())]); |
| 442 | ayllu_database::migrate(&db_path).unwrap(); |
| 443 | diff --git a/ayllu-build/src/runtime/oci/runtime.rs b/ayllu-build/src/runtime/oci/runtime.rs |
| 444 | index 8324fab..27fbbfa 100644 |
| 445 | --- a/ayllu-build/src/runtime/oci/runtime.rs |
| 446 | +++ b/ayllu-build/src/runtime/oci/runtime.rs |
| 447 | @@ -24,7 +24,7 @@ use crate::{ |
| 448 | workspace::{RootBuilder, Workspace, Workspaces}, |
| 449 | }, |
| 450 | }, |
| 451 | - source::Resolved, |
| 452 | + source::Source, |
| 453 | }; |
| 454 | |
| 455 | // const PID_1_COMM_PATH: &str = "/proc/1/comm"; |
| 456 | @@ -67,7 +67,7 @@ pub struct OciRuntime { |
| 457 | impl crate::runtime::Runtime for OciRuntime { |
| 458 | type Handle = super::OciHandle; |
| 459 | |
| 460 | - fn initalize(&mut self, source: &Resolved, graph: &Graph) -> Result<(), Error> { |
| 461 | + fn initalize(&mut self, source: &Source, graph: &Graph) -> Result<(), Error> { |
| 462 | let root = RootBuilder::default() |
| 463 | .root(self.runtime_dir.clone()) |
| 464 | .graph(graph.clone()) |
| 465 | diff --git a/ayllu-build/src/runtime/oci/template.rs b/ayllu-build/src/runtime/oci/template.rs |
| 466 | index 38953a3..74b4b98 100644 |
| 467 | --- a/ayllu-build/src/runtime/oci/template.rs |
| 468 | +++ b/ayllu-build/src/runtime/oci/template.rs |
| 469 | @@ -54,13 +54,17 @@ impl Template { |
| 470 | fn set_envs(&self, proc: &mut Process, others: &[(String, String)]) { |
| 471 | let envs = proc.env_mut().as_mut().unwrap(); |
| 472 | envs.extend(others.iter().map(|(name, value)| format!("{name}={value}"))); |
| 473 | - if let Some(ref_name) = self.source.as_ref().map(|source| source.reference()) { |
| 474 | - if let Some(ref_name) = ref_name { |
| 475 | - envs.push(format!("AYLLU_BUILD_REFNAME={ref_name}")); |
| 476 | - } |
| 477 | + if let Some(ref_name) = match &self.source { |
| 478 | + Some(Source::Repository { repository, .. }) => Some(repository.reference.as_ref()), |
| 479 | + Some(Source::Anonymous { .. }) => None, |
| 480 | + None => None, |
| 481 | + } |
| 482 | + .flatten() |
| 483 | + { |
| 484 | + envs.push(format!("AYLLU_BUILD_REFNAME={ref_name}")); |
| 485 | } else { |
| 486 | envs.push(format!("AYLLU_BUILD_REFNAME={}", self.default_branch)); |
| 487 | - }; |
| 488 | + } |
| 489 | } |
| 490 | |
| 491 | fn current_ids() -> (u32, u32) { |
| 492 | diff --git a/ayllu-build/src/runtime/oci/workspace.rs b/ayllu-build/src/runtime/oci/workspace.rs |
| 493 | index 5197c39..0960807 100644 |
| 494 | --- a/ayllu-build/src/runtime/oci/workspace.rs |
| 495 | +++ b/ayllu-build/src/runtime/oci/workspace.rs |
| 496 | @@ -15,7 +15,7 @@ use crate::{ |
| 497 | error::Error, |
| 498 | graph::Graph, |
| 499 | runtime::{Sha256Sum, oci::template::Template}, |
| 500 | - source::Resolved, |
| 501 | + source::Source, |
| 502 | }; |
| 503 | |
| 504 | const CHUNK_SIZE: usize = 32_000; |
| 505 | @@ -360,7 +360,7 @@ impl Workspaces { |
| 506 | pub struct Root { |
| 507 | root: PathBuf, |
| 508 | template: Template, |
| 509 | - source: Resolved, |
| 510 | + source: Source, |
| 511 | images: HashMap<String, Image>, |
| 512 | graph: Graph, |
| 513 | } |
| 514 | @@ -433,12 +433,12 @@ impl Root { |
| 515 | let debug_spec = self.template.debug(&tmpl_spec); |
| 516 | let debug_json = serde_json::ser::to_vec(&debug_spec).unwrap(); |
| 517 | std::fs::write(workspace.debug(), debug_json)?; |
| 518 | - if let Some(source_dir) = self |
| 519 | - .source |
| 520 | - .repository |
| 521 | - .as_ref() |
| 522 | - .map(|repo| repo.path.as_path()) |
| 523 | - { |
| 524 | + if let Some(source_dir) = match &self.source { |
| 525 | + Source::Anonymous { .. } => None, |
| 526 | + Source::Repository { repository, .. } => { |
| 527 | + Some(repository.path.as_path()) |
| 528 | + } |
| 529 | + } { |
| 530 | ayllu_git::bundle( |
| 531 | source_dir, |
| 532 | &workspace.special().join("src.bundle"), |
| 533 | @@ -487,10 +487,13 @@ impl Root { |
| 534 | #[cfg(test)] |
| 535 | mod test { |
| 536 | |
| 537 | - use crate::{graph::Allocator, runtime::oci::template::TemplateBuilder, source::Source}; |
| 538 | + use crate::{ |
| 539 | + graph::Allocator, manifest::Manifest, runtime::oci::template::TemplateBuilder, |
| 540 | + source::Source, |
| 541 | + }; |
| 542 | |
| 543 | use super::*; |
| 544 | - use ayllu_git::{Collection, testing}; |
| 545 | + use ayllu_git::{Collection, Repository, testing}; |
| 546 | |
| 547 | #[test] |
| 548 | fn workflow_lifecycle() { |
| 549 | @@ -534,9 +537,6 @@ mod test { |
| 550 | ) |
| 551 | .unwrap(); |
| 552 | |
| 553 | - let repo = ayllu_git::Wrapper::new(&test_repo_path).unwrap(); |
| 554 | - let head = repo.latest_hash().unwrap().unwrap(); |
| 555 | - |
| 556 | let fake_rootfs = test_dir.path().join("rootfs"); |
| 557 | std::fs::create_dir_all(&fake_rootfs).unwrap(); |
| 558 | std::fs::write(fake_rootfs.join("hello"), "world").unwrap(); |
| 559 | @@ -545,35 +545,22 @@ mod test { |
| 560 | |
| 561 | let config = crate::config::Config::default(); |
| 562 | |
| 563 | - let source = Source::Repository { |
| 564 | - manifest: None, |
| 565 | - path: test_repo_path.clone(), |
| 566 | - reference: Some(head), |
| 567 | - }; |
| 568 | + let manifest = serde_json::from_str::<Manifest>(FAKE_MANIFEST_CONTENT).unwrap(); |
| 569 | |
| 570 | - let resolved_source = source |
| 571 | - .read( |
| 572 | - &[Collection { |
| 573 | - name: String::default(), |
| 574 | - description: None, |
| 575 | - path: test_repo_path |
| 576 | - .parent() |
| 577 | - .map(|path| path.to_path_buf()) |
| 578 | - .unwrap(), |
| 579 | - hidden: None, |
| 580 | - }], |
| 581 | - None, |
| 582 | - ) |
| 583 | - .unwrap(); |
| 584 | + let resolved_source = Source::Repository { |
| 585 | + manifest, |
| 586 | + repository: Repository { |
| 587 | + name: String::from("test"), |
| 588 | + path: test_repo_path.to_path_buf(), |
| 589 | + reference: Some(String::from("main")), |
| 590 | + }, |
| 591 | + collection: Collection::default(), |
| 592 | + }; |
| 593 | |
| 594 | let tmpl = TemplateBuilder::default() |
| 595 | .init_script(config.build.init_script) |
| 596 | .default_branch(config.common.default_branch) |
| 597 | - .source(crate::source::Source::Repository { |
| 598 | - path: test_repo_path.clone(), |
| 599 | - reference: None, |
| 600 | - manifest: None, |
| 601 | - }) |
| 602 | + .source(resolved_source.clone()) |
| 603 | .build() |
| 604 | .unwrap(); |
| 605 | |
| 606 | diff --git a/ayllu-build/src/source.rs b/ayllu-build/src/source.rs |
| 607 | index 2a7086d..fa04ce5 100644 |
| 608 | --- a/ayllu-build/src/source.rs |
| 609 | +++ b/ayllu-build/src/source.rs |
| 610 | @@ -1,206 +1,96 @@ |
| 611 | use std::{ |
| 612 | io::{Read, Write}, |
| 613 | - path::{Path, PathBuf}, |
| 614 | + path::Path, |
| 615 | process::{Command, Stdio}, |
| 616 | }; |
| 617 | |
| 618 | use ayllu_git::{Collection, Repository}; |
| 619 | + use serde_json::from_str; |
| 620 | |
| 621 | use crate::{config::PreProcessor, error::Error, manifest::Manifest}; |
| 622 | |
| 623 | - const DEFAULT_BUILD_FILE: &str = ".ayllu-build.json"; |
| 624 | - |
| 625 | - #[derive(Clone, Default)] |
| 626 | - pub struct Resolved { |
| 627 | - pub manifest: Manifest, |
| 628 | - pub collection: Option<String>, |
| 629 | - pub name: Option<String>, |
| 630 | - pub git_hash: Option<String>, |
| 631 | - pub repository: Option<Repository>, |
| 632 | + pub struct SourceArgs<'a> { |
| 633 | + pub source: &'a str, |
| 634 | + pub collections: &'a [Collection], |
| 635 | + pub pre_processor: Option<&'a PreProcessor>, |
| 636 | + // Default path within a repository to load a manifest template from |
| 637 | + pub config_source: &'a Path, |
| 638 | + pub anonymous: bool, |
| 639 | } |
| 640 | |
| 641 | /// Source git repository which shall be built |
| 642 | - #[derive(Clone, Debug)] |
| 643 | + #[derive(Clone, Debug, PartialEq, Eq)] |
| 644 | pub enum Source { |
| 645 | /// Manifest file without a backing repository |
| 646 | - Path { path: Option<PathBuf> }, |
| 647 | - /// Path to a git repository |
| 648 | + Anonymous { manifest: Manifest }, |
| 649 | + /// Manifest with associated Git Repository |
| 650 | Repository { |
| 651 | - // Explicit path to a manifest outside the repository |
| 652 | - manifest: Option<PathBuf>, |
| 653 | - // Path to the source repository |
| 654 | - path: PathBuf, |
| 655 | - // Optional git ref |
| 656 | - reference: Option<String>, |
| 657 | - }, |
| 658 | - // Pair of Collection / Name |
| 659 | - Named { |
| 660 | - manifest: Option<PathBuf>, |
| 661 | - collection: String, |
| 662 | - name: String, |
| 663 | - reference: Option<String>, |
| 664 | + /// Manifest content |
| 665 | + manifest: Manifest, |
| 666 | + /// Associated Repository |
| 667 | + repository: Repository, |
| 668 | + /// Associated Collection |
| 669 | + collection: Collection, |
| 670 | }, |
| 671 | } |
| 672 | |
| 673 | impl Source { |
| 674 | - pub fn from_arg(manifest: Option<&Path>, arg: Option<&str>) -> Result<Self, Error> { |
| 675 | - match arg { |
| 676 | - Some("-") => Ok(Source::Path { path: None }), |
| 677 | - Some(path) => { |
| 678 | - let path = Path::new(&path); |
| 679 | - if path.is_file() { |
| 680 | - Ok(Source::Path { |
| 681 | - path: Some(path.to_path_buf()), |
| 682 | - }) |
| 683 | - } else { |
| 684 | - let repository = |
| 685 | - ayllu_git::Wrapper::new(path).map_err(Error::CannotResolveHead)?; |
| 686 | - let head = repository.default_ref().map_err(Error::CannotResolveHead)?; |
| 687 | - Ok(Source::Repository { |
| 688 | - manifest: manifest.map(|manifest| manifest.to_path_buf()), |
| 689 | - path: path.to_path_buf(), |
| 690 | - reference: Some(head), |
| 691 | - }) |
| 692 | - } |
| 693 | - } |
| 694 | - None => Ok(Source::Repository { |
| 695 | - manifest: manifest.map(|manifest| manifest.to_path_buf()), |
| 696 | - path: std::fs::canonicalize(Path::new("."))?, |
| 697 | - reference: None, |
| 698 | - }), |
| 699 | - } |
| 700 | - } |
| 701 | - |
| 702 | - pub fn reference(&self) -> Option<&String> { |
| 703 | - match self { |
| 704 | - Source::Path { .. } => None, |
| 705 | - Source::Repository { reference, .. } => reference.as_ref(), |
| 706 | - Source::Named { reference, .. } => reference.as_ref(), |
| 707 | - } |
| 708 | - } |
| 709 | - |
| 710 | - fn repository(&self, collections: &[Collection]) -> Result<Option<Repository>, Error> { |
| 711 | - match self { |
| 712 | - Source::Repository { |
| 713 | - path, reference, .. |
| 714 | - } => { |
| 715 | - let abs_path = std::fs::canonicalize(path.as_path()).unwrap(); |
| 716 | - if ayllu_git::git_dir(path)? { |
| 717 | - Ok(Some(Repository { |
| 718 | - name: ayllu_git::name(&abs_path), |
| 719 | - path: abs_path, |
| 720 | - reference: reference.clone(), |
| 721 | - })) |
| 722 | - } else { |
| 723 | - todo!() |
| 724 | - } |
| 725 | - } |
| 726 | - Source::Named { |
| 727 | - collection, |
| 728 | - name, |
| 729 | - reference, |
| 730 | - .. |
| 731 | - } => { |
| 732 | - let mut repo = ayllu_git::find(collections, collection, name)?.map_or_else( |
| 733 | - || { |
| 734 | - Err(Error::CannotFindManagedRepository { |
| 735 | - collection: collection.clone(), |
| 736 | - name: name.clone(), |
| 737 | - }) |
| 738 | - }, |
| 739 | - Ok::<_, Error>, |
| 740 | - )?; |
| 741 | - repo.reference = reference.clone(); |
| 742 | - Ok(Some(repo)) |
| 743 | - } |
| 744 | - Source::Path { path: _ } => Ok(None), |
| 745 | - } |
| 746 | - } |
| 747 | - |
| 748 | - fn git_information(&self, repository: &Repository) -> Result<(String, String, String), Error> { |
| 749 | - let mut latest_hash = repository |
| 750 | + fn from_repository( |
| 751 | + args: &SourceArgs, |
| 752 | + path: &Path, |
| 753 | + reference: Option<&str>, |
| 754 | + ) -> Result<Self, Error> { |
| 755 | + let path = std::fs::canonicalize(path)?; |
| 756 | + let (collection, repository_name) = ayllu_git::collection_and_name(&path); |
| 757 | + let repository = ayllu_git::find(args.collections, &collection, &repository_name)?.ok_or( |
| 758 | + Error::CannotFindManagedRepository { |
| 759 | + collection: collection.clone(), |
| 760 | + name: repository_name.clone(), |
| 761 | + }, |
| 762 | + )?; |
| 763 | + let repository = repository |
| 764 | .open() |
| 765 | + .map_err(Error::CannotReadManifestFromRepository)?; |
| 766 | + let reference = reference |
| 767 | + .map(|name| name.to_string()) |
| 768 | + .unwrap_or(repository.default_ref().map_err(Error::CannotResolveHead)?); |
| 769 | + let manifest_content = repository |
| 770 | + .read_string( |
| 771 | + args.pre_processor |
| 772 | + .as_ref() |
| 773 | + .map(|pre_processor| pre_processor.source.as_path()) |
| 774 | + .unwrap_or(args.config_source), |
| 775 | + None, |
| 776 | + ) |
| 777 | .map_err(Error::CannotReadManifestFromRepository)? |
| 778 | - .latest_hash() |
| 779 | - .map_err(Error::CannotReadManifestFromRepository)? |
| 780 | - .unwrap(); |
| 781 | - // If a manifest is explicitly set then the build is considered dirty |
| 782 | - // since it's not a complete build of the contents within the |
| 783 | - // repository. |
| 784 | - if match self { |
| 785 | - Source::Repository { manifest, .. } => manifest.is_some(), |
| 786 | - Source::Named { manifest, .. } => manifest.is_some(), |
| 787 | - _ => false, |
| 788 | - } { |
| 789 | - latest_hash = format!("dirty-{latest_hash}"); |
| 790 | - } |
| 791 | - let (collection, name) = ayllu_git::collection_and_name(&repository.path); |
| 792 | - Ok((collection, name, latest_hash)) |
| 793 | + .ok_or(Error::RepositoryDoesNotContainManifest { |
| 794 | + path: args.config_source.to_path_buf(), |
| 795 | + })?; // Default to HEAD |
| 796 | + let manifest_content = Source::pre_process(&manifest_content, args.pre_processor)?; |
| 797 | + from_str::<Manifest>(&manifest_content) |
| 798 | + .map_err(Error::InvalidManifest) |
| 799 | + .map(|manifest| { |
| 800 | + let collection = args |
| 801 | + .collections |
| 802 | + .iter() |
| 803 | + .find(|other| other.name.eq(&collection)) |
| 804 | + .cloned() |
| 805 | + .unwrap(); |
| 806 | + let repository_path = collection.path.join(&repository_name); |
| 807 | + Source::Repository { |
| 808 | + manifest, |
| 809 | + collection, |
| 810 | + repository: Repository { |
| 811 | + name: repository_name.clone(), |
| 812 | + path: repository_path, |
| 813 | + reference: Some(reference), |
| 814 | + }, |
| 815 | + } |
| 816 | + }) |
| 817 | } |
| 818 | |
| 819 | - /// Read the manifest from the Source |
| 820 | - pub fn read( |
| 821 | - &self, |
| 822 | - collections: &[Collection], |
| 823 | - pre_processor: Option<&PreProcessor>, |
| 824 | - ) -> Result<Resolved, Error> { |
| 825 | - tracing::info!("Reading manifest from source: {self:?}"); |
| 826 | - let (manifest_raw_str, repository, git_info) = match self { |
| 827 | - Source::Path { path: None } => { |
| 828 | - let mut manifest_str = String::default(); |
| 829 | - std::io::stdin().read_to_string(&mut manifest_str)?; |
| 830 | - (manifest_str, None, None) |
| 831 | - } |
| 832 | - Source::Path { path: Some(path) } => { |
| 833 | - let manifest_str = |
| 834 | - std::fs::read_to_string(path).map_err(Error::CannotReadManifest)?; |
| 835 | - (manifest_str, None, None) |
| 836 | - } |
| 837 | - Source::Repository { |
| 838 | - manifest, |
| 839 | - reference, |
| 840 | - .. |
| 841 | - } |
| 842 | - | Source::Named { |
| 843 | - manifest, |
| 844 | - reference, |
| 845 | - .. |
| 846 | - } => { |
| 847 | - let repository = self.repository(collections)?.unwrap(); |
| 848 | - let git_info = self.git_information(&repository)?; |
| 849 | - let handle = repository |
| 850 | - .open() |
| 851 | - .map_err(Error::CannotReadManifestFromRepository)?; |
| 852 | - let raw_tmpl_path = manifest.as_deref().unwrap_or( |
| 853 | - pre_processor |
| 854 | - .as_ref() |
| 855 | - .map(|pre_processor| pre_processor.source.as_path()) |
| 856 | - .unwrap_or(Path::new(DEFAULT_BUILD_FILE)), |
| 857 | - ); |
| 858 | - let mut commit = handle |
| 859 | - .resolve_commit(reference.as_deref()) |
| 860 | - .map_err(Error::CannotReadManifestFromRepository)? |
| 861 | - .ok_or(Error::CannotResolveCommit(reference.clone()))?; |
| 862 | - let manifest_str = if manifest.is_some() { |
| 863 | - std::fs::read_to_string(raw_tmpl_path).map_err(Error::CannotReadManifest)? |
| 864 | - } else { |
| 865 | - handle |
| 866 | - .read_string(raw_tmpl_path, Some(commit.id.as_str())) |
| 867 | - .map_err(Error::CannotReadManifestFromRepository)? |
| 868 | - .map_or_else( |
| 869 | - || { |
| 870 | - Err(Error::RepositoryDoesNotContainManifest { |
| 871 | - path: raw_tmpl_path.to_path_buf(), |
| 872 | - }) |
| 873 | - }, |
| 874 | - Ok::<_, Error>, |
| 875 | - )? |
| 876 | - }; |
| 877 | - (manifest_str, Some(repository), Some(git_info)) |
| 878 | - } |
| 879 | - }; |
| 880 | - |
| 881 | - let manifest_str = if let Some(pre_processor) = pre_processor.as_ref() { |
| 882 | + fn pre_process(original: &str, pre_processor: Option<&PreProcessor>) -> Result<String, Error> { |
| 883 | + if let Some(pre_processor) = pre_processor { |
| 884 | tracing::info!( |
| 885 | "Passing source {:?} through pre-processor: {}", |
| 886 | pre_processor.source, |
| 887 | @@ -220,34 +110,288 @@ impl Source { |
| 888 | })?; |
| 889 | fp.stdin |
| 890 | .as_ref() |
| 891 | - .map(|mut stdin| stdin.write_all(manifest_raw_str.as_bytes())) |
| 892 | + .map(|mut stdin| stdin.write_all(original.as_bytes())) |
| 893 | .unwrap()?; |
| 894 | let output = fp.wait_with_output()?; |
| 895 | - String::from_utf8_lossy(output.stdout.as_slice()).to_string() |
| 896 | + Ok(String::from_utf8_lossy(output.stdout.as_slice()).to_string()) |
| 897 | } else { |
| 898 | - manifest_raw_str |
| 899 | - }; |
| 900 | - |
| 901 | - tracing::debug!("Processing configuration:\n{manifest_str}"); |
| 902 | - let manifest = |
| 903 | - serde_json::de::from_str::<Manifest>(&manifest_str).map_err(Error::InvalidManifest)?; |
| 904 | - |
| 905 | - if let Some(git_info) = git_info { |
| 906 | - Ok(Resolved { |
| 907 | - manifest, |
| 908 | - collection: Some(git_info.0.clone()), |
| 909 | - name: Some(git_info.1.clone()), |
| 910 | - git_hash: Some(git_info.2.clone()), |
| 911 | - repository, |
| 912 | - }) |
| 913 | + Ok(original.to_string()) |
| 914 | + } |
| 915 | + } |
| 916 | + |
| 917 | + pub fn manifest(&self) -> &Manifest { |
| 918 | + match self { |
| 919 | + Source::Anonymous { manifest } => manifest, |
| 920 | + Source::Repository { manifest, .. } => manifest, |
| 921 | + } |
| 922 | + } |
| 923 | + |
| 924 | + pub fn new(args: &SourceArgs, stdin: Option<&mut dyn Read>) -> Result<Self, Error> { |
| 925 | + let path = Path::new(args.source); |
| 926 | + if path.eq(Path::new("-")) { |
| 927 | + let mut buf = String::default(); |
| 928 | + stdin |
| 929 | + .expect("no stdin passed but arg suggests it should have been") |
| 930 | + .read_to_string(&mut buf) |
| 931 | + .map_or_else( |
| 932 | + |e| Err(Error::CannotReadManifest(e)), |
| 933 | + |_| { |
| 934 | + let buf = Source::pre_process(&buf, args.pre_processor)?; |
| 935 | + from_str::<Manifest>(&buf) |
| 936 | + .map_err(Error::InvalidManifest) |
| 937 | + .map(|manifest| Source::Anonymous { manifest }) |
| 938 | + }, |
| 939 | + ) |
| 940 | + } else if path.is_file() { |
| 941 | + let path = std::fs::canonicalize(path)?; |
| 942 | + let manifest = std::fs::read_to_string(&path) |
| 943 | + .map_err(Error::CannotReadManifest) |
| 944 | + .and_then(|manifest_content| { |
| 945 | + let manifest_content = |
| 946 | + Source::pre_process(&manifest_content, args.pre_processor)?; |
| 947 | + from_str::<Manifest>(&manifest_content).map_err(Error::InvalidManifest) |
| 948 | + })?; |
| 949 | + |
| 950 | + let path_components = path.iter().count(); |
| 951 | + let repository_path = path.parent().expect("manifest file has no parent"); |
| 952 | + let collection_path = repository_path.parent().expect("repository has no parent"); |
| 953 | + // Check if the path belongs to a managed repository |
| 954 | + if ayllu_git::git_dir(repository_path)? |
| 955 | + && !args.anonymous |
| 956 | + && args |
| 957 | + .collections |
| 958 | + .iter() |
| 959 | + .find(|c| c.path.eq(collection_path)) |
| 960 | + .is_some() |
| 961 | + && path_components >= 2 |
| 962 | + { |
| 963 | + let (collection_name, repository_name) = |
| 964 | + ayllu_git::collection_and_name(repository_path); |
| 965 | + let mut repository = |
| 966 | + ayllu_git::find(args.collections, &collection_name, &repository_name)?.ok_or( |
| 967 | + Error::CannotFindManagedRepository { |
| 968 | + collection: collection_name.to_string(), |
| 969 | + name: repository_name.to_string(), |
| 970 | + }, |
| 971 | + )?; |
| 972 | + let head = repository |
| 973 | + .open() |
| 974 | + .map_err(Error::CannotReadManifestFromRepository)? |
| 975 | + .default_ref() |
| 976 | + .map_err(Error::CannotReadManifestFromRepository)?; |
| 977 | + repository.reference = Some(head); |
| 978 | + Ok(Source::Repository { |
| 979 | + manifest, |
| 980 | + repository, |
| 981 | + collection: Collection { |
| 982 | + name: collection_name, |
| 983 | + description: None, |
| 984 | + path: collection_path.to_path_buf(), |
| 985 | + hidden: None, |
| 986 | + }, |
| 987 | + }) |
| 988 | + } else { |
| 989 | + // Otherwise manifest considered anonymous |
| 990 | + Ok(Source::Anonymous { manifest }) |
| 991 | + } |
| 992 | + } else if path.is_dir() { |
| 993 | + Source::from_repository(args, path, None) |
| 994 | } else { |
| 995 | - Ok(Resolved { |
| 996 | - manifest, |
| 997 | - collection: None, |
| 998 | - name: None, |
| 999 | - git_hash: None, |
| 1000 | - repository, |
| 1001 | - }) |
| 1002 | + let arg = args.source; |
| 1003 | + tracing::info!("Looking up repository: {arg}"); |
| 1004 | + let mut split = arg.split("/"); |
| 1005 | + let collection = split |
| 1006 | + .next() |
| 1007 | + .map_or_else(|| Err(Error::InvalidSource(arg.to_string())), Ok)?; |
| 1008 | + let name = split |
| 1009 | + .next() |
| 1010 | + .map_or_else(|| Err(Error::InvalidSource(arg.to_string())), Ok)?; |
| 1011 | + let reference: Vec<&str> = name.split(':').collect(); |
| 1012 | + let (name, reference) = match reference.len() { |
| 1013 | + 1 => (reference[0].to_string(), None), |
| 1014 | + 2 => { |
| 1015 | + let reference = reference[1].to_string(); |
| 1016 | + let to_trim = format!(":{reference}"); |
| 1017 | + (name.replace(&to_trim, ""), Some(reference)) |
| 1018 | + } |
| 1019 | + _ => return Err(Error::InvalidSource(arg.to_string())), |
| 1020 | + }; |
| 1021 | + let repository = ayllu_git::find(args.collections, collection, &name) |
| 1022 | + .map_err(Error::CannotReadManifest)? |
| 1023 | + .ok_or(Error::CannotFindManagedRepository { |
| 1024 | + collection: collection.to_string(), |
| 1025 | + name, |
| 1026 | + })?; |
| 1027 | + tracing::info!("Resolved repository @ {:?}", repository.path); |
| 1028 | + Source::from_repository(args, &repository.path, reference.as_deref()) |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | + |
| 1033 | + #[cfg(test)] |
| 1034 | + mod test { |
| 1035 | + use std::{ |
| 1036 | + io::Cursor, |
| 1037 | + path::{Path, PathBuf}, |
| 1038 | + }; |
| 1039 | + |
| 1040 | + use ayllu_git::{Collection, Repository}; |
| 1041 | + |
| 1042 | + use crate::{ |
| 1043 | + config::PreProcessor, |
| 1044 | + manifest::Manifest, |
| 1045 | + source::{Source, SourceArgs}, |
| 1046 | + }; |
| 1047 | + |
| 1048 | + const FAKE_MANIFEST_1: &str = r#" |
| 1049 | + { |
| 1050 | + "workflows": [{"name": "fake-1", "image": "noop", "steps": []}] |
| 1051 | + } |
| 1052 | + "#; |
| 1053 | + const FAKE_MANIFEST_2: &str = r#" |
| 1054 | + { |
| 1055 | + "workflows": [{"name": "fake-2", "image": "noop", "steps": []}] |
| 1056 | + } |
| 1057 | + "#; |
| 1058 | + |
| 1059 | + const TEST_CONFIG_SOURCE: &str = ".ayllu-build.json"; |
| 1060 | + const TEST_COLLECTION_NAME: &str = "test"; |
| 1061 | + const TEST_REPO_NAME: &str = "src"; |
| 1062 | + |
| 1063 | + fn setup_repository(base_dir: &Path) -> (Collection, Repository, PathBuf) { |
| 1064 | + let collection_base = base_dir.join(TEST_COLLECTION_NAME); |
| 1065 | + std::fs::create_dir_all(&collection_base).unwrap(); |
| 1066 | + let test_repo_path = ayllu_git::testing::init( |
| 1067 | + &collection_base, |
| 1068 | + false, |
| 1069 | + &[ |
| 1070 | + &format!(r#"echo '{FAKE_MANIFEST_1}' > .ayllu-build.json && git add .ayllu-build.json && git commit -m 'add manifest'"#,), |
| 1071 | + ], |
| 1072 | + ) |
| 1073 | + .unwrap(); |
| 1074 | + ( |
| 1075 | + Collection { |
| 1076 | + name: TEST_COLLECTION_NAME.to_string(), |
| 1077 | + description: None, |
| 1078 | + path: base_dir.join(TEST_COLLECTION_NAME), |
| 1079 | + hidden: None, |
| 1080 | + }, |
| 1081 | + Repository { |
| 1082 | + name: TEST_REPO_NAME.to_string(), |
| 1083 | + path: base_dir.join(TEST_COLLECTION_NAME).join(TEST_REPO_NAME), |
| 1084 | + reference: Some(String::from("main")), |
| 1085 | + }, |
| 1086 | + test_repo_path, |
| 1087 | + ) |
| 1088 | + } |
| 1089 | + |
| 1090 | + #[test] |
| 1091 | + fn source_anonymous_stdin() { |
| 1092 | + let mut cursor = Cursor::new(FAKE_MANIFEST_1.to_string().into_bytes()); |
| 1093 | + let source = Source::new( |
| 1094 | + &SourceArgs { |
| 1095 | + source: "-", |
| 1096 | + collections: &[], |
| 1097 | + pre_processor: None, |
| 1098 | + config_source: Path::new(TEST_CONFIG_SOURCE), |
| 1099 | + anonymous: false, |
| 1100 | + }, |
| 1101 | + Some(&mut cursor), |
| 1102 | + ) |
| 1103 | + .unwrap(); |
| 1104 | + assert!(source.eq(&Source::Anonymous { |
| 1105 | + manifest: serde_json::from_str::<Manifest>(FAKE_MANIFEST_1).unwrap() |
| 1106 | + })) |
| 1107 | + } |
| 1108 | + |
| 1109 | + #[test] |
| 1110 | + fn source_anonymous_stdin_preprocesor() { |
| 1111 | + let mut cursor = Cursor::new(FAKE_MANIFEST_1.to_string().into_bytes()); |
| 1112 | + let source = Source::new( |
| 1113 | + &SourceArgs { |
| 1114 | + source: "-", |
| 1115 | + collections: &[], |
| 1116 | + pre_processor: Some(&PreProcessor { |
| 1117 | + source: PathBuf::default(), |
| 1118 | + program: String::from("/bin/cat"), |
| 1119 | + args: Vec::default(), |
| 1120 | + }), |
| 1121 | + config_source: Path::new(TEST_CONFIG_SOURCE), |
| 1122 | + anonymous: false, |
| 1123 | + }, |
| 1124 | + Some(&mut cursor), |
| 1125 | + ) |
| 1126 | + .unwrap(); |
| 1127 | + assert!(source.eq(&Source::Anonymous { |
| 1128 | + manifest: serde_json::from_str::<Manifest>(FAKE_MANIFEST_1).unwrap() |
| 1129 | + })); |
| 1130 | + } |
| 1131 | + |
| 1132 | + #[test] |
| 1133 | + fn source_repository_named() { |
| 1134 | + let test_dir = tempfile::tempdir().unwrap(); |
| 1135 | + let (collection, repository, _) = setup_repository(test_dir.path()); |
| 1136 | + let source = Source::new( |
| 1137 | + &SourceArgs { |
| 1138 | + source: format!("{TEST_COLLECTION_NAME}/{TEST_REPO_NAME}:main").as_str(), |
| 1139 | + collections: std::slice::from_ref(&collection), |
| 1140 | + pre_processor: None, |
| 1141 | + config_source: Path::new(TEST_CONFIG_SOURCE), |
| 1142 | + anonymous: false, |
| 1143 | + }, |
| 1144 | + None, |
| 1145 | + ) |
| 1146 | + .unwrap(); |
| 1147 | + assert!(source.eq(&Source::Repository { |
| 1148 | + manifest: serde_json::from_str::<Manifest>(FAKE_MANIFEST_1).unwrap(), |
| 1149 | + collection, |
| 1150 | + repository, |
| 1151 | + })); |
| 1152 | + } |
| 1153 | + |
| 1154 | + #[test] |
| 1155 | + fn source_repository_path() { |
| 1156 | + let test_dir = tempfile::tempdir().unwrap(); |
| 1157 | + let (collection, repository, test_repo_path) = setup_repository(test_dir.path()); |
| 1158 | + let source = Source::new( |
| 1159 | + &SourceArgs { |
| 1160 | + source: test_repo_path.to_str().unwrap(), |
| 1161 | + collections: std::slice::from_ref(&collection), |
| 1162 | + pre_processor: None, |
| 1163 | + config_source: Path::new(TEST_CONFIG_SOURCE), |
| 1164 | + anonymous: false, |
| 1165 | + }, |
| 1166 | + None, |
| 1167 | + ) |
| 1168 | + .unwrap(); |
| 1169 | + assert!(source.eq(&Source::Repository { |
| 1170 | + manifest: serde_json::from_str::<Manifest>(FAKE_MANIFEST_1).unwrap(), |
| 1171 | + collection, |
| 1172 | + repository, |
| 1173 | + })); |
| 1174 | + } |
| 1175 | + |
| 1176 | + #[test] |
| 1177 | + fn source_repository_path_with_manifest() { |
| 1178 | + let test_dir = tempfile::tempdir().unwrap(); |
| 1179 | + let (collection, repository, _test_repo_path) = setup_repository(test_dir.path()); |
| 1180 | + let manifest_path = _test_repo_path.join("manifest.json"); |
| 1181 | + std::fs::write(&manifest_path, FAKE_MANIFEST_2).unwrap(); |
| 1182 | + let source = Source::new( |
| 1183 | + &SourceArgs { |
| 1184 | + source: manifest_path.to_str().unwrap(), |
| 1185 | + collections: std::slice::from_ref(&collection), |
| 1186 | + pre_processor: None, |
| 1187 | + config_source: Path::new(TEST_CONFIG_SOURCE), |
| 1188 | + anonymous: false, |
| 1189 | + }, |
| 1190 | + None, |
| 1191 | + ) |
| 1192 | + .unwrap(); |
| 1193 | + assert!(source.eq(&Source::Repository { |
| 1194 | + manifest: serde_json::from_str::<Manifest>(FAKE_MANIFEST_2).unwrap(), |
| 1195 | + collection, |
| 1196 | + repository, |
| 1197 | + })); |
| 1198 | + } |
| 1199 | + } |
| 1200 | diff --git a/ayllu-shell/src/ui/repository_build.rs b/ayllu-shell/src/ui/repository_build.rs |
| 1201 | index caf726a..ed3c098 100644 |
| 1202 | --- a/ayllu-shell/src/ui/repository_build.rs |
| 1203 | +++ b/ayllu-shell/src/ui/repository_build.rs |
| 1204 | @@ -105,7 +105,7 @@ impl MenuItem for Item { |
| 1205 | fn apply(&self) -> Result<(), Error> { |
| 1206 | let item = self.0.build()?; |
| 1207 | let source = format!( |
| 1208 | - "{}/{}/{}", |
| 1209 | + "{}/{}:{}", |
| 1210 | item.collection, item.repository.name, item.branch |
| 1211 | ); |
| 1212 | if Prompter::confirm("Execute job?") { |
| 1213 | @@ -113,16 +113,10 @@ impl MenuItem for Item { |
| 1214 | Behavior::Foreground => { |
| 1215 | println!("Executing job for {source} in the foreground"); |
| 1216 | let mut proc = std::process::Command::new("ayllu-build") |
| 1217 | - .args(["evaluate", "--forward", "--stdin"]) |
| 1218 | - .stdin(Stdio::piped()) |
| 1219 | + .args(["evaluate", "--forward", &source]) |
| 1220 | .stdout(Stdio::inherit()) |
| 1221 | .stderr(Stdio::inherit()) |
| 1222 | .spawn()?; |
| 1223 | - |
| 1224 | - let mut stdin = proc.stdin.take().unwrap(); |
| 1225 | - stdin.write_all(source.as_bytes())?; |
| 1226 | - stdin.write_all("\n".as_bytes())?; |
| 1227 | - stdin.flush()?; |
| 1228 | let status = proc.wait()?; |
| 1229 | if !status.success() { |
| 1230 | eprintln!("Build returned non-zero exit code: {status:?}"); |
| 1231 | diff --git a/config.example.toml b/config.example.toml |
| 1232 | index 5606ca5..c35457a 100644 |
| 1233 | --- a/config.example.toml |
| 1234 | +++ b/config.example.toml |
| 1235 | @@ -253,6 +253,9 @@ source = ".ayllu-build.jsonnet" |
| 1236 | # Program and arguments to pass to the pre-processor |
| 1237 | program = "jsonnet" |
| 1238 | args = ["-"] |
| 1239 | + # Default path Ayllu configuration will be loaded from. NOTE that you can |
| 1240 | + # override this value by configuring pre_processor.source as defined above. |
| 1241 | + default_build_config_path = ".ayllu-build.json" |
| 1242 | |
| 1243 | ### |
| 1244 | ##### Quipu Configuration |
| 1245 | diff --git a/crates/cmd/src/build.rs b/crates/cmd/src/build.rs |
| 1246 | index cfe091c..b0740cb 100644 |
| 1247 | --- a/crates/cmd/src/build.rs |
| 1248 | +++ b/crates/cmd/src/build.rs |
| 1249 | @@ -51,39 +51,24 @@ initialization and validation steps."#)] |
| 1250 | }, |
| 1251 | /// Evaluate a local build script |
| 1252 | #[clap(long_about = r#" |
| 1253 | - |
| 1254 | - Evaluate a build configuration from the given source. If --stdin is active then |
| 1255 | - the desired collection/name/[branch] will be read from the input. This will |
| 1256 | - evaluate each step in the build process until a non-continuable error is reached |
| 1257 | - or all steps complete successfully. You can use the quipu(1) command to inspect |
| 1258 | - build artifacts, logs, and other properties. |
| 1259 | + Evaluate a build configuration from the given source. This will evaluate |
| 1260 | + each step in the build process until a non-continuable error is reached or all |
| 1261 | + steps complete successfully. You can use the quipu(1) command to inspect build |
| 1262 | + artifacts, logs, and other properties. |
| 1263 | |
| 1264 | Sometimes you want to make a quick modification to your build configuration and |
| 1265 | test it against the source repository without having to commit the change into |
| 1266 | - your VCS only to revert or squash it later. In this scenario you can use the |
| 1267 | - --manifest flag in combination with the SOURCE specification. |
| 1268 | - |
| 1269 | - Examples: |
| 1270 | - |
| 1271 | - Build the local repository with the uncommitted manifest file |
| 1272 | - |
| 1273 | - ayllu-build evaluate --manifest .ayllu-build.jsonnet . |
| 1274 | - |
| 1275 | - Pipe an alternative manifest into the current repository build |
| 1276 | - |
| 1277 | - ayllu-build evaluate --manifest - . < test.jsonnet |
| 1278 | - |
| 1279 | - In this scenario the HEAD of your repository will be used as part of the build |
| 1280 | - but the manifest configuration is loaded from the uncommitted file in your git |
| 1281 | - repsoitory. |
| 1282 | + your VCS only to revert or squash it later. In this scenario you can specify |
| 1283 | + the SOURCE to be the modified manifest in your project directory, e.g. |
| 1284 | + ayllu-build evaluate .ayllu-build.json. |
| 1285 | |
| 1286 | When the --debug flag is enabled anytime a step fails the user will be prompted |
| 1287 | to attach to the failed build in an external terminal and investigate the error. |
| 1288 | - Once the user is finished they can press enter and the build will continue. |
| 1289 | + Once the user is finished they can press enter and the build will be cancelled. |
| 1290 | |
| 1291 | Users have many different desires in terms of configuration file format. |
| 1292 | Natively Ayllu will only accept a JSON configuration file for it's build input |
| 1293 | - but supports the use pre-processors to allow users to specify their build in |
| 1294 | + but supports the use of pre-processors to allow users to specify their build in |
| 1295 | the configuration language of their choosing. Jsonnet is the recommend |
| 1296 | configuration system supported by Ayllu. |
| 1297 | |
| 1298 | @@ -92,7 +77,35 @@ You can configure it in your Ayllu config file as such: |
| 1299 | [build.pre_processor] |
| 1300 | source = ".ayllu-build.jsonnet" |
| 1301 | program = "jsonnet" |
| 1302 | - args = ["-"] "#)] |
| 1303 | + args = ["-"] " |
| 1304 | + |
| 1305 | + Each time ayllu-build evaluates a repository it will look for an |
| 1306 | + .ayllu-build.jsonnet file, pipe into the jsonnet program, and then return the |
| 1307 | + json output it produces. |
| 1308 | + |
| 1309 | + Usage Examples: |
| 1310 | + |
| 1311 | + Build the local repository at HEAD |
| 1312 | + |
| 1313 | + ayllu-build evaluate |
| 1314 | + ayllu-build evaluate . |
| 1315 | + |
| 1316 | + Build the local repository at HEAD with an uncommitted manifest file |
| 1317 | + |
| 1318 | + ayllu-build evaluate .ayllu-build.jsonnet |
| 1319 | + |
| 1320 | + Build the local repository at HEAD with a manifest piped in via STDIN |
| 1321 | + |
| 1322 | + ayllu-build evaluate - < $MANIFEST_CONTENT |
| 1323 | + |
| 1324 | + Perform an anonymous build in a managed source repository |
| 1325 | + |
| 1326 | + ayllu-build evaluate --anonymous path/to/your/manifest |
| 1327 | + |
| 1328 | + Or if you're outside of a managed repository |
| 1329 | + |
| 1330 | + ayllu-build evaluate path/to/your/manifest |
| 1331 | + ayllu-build evaluate - < $MANIFEST_CONTENT"#)] |
| 1332 | Evaluate { |
| 1333 | /// If the standard streams should be forwarded to the terminal |
| 1334 | #[arg(short, long, action)] |
| 1335 | @@ -100,12 +113,9 @@ You can configure it in your Ayllu config file as such: |
| 1336 | /// Pause on step failures so you can attach to the environment |
| 1337 | #[arg(short, long, action)] |
| 1338 | debug: bool, |
| 1339 | - /// Unique build name |
| 1340 | - #[arg(short, long)] |
| 1341 | - name: Option<String>, |
| 1342 | - /// Path to a manifest file regardless of source |
| 1343 | - #[arg(short, long)] |
| 1344 | - manifest: Option<PathBuf>, |
| 1345 | + /// Ignore any repository in the current directory |
| 1346 | + #[arg(short, long, action)] |
| 1347 | + anonymous: bool, |
| 1348 | /// Build Source can be - for STDIN, PATH, or collection/name |
| 1349 | #[arg(value_name = "SOURCE")] |
| 1350 | source: Option<String>, |
| 1351 | diff --git a/crates/git/src/lib.rs b/crates/git/src/lib.rs |
| 1352 | index 7789046..bccf960 100644 |
| 1353 | --- a/crates/git/src/lib.rs |
| 1354 | +++ b/crates/git/src/lib.rs |
| 1355 | @@ -29,7 +29,7 @@ pub mod testing; |
| 1356 | pub const DEFAULT_GIT_BRANCH: &str = "master"; |
| 1357 | |
| 1358 | /// A Collection is a directory of Git repositories on disk |
| 1359 | - #[derive(Clone, Debug, Serialize, Deserialize)] |
| 1360 | + #[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 1361 | pub struct Collection { |
| 1362 | pub name: String, |
| 1363 | pub description: Option<String>, |
| 1364 | @@ -44,7 +44,7 @@ impl Display for Collection { |
| 1365 | } |
| 1366 | |
| 1367 | /// A Repository is a Git backed code repository on disk |
| 1368 | - #[derive(Clone, Debug, Serialize, Deserialize)] |
| 1369 | + #[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 1370 | pub struct Repository { |
| 1371 | pub name: String, |
| 1372 | pub path: PathBuf, |