Skip to content
This repository has been archived by the owner on Mar 13, 2021. It is now read-only.

Commit

Permalink
Spec Updates
Browse files Browse the repository at this point in the history
This change updates the library to match upcoming spec changes.  It adds the
Platform to Detect, updates the Detect logger to use stdout, updates Detect's
build plan output to write to <plan>, and updates Build to allow claims on
build plan dependencies via <plan>.

[buildpacks/spec#27]

Signed-off-by: Ben Hale <bhale@pivotal.io>
  • Loading branch information
nebhale committed Nov 30, 2018
1 parent 368a9de commit 2ac60ef
Show file tree
Hide file tree
Showing 17 changed files with 525 additions and 602 deletions.
57 changes: 26 additions & 31 deletions build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,60 @@ package main

import (
"fmt"
"github.com/projectriff/riff-buildpack/command"
"github.com/projectriff/riff-buildpack/node"
"os"

"github.com/cloudfoundry/libjavabuildpack"
"github.com/buildpack/libbuildpack/buildplan"
buildPkg "github.com/cloudfoundry/libcfbuildpack/build"
"github.com/projectriff/riff-buildpack/command"
"github.com/projectriff/riff-buildpack/java"
"github.com/projectriff/riff-buildpack/node"
)

func main() {
build, err := libjavabuildpack.DefaultBuild()
build, err := buildPkg.DefaultBuild()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize Build: %s\n", err.Error())
_, _ = fmt.Fprintf(os.Stderr, "Failed to initialize Build: %s\n", err)
os.Exit(101)
}

build.Logger.FirstLine(build.Logger.PrettyVersion(build.Buildpack))
if code, err := b(build); err != nil {
build.Logger.Info(err.Error())
os.Exit(code)
} else {
os.Exit(code)
}
}

func b(build buildPkg.Build) (int, error) {
build.Logger.FirstLine(build.Logger.PrettyIdentity(build.Buildpack))

if invoker, ok, err := java.NewRiffInvoker(build); err != nil {
build.Logger.Info(err.Error())
build.Failure(102)
return
return build.Failure(102), err
} else if ok {
if err = invoker.Contribute(); err != nil {
build.Logger.Info(err.Error())
build.Failure(103)
return
return build.Failure(103), err
}
build.Success()
return
return build.Success(buildplan.BuildPlan{})
}

if invoker, ok, err := node.NewNodeInvoker(build); err != nil {
build.Logger.Info(err.Error())
build.Failure(105)
return
return build.Failure(105), err
} else if ok {
if err = invoker.Contribute(); err != nil {
build.Logger.Info(err.Error())
build.Failure(106)
return
return build.Failure(106), err
}
build.Success()
return
return build.Success(buildplan.BuildPlan{})
}

if invoker, ok, err := command.NewCommandInvoker(build); err != nil {
build.Logger.Info(err.Error())
build.Failure(102)
return
return build.Failure(102), err
} else if ok {
if err = invoker.Contribute(); err != nil {
build.Logger.Info(err.Error())
build.Failure(103)
return
return build.Failure(103), err
}
build.Success()
return
return build.Success(buildplan.BuildPlan{})
}

build.Logger.Info("Buildpack passed detection but did not know how to actually build. Should never happen.")
build.Failure(104)
return build.Failure(104), nil
}
16 changes: 8 additions & 8 deletions build/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"testing"

