feat(tvix/build), add OciBuildService, the old way
This is just patchset 10 of CL10855, before the color_eyre changes,
rebased to the tvix_castore api.
Change-Id: If4b42412ff8568058908cda971ad7d6f2d9f9b7b
---
This provides a build service invoking runc. It can be used by using the
`oci://$path_to_some_tempdir` builder URL for now.
For now, it can be tested as such:
```
BUILD_SERVICE_ADDR=oci://$PWD/bundles target/debug/tvix
let pkgs = (import <nixpkgs> {}); in builtins.readDir pkgs.perl
```
readDir is to actually trigger IO into the store path (which triggers
the builds).
For now it fails due to missing reference scanning (see followup CLs).
Change-Id: I09b40e410114ce69966a41a0e3c33281b859e443
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12526
Autosubmit: yuka <yuka@yuka.dev>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
cf91917a9d
commit
2414c87282
14 changed files with 1270 additions and 18 deletions
133
tvix/build/src/oci/bundle.rs
Normal file
133
tvix/build/src/oci/bundle.rs
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
//! Module to create an OCI runtime bundle for a given [BuildRequest].
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use super::scratch_name;
|
||||
use crate::proto::BuildRequest;
|
||||
use anyhow::{bail, Context};
|
||||
use tracing::{debug, instrument};
|
||||
|
||||
/// Produce an OCI bundle in a given path.
|
||||
/// Check [make_spec] for a description about the paths produced.
|
||||
#[instrument(err)]
|
||||
pub(crate) fn make_bundle<'a>(
|
||||
request: &BuildRequest,
|
||||
runtime_spec: &oci_spec::runtime::Spec,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<()> {
|
||||
fs::create_dir_all(path).context("failed to create bundle path")?;
|
||||
|
||||
let spec_json = serde_json::to_string(runtime_spec).context("failed to render spec to json")?;
|
||||
fs::write(path.join("config.json"), spec_json).context("failed to write config.json")?;
|
||||
|
||||
fs::create_dir_all(path.join("inputs")).context("failed to create inputs dir")?;
|
||||
|
||||
let root_path = path.join("root");
|
||||
|
||||
fs::create_dir_all(&root_path).context("failed to create root path dir")?;
|
||||
fs::create_dir_all(root_path.join("etc")).context("failed to create root/etc dir")?;
|
||||
|
||||
// TODO: populate /etc/{group,passwd}. It's a mess?
|
||||
|
||||
let scratch_root = path.join("scratch");
|
||||
fs::create_dir_all(&scratch_root).context("failed to create scratch/ dir")?;
|
||||
|
||||
// for each scratch path, calculate its name inside scratch, and ensure the
|
||||
// directory exists.
|
||||
for p in request.scratch_paths.iter() {
|
||||
let scratch_path = scratch_root.join(scratch_name(p));
|
||||
debug!(scratch_path=?scratch_path, path=?p, "about to create scratch dir");
|
||||
fs::create_dir_all(scratch_path).context("Unable to create scratch dir")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Determine the path of all outputs specified in a [BuildRequest]
|
||||
/// as seen from the host, for post-build ingestion.
|
||||
/// This lookup needs to take scratch paths into consideration, as the build
|
||||
/// root is not writable on its own.
|
||||
/// If a path can't be determined, an error is returned.
|
||||
pub(crate) fn get_host_output_paths(
|
||||
request: &BuildRequest,
|
||||
bundle_path: &Path,
|
||||
) -> anyhow::Result<Vec<PathBuf>> {
|
||||
let scratch_root = bundle_path.join("scratch");
|
||||
|
||||
let mut host_output_paths: Vec<PathBuf> = Vec::with_capacity(request.outputs.len());
|
||||
|
||||
for output_path in request.outputs.iter() {
|
||||
// calculate the location of the path.
|
||||
if let Some((mp, relpath)) =
|
||||
find_path_in_scratchs(output_path, request.scratch_paths.as_slice())
|
||||
{
|
||||
host_output_paths.push(scratch_root.join(scratch_name(mp)).join(relpath));
|
||||
} else {
|
||||
bail!("unable to find path {}", output_path);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(host_output_paths)
|
||||
}
|
||||
|
||||
/// For a given list of mountpoints (sorted) and a search_path, find the
|
||||
/// specific mountpoint parenting that search_path and return it, as well as the
|
||||
/// relative path from there to the search_path.
|
||||
/// mountpoints must be sorted, so we can iterate over the list from the back
|
||||
/// and match on the prefix.
|
||||
fn find_path_in_scratchs<'a, 'b>(
|
||||
search_path: &'a str,
|
||||
mountpoints: &'b [String],
|
||||
) -> Option<(&'b str, &'a str)> {
|
||||
mountpoints.iter().rev().find_map(|mp| {
|
||||
Some((
|
||||
mp.as_str(),
|
||||
search_path.strip_prefix(mp)?.strip_prefix('/')?,
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use rstest::rstest;
|
||||
|
||||
use crate::{oci::scratch_name, proto::BuildRequest};
|
||||
|
||||
use super::{find_path_in_scratchs, get_host_output_paths};
|
||||
|
||||
#[rstest]
|
||||
#[case::simple("nix/store/aaaa", &["nix/store".into()], Some(("nix/store", "aaaa")))]
|
||||
#[case::prefix_no_sep("nix/store/aaaa", &["nix/sto".into()], None)]
|
||||
#[case::not_found("nix/store/aaaa", &["build".into()], None)]
|
||||
fn test_test_find_path_in_scratchs(
|
||||
#[case] search_path: &str,
|
||||
#[case] mountpoints: &[String],
|
||||
#[case] expected: Option<(&str, &str)>,
|
||||
) {
|
||||
assert_eq!(find_path_in_scratchs(search_path, mountpoints), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_host_output_paths_simple() {
|
||||
let request = BuildRequest {
|
||||
outputs: vec!["nix/store/fhaj6gmwns62s6ypkcldbaj2ybvkhx3p-foo".into()],
|
||||
scratch_paths: vec!["build".into(), "nix/store".into()],
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let paths =
|
||||
get_host_output_paths(&request, Path::new("bundle-root")).expect("must succeed");
|
||||
|
||||
let mut expected_path = PathBuf::new();
|
||||
expected_path.push("bundle-root");
|
||||
expected_path.push("scratch");
|
||||
expected_path.push(scratch_name("nix/store"));
|
||||
expected_path.push("fhaj6gmwns62s6ypkcldbaj2ybvkhx3p-foo");
|
||||
|
||||
assert_eq!(vec![expected_path], paths)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue