Skip to content

Commit

Permalink
Move shared caching logic to public location, rename to DiffMigrateLa…
Browse files Browse the repository at this point in the history
…yer (#376)

* Update magic_migrate version

* Update generic constraints

The `magic_migrate` library now requires errors to be Display + Debug, so this extra constraint is no longer needed on these interfaces.

* Make deprecation more specific

* Make Meta struct public

* Make invalid_metadata_action public

* Make restored_layer_action public

* Make cached_layer_write_metadata public

* Remove strip_ansi helper method

* Move test

* Move test

* Add testing helper to cache_buddy

* Move test

* Remove unused test module

* Update use statements

* Introduce CacheBuddy struct

It's basically the same logic as `cached_layer_write_metadata` but with configurable build/launch logic

* Switch layer to CacheBuddy

* Switch layer to CacheBuddy

* Switch layer to CacheBuddy

* Remove unused function

* Docs

* More docs

* Clarify intermediate struct docs

* Fix changelog reference

The dir is `layer` singular, not `layers` plural.

* Update changelog

* Remove unused `since` versions

* Move test helper into mod tests

* Require explicit values

These map 1:1 with `CachedLayerDefinition`. The extra indirection of supporting default values isn't buying us much.

* Use `cached_layer` to match API with BuildContext::cached_layer

* Use more informative name

* Rename module

* Update magic_migrate

* Remove unused imports

* Update docs and add a tutorial

* Update cache_diff
  • Loading branch information
schneems authored Jan 7, 2025
1 parent 28150cc commit 07a6f0b
Show file tree
Hide file tree
Showing 15 changed files with 997 additions and 385 deletions.
11 changes: 7 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion buildpacks/ruby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ tempfile = "3"
thiserror = "2"
ureq = { version = "2", default-features = false, features = ["tls"] }
url = "2"
magic_migrate = "0.2"
magic_migrate = "1.0"
toml = "0.8"
cache_diff = { version = "1.0.0", features = ["bullet_stream"] }

Expand Down
13 changes: 8 additions & 5 deletions buildpacks/ruby/src/layers/bundle_download_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
//!
//! Installs a copy of `bundler` to the `<layer-dir>` with a bundler executable in
//! `<layer-dir>/bin`. Must run before [`crate.steps.bundle_install`].
use crate::layers::shared::cached_layer_write_metadata;
use crate::RubyBuildpack;
use crate::RubyBuildpackError;
use bullet_stream::state::SubBullet;
use bullet_stream::{style, Print};
use cache_diff::CacheDiff;
use commons::gemfile_lock::ResolvedBundlerVersion;
use commons::layer::diff_migrate::DiffMigrateLayer;
use fun_run::{self, CommandWithName};
use libcnb::data::layer_name;
use libcnb::layer::{EmptyLayerCause, LayerState};
use libcnb::layer_env::{LayerEnv, ModificationBehavior, Scope};
use libcnb::Env;
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
use serde::{Deserialize, Deserializer, Serialize};
use std::convert::Infallible;
use serde::{Deserialize, Serialize};
use std::io::Stdout;
use std::path::Path;
use std::process::Command;
Expand All @@ -29,7 +28,11 @@ pub(crate) fn handle(
mut bullet: Print<SubBullet<Stdout>>,
metadata: &Metadata,
) -> libcnb::Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let layer_ref = cached_layer_write_metadata(layer_name!("bundler"), context, metadata)?;
let layer_ref = DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("bundler"), context, metadata)?;
match &layer_ref.state {
LayerState::Restored { cause } => {
bullet = bullet.sub_bullet(cause);
Expand Down Expand Up @@ -123,7 +126,7 @@ fn download_bundler(
#[cfg(test)]
mod test {
use super::*;
use crate::layers::shared::strip_ansi;
use bullet_stream::strip_ansi;

#[test]
fn test_metadata_diff() {
Expand Down
15 changes: 9 additions & 6 deletions buildpacks/ruby/src/layers/bundle_install_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
//! must be compiled and will then be invoked via FFI. These native extensions are
//! OS, Architecture, and Ruby version dependent. Due to this, when one of these changes
//! we must clear the cache and re-run `bundle install`.
use crate::layers::shared::{cached_layer_write_metadata, Meta};
use crate::target_id::{OsDistribution, TargetId, TargetIdError};
use crate::{BundleWithout, RubyBuildpack, RubyBuildpackError};
use bullet_stream::state::SubBullet;
use bullet_stream::{style, Print};
use cache_diff::CacheDiff;
use commons::layer::diff_migrate::{DiffMigrateLayer, Meta};
use commons::{
display::SentenceList, gemfile_lock::ResolvedRubyVersion, metadata_digest::MetadataDigest,
};
Expand All @@ -31,8 +31,7 @@ use libcnb::{
Env,
};
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
use serde::{Deserialize, Deserializer, Serialize};
use std::convert::Infallible;
use serde::{Deserialize, Serialize};
use std::io::Stdout;
use std::{path::Path, process::Command};

Expand All @@ -52,7 +51,11 @@ pub(crate) fn handle(
metadata: &Metadata,
without: &BundleWithout,
) -> libcnb::Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let layer_ref = cached_layer_write_metadata(layer_name!("gems"), context, metadata)?;
let layer_ref = DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("gems"), context, metadata)?;
let install_state = match &layer_ref.state {
LayerState::Restored { cause } => {
bullet = bullet.sub_bullet(cause);
Expand Down Expand Up @@ -198,7 +201,7 @@ impl TryFrom<MetadataV1> for MetadataV2 {
}

impl TryFrom<MetadataV2> for MetadataV3 {
type Error = Infallible;
type Error = std::convert::Infallible;

fn try_from(v2: MetadataV2) -> Result<Self, Self::Error> {
Ok(Self {
Expand Down Expand Up @@ -319,7 +322,7 @@ fn display_name(cmd: &mut Command, env: &Env) -> String {

#[cfg(test)]
mod test {
use crate::layers::shared::strip_ansi;
use bullet_stream::strip_ansi;

use super::*;
use std::path::PathBuf;
Expand Down
36 changes: 27 additions & 9 deletions buildpacks/ruby/src/layers/ruby_install_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//!
//! When the Ruby version changes, invalidate and re-run.
//!
use crate::layers::shared::cached_layer_write_metadata;
use crate::target_id::OsDistribution;
use crate::{
target_id::{TargetId, TargetIdError},
Expand All @@ -21,13 +20,13 @@ use bullet_stream::state::SubBullet;
use bullet_stream::Print;
use cache_diff::CacheDiff;
use commons::gemfile_lock::ResolvedRubyVersion;
use commons::layer::diff_migrate::DiffMigrateLayer;
use flate2::read::GzDecoder;
use libcnb::data::layer_name;
use libcnb::layer::{EmptyLayerCause, LayerState};
use libcnb::layer_env::LayerEnv;
use magic_migrate::{try_migrate_deserializer_chain, TryMigrate};
use serde::{Deserialize, Deserializer, Serialize};
use std::convert::Infallible;
use serde::{Deserialize, Serialize};
use std::io::{self, Stdout};
use std::path::Path;
use tar::Archive;
Expand All @@ -39,7 +38,11 @@ pub(crate) fn handle(
mut bullet: Print<SubBullet<Stdout>>,
metadata: &Metadata,
) -> libcnb::Result<(Print<SubBullet<Stdout>>, LayerEnv), RubyBuildpackError> {
let layer_ref = cached_layer_write_metadata(layer_name!("ruby"), context, metadata)?;
let layer_ref = DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("ruby"), context, metadata)?;
match &layer_ref.state {
LayerState::Restored { cause } => {
bullet = bullet.sub_bullet(cause);
Expand Down Expand Up @@ -237,9 +240,9 @@ pub(crate) enum RubyInstallError {

#[cfg(test)]
mod tests {
use crate::layers::shared::{strip_ansi, temp_build_context};

use super::*;
use crate::layers::shared::temp_build_context;
use bullet_stream::strip_ansi;

/// If this test fails due to a change you'll need to
/// implement `TryMigrate` for the new layer data and add
Expand Down Expand Up @@ -388,8 +391,18 @@ version = "3.1.3"
let differences = old.diff(&old);
assert_eq!(differences, Vec::<String>::new());

cached_layer_write_metadata(layer_name!("ruby"), &context, &old).unwrap();
let result = cached_layer_write_metadata(layer_name!("ruby"), &context, &old).unwrap();
DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("ruby"), &context, &old)
.unwrap();
let result = DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("ruby"), &context, &old)
.unwrap();
let actual = result.state;
assert!(matches!(actual, LayerState::Restored { .. }));

Expand All @@ -400,7 +413,12 @@ version = "3.1.3"
let differences = now.diff(&old);
assert_eq!(differences.len(), 1);

let result = cached_layer_write_metadata(layer_name!("ruby"), &context, &now).unwrap();
let result = DiffMigrateLayer {
build: true,
launch: true,
}
.cached_layer(layer_name!("ruby"), &context, &now)
.unwrap();
assert!(matches!(
result.state,
LayerState::Empty {
Expand Down
Loading

0 comments on commit 07a6f0b

Please sign in to comment.