"github.com/cloudfoundry/libjavabuildpack/test"
"github.com/cloudfoundry/libcfbuildpack/test"
"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)
Expand All @@ -31,15 +31,15 @@ func TestBuild(t *testing.T) {
func testBuild(t *testing.T, when spec.G, it spec.S) {

it("fails if unsupported type", func() {
f := test.NewEnvironmentFactory(t)
defer f.Restore()
f := test.NewBuildFactory(t)

f.Console.In(t, "")

main()
exitStatus, err := b(f.Build)
if err != nil {
t.Fatal(err)
}

if *f.ExitStatus != 104 {
t.Errorf("os.Exit = %d, expected 104", *f.ExitStatus)
if exitStatus != 104 {
t.Errorf("os.Exit = %d, expected 104", exitStatus)
}
})
}
2 changes: 0 additions & 2 deletions buildpack.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ stacks = [ "io.buildpacks.stacks.bionic" ]
type = "Apache-2.0"
uri = "https://github.com/projectriff/node-function-invoker/blob/master/LICENSE"



[metadata]
pre_package = "ci/build.sh"
include_files = [
Expand Down
10 changes: 6 additions & 4 deletions command/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@
package command

import (
"github.com/cloudfoundry/libjavabuildpack"
"github.com/projectriff/riff-buildpack"
"os"
"path/filepath"

"github.com/cloudfoundry/libcfbuildpack/detect"
"github.com/cloudfoundry/libcfbuildpack/layers"
"github.com/projectriff/riff-buildpack/metadata"
)

func DetectCommand(detect libjavabuildpack.Detect, metadata riff_buildpack.Metadata) (bool, error) {
func DetectCommand(detect detect.Detect, metadata metadata.Metadata) (bool, error) {
if metadata.Artifact == "" {
return false, nil
}

path := filepath.Join(detect.Application.Root, metadata.Artifact)

ok, err := libjavabuildpack.FileExists(path)
ok, err := layers.FileExists(path)
if err != nil || !ok {
return false, err
}
Expand Down
71 changes: 40 additions & 31 deletions command/riff_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ package command

import (
"fmt"
"github.com/buildpack/libbuildpack"
"github.com/cloudfoundry/libjavabuildpack"
"github.com/projectriff/riff-buildpack"
"path/filepath"

"github.com/buildpack/libbuildpack/application"
"github.com/buildpack/libbuildpack/buildplan"
"github.com/cloudfoundry/libcfbuildpack/build"
"github.com/cloudfoundry/libcfbuildpack/layers"
"github.com/projectriff/riff-buildpack/metadata"
)

const (
// RiffCommandInvokerDependency is a key identifying the command invoker dependency in the build plan.
RiffCommandInvokerDependency = "riff-invoker-command"
// Dependency is a key identifying the command invoker dependency in the build plan.
Dependency = "riff-invoker-command"

// command is the key identifying the command executable in the build plan.
Command = "command"
Expand All @@ -39,25 +42,25 @@ const (
// RiffCommandInvoker represents the Command invoker contributed by the buildpack.
type RiffCommandInvoker struct {
// A reference to the user function source tree.
application libbuildpack.Application
application application.Application

// The function executable. Must have exec permissions.
executable string

// Provides access to the launch layers, used to craft the process commands.
launch libjavabuildpack.Launch
layers layers.Layers

// A dedicated layer for the command invoker itself. Cacheable.
invokerLayer libjavabuildpack.DependencyLaunchLayer
invokerLayer layers.DependencyLayer

// A dedicated layer for the function location. Not cacheable, as it changes with the value of executable.
functionLayer libbuildpack.LaunchLayer
functionLayer layers.Layer
}

func BuildPlanContribution(metadata riff_buildpack.Metadata) libbuildpack.BuildPlan {
plans := libbuildpack.BuildPlan{
RiffCommandInvokerDependency: libbuildpack.BuildPlanDependency{
Metadata: libbuildpack.BuildPlanDependencyMetadata{
func BuildPlanContribution(metadata metadata.Metadata) buildplan.BuildPlan {
plans := buildplan.BuildPlan{
Dependency: buildplan.Dependency{
Metadata: buildplan.Metadata{
Command: metadata.Artifact,
},
},
Expand All @@ -67,32 +70,31 @@ func BuildPlanContribution(metadata riff_buildpack.Metadata) libbuildpack.BuildP

// Contribute makes the contribution to the launch layer
func (r RiffCommandInvoker) Contribute() error {
if err := r.invokerLayer.Contribute(func(artifact string, layer libjavabuildpack.DependencyLaunchLayer) error {
if err := r.invokerLayer.Contribute(func(artifact string, layer layers.DependencyLayer) error {
layer.Logger.SubsequentLine("Expanding %s to %s", artifact, layer.Root)
return libjavabuildpack.ExtractTarGz(artifact, layer.Root, 0)
}); err != nil {
return layers.ExtractTarGz(artifact, layer.Root, 0)
}, layers.Cache, layers.Launch); err != nil {
return err
}

functionURI := filepath.Join(r.application.Root, r.executable)
if err := r.functionLayer.WriteProfile("FUNCTION_URI", fmt.Sprintf(`export FUNCTION_URI='%s'`, functionURI)) ; err != nil {
return err
}
if err := r.functionLayer.WriteMetadata(struct{}{}) ; err != nil {
if err := r.functionLayer.Contribute(marker{r.executable}, func(layer layers.Layer) error {
return layer.OverrideLaunchEnv("FUNCTION_URI", filepath.Join(r.application.Root, r.executable))
}, layers.Cache, layers.Launch); err != nil {
return err
}

command := filepath.Join(r.invokerLayer.Root, functionInvokerExecutable)
return r.launch.WriteMetadata(libbuildpack.LaunchMetadata{
Processes: libbuildpack.Processes{
libbuildpack.Process{Type: "web", Command: command},
libbuildpack.Process{Type: "function", Command: command},

return r.layers.WriteMetadata(layers.Metadata{
Processes: layers.Processes{
layers.Process{Type: "web", Command: command},
layers.Process{Type: "function", Command: command},
},
})
}

func NewCommandInvoker(build libjavabuildpack.Build) (RiffCommandInvoker, bool, error) {
bp, ok := build.BuildPlan[RiffCommandInvokerDependency]
func NewCommandInvoker(build build.Build) (RiffCommandInvoker, bool, error) {
bp, ok := build.BuildPlan[Dependency]
if !ok {
return RiffCommandInvoker{}, false, nil
}
Expand All @@ -102,7 +104,7 @@ func NewCommandInvoker(build libjavabuildpack.Build) (RiffCommandInvoker, bool,
return RiffCommandInvoker{}, false, err
}

dep, err := deps.Best(RiffCommandInvokerDependency, bp.Version, build.Stack)
dep, err := deps.Best(Dependency, bp.Version, build.Stack)
if err != nil {
return RiffCommandInvoker{}, false, err
}
Expand All @@ -115,9 +117,16 @@ func NewCommandInvoker(build libjavabuildpack.Build) (RiffCommandInvoker, bool,
return RiffCommandInvoker{
application: build.Application,
executable: exec,
launch: build.Launch,
invokerLayer: build.Launch.DependencyLayer(dep),
functionLayer: build.Launch.Layer("function"),
layers: build.Layers,
invokerLayer: build.Layers.DependencyLayer(dep),
functionLayer: build.Layers.Layer("function"),
}, true, nil
}

type marker struct {
Function string `toml:"function"`
}

func (m marker) Identity() (string, string) {
return "Function URI", ""
}
Loading

0 comments on commit 2ac60ef

Please sign in to comment.