-
Notifications
You must be signed in to change notification settings - Fork 279
/
phase.go
69 lines (58 loc) · 1.67 KB
/
phase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package build
import (
"context"
"io"
"github.com/docker/docker/api/types"
dcontainer "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/buildpacks/pack/internal/container"
)
type Phase struct {
name string
infoWriter io.Writer
errorWriter io.Writer
docker client.CommonAPIClient
handler container.Handler
ctrConf *dcontainer.Config
hostConf *dcontainer.HostConfig
ctr dcontainer.ContainerCreateCreatedBody
uid, gid int
appPath string
containerOps []ContainerOperation
postContainerRunOps []ContainerOperation
fileFilter func(string) bool
}
func (p *Phase) Run(ctx context.Context) error {
var err error
p.ctr, err = p.docker.ContainerCreate(ctx, p.ctrConf, p.hostConf, nil, nil, "")
if err != nil {
return errors.Wrapf(err, "failed to create '%s' container", p.name)
}
for _, containerOp := range p.containerOps {
if err := containerOp(p.docker, ctx, p.ctr.ID, p.infoWriter, p.errorWriter); err != nil {
return err
}
}
handler := container.DefaultHandler(p.infoWriter, p.errorWriter)
if p.handler != nil {
handler = p.handler
}
err = container.RunWithHandler(
ctx,
p.docker,
p.ctr.ID,
handler)
if err != nil {
return err
}
for _, containerOp := range p.postContainerRunOps {
if err := containerOp(p.docker, ctx, p.ctr.ID, p.infoWriter, p.errorWriter); err != nil {
return err
}
}
return nil
}
func (p *Phase) Cleanup() error {
return p.docker.ContainerRemove(context.Background(), p.ctr.ID, types.ContainerRemoveOptions{Force: true})
}