Skip to content

Commit

Permalink
chore: load layers from dedicated directory
Browse files Browse the repository at this point in the history
  • Loading branch information
koehlma committed Jan 8, 2024
1 parent 1980966 commit d8459af
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 267 deletions.
9 changes: 4 additions & 5 deletions crates/rugpi-bakery/src/bake/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ pub fn bake_image(project: &Project, image: &str, output: &Path) -> Anyhow<()> {
}

pub fn bake_layer(project: &Project, arch: Architecture, layer: &str) -> Anyhow<PathBuf> {
let layer_config = project
.config
.layers
.get(layer)
.ok_or_else(|| anyhow!("unable to find layer {layer}"))?;
let library = project.load_library()?;
let layer_config = &library.layers[library
.lookup_layer(library.repositories.root_repository, layer)
.unwrap()];
if let Some(url) = &layer_config.url {
let layer_id = sha1(url);
let system_tar = project
Expand Down
1 change: 0 additions & 1 deletion crates/rugpi-bakery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rugpi_common::Anyhow;
pub mod bake;
pub mod idx_vec;
pub mod project;
pub mod tasks;
pub mod utils;

#[derive(Debug, Parser)]
Expand Down
5 changes: 1 addition & 4 deletions crates/rugpi-bakery/src/project/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@ use clap::ValueEnum;
use rugpi_common::Anyhow;
use serde::{Deserialize, Serialize};

use super::{images::ImageConfig, layers::LayerConfig, repositories::Source};
use super::{images::ImageConfig, repositories::Source};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BakeryConfig {
/// The repositories to use.
#[serde(default)]
pub repositories: HashMap<String, Source>,
/// The layers of the project.
#[serde(default)]
pub layers: HashMap<String, LayerConfig>,
/// The images of the project.
#[serde(default)]
pub images: HashMap<String, ImageConfig>,
Expand Down
18 changes: 17 additions & 1 deletion crates/rugpi-bakery/src/project/layers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use std::collections::{HashMap, HashSet};
use std::{
collections::{HashMap, HashSet},
fs,
path::Path,
};

use anyhow::Context;
use rugpi_common::Anyhow;
use serde::{Deserialize, Serialize};

use super::recipes::{ParameterValue, RecipeName};
Expand All @@ -20,3 +26,13 @@ pub struct LayerConfig {
#[serde(default)]
pub parameters: HashMap<RecipeName, HashMap<String, ParameterValue>>,
}

impl LayerConfig {
pub fn load(path: &Path) -> Anyhow<Self> {
toml::from_str(
&fs::read_to_string(path)
.with_context(|| format!("error reading layer config from `{path:?}`"))?,
)
.with_context(|| format!("error parsing layer config from path `{path:?}`"))
}
}
55 changes: 51 additions & 4 deletions crates/rugpi-bakery/src/project/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashMap, fs, ops::Deref, sync::Arc};
use rugpi_common::Anyhow;

use super::{
layers::LayerConfig,
recipes::{Recipe, RecipeLoader},
repositories::{ProjectRepositories, RepositoryIdx},
};
Expand All @@ -11,7 +12,9 @@ use crate::idx_vec::{new_idx_type, IdxVec};
pub struct Library {
pub repositories: ProjectRepositories,
pub recipes: IdxVec<RecipeIdx, Arc<Recipe>>,
pub tables: IdxVec<RepositoryIdx, HashMap<String, RecipeIdx>>,
pub layers: IdxVec<LayerIdx, LayerConfig>,
pub recipe_tables: IdxVec<RepositoryIdx, HashMap<String, RecipeIdx>>,
pub layer_tables: IdxVec<RepositoryIdx, HashMap<String, LayerIdx>>,
}

impl Library {
Expand All @@ -35,10 +38,35 @@ impl Library {
})
.collect::<Anyhow<_>>()?,
);
let mut layers = IdxVec::new();
let layer_tables = IdxVec::<RepositoryIdx, _>::from_vec(
repositories
.repositories
.iter()
.map(|(_, repository)| -> Anyhow<_> {
let mut table = HashMap::new();
let layers_dir = repository.source.dir.join("layers");
if !layers_dir.exists() {
return Ok(table);
}
for entry in fs::read_dir(layers_dir)? {
let entry = entry?;
let path = entry.path();
let layer_name = path.file_stem().unwrap();
let layer_config = LayerConfig::load(&path)?;
let layer_idx = layers.push(layer_config);
table.insert(layer_name.to_string_lossy().into_owned(), layer_idx);
}
Ok(table)
})
.collect::<Anyhow<_>>()?,
);
Ok(Self {
repositories,
recipes,
tables,
recipe_tables: tables,
layers,
layer_tables,
})
}

Expand All @@ -50,9 +78,23 @@ impl Library {
.repositories
.get(dependency_name)?,
};
self.tables[dependency_idx].get(recipe_name).cloned()
self.recipe_tables[dependency_idx].get(recipe_name).cloned()
} else {
self.tables[repository].get(name).cloned()
self.recipe_tables[repository].get(name).cloned()
}
}

pub fn lookup_layer(&self, repo: RepositoryIdx, name: &str) -> Option<LayerIdx> {
if let Some((dependency_name, layer_name)) = name.split_once('/') {
let dependency_idx = match dependency_name {
"core" => self.repositories.core_repository,
_ => *self.repositories.repositories[repo]
.repositories
.get(dependency_name)?,
};
self.layer_tables[dependency_idx].get(layer_name).cloned()
} else {
self.layer_tables[repo].get(name).cloned()
}
}
}
Expand All @@ -61,3 +103,8 @@ new_idx_type! {
/// Uniquely identifies a recipe in [`Library`].
pub RecipeIdx
}

new_idx_type! {
/// Uniquely identifies a layer in [`Library`].
pub LayerIdx
}
3 changes: 0 additions & 3 deletions crates/rugpi-bakery/src/tasks.rs

This file was deleted.

198 changes: 0 additions & 198 deletions crates/rugpi-bakery/src/tasks/customize.rs

This file was deleted.

Loading

0 comments on commit d8459af

Please sign in to comment.