Skip to content

Commit

Permalink
Adds support of buildpackages in buildpack store and updates freezer (#…
Browse files Browse the repository at this point in the history
…302)

* Adds support of buildpackages in buildpack store and updates freezer

* Creates helpful variable name
  • Loading branch information
ForestEckhardt authored Jun 5, 2024
1 parent dda57be commit e9fee75
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 28 deletions.
2 changes: 0 additions & 2 deletions buildpack_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func NewBuildpackStore() BuildpackStore {
cacheManager := freezer.NewCacheManager(filepath.Join(os.Getenv("HOME"), ".freezer-cache"))
releaseService := github.NewReleaseService(github.NewConfig("https://api.github.com", gitToken))
packager := packagers.NewJam()
fileSystem := freezer.NewFileSystem(os.MkdirTemp)
namer := freezer.NewNameGenerator()

return BuildpackStore{
Expand All @@ -54,7 +53,6 @@ func NewBuildpackStore() BuildpackStore {
remote: freezer.NewRemoteFetcher(
&cacheManager,
releaseService, packager,
fileSystem,
),
cacheManager: &cacheManager,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.10

require (
github.com/ForestEckhardt/freezer v0.0.12
github.com/ForestEckhardt/freezer v0.1.0
github.com/docker/docker v26.1.3+incompatible
github.com/google/go-containerregistry v0.19.1
github.com/oklog/ulid v1.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,8 @@ github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs=
github.com/ForestEckhardt/freezer v0.0.12 h1:n3AcMZQQ1E9uNCqbd/0sUVSeR3/nKIxqx0cAxye73tQ=
github.com/ForestEckhardt/freezer v0.0.12/go.mod h1:fQApI/B3u/Pu6DD4rxmzBJQUBDc9EDu7ieqM6l4JvZA=
github.com/ForestEckhardt/freezer v0.1.0 h1:1Av1G+uJTmhhJGuV8MkVLdiEH8ljuiBaV+Cgy30QbRE=
github.com/ForestEckhardt/freezer v0.1.0/go.mod h1:fQApI/B3u/Pu6DD4rxmzBJQUBDc9EDu7ieqM6l4JvZA=
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20191009163259-e802c2cb94ae/go.mod h1:mjwGPas4yKduTyubHvD1Atl9r1rUq8DfVy+gkVvZ+oo=
github.com/GoogleCloudPlatform/cloudsql-proxy v1.27.0/go.mod h1:bn9iHmAjogMoIPkqBGyJ9R1m9cXGCjBE/cuhBs3oEsQ=
github.com/GoogleCloudPlatform/docker-credential-gcr v2.0.5+incompatible/go.mod h1:BB1eHdMLYEFuFdBlRMb0N7YGVdM5s6Pt0njxgvfbGGs=
Expand Down
44 changes: 42 additions & 2 deletions packagers/jam.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package packagers

import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/paketo-buildpacks/packit/v2/fs"
"github.com/paketo-buildpacks/packit/v2/pexec"
Expand All @@ -19,11 +21,15 @@ type Executable interface {
// occam.BuildpackStore.WithPackager().
type Jam struct {
executable Executable
pack Executable
tempOutput func(dir string, pattern string) (string, error)
}

func NewJam() Jam {
return Jam{
executable: pexec.NewExecutable("jam"),
pack: pexec.NewExecutable("pack"),
tempOutput: os.MkdirTemp,
}
}

Expand All @@ -32,7 +38,24 @@ func (j Jam) WithExecutable(executable Executable) Jam {
return j
}

func (j Jam) WithPack(pack Executable) Jam {
j.pack = pack
return j
}

func (j Jam) WithTempOutput(tempOutput func(string, string) (string, error)) Jam {
j.tempOutput = tempOutput
return j
}

func (j Jam) Execute(buildpackDir, output, version string, offline bool) error {
jamOutput, err := j.tempOutput("", "")
if err != nil {
return err
}
defer os.RemoveAll(jamOutput)

buildpackTarballPath := filepath.Join(jamOutput, fmt.Sprintf("%s.tgz", version))

extensionTomlPath := filepath.Join(buildpackDir, "extension.toml")

Expand All @@ -47,15 +70,32 @@ func (j Jam) Execute(buildpackDir, output, version string, offline bool) error {
args := []string{
"pack",
command, filepath.Join(buildpackDir, buildpackOrExtensionToml),
"--output", output,
"--output", buildpackTarballPath,
"--version", version,
}

if offline {
args = append(args, "--offline")
}

return j.executable.Execute(pexec.Execution{
err = j.executable.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
return err
}

args = []string{
"buildpack", "package",
output,
"--path", buildpackTarballPath,
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

return j.pack.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
Expand Down
105 changes: 98 additions & 7 deletions packagers/jam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package packagers_test

import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"testing"

. "github.com/onsi/gomega"
Expand All @@ -16,12 +19,22 @@ func testJam(t *testing.T, context spec.G, it spec.S) {
Expect = NewWithT(t).Expect

executable *fakes.Executable
packager packagers.Jam
pack *fakes.Executable

tempOutput func(string, string) (string, error)

packager packagers.Jam
)

it.Before(func() {
executable = &fakes.Executable{}
packager = packagers.NewJam().WithExecutable(executable)
pack = &fakes.Executable{}

tempOutput = func(string, string) (string, error) {
return "some-jam-output", nil
}

packager = packagers.NewJam().WithExecutable(executable).WithPack(pack).WithTempOutput(tempOutput)

})

Expand All @@ -33,9 +46,17 @@ func testJam(t *testing.T, context spec.G, it spec.S) {
Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"pack",
"--buildpack", filepath.Join("some-buildpack-dir", "buildpack.toml"),
"--output", "some-output",
"--output", filepath.Join("some-jam-output", "some-version.tgz"),
"--version", "some-version",
}))

Expect(pack.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"buildpack", "package",
"some-output",
"--path", filepath.Join("some-jam-output", "some-version.tgz"),
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}))
})

context("when packaging with offline dependencies", func() {
Expand All @@ -46,21 +67,91 @@ func testJam(t *testing.T, context spec.G, it spec.S) {
Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"pack",
"--buildpack", filepath.Join("some-buildpack-dir", "buildpack.toml"),
"--output", "some-output",
"--output", filepath.Join("some-jam-output", "some-version.tgz"),
"--version", "some-version",
"--offline",
}))

Expect(pack.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"buildpack", "package",
"some-output",
"--path", filepath.Join("some-jam-output", "some-version.tgz"),
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}))
})
})

