Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to libherokubuildpack inventory #926

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions buildpacks/nodejs-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ edition.workspace = true
workspace = true

[dependencies]
heroku-inventory-utils = { git = "https://github.com/heroku/buildpacks-go/", rev = "2a86fae18332b9bd495eb29422c13ac3fcb2d0dc" }
heroku-nodejs-utils.workspace = true
libcnb = { version = "=0.23.0", features = ["trace"] }
libherokubuildpack = { version = "=0.23.0", default-features = false, features = ["download", "fs", "log", "tar"] }
libherokubuildpack = { version = "=0.24.0", default-features = false, features = ["download", "fs", "inventory", "log", "tar"] }
serde = "1"
sha2 = "0.10.8"
tempfile = "3"
Expand Down
48 changes: 27 additions & 21 deletions buildpacks/nodejs-engine/src/install_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::fs;
use std::io::Read;
use std::path::Path;

use heroku_inventory_utils::inv::Artifact;
use libcnb::build::BuildContext;
use libcnb::data::layer_name;
use libcnb::layer::{
CachedLayerDefinition, InvalidMetadataAction, LayerState, RestoredLayerAction,
};
use libherokubuildpack::download::download_file;
use libherokubuildpack::fs::move_directory_contents;
use libherokubuildpack::inventory::artifact::Artifact;
use libherokubuildpack::log::log_info;
use libherokubuildpack::tar::decompress_tarball;
use serde::{Deserialize, Serialize};
Expand All @@ -23,7 +23,7 @@ use crate::{NodeJsEngineBuildpack, NodeJsEngineBuildpackError};

