Skip to content

Commit

Permalink
Merge pull request #1230 from aemengo/pack-interact-mode
Browse files Browse the repository at this point in the history
Show detect screen during detect phase
  • Loading branch information
jromero authored Sep 1, 2021
2 parents a259dcb + 8dd4ab5 commit b3f9390
Show file tree
Hide file tree
Showing 23 changed files with 718 additions and 47 deletions.
6 changes: 6 additions & 0 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"
projectTypes "github.com/buildpacks/pack/pkg/project/types"
Expand Down Expand Up @@ -125,6 +126,9 @@ type BuildOptions struct {
// Only trust builders from reputable sources.
TrustBuilder bool

// Launch a terminal UI to depict the build process
Interactive bool

// List of buildpack images or archives to add to a builder.
// These buildpacks may overwrite those on the builder if they
// share both an ID and Version with a buildpack on the builder.
Expand Down Expand Up @@ -329,6 +333,8 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error {
Workspace: opts.Workspace,
GID: opts.GroupID,
PreviousImage: opts.PreviousImage,
Interactive: opts.Interactive,
Termui: termui.NewTermui(),
}

lifecycleVersion := ephemeralBuilder.LifecycleDescriptor().Info.Version
Expand Down
11 changes: 11 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,17 @@ func testBuild(t *testing.T, when spec.G, it spec.S) {
h.AssertEq(t, fakeLifecycle.Opts.PreviousImage, "example.com/some/new:tag")
})
})

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

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/buildpacks/lifecycle v0.11.4
github.com/docker/docker v20.10.8+incompatible
github.com/docker/go-connections v0.4.0
github.com/gdamore/tcell/v2 v2.3.3
github.com/ghodss/yaml v1.0.0
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.6
Expand All @@ -22,6 +23,7 @@ require (
github.com/opencontainers/image-spec v1.0.1
github.com/pelletier/go-toml v1.9.3
github.com/pkg/errors v0.9.1
github.com/rivo/tview v0.0.0-20210624165335-29d673af0ce2
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
42 changes: 39 additions & 3 deletions go.sum

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions internal/build/container_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ func copyDirWindows(ctx context.Context, ctrClient client.CommonAPIClient, conta
return errors.Wrap(err, "copy app to container")
}

return container.Run(
return container.RunWithHandler(
ctx,
ctrClient,
ctr.ID,
ioutil.Discard, // Suppress xcopy output
stderr,
container.DefaultHandler(
ioutil.Discard, // Suppress xcopy output
stderr,
),
)
}

Expand Down Expand Up @@ -268,12 +270,14 @@ func EnsureVolumeAccess(uid, gid int, os string, volumeNames ...string) Containe
}
defer ctrClient.ContainerRemove(context.Background(), ctr.ID, types.ContainerRemoveOptions{Force: true})

return container.Run(
return container.RunWithHandler(
ctx,
ctrClient,
ctr.ID,
ioutil.Discard, // Suppress icacls output
stderr,
container.DefaultHandler(
ioutil.Discard, // Suppress icacls output
stderr,
),
)
}
}
18 changes: 9 additions & 9 deletions internal/build/container_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func testContainerOps(t *testing.T, when spec.G, it spec.S) {
err = copyDirOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -147,7 +147,7 @@ lrwxrwxrwx 1 123 456 (.*) fake-app-symlink -> fake-app-file
err = copyDirOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -196,7 +196,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = copyDirOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -226,7 +226,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = copyDirOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -276,7 +276,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -319,7 +319,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -364,7 +364,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down Expand Up @@ -409,7 +409,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)

err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertEq(t, errBuf.String(), "")
h.AssertNil(t, err)

Expand Down Expand Up @@ -458,7 +458,7 @@ drwsrwsrwt 2 123 456 (.*) some-vol
initVolumeOp := build.EnsureVolumeAccess(123, 456, osType, ctrVolumes[0], ctrVolumes[0])
err = initVolumeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
h.AssertNil(t, err)
err = container.Run(ctx, ctrClient, ctr.ID, &outBuf, &errBuf)
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
h.AssertNil(t, err)

h.AssertEq(t, errBuf.String(), "")
Expand Down
75 changes: 75 additions & 0 deletions internal/build/fakes/fake_termui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package fakes

import (
"io"

"github.com/buildpacks/pack/internal/build"
"github.com/buildpacks/pack/internal/container"
)

type FakeTermui struct {
handler container.Handler
}

func NewFakeTermui(handler container.Handler) *FakeTermui {
return &FakeTermui{
handler: handler,
}
}

func (f *FakeTermui) Run(funk func()) error {
return nil
}

func (f *FakeTermui) Handler() container.Handler {
return f.handler
}

func WithTermui(screen build.Termui) func(*build.LifecycleOptions) {
return func(opts *build.LifecycleOptions) {
opts.Interactive = true
opts.Termui = screen
}
}

