Skip to content

Commit

Permalink
Show detect Termui screen during detect phase
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Emengo <aemengo@vmware.com>
  • Loading branch information
Anthony Emengo committed Aug 9, 2021
1 parent 66a4f32 commit 81d6203
Show file tree
Hide file tree
Showing 32 changed files with 908 additions and 47 deletions.
25 changes: 25 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,31 @@ include = [ "*.jar", "media/mountain.jpg", "/media/person.png", ]
})
})
})

when("--interactive", func() {
it.Before(func() {
os.Setenv("PACK_SHOW_TERMINAL_UI", "false")
})

it.After(func() {
os.Unsetenv("PACK_SHOW_TERMINAL_UI")
})

it("runs the interactive workflow", func() {
packAgentPath := h.BuildPackAgent(t, imageManager.HostOS())
appPath := filepath.Join("testdata", "mock_app")

output := pack.RunSuccessfully(
"build", repoName,
"-p", appPath,
"--experimental-interactive-agent-path", packAgentPath,
)

assertOutput := assertions.NewOutputAssertionManager(t, output)

assertOutput.ReportsInteractiveOutput()
})
})
})
})

Expand Down
9 changes: 9 additions & 0 deletions acceptance/assertions/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,12 @@ func (o OutputAssertionManager) ReportsInvalidBuilderToml() {

o.assert.Contains(o.output, "invalid builder toml")
}

func (o OutputAssertionManager) ReportsInteractiveOutput() {
o.testObject.Helper()

o.assert.ContainsAll(o.output,
`{"phase":"DETECT","msg":"Starting"`,
`{"phase":"DETECT","msg":"Ending"`,
)
}
12 changes: 10 additions & 2 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/buildpacks/pack/internal/stack"
"github.com/buildpacks/pack/internal/stringset"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/internal/termui"
"github.com/buildpacks/pack/logging"
"github.com/buildpacks/pack/pkg/archive"
"github.com/buildpacks/pack/project"
Expand Down Expand Up @@ -160,6 +161,9 @@ type BuildOptions struct {
// The location at which to mount the AppDir in the build image.
Workspace string

// Path to compiled binary of pack-agent for use in interactive mode
PackAgentPath string

// User's group id used to build the image
GroupID int
}
Expand Down Expand Up @@ -254,7 +258,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
buildEnvs[k] = v
}

ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs)
ephemeralBuilder, err := c.createEphemeralBuilder(rawBuilderImage, buildEnvs, order, fetchedBPs, opts.PackAgentPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,6 +321,8 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
CacheImage: opts.CacheImage,
Workspace: opts.Workspace,
GID: opts.GroupID,
Interactive: opts.PackAgentPath != "",
Termui: termui.NewTermui(),
}

lifecycleVersion := ephemeralBuilder.LifecycleDescriptor().Info.Version
Expand Down Expand Up @@ -810,14 +816,16 @@ func decomposeBuildpack(blob blob.Blob, imageOS string) (mainBP dist.Buildpack,
return mainBP, depBPs, nil
}

func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[string]string, order dist.Order, buildpacks []dist.Buildpack) (*builder.Builder, error) {
func (c *Client) createEphemeralBuilder(rawBuilderImage imgutil.Image, env map[string]string, order dist.Order, buildpacks []dist.Buildpack, packAgentPath string) (*builder.Builder, error) {
origBuilderName := rawBuilderImage.Name()
bldr, err := builder.New(rawBuilderImage, fmt.Sprintf("pack.local/builder/%x:latest", randString(10)))
if err != nil {
return nil, errors.Wrapf(err, "invalid builder %s", style.Symbol(origBuilderName))
}

bldr.SetEnv(env)
bldr.SetPackAgentPath(packAgentPath)

for _, bp := range buildpacks {
bpInfo := bp.Descriptor().Info
c.logger.Debugf("Adding buildpack %s version %s to builder", style.Symbol(bpInfo.ID), style.Symbol(bpInfo.Version))
Expand Down
24 changes: 24 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,30 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, fakeLifecycle.Opts.RunImage, "10.0.0.1/default/run:latest")
})
})

when("interactive option", func() {
var fakePackAgentPath string

it.Before(func() {
f, err := ioutil.TempFile("", "pack.build.test.agent.")
h.AssertNil(t, err)

fakePackAgentPath = f.Name()
})

it.After(func() {
os.RemoveAll(fakePackAgentPath)
})

it("passthroughs to lifecycle", func() {
h.AssertNil(t, subject.Build(context.TODO(), BuildOptions{
Builder: defaultBuilderName,
Image: "example.com/some/repo:tag",
PackAgentPath: fakePackAgentPath,
}))
h.AssertEq(t, fakeLifecycle.Opts.Interactive, true)
})
})
})
}

Expand Down
51 changes: 51 additions & 0 deletions cmd/pack-agent/fixtures/fake_lifecycle_build.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
===> DETECTING
3 of 6 buildpacks participating
paketo-buildpacks/ca-certificates 2.3.2
paketo-buildpacks/go-dist 0.5.2
paketo-buildpacks/go-build 0.4.0
===> ANALYZING
Previous image with name "test" not found
===> RESTORING
===> BUILDING

Paketo CA Certificates Buildpack 2.3.2
https://github.com/paketo-buildpacks/ca-certificates
Launch Helper: Contributing to layer
Creating /layers/paketo-buildpacks_ca-certificates/helper/exec.d/ca-certificates-helper
Paketo Go Distribution Buildpack 0.5.2
Resolving Go version
Candidate version sources (in priority order):
<unknown> -> ""