pub(crate) fn install_node(
context: &BuildContext<NodeJsEngineBuildpack>,
distribution_artifact: &Artifact<Version, Sha256>,
distribution_artifact: &Artifact<Version, Sha256, Option<()>>,
colincasey marked this conversation as resolved.
Show resolved Hide resolved
) -> Result<(), libcnb::Error<NodeJsEngineBuildpackError>> {
let new_metadata = DistLayerMetadata {
artifact: distribution_artifact.clone(),
Expand All @@ -46,9 +46,13 @@ pub(crate) fn install_node(
},
)?;

let version_tag = format!(
"{} ({}-{})",
distribution_artifact.version, distribution_artifact.os, distribution_artifact.arch
);
match distribution_layer.state {
LayerState::Restored { .. } => {
log_info(format!("Reusing Node.js {distribution_artifact}"));
log_info(format!("Reusing Node.js {version_tag}"));
}
LayerState::Empty { .. } => {
distribution_layer.write_metadata(new_metadata)?;
Expand All @@ -57,7 +61,7 @@ pub(crate) fn install_node(

log_info(format!(
"Downloading Node.js {} from {}",
distribution_artifact, distribution_artifact.url
version_tag, distribution_artifact.url
));
download_file(&distribution_artifact.url, node_tgz.path())
.map_err(DistLayerError::Download)?;
Expand All @@ -68,11 +72,11 @@ pub(crate) fn install_node(
Err(DistLayerError::ChecksumVerification)?;
}

log_info(format!("Extracting Node.js {distribution_artifact}"));
log_info(format!("Extracting Node.js {version_tag}"));
decompress_tarball(&mut node_tgz.into_file(), distribution_layer.path())
.map_err(DistLayerError::Untar)?;

log_info(format!("Installing Node.js {distribution_artifact}"));
log_info(format!("Installing Node.js {version_tag}"));

let dist_name = extract_tarball_prefix(&distribution_artifact.url)
.ok_or_else(|| DistLayerError::TarballPrefix(distribution_artifact.url.clone()))?;
Expand Down Expand Up @@ -111,7 +115,7 @@ const LAYER_VERSION: &str = "1";
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub(crate) struct DistLayerMetadata {
artifact: Artifact<Version, Sha256>,
artifact: Artifact<Version, Sha256, Option<()>>,
layer_version: String,
}

Expand Down Expand Up @@ -143,8 +147,10 @@ impl From<DistLayerError> for libcnb::Error<NodeJsEngineBuildpackError> {
mod tests {
use std::str::FromStr;

use heroku_inventory_utils::checksum::Checksum;
use heroku_inventory_utils::inv::{Arch, Os};
use libherokubuildpack::inventory::{
artifact::{Arch, Os},
checksum::Checksum,
};

use super::*;

Expand All @@ -156,21 +162,21 @@ mod tests {
arch: Arch::Arm64,
url: "https://nodejs.org/download/release/v22.1.0/node-v22.1.0-linux-arm64.tar.gz"
.to_string(),
checksum: Checksum::<Sha256>::try_from(
"9c111af1f951e8869615bca3601ce7ab6969374933bdba6397469843b808f222".to_string(),
)
.unwrap(),
checksum: "sha256:9c111af1f951e8869615bca3601ce7ab6969374933bdba6397469843b808f222"
.parse::<Checksum<Sha256>>()
.unwrap(),
metadata: None,
};
let node_version_22_1_0_linux_amd = Artifact {
version: Version::from_str("22.1.0").unwrap(),
os: Os::Linux,
arch: Arch::Amd64,
url: "https://nodejs.org/download/release/v22.1.0/node-v22.1.0-linux-x64.tar.gz"
.to_string(),
checksum: Checksum::<Sha256>::try_from(
"d8ae35a9e2bb0c0c0611ee9bacf564ea51cc8291ace1447f95ee6aeaf4f1d61d".to_string(),
)
.unwrap(),
checksum: "sha256:d8ae35a9e2bb0c0c0611ee9bacf564ea51cc8291ace1447f95ee6aeaf4f1d61d"
.parse::<Checksum<Sha256>>()
.unwrap(),
metadata: None,
};

// this is a check to ensure that the same node.js artifact doesn't invalidate the cache
Expand Down Expand Up @@ -207,10 +213,10 @@ mod tests {
arch: Arch::Amd64,
url: "https://nodejs.org/download/release/v22.1.0/node-v22.1.0-linux-x64.tar.gz"
.to_string(),
checksum: Checksum::<Sha256>::try_from(
"d8ae35a9e2bb0c0c0611ee9bacf564ea51cc8291ace1447f95ee6aeaf4f1d61d".to_string(),
)
.unwrap(),
checksum: "sha256:d8ae35a9e2bb0c0c0611ee9bacf564ea51cc8291ace1447f95ee6aeaf4f1d61d"
.parse::<Checksum<Sha256>>()
.unwrap(),
metadata: None,
},
layer_version: LAYER_VERSION.to_string(),
};
Expand Down
7 changes: 4 additions & 3 deletions buildpacks/nodejs-engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::env::consts;
use crate::attach_runtime_metrics::{attach_runtime_metrics, NodeRuntimeMetricsError};
use crate::configure_web_env::configure_web_env;
use crate::install_node::{install_node, DistLayerError};
use heroku_inventory_utils::inv::{resolve, Arch, Inventory, Os};
use heroku_nodejs_utils::package_json::{PackageJson, PackageJsonError};
use heroku_nodejs_utils::vrs::{Requirement, Version};
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
Expand All @@ -16,6 +15,8 @@ use libcnb::generic::GenericPlatform;
use libcnb::{buildpack_main, Buildpack};
#[cfg(test)]
use libcnb_test as _;
use libherokubuildpack::inventory::artifact::{Arch, Os};
use libherokubuildpack::inventory::Inventory;
use libherokubuildpack::log::{log_error, log_header, log_info};
#[cfg(test)]
use serde_json as _;
Expand Down Expand Up @@ -73,7 +74,7 @@ impl Buildpack for NodeJsEngineBuildpack {
log_header("Heroku Node.js Engine Buildpack");
log_header("Checking Node.js version");

let inv: Inventory<Version, Sha256> =
let inv: Inventory<Version, Sha256, Option<()>> =
toml::from_str(INVENTORY).map_err(NodeJsEngineBuildpackError::InventoryParseError)?;

let requested_version_range = PackageJson::read(context.app_dir.join("package.json"))
Expand All @@ -91,7 +92,7 @@ impl Buildpack for NodeJsEngineBuildpack {
};

let target_artifact = match (consts::OS.parse::<Os>(), consts::ARCH.parse::<Arch>()) {
(Ok(os), Ok(arch)) => resolve(&inv.artifacts, os, arch, &version_range),
(Ok(os), Ok(arch)) => inv.resolve(os, arch, &version_range),
(_, _) => None,
}
.ok_or(NodeJsEngineBuildpackError::UnknownVersionError(
Expand Down
2 changes: 1 addition & 1 deletion common/nodejs-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ workspace = true
anyhow = "1"
chrono = { version = "0.4", default-features = false, features = ["serde"] }
commons = { git = "https://github.com/heroku/buildpacks-ruby", branch = "main" }
heroku-inventory-utils = { git = "https://github.com/heroku/buildpacks-go/", rev = "2a86fae18332b9bd495eb29422c13ac3fcb2d0dc" }
indoc = "2"
keep_a_changelog_file = "0.1.0"
libcnb-data = "=0.23.0"
libherokubuildpack = { version = "=0.24.0", default-features = false, features = ["inventory", "inventory-sha2"] }
node-semver = "2"
opentelemetry = "0.24"
opentelemetry_sdk = { version = "0.24", features = ["trace"] }
Expand Down
Loading