Skip to content

Commit

Permalink
issue(#15): Add PlanEntryResolver handle
Browse files Browse the repository at this point in the history
  • Loading branch information
lispking committed Jan 22, 2024
1 parent 47fa100 commit c615e37
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 77 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ A Cloud Native Buildpack for Leo
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `$BP_LEO_VERSION` | Configure the version of Leo to install. It can be a specific version or a wildcard like `1.*`. It defaults to the latest `1.*` version. |
| `$BP_LEO_LIBC` | Configure the libc implementation used by the installed toolchain. Available options: `gnu` or `musl`. Defaults to `gnu` for compatiblity. You do not need to set this option with the Paketo full/base/tiny/static stacks. It can be used for compatibility with more exotic or custom stacks. |
| `$BP_ENABLE_LEO_PROCESS` | Configure the Leo launch process, default: `false`. Set to `true` means execute the `leo run` command. |

## Usage

Expand Down
6 changes: 0 additions & 6 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ api = "0.8"
include-files = ["LICENSE", "README.md", "bin/build", "bin/detect", "bin/main", "buildpack.toml"]
pre-package = "scripts/build.sh"

[[metadata.configurations]]
build = true
default = "false"
description = "enable the Leo run process"
name = "BP_ENABLE_LEO_PROCESS"

[[metadata.configurations]]
build = true
default = "1.10.0"
Expand Down
56 changes: 28 additions & 28 deletions leo/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,40 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
b.Logger.Title(context.Buildpack)
result := libcnb.NewBuildResult()

cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}
pr := libpak.PlanEntryResolver{Plan: context.Plan}

dc, err := libpak.NewDependencyCache(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err)
}
dc.Logger = b.Logger
if _, ok, err := pr.Resolve(PlanEntryLeo); err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve Leo plan entry\n%w", err)
} else if ok {
cr, err := libpak.NewConfigurationResolver(context.Buildpack, &b.Logger)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}

dr, err := libpak.NewDependencyResolver(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency resolver\n%w", err)
}
dc, err := libpak.NewDependencyCache(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency cache\n%w", err)
}
dc.Logger = b.Logger

// install leo
v, _ := cr.Resolve("BP_LEO_VERSION")
libc, _ := cr.Resolve("BP_LEO_LIBC")
dr, err := libpak.NewDependencyResolver(context)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create dependency resolver\n%w", err)
}

leoDependency, err := dr.Resolve(fmt.Sprintf("leo-%s", libc), v)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}
// install leo
v, _ := cr.Resolve("BP_LEO_VERSION")
libc, _ := cr.Resolve("BP_LEO_LIBC")

leoLayer := NewLeo(leoDependency, dc)
leoLayer.Logger = b.Logger
leoDependency, err := dr.Resolve(fmt.Sprintf("%s-%s", PlanEntryLeo, libc), v)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}

enableProcess, _ := cr.Resolve("BP_ENABLE_LEO_PROCESS")
result.Processes, err = leoLayer.BuildProcessTypes(enableProcess)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to build list of process types\n%w", err)
}
result.Layers = append(result.Layers, leoLayer)
leoLayer := NewLeo(leoDependency, dc)
leoLayer.Logger = b.Logger

result.Layers = append(result.Layers, leoLayer)
}
return result, nil
}
3 changes: 3 additions & 0 deletions leo/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error
Provides: []libcnb.BuildPlanProvide{
{Name: PlanEntryLeo},
},
Requires: []libcnb.BuildPlanRequire{
{Name: PlanEntryLeo},
},
},
},
}, nil
Expand Down
48 changes: 6 additions & 42 deletions leo/leo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/crush"
"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/sbom"
"github.com/paketo-buildpacks/libpak/sherpa"
)

Expand Down Expand Up @@ -59,7 +58,7 @@ func (r Leo) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
return libcnb.Layer{}, fmt.Errorf("unable to expand %s\n%w", artifact.Name(), err)
}

file := filepath.Join(bin, "leo")
file := filepath.Join(bin, PlanEntryLeo)
r.Logger.Bodyf("Setting %s as executable", file)
if err := os.Chmod(file, 0755); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to chmod %s\n%w", file, err)
Expand All @@ -71,40 +70,19 @@ func (r Leo) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
}

// compile contract
r.Logger.Bodyf("Compiling contracts by %s build", file)
if _, err := r.Execute(file, []string{"build"}); err != nil {
r.Logger.Bodyf("Compiling contracts by '%s build'", PlanEntryLeo)
if _, err := r.Execute(PlanEntryLeo, []string{"build"}); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to compile contract\n%w", err)
}

// get version
buf, err := r.Execute(file, []string{"-V"})
buf, err := r.Execute(PlanEntryLeo, []string{"-V"})
if err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to get %s version: %s\n%w", file, buf.String(), err)
return libcnb.Layer{}, fmt.Errorf("unable to get %s version: %s\n%w", PlanEntryLeo, buf.String(), err)
}
ver := strings.Split(strings.TrimSpace(buf.String()), " ")
r.Logger.Bodyf("Checking %s version: %s", file, ver[1])
r.Logger.Bodyf("Checking %s version: %s", PlanEntryLeo, ver[1])

// generate SBOM
sbomPath := layer.SBOMPath(libcnb.SyftJSON)
dep := sbom.NewSyftDependency(layer.Path, []sbom.SyftArtifact{
{
ID: "leo",
Name: "Leo",
Version: ver[1],
Type: "UnknownPackage",
FoundBy: "amp-buildpacks/leo-dist",
Locations: []sbom.SyftLocation{
{Path: "amp-buildpacks/leo-dist/leo/leo.go"},
},
Licenses: []string{"GNU"},
CPEs: []string{fmt.Sprintf("cpe:2.3:a:leo:leo:%s:*:*:*:*:*:*:*", ver[1])},
PURL: fmt.Sprintf("pkg:generic/leo-dist@%s", ver[1]),
},
})
r.Logger.Debugf("Writing Syft SBOM at %s: %+v", sbomPath, dep)
if err := dep.WriteTo(sbomPath); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to write SBOM\n%w", err)
}
return layer, nil
})
}
Expand All @@ -122,20 +100,6 @@ func (r Leo) Execute(command string, args []string) (*bytes.Buffer, error) {
return buf, nil
}

func (r Leo) BuildProcessTypes(enableProcess string) ([]libcnb.Process, error) {
processes := []libcnb.Process{}

if enableProcess == "true" {
processes = append(processes, libcnb.Process{
Type: "web",
Command: "leo",
Arguments: []string{"run"},
Default: true,
})
}
return processes, nil
}

func (r Leo) Name() string {
return r.LayerContributor.LayerName()
}

0 comments on commit c615e37

Please sign in to comment.