Skip to content

Commit

Permalink
Move target information to shared ContextTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Feb 12, 2024
1 parent dfdd601 commit 55c77c6
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Buildpack for HelloWorldBuildpack {
// build phase (`bin/build`).
fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
println!("Hello World!");
println!("The build is running on: {} ({})!", context.target_os, context.target_arch);
println!("The build is running on: {} ({})!", context.target.os, context.target.arch);
BuildResultBuilder::new()
.launch(
Expand Down
2 changes: 1 addition & 1 deletion examples/basics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Buildpack for BasicBuildpack {
fn build(&self, context: BuildContext<Self>) -> libcnb::Result<BuildResult, Self::Error> {
println!(
"Build runs on: {} ({})!",
context.target_os, context.target_arch
context.target.os, context.target.arch
);

BuildResultBuilder::new().build()
Expand Down
36 changes: 2 additions & 34 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,15 @@ use crate::data::{
};
use crate::layer::{HandleLayerErrorOrBuildpackError, Layer, LayerData};
use crate::sbom::Sbom;
use crate::target::ContextTarget;
use std::path::PathBuf;

/// Context for the build phase execution.
pub struct BuildContext<B: Buildpack + ?Sized> {
pub layers_dir: PathBuf,
pub app_dir: PathBuf,
pub buildpack_dir: PathBuf,
/// The name of the target operating system.
///
/// The value should conform to [Go's `$GOOS`](https://golang.org/doc/install/source#environment), for example
/// `linux` or `windows`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`os` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub target_os: String,
/// The name of the target CPU architecture.
///
/// The value should conform to [Go's $GOARCH](https://golang.org/doc/install/source#environment), for example
/// `amd64` or `arm64`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`architecture` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
/// ``
pub target_arch: String,
/// The variant of the specified CPU architecture.
///
/// The value should conform to [OCI image spec platform variants](https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants), for example
/// `v7` or `v8`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`variant` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub target_arch_variant: Option<String>,
/// The name of the operating system distribution. Should be empty for windows.
///
/// For example: `ubuntu` or `arch`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.name` label.
pub target_distro_name: Option<String>,
/// The version of the operating system distribution.
///
/// For example: `18.02` or `2024.02.01`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.version` label.
pub target_distro_version: Option<String>,
pub target: ContextTarget,
pub platform: B::Platform,
pub buildpack_plan: BuildpackPlan,
pub buildpack_descriptor: ComponentBuildpackDescriptor<B::Metadata>,
Expand Down
36 changes: 2 additions & 34 deletions libcnb/src/detect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Provides detect phase specific types and helpers.
use crate::buildpack::Buildpack;
use crate::target::ContextTarget;
use crate::{data::build_plan::BuildPlan, data::buildpack::ComponentBuildpackDescriptor};
use std::fmt::Debug;
use std::path::PathBuf;
Expand All @@ -9,40 +10,7 @@ use std::path::PathBuf;
pub struct DetectContext<B: Buildpack + ?Sized> {
pub app_dir: PathBuf,
pub buildpack_dir: PathBuf,
/// The name of the target operating system.
///
/// The value should conform to [Go's `$GOOS`](https://golang.org/doc/install/source#environment), for example
/// `linux` or `windows`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`os` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub target_os: String,
/// The name of the target CPU architecture.
///
/// The value should conform to [Go's $GOARCH](https://golang.org/doc/install/source#environment), for example
/// `amd64` or `arm64`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`architecture` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
/// ``
pub target_arch: String,
/// The variant of the specified CPU architecture.
///
/// The value should conform to [OCI image spec platform variants](https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants), for example
/// `v7` or `v8`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`variant` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub target_arch_variant: Option<String>,
/// The name of the operating system distribution. Should be empty for windows.
///
/// For example: `ubuntu` or `arch`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.name` label.
pub target_distro_name: Option<String>,
/// The version of the operating system distribution.
///
/// For example: `18.02` or `2024.02.01`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.version` label.
pub target_distro_version: Option<String>,
pub target: ContextTarget,
pub platform: B::Platform,
pub buildpack_descriptor: ComponentBuildpackDescriptor<B::Metadata>,
}
Expand Down
13 changes: 8 additions & 5 deletions libcnb/src/layer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::layer::{
MetadataMigration,
};
use crate::layer_env::{LayerEnv, ModificationBehavior, Scope};
use crate::target::ContextTarget;
use crate::{read_toml_file, Buildpack, Env, LIBCNB_SUPPORTED_BUILDPACK_API};
use libcnb_data::buildpack::{BuildpackVersion, ComponentBuildpackDescriptor, Target};
use libcnb_data::buildpack_plan::BuildpackPlan;
Expand Down Expand Up @@ -901,11 +902,13 @@ fn build_context(temp_dir: &TempDir) -> BuildContext<TestBuildpack> {
layers_dir,
app_dir,
buildpack_dir,
target_os: String::from("linux"),
target_arch: String::from("amd64"),
target_arch_variant: None,
target_distro_name: Some(String::from("ubuntu")),
target_distro_version: Some(String::from("22.04")),
target: ContextTarget {
os: String::from("linux"),
arch: String::from("amd64"),
arch_variant: None,
distro_name: Some(String::from("ubuntu")),
distro_version: Some(String::from("22.04")),
},
platform: GenericPlatform::new(Env::new()),
buildpack_plan: BuildpackPlan {
entries: Vec::new(),
Expand Down
1 change: 1 addition & 0 deletions libcnb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod runtime;
#[cfg(feature = "trace")]
mod tracing;
mod util;
mod target;

pub use buildpack::Buildpack;
pub use env::*;
Expand Down
81 changes: 35 additions & 46 deletions libcnb/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::detect::{DetectContext, InnerDetectResult};
use crate::error::Error;
use crate::platform::Platform;
use crate::sbom::cnb_sbom_path;
use crate::target::ContextTarget;
#[cfg(feature = "trace")]
use crate::tracing::start_trace;
use crate::util::is_not_found_error_kind;
Expand Down Expand Up @@ -146,32 +147,10 @@ pub fn libcnb_runtime_detect<B: Buildpack>(

let build_plan_path = args.build_plan_path;

let target_os = env::var("CNB_TARGET_OS")
.map_err(Error::CannotDetermineTargetOs)
.map_err(|err| {
trace_error(&err);
err
})?;

let target_arch = env::var("CNB_TARGET_ARCH")
.map_err(Error::CannotDetermineTargetArch)
.map_err(|err| {
trace_error(&err);
err
})?;

let target_arch_variant = env::var("CNB_TARGET_ARCH_VARIANT").ok();
let target_distro_name = env::var("CNB_TARGET_DISTRO_NAME").ok();
let target_distro_version = env::var("CNB_TARGET_DISTRO_VERSION").ok();

let detect_context = DetectContext {
app_dir,
buildpack_dir,
target_os,
target_arch,
target_arch_variant,
target_distro_name,
target_distro_version,
target: context_target(&mut trace_error)?,
platform,
buildpack_descriptor,
};
Expand Down Expand Up @@ -243,33 +222,11 @@ pub fn libcnb_runtime_build<B: Buildpack>(
.map_err(Error::CannotReadStore)
.inspect_err(|err| trace_error(err))?;

let target_os = env::var("CNB_TARGET_OS")
.map_err(Error::CannotDetermineTargetOs)
.map_err(|err| {
trace_error(&err);
err
})?;

let target_arch = env::var("CNB_TARGET_ARCH")
.map_err(Error::CannotDetermineTargetArch)
.map_err(|err| {
trace_error(&err);
err
})?;

let target_arch_variant = env::var("CNB_TARGET_ARCH_VARIANT").ok();
let target_distro_name = env::var("CNB_TARGET_DISTRO_NAME").ok();
let target_distro_version = env::var("CNB_TARGET_DISTRO_VERSION").ok();

let build_context = BuildContext {
layers_dir: layers_dir.clone(),
app_dir,
platform,
target_os,
target_arch,
target_arch_variant,
target_distro_name,
target_distro_version,
target: context_target(&mut trace_error)?,
buildpack_plan,
buildpack_dir,
buildpack_descriptor,
Expand Down Expand Up @@ -396,3 +353,35 @@ fn read_buildpack_descriptor<BD: DeserializeOwned, E: Debug>() -> crate::Result<
.map_err(Error::CannotReadBuildpackDescriptor)
})
}

fn context_target<E, F>(trace_error: &mut F) -> crate::Result<ContextTarget, E>
where
E: Debug,
F: FnMut(&dyn std::error::Error),
{
let os = env::var("CNB_TARGET_OS")
.map_err(Error::CannotDetermineTargetOs)
.map_err(|err| {
trace_error(&err);
err
})?;

let arch = env::var("CNB_TARGET_ARCH")
.map_err(Error::CannotDetermineTargetArch)
.map_err(|err| {
trace_error(&err);
err
})?;

let arch_variant = env::var("CNB_TARGET_ARCH_VARIANT").ok();
let distro_name = env::var("CNB_TARGET_DISTRO_NAME").ok();
let distro_version = env::var("CNB_TARGET_DISTRO_VERSION").ok();

Ok(ContextTarget {
os,
arch,
arch_variant,
distro_name,
distro_version,
})
}
36 changes: 36 additions & 0 deletions libcnb/src/target.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pub struct ContextTarget {
/// The name of the target operating system.
///
/// The value should conform to [Go's `$GOOS`](https://golang.org/doc/install/source#environment), for example
/// `linux` or `windows`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`os` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub os: String,
/// The name of the target CPU architecture.
///
/// The value should conform to [Go's $GOARCH](https://golang.org/doc/install/source#environment), for example
/// `amd64` or `arm64`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`architecture` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
/// ``
pub arch: String,
/// The variant of the specified CPU architecture.
///
/// The value should conform to [OCI image spec platform variants](https://github.com/opencontainers/image-spec/blob/main/image-index.md#platform-variants), for example
/// `v7` or `v8`.
///
/// CNB `lifecycle` sources this value from the build OCI image's [`variant` property](https://github.com/opencontainers/image-spec/blob/main/config.md#properties).
pub arch_variant: Option<String>,
/// The name of the operating system distribution. Should be empty for windows.
///
/// For example: `ubuntu` or `arch`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.name` label.
pub distro_name: Option<String>,
/// The version of the operating system distribution.
///
/// For example: `18.02` or `2024.02.01`.
///
/// CNB `lifecycle` sources this value from the build OCI image's `io.buildpacks.distro.version` label.
pub distro_version: Option<String>,
}

0 comments on commit 55c77c6

Please sign in to comment.