func (f *FakeTermui) Debug(msg string) {
// not implemented
}

func (f *FakeTermui) Debugf(fmt string, v ...interface{}) {
// not implemented
}

func (f *FakeTermui) Info(msg string) {
// not implemented
}

func (f *FakeTermui) Infof(fmt string, v ...interface{}) {
// not implemented
}

func (f *FakeTermui) Warn(msg string) {
// not implemented
}

func (f *FakeTermui) Warnf(fmt string, v ...interface{}) {
// not implemented
}

func (f *FakeTermui) Error(msg string) {
// not implemented
}

func (f *FakeTermui) Errorf(fmt string, v ...interface{}) {
// not implemented
}

func (f *FakeTermui) Writer() io.Writer {
// not implemented
return nil
}

func (f *FakeTermui) IsVerbose() bool {
// not implemented
return false
}
4 changes: 4 additions & 0 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func NewLifecycleExecution(logger logging.Logger, docker client.CommonAPIClient,
mountPaths: mountPathsForOS(osType, opts.Workspace),
}

if opts.Interactive {
exec.logger = opts.Termui
}

return exec, nil
}

Expand Down
25 changes: 21 additions & 4 deletions internal/build/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"math/rand"
"time"

"github.com/buildpacks/pack/internal/cache"

"github.com/buildpacks/imgutil"
"github.com/buildpacks/lifecycle/api"
"github.com/buildpacks/lifecycle/platform"
"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/name"

"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/cache"
"github.com/buildpacks/pack/internal/container"
"github.com/buildpacks/pack/logging"
)

Expand Down Expand Up @@ -47,6 +47,13 @@ type Cache interface {
Type() cache.Type
}

type Termui interface {
logging.Logger

Run(funk func()) error
Handler() container.Handler
}

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
Expand All @@ -63,6 +70,8 @@ type LifecycleOptions struct {
Publish bool
TrustBuilder bool
UseCreator bool
Interactive bool
Termui Termui
DockerHost string
CacheImage string
HTTPProxy string
Expand All @@ -87,6 +96,14 @@ func (l *LifecycleExecutor) Execute(ctx context.Context, opts LifecycleOptions)
if err != nil {
return err
}
defer lifecycleExec.Cleanup()
return lifecycleExec.Run(ctx, NewDefaultPhaseFactory)

if !opts.Interactive {
defer lifecycleExec.Cleanup()
return lifecycleExec.Run(ctx, NewDefaultPhaseFactory)
}

return opts.Termui.Run(func() {
defer lifecycleExec.Cleanup()
lifecycleExec.Run(ctx, NewDefaultPhaseFactory)
})
}
11 changes: 8 additions & 3 deletions internal/build/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Phase struct {
infoWriter io.Writer
errorWriter io.Writer
docker client.CommonAPIClient
handler container.Handler
ctrConf *dcontainer.Config
hostConf *dcontainer.HostConfig
ctr dcontainer.ContainerCreateCreatedBody
Expand All @@ -39,12 +40,16 @@ func (p *Phase) Run(ctx context.Context) error {
}
}

return container.Run(
handler := container.DefaultHandler(p.infoWriter, p.errorWriter)
if p.handler != nil {
handler = p.handler
}

return container.RunWithHandler(
ctx,
p.docker,
p.ctr.ID,
p.infoWriter,
p.errorWriter,
handler,
)
}

Expand Down
11 changes: 11 additions & 0 deletions internal/build/phase_config_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/docker/docker/api/types/container"

pcontainer "github.com/buildpacks/pack/internal/container"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/logging"
)
Expand All @@ -28,6 +29,7 @@ type PhaseConfigProvider struct {
containerOps []ContainerOperation
infoWriter io.Writer
errorWriter io.Writer
handler pcontainer.Handler
}

func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops ...PhaseConfigProviderOperation) *PhaseConfigProvider {
Expand Down Expand Up @@ -73,6 +75,11 @@ func NewPhaseConfigProvider(name string, lifecycleExec *LifecycleExecution, ops
lifecycleExec.logger.Debug("Host Settings:")
lifecycleExec.logger.Debugf(" Binds: %s", style.Symbol(strings.Join(provider.hostConf.Binds, " ")))
lifecycleExec.logger.Debugf(" Network Mode: %s", style.Symbol(string(provider.hostConf.NetworkMode)))

if lifecycleExec.opts.Interactive {
provider.handler = lifecycleExec.opts.Termui.Handler()
}

return provider
}

Expand All @@ -88,6 +95,10 @@ func (p *PhaseConfigProvider) HostConfig() *container.HostConfig {
return p.hostConf
}

func (p *PhaseConfigProvider) Handler() pcontainer.Handler {
return p.handler
}

func (p *PhaseConfigProvider) Name() string {
return p.name
}
Expand Down
Loading

0 comments on commit b3f9390

Please sign in to comment.