context("when packaging a stack extension", func() {
var extensionDir string

it.Before(func() {
var err error
extensionDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())

_, err = os.Create(filepath.Join(extensionDir, "extension.toml"))
Expect(err).NotTo(HaveOccurred())
})

it.After(func() {
Expect(os.RemoveAll(extensionDir)).To(Succeed())
})

it("creates a correct pexec.Execution", func() {
err := packager.Execute(extensionDir, "some-output", "some-version", true)
Expect(err).NotTo(HaveOccurred())

Expect(executable.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"pack",
"--extension", filepath.Join(extensionDir, "extension.toml"),
"--output", filepath.Join("some-jam-output", "some-version.tgz"),
"--version", "some-version",
"--offline",
}))

Expect(pack.ExecuteCall.Receives.Execution.Args).To(Equal([]string{
"buildpack", "package",
"some-output",
"--path", filepath.Join("some-jam-output", "some-version.tgz"),
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}))
})
})

context("failure cases", func() {
context("when the execution returns an error", func() {
context("when the tempDir creation fails returns an error", func() {
it.Before(func() {
tempOutput = func(string, string) (string, error) {
return "", errors.New("some tempDir error")
}

packager = packager.WithTempOutput(tempOutput)
})
it("returns an error", func() {
err := packager.Execute("some-buildpack-dir", "some-output", "some-version", true)
Expect(err).To(MatchError("some tempDir error"))
})
})

context("when the jam execution returns an error", func() {
it.Before(func() {
executable.ExecuteCall.Returns.Error = errors.New("some jam error")
})
it("returns an error", func() {
err := packager.Execute("some-buildpack-dir", "some-output", "some-version", true)
Expect(err).To(MatchError("some jam error"))
})
})

context("when the pack execution returns an error", func() {
it.Before(func() {
executable.ExecuteCall.Returns.Error = errors.New("some error")
pack.ExecuteCall.Returns.Error = errors.New("some pack error")
})
it("returns an error", func() {
err := packager.Execute("some-buildpack-dir", "some-output", "some-version", true)
Expect(err).To(MatchError("some error"))
Expect(err).To(MatchError("some pack error"))
})
})
})
Expand Down
52 changes: 46 additions & 6 deletions packagers/libpak.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package packagers

import (
"fmt"
"os"
"runtime"

"github.com/paketo-buildpacks/packit/v2/pexec"
)
Expand All @@ -12,33 +14,71 @@ import (
// occam.BuildpackStore.WithPackager().
type Libpak struct {
executable Executable
pack Executable
tempOutput func(dir string, pattern string) (string, error)
}

func NewLibpak() Libpak {
return Libpak{
executable: pexec.NewExecutable("create-package"),
pack: pexec.NewExecutable("pack"),
tempOutput: os.MkdirTemp,
}
}

func (l Libpak) WithExecutable(executable Executable) Libpak {
l.executable = executable
return l
}

func (l Libpak) WithPack(pack Executable) Libpak {
l.pack = pack
return l
}

func (l Libpak) WithTempOutput(tempOutput func(string, string) (string, error)) Libpak {
l.tempOutput = tempOutput
return l
}

func (l Libpak) Execute(buildpackDir, output, version string, cached bool) error {
libpakOutput, err := l.tempOutput("", "")
if err != nil {
return err
}
defer os.RemoveAll(libpakOutput)

args := []string{
"--destination", output,
"--destination", libpakOutput,
"--version", version,
}

if cached {
args = append(args, "--include-dependencies")
}

return l.executable.Execute(pexec.Execution{
err = l.executable.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
Dir: buildpackDir,
})
}

func (l Libpak) WithExecutable(executable Executable) Libpak {
l.executable = executable
return l
if err != nil {
return err
}

args = []string{
"buildpack", "package",
output,
"--path", libpakOutput,
"--format", "file",
"--target", fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}

return l.pack.Execute(pexec.Execution{
Args: args,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
}
Loading

0 comments on commit e9fee75

Please sign in to comment.