Selected Go version (using <unknown>): 1.16.6

Executing build process
Installing Go 1.16.6
Completed in 6.422s

Paketo Go Build Buildpack 0.4.0
Executing build process
Running 'go build -o /layers/paketo-buildpacks_go-build/targets/bin -buildmode pie .'
Completed in 6.21s

Assigning launch processes:
web: /layers/paketo-buildpacks_go-build/targets/bin/workspace
workspace: /layers/paketo-buildpacks_go-build/targets/bin/workspace

===> EXPORTING
Adding layer 'paketo-buildpacks/ca-certificates:helper'
Adding layer 'paketo-buildpacks/go-build:targets'
Adding 1/1 app layer(s)
Adding layer 'launcher'
Adding layer 'config'
Adding layer 'process-types'
Adding label 'io.buildpacks.lifecycle.metadata'
Adding label 'io.buildpacks.build.metadata'
Adding label 'io.buildpacks.project.metadata'
Setting default process type 'web'
Saving test...
*** Images (00a70b0025d8):
test
Adding cache layer 'paketo-buildpacks/go-dist:go'
Adding cache layer 'paketo-buildpacks/go-build:gocache'
Successfully built image 'test'
78 changes: 78 additions & 0 deletions cmd/pack-agent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"

"github.com/buildpacks/pack/internal/termui"
)

func main() {
command := exec.Command(os.Args[1], os.Args[2:]...)

reader, err := start(command)
if err != nil {
panic(err)
}

err = command.Start()
if err != nil {
panic(err)
}

err = handleOutput(reader)
if err != nil {
panic(err)
}

err = command.Wait()
if err != nil {
panic(err)
}

log(termui.NewEvent("EXPORT", "Ending"))
os.Exit(command.ProcessState.ExitCode())
}

func log(evt termui.Event) {
data, _ := json.Marshal(evt)
fmt.Println(string(data))
}

func handleOutput(reader io.Reader) error {
scanner := bufio.NewScanner(reader)

for scanner.Scan() {
text := scanner.Text()

switch {
case strings.Contains(text, "===> DETECTING"):
log(termui.NewEvent("DETECT", "Starting"))
case strings.Contains(text, "===> ANALYZING"):
log(termui.NewEvent("DETECT", "Ending"))
log(termui.NewEvent("ANALYZE", "Starting"))
}
}

return scanner.Err()
}

func start(command *exec.Cmd) (io.Reader, error) {
stdout, err := command.StdoutPipe()
if err != nil {
return nil, err
}

stderr, err := command.StderrPipe()
if err != nil {
return nil, err
}

reader := io.MultiReader(stdout, stderr)
return reader, nil
}
45 changes: 45 additions & 0 deletions cmd/pack-agent/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main_test

import (
"os/exec"
"path/filepath"
"runtime"
"testing"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"

h "github.com/buildpacks/pack/testhelpers"
)

func TestPackAgent(t *testing.T) {
binaryPath := h.BuildPackAgent(t)

spec.Run(t, "PackAgent", func(t *testing.T, g spec.G, s spec.S) {
testPackAgent(t, g, s, binaryPath)
}, spec.Report(report.Terminal{}))
}

func testPackAgent(t *testing.T, when spec.G, it spec.S, packAgentPath string) {
var (
assert = h.NewAssertionManager(t)
)

when("PackAgent", func() {
it("translates lifecycle output to JSON", func() {
args := []string{"cat", filepath.Join("fixtures", "fake_lifecycle_build.txt")}
if runtime.GOOS == "windows" {
args = append([]string{"powershell.exe", "-Command"}, args...)
}

command := exec.Command(packAgentPath, args...)
output, err := command.CombinedOutput()
assert.NilWithMessage(err, string(output))

assert.ContainsAll(string(output),
`{"phase":"DETECT","msg":"Starting"`,
`{"phase":"DETECT","msg":"Ending"`,
)
})
})
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/containerd/continuity v0.0.0-20200107194136-26c1120b8d41 // indirect
github.com/docker/docker v20.10.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/gdamore/tcell/v2 v2.3.3 // indirect
github.com/ghodss/yaml v1.0.0
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.6
Expand All @@ -27,6 +28,7 @@ require (
github.com/opencontainers/selinux v1.6.0 // indirect
github.com/pelletier/go-toml v1.9.3
github.com/pkg/errors v0.9.1
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20201211074657-223ce5d391b0
github.com/sclevine/spec v1.4.0
github.com/sergi/go-diff v1.1.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.3.3 h1:RKoI6OcqYrr/Do8yHZklecdGzDTJH9ACKdfECbRdw3M=
github.com/gdamore/tcell/v2 v2.3.3/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -341,6 +345,9 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mailru/easyjson v0.0.0-20160728113105-d5b7844b561a/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand All @@ -357,6 +364,8 @@ github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
Expand Down Expand Up @@ -446,6 +455,11 @@ github.com/prometheus/procfs v0.0.0-20180125133057-cb4147076ac7/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2 h1:I5N0WNMgPSq5NKUFspB4jMJ6n2P0ipz5FlOlB4BXviQ=
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2/go.mod h1:IxQujbYMAh4trWr0Dwa8jfciForjVmxyHpskZX6aydQ=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.1.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down Expand Up @@ -726,6 +740,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -734,6 +749,9 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
Loading

0 comments on commit 81d6203

Please sign in to comment.