-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathdocker.go
85 lines (75 loc) · 2.15 KB
/
docker.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package provider
import (
"context"
"fmt"
"io/ioutil"
"os"
"get.porter.sh/porter/pkg/build"
portercontext "get.porter.sh/porter/pkg/context"
"get.porter.sh/porter/pkg/manifest"
"github.com/docker/cli/cli/command"
clibuild "github.com/docker/cli/cli/command/image/build"
cliflags "github.com/docker/cli/cli/flags"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/pkg/errors"
)
type DockerBuilder struct {
*portercontext.Context
}
func NewDockerBuilder(cxt *portercontext.Context) *DockerBuilder {
return &DockerBuilder{
Context: cxt,
}
}
func (b *DockerBuilder) BuildInvocationImage(manifest *manifest.Manifest) error {
fmt.Fprintf(b.Out, "\nStarting Invocation Image Build =======> \n")
path, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "could not get current working directory")
}
buildOptions := types.ImageBuildOptions{
SuppressOutput: false,
PullParent: false,
Remove: true,
Tags: []string{manifest.Image},
Dockerfile: "Dockerfile",
BuildArgs: map[string]*string{
"BUNDLE_DIR": &build.BUNDLE_DIR,
},
}
excludes, err := clibuild.ReadDockerignore(path)
if err != nil {
return err
}
tar, err := archive.TarWithOptions(path, &archive.TarOptions{ExcludePatterns: excludes})
if err != nil {
return err
}
cli, err := command.NewDockerCli()
if err != nil {
return errors.Wrap(err, "could not create new docker client")
}
if err := cli.Initialize(cliflags.NewClientOptions()); err != nil {
return err
}
response, err := cli.Client().ImageBuild(context.Background(), tar, buildOptions)
if err != nil {
return err
}
dockerOutput := ioutil.Discard
if b.IsVerbose() {
dockerOutput = b.Out
}
termFd, _ := term.GetFdInfo(dockerOutput)
// Setting this to false here because Moby os.Exit(1) all over the place and this fails on WSL (only)
// when Term is true.
isTerm := false
err = jsonmessage.DisplayJSONMessagesStream(response.Body, dockerOutput, termFd, isTerm, nil)
if err != nil {
return errors.Wrap(err, "failed to stream docker build output")
}
return nil
}