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

Dependency mappings #4

Merged
merged 3 commits into from
Jul 17, 2020
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
6 changes: 3 additions & 3 deletions buildpack_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
Name: "test-name-1",
},
{
Name: "test-name-2",
Name: "test-name-2",
},
{
Name: "test-name-2",
Name: "test-name-2",
},
},
}
Expand Down Expand Up @@ -159,7 +159,7 @@ func testBuildpackPlan(t *testing.T, context spec.G, it spec.S) {
Expect(err).NotTo(HaveOccurred())
Expect(ok).To(BeTrue())
Expect(e).To(Equal(libcnb.BuildpackPlanEntry{
Name: "test-name-2",
Name: "test-name-2",
}))
})
})
Expand Down
8 changes: 4 additions & 4 deletions carton/package_dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func testPackageDependency(t *testing.T, context spec.G, it spec.S) {

p := carton.PackageDependency{
BuilderPath: path,
ID: "gcr.io/paketo-buildpacks/test-1",
Version: "test-version-3",
ID: "gcr.io/paketo-buildpacks/test-1",
Version: "test-version-3",
}

p.Update(carton.WithExitHandler(exitHandler))
Expand All @@ -101,8 +101,8 @@ func testPackageDependency(t *testing.T, context spec.G, it spec.S) {

p := carton.PackageDependency{
PackagePath: path,
ID: "gcr.io/paketo-buildpacks/test-1",
Version: "test-version-3",
ID: "gcr.io/paketo-buildpacks/test-1",
Version: "test-version-3",
}

p.Update(carton.WithExitHandler(exitHandler))
Expand Down
2 changes: 1 addition & 1 deletion crush/crush_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func testCrush(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

path string
path string
)

it.Before(func() {
Expand Down
48 changes: 34 additions & 14 deletions dependency_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,29 @@ type DependencyCache struct {

// UserAgent is the User-Agent string to use with requests.
UserAgent string

// Mappings optionally provides URIs mapping for BuildpackDependencies
Mappings []DependencyMapping
}

// NewDependencyCache creates a new instance setting the default cache path (<BUILDPACK_PATH>/dependencies) and user
// agent (<BUILDPACK_ID>/<BUILDPACK_VERSION>).
func NewDependencyCache(buildpack libcnb.Buildpack) DependencyCache {
return DependencyCache{
CachePath: filepath.Join(buildpack.Path, "dependencies"),
// Mappings will be read from <PLATFORM_DIR>/dependencies/mappings.toml
func NewDependencyCache(context libcnb.BuildContext) (DependencyCache, error) {
cache := DependencyCache{
CachePath: filepath.Join(context.Buildpack.Path, "dependencies"),
DownloadPath: os.TempDir(),
UserAgent: fmt.Sprintf("%s/%s", buildpack.Info.ID, buildpack.Info.Version),
UserAgent: fmt.Sprintf("%s/%s", context.Buildpack.Info.ID, context.Buildpack.Info.Version),
}
mappings, err := ReadMappingsForBuildpack(
DefaultMappingsFilePath(context.Platform.Path),
context.Buildpack.Info.ID,
)
if err != nil {
return DependencyCache{}, fmt.Errorf("unable to read dependency mappings file\n%w", err)
}
cache.Mappings = mappings
return cache, nil
}

// RequestModifierFunc is a callback that enables modification of a download request before it is sent. It is often
Expand Down Expand Up @@ -90,16 +103,23 @@ func (d *DependencyCache) ArtifactWithRequestModification(dependency BuildpackDe
actual BuildpackDependency
artifact string
file string
uri = dependency.URI
)
for _, override := range d.Mappings {
if override.ID == dependency.ID && override.Version == dependency.Version {
uri = override.URI
break
}
}

if dependency.SHA256 == "" {
d.Logger.Headerf("%s Dependency has no SHA256. Skipping cache.",
color.New(color.FgYellow, color.Bold).Sprint("Warning:"))

d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), dependency.URI)
artifact = filepath.Join(d.DownloadPath, filepath.Base(dependency.URI))
if err := d.download(dependency.URI, artifact, f); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", dependency.URI, err)
d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri)
artifact = filepath.Join(d.DownloadPath, filepath.Base(uri))
if err := d.download(uri, artifact, f); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", uri, err)
}

return os.Open(artifact)
Expand All @@ -112,7 +132,7 @@ func (d *DependencyCache) ArtifactWithRequestModification(dependency BuildpackDe

if reflect.DeepEqual(dependency, actual) {
d.Logger.Bodyf("%s cached download from buildpack", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.CachePath, dependency.SHA256, filepath.Base(dependency.URI)))
return os.Open(filepath.Join(d.CachePath, dependency.SHA256, filepath.Base(uri)))
}

file = filepath.Join(d.DownloadPath, fmt.Sprintf("%s.toml", dependency.SHA256))
Expand All @@ -122,13 +142,13 @@ func (d *DependencyCache) ArtifactWithRequestModification(dependency BuildpackDe

if reflect.DeepEqual(dependency, actual) {
d.Logger.Bodyf("%s previously cached download", color.GreenString("Reusing"))
return os.Open(filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(dependency.URI)))
return os.Open(filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri)))
}

d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), dependency.URI)
artifact = filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(dependency.URI))
if err := d.download(dependency.URI, artifact, f); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", dependency.URI, err)
d.Logger.Bodyf("%s from %s", color.YellowString("Downloading"), uri)
artifact = filepath.Join(d.DownloadPath, dependency.SHA256, filepath.Base(uri))
if err := d.download(uri, artifact, f); err != nil {
return nil, fmt.Errorf("unable to download %s\n%w", uri, err)
}

d.Logger.Body("Verifying checksum")
Expand Down
Loading