-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerbuild.go
177 lines (153 loc) · 5.09 KB
/
dockerbuild.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package dockerbuild
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
dockertypes "github.com/docker/engine-api/types"
docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/credentialprovider"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/interrupt"
dockerbuilder "github.com/openshift/imagebuilder/dockerclient"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
"github.com/openshift/origin/pkg/cmd/util/clientcmd"
)
const (
dockerbuildLong = `
Build a Dockerfile into a single layer
Builds the provided directory with a Dockerfile into a single layered image.
Requires that you have a working connection to a Docker engine. You may mount
secrets or config into the build with the --mount flag - these files will not
be included in the final image.
Experimental: This command is under active development and may change without notice.`
dockerbuildExample = ` # Build the current directory into a single layer and tag
%[1]s ex dockerbuild . myimage:latest
# Mount a client secret into the build at a certain path
%[1]s ex dockerbuild . myimage:latest --mount ~/mysecret.pem:/etc/pki/secret/mysecret.pem`
)
type DockerbuildOptions struct {
Out io.Writer
Err io.Writer
Client *docker.Client
MountSpecs []string
Mounts []dockerbuilder.Mount
Directory string
Tag string
DockerfilePath string
AllowPull bool
Keyring credentialprovider.DockerKeyring
Arguments cmdutil.Environment
}
func NewCmdDockerbuild(fullName string, f *clientcmd.Factory, out, errOut io.Writer) *cobra.Command {
options := &DockerbuildOptions{
Out: out,
Err: errOut,
}
cmd := &cobra.Command{
Use: "dockerbuild DIRECTORY TAG [--dockerfile=PATH]",
Short: "Perform a direct Docker build",
Long: dockerbuildLong,
Example: fmt.Sprintf(dockerbuildExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
kcmdutil.CheckErr(options.Complete(f, cmd, args))
kcmdutil.CheckErr(options.Validate())
if err := options.Run(); err != nil {
// TODO: move met to kcmdutil
if err == cmdutil.ErrExit {
os.Exit(1)
}
kcmdutil.CheckErr(err)
}
},
}
cmd.Flags().StringSliceVar(&options.MountSpecs, "mount", options.MountSpecs, "An optional list of files and directories to mount during the build. Use SRC:DST syntax for each path.")
cmd.Flags().StringVar(&options.DockerfilePath, "dockerfile", options.DockerfilePath, "An optional path to a Dockerfile to use.")
cmd.Flags().BoolVar(&options.AllowPull, "allow-pull", true, "Pull the images that are not present.")
cmd.MarkFlagFilename("dockerfile")
return cmd
}
func (o *DockerbuildOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
paths, envArgs, ok := cmdutil.SplitEnvironmentFromResources(args)
if !ok {
return kcmdutil.UsageError(cmd, "context directory must be specified before environment changes: %s", strings.Join(args, " "))
}
if len(paths) != 2 {
return kcmdutil.UsageError(cmd, "the directory to build and tag must be specified")
}
o.Arguments, _, _ = cmdutil.ParseEnvironmentArguments(envArgs)
o.Directory = paths[0]
o.Tag = paths[1]
if len(o.DockerfilePath) == 0 {
o.DockerfilePath = filepath.Join(o.Directory, "Dockerfile")
}
var mounts []dockerbuilder.Mount
for _, s := range o.MountSpecs {
segments := strings.Split(s, ":")
if len(segments) != 2 {
return kcmdutil.UsageError(cmd, "--mount must be of the form SOURCE:DEST")
}
mounts = append(mounts, dockerbuilder.Mount{SourcePath: segments[0], DestinationPath: segments[1]})
}
o.Mounts = mounts
client, err := docker.NewClientFromEnv()
if err != nil {
return err
}
o.Client = client
o.Keyring = credentialprovider.NewDockerKeyring()
return nil
}
func (o *DockerbuildOptions) Validate() error {
return nil
}
func (o *DockerbuildOptions) Run() error {
f, err := os.Open(o.DockerfilePath)
if err != nil {
return err
}
defer f.Close()
e := dockerbuilder.NewClientExecutor(o.Client)
e.Out, e.ErrOut = o.Out, o.Err
e.AllowPull = o.AllowPull
e.Directory = o.Directory
e.TransientMounts = o.Mounts
e.Tag = o.Tag
e.AuthFn = func(image string) ([]dockertypes.AuthConfig, bool) {
auth, ok := o.Keyring.Lookup(image)
if !ok {
return nil, false
}
var engineAuth []dockertypes.AuthConfig
for _, c := range auth {
engineAuth = append(engineAuth, c.AuthConfig)
}
return engineAuth, true
}
e.LogFn = func(format string, args ...interface{}) {
if glog.V(2) {
glog.Infof("Builder: "+format, args...)
} else {
fmt.Fprintf(e.ErrOut, "--> %s\n", fmt.Sprintf(format, args...))
}
}
safe := interrupt.New(func(os.Signal) { os.Exit(1) }, func() {
glog.V(5).Infof("invoking cleanup")
if err := e.Cleanup(); err != nil {
fmt.Fprintf(o.Err, "error: Unable to clean up build: %v\n", err)
}
})
return safe.Run(func() error { return stripLeadingError(e.Build(f, o.Arguments)) })
}
func stripLeadingError(err error) error {
if err == nil {
return nil
}
if strings.HasPrefix(err.Error(), "Error: ") {
return fmt.Errorf(strings.TrimPrefix(err.Error(), "Error: "))
}
return err
}