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 metrics agent layer to Struct API and bullet Stream #321

Merged
merged 7 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
159 changes: 65 additions & 94 deletions buildpacks/ruby/src/layers/metrics_agent_install.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{RubyBuildpack, RubyBuildpackError};
use commons::output::section_log::{log_step, log_step_timed, SectionLogger};
use flate2::read::GzDecoder;
use libcnb::data::layer_content_metadata::LayerTypes;
use libcnb::layer::ExistingLayerStrategy;
use libcnb::{
additional_buildpack_binary_path,
generic::GenericMetadata,
layer::{Layer, LayerResultBuilder},
use libcnb::additional_buildpack_binary_path;
use libcnb::data::layer_name;
use libcnb::layer::{
CachedLayerDefinition, EmptyLayerCause, InvalidMetadataAction, LayerRef, LayerState,
RestoredLayerAction,
};
use libherokubuildpack::digest::sha256;
use serde::{Deserialize, Serialize};
Expand All @@ -29,11 +28,6 @@ const DOWNLOAD_URL: &str =
"https://agentmon-releases.s3.us-east-1.amazonaws.com/agentmon-0.3.1-linux-amd64.tar.gz";
const DOWNLOAD_SHA: &str = "f9bf9f33c949e15ffed77046ca38f8dae9307b6a0181c6af29a25dec46eb2dac";

#[derive(Debug)]
pub(crate) struct MetricsAgentInstall<'a> {
pub(crate) _in_section: &'a dyn SectionLogger, // force the layer to be called within a Section logging context, not necessary but it's safer
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub(crate) struct Metadata {
download_url: Option<String>,
Expand Down Expand Up @@ -64,97 +58,74 @@ pub(crate) enum MetricsAgentInstallError {
ChecksumFailed(String),
}

impl<'a> Layer for MetricsAgentInstall<'a> {
type Buildpack = RubyBuildpack;
type Metadata = Metadata;

fn types(&self) -> libcnb::data::layer_content_metadata::LayerTypes {
LayerTypes {
pub(crate) fn handle_metrics_agent_layer(
context: &libcnb::build::BuildContext<RubyBuildpack>,
// TODO: Replace when implementing bullet_stream. Included with original comment:
// force the layer to be called within a Section logging context, not necessary but it's safer
_in_section_logger: &dyn SectionLogger,
) -> libcnb::Result<LayerRef<RubyBuildpack, (), Option<String>>, RubyBuildpackError> {
let layer_ref = context.cached_layer(
layer_name!("metrics_agent"),
CachedLayerDefinition {
build: true,
launch: true,
cache: true,
invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
restored_layer_action: &|metadata: &Metadata, _| {
if metadata.download_url == Some(DOWNLOAD_URL.to_string()) {
(
RestoredLayerAction::KeepLayer,
metadata.download_url.clone(),
)
} else {
(
RestoredLayerAction::DeleteLayer,
metadata.download_url.clone(),
)
}
},
},
)?;

match layer_ref.state.clone() {
LayerState::Restored { .. } => {
log_step("Using cached metrics agent");
}
}

fn create(
&mut self,
_context: &libcnb::build::BuildContext<Self::Buildpack>,
layer_path: &std::path::Path,
) -> Result<
libcnb::layer::LayerResult<Self::Metadata>,
<Self::Buildpack as libcnb::Buildpack>::Error,
> {
let bin_dir = layer_path.join("bin");

let agentmon = log_step_timed("Downloading", || {
install_agentmon(&bin_dir).map_err(RubyBuildpackError::MetricsAgentError)
})?;

log_step("Writing scripts");
let execd = write_execd_script(&agentmon, layer_path)
.map_err(RubyBuildpackError::MetricsAgentError)?;
LayerState::Empty { cause } => {
match cause {
EmptyLayerCause::NewlyCreated => {}
EmptyLayerCause::InvalidMetadataAction { .. } => {
log_step("Clearing cache (invalid metadata)");
}
EmptyLayerCause::RestoredLayerAction { cause: Some(url) } => {
// This should probably be updated to "Updating cached metrics agent (from {url} to {DOWNLOAD_URL}"
// or some other language that reflects the cache metrics agent is being updated. Alternatively,
// this could simply state "Deleting cached metrics agent ({url}) if the "Downloading" log below
// is updated to include the new download URL.
// Including original language to show a 1:1 migration from Layer to Struct API, retaining full
// functionality/behavior without refactoring.
log_step(format!(
"Using cached metrics agent ({url} to {DOWNLOAD_URL}"
));
}
EmptyLayerCause::RestoredLayerAction { cause: None } => {}
}
let bin_dir = layer_ref.path().join("bin");

LayerResultBuilder::new(Metadata {
download_url: Some(DOWNLOAD_URL.to_string()),
})
.exec_d_program("spawn_metrics_agent", execd)
.build()
}
let agentmon = log_step_timed("Downloading", || {
install_agentmon(&bin_dir).map_err(RubyBuildpackError::MetricsAgentError)
})?;

fn update(
&mut self,
_context: &libcnb::build::BuildContext<Self::Buildpack>,
layer_data: &libcnb::layer::LayerData<Self::Metadata>,
) -> Result<
libcnb::layer::LayerResult<Self::Metadata>,
<Self::Buildpack as libcnb::Buildpack>::Error,
> {
let layer_path = &layer_data.path;

log_step("Writing scripts");
let execd = write_execd_script(&layer_path.join("bin").join("agentmon"), layer_path)
.map_err(RubyBuildpackError::MetricsAgentError)?;

LayerResultBuilder::new(Metadata {
download_url: Some(DOWNLOAD_URL.to_string()),
})
.exec_d_program("spawn_metrics_agent", execd)
.build()
}
log_step("Writing scripts");
let execd = write_execd_script(&agentmon, layer_ref.path().as_path())
.map_err(RubyBuildpackError::MetricsAgentError)?;

fn existing_layer_strategy(
&mut self,
_context: &libcnb::build::BuildContext<Self::Buildpack>,
layer_data: &libcnb::layer::LayerData<Self::Metadata>,
) -> Result<libcnb::layer::ExistingLayerStrategy, <Self::Buildpack as libcnb::Buildpack>::Error>
{
match &layer_data.content_metadata.metadata.download_url {
Some(url) if url == DOWNLOAD_URL => {
log_step("Using cached metrics agent");
Ok(ExistingLayerStrategy::Update)
}
Some(url) => {
log_step(format!(
"Using cached metrics agent ({url} to {DOWNLOAD_URL}"
));
Ok(ExistingLayerStrategy::Recreate)
}
None => Ok(ExistingLayerStrategy::Recreate),
layer_ref.write_exec_d_programs([("spawn_metrics_agent".to_string(), execd)])?;
layer_ref.write_metadata(Metadata {
download_url: Some(DOWNLOAD_URL.to_string()),
})?;
}
}

fn migrate_incompatible_metadata(
&mut self,
_context: &libcnb::build::BuildContext<Self::Buildpack>,
_metadata: &GenericMetadata,
) -> Result<
libcnb::layer::MetadataMigration<Self::Metadata>,
<Self::Buildpack as libcnb::Buildpack>::Error,
> {
log_step("Clearing cache (invalid metadata)");

Ok(libcnb::layer::MetadataMigration::RecreateLayer)
}
Ok(layer_ref)
}

fn write_execd_script(
Expand Down
15 changes: 7 additions & 8 deletions buildpacks/ruby/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use fun_run::CmdError;
use layers::{
bundle_download_layer::{BundleDownloadLayer, BundleDownloadLayerMetadata},
bundle_install_layer::{BundleInstallLayer, BundleInstallLayerMetadata},
metrics_agent_install::{MetricsAgentInstall, MetricsAgentInstallError},
metrics_agent_install::MetricsAgentInstallError,
ruby_install_layer::{RubyInstallError, RubyInstallLayer, RubyInstallLayerMetadata},
};
use libcnb::build::{BuildContext, BuildResult, BuildResultBuilder};
Expand Down Expand Up @@ -134,16 +134,15 @@ impl Buildpack for RubyBuildpack {
(logger, env) = {
let section = logger.section("Metrics agent");
if lockfile_contents.contains("barnes") {
let layer_data = context.handle_layer(
layer_name!("metrics_agent"),
MetricsAgentInstall {
_in_section: section.as_ref(),
},
let layer_ref = layers::metrics_agent_install::handle_metrics_agent_layer(
&context,
section.as_ref(),
)?;

(
section.end_section(),
layer_data.env.apply(Scope::Build, &env),
// TODO: This should likely be removed (also eliminating the need for the `handle_metrics_agent_layer` function to return a `LayerRef`)?
// Included the logic here for reference, but it doesn't seem necessary (the layer env isn't explicitly modified as far as I can tell, but perhaps there's some trait API magic happening I'm not familiar with?).
layer_ref.read_env()?.apply(Scope::Build, &env),
runesoerensen marked this conversation as resolved.
Show resolved Hide resolved
)
} else {
(
Expand Down