2014-08-11 11:44:31 -04:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2015-01-09 16:07:00 -05:00
|
|
|
"bytes"
|
2015-04-04 10:54:43 -04:00
|
|
|
"errors"
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-08-11 11:44:31 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-05 06:26:05 -05:00
|
|
|
"strings"
|
2015-04-16 14:11:26 -04:00
|
|
|
"sync"
|
2014-08-11 11:44:31 -04:00
|
|
|
|
2014-09-11 10:42:17 -04:00
|
|
|
"github.com/docker/docker/api"
|
2015-01-09 16:07:00 -05:00
|
|
|
"github.com/docker/docker/builder/parser"
|
2015-04-22 08:06:58 -04:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
2015-05-11 13:23:32 -04:00
|
|
|
"github.com/docker/docker/graph/tags"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-04-04 10:54:43 -04:00
|
|
|
"github.com/docker/docker/pkg/progressreader"
|
2015-03-17 22:18:41 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2014-11-24 18:47:42 -05:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/registry"
|
2015-01-09 16:07:00 -05:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-04-23 13:19:34 -04:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-11 11:44:31 -04:00
|
|
|
)
|
|
|
|
|
2015-04-04 10:54:43 -04:00
|
|
|
// When downloading remote contexts, limit the amount (in bytes)
|
|
|
|
// to be read from the response body in order to detect its Content-Type
|
|
|
|
const maxPreambleLength = 100
|
|
|
|
|
2015-02-05 06:26:05 -05:00
|
|
|
// whitelist of commands allowed for a commit/import
|
2014-10-24 17:11:00 -04:00
|
|
|
var validCommitCommands = map[string]bool{
|
|
|
|
"entrypoint": true,
|
|
|
|
"cmd": true,
|
|
|
|
"user": true,
|
|
|
|
"workdir": true,
|
|
|
|
"env": true,
|
|
|
|
"volume": true,
|
|
|
|
"expose": true,
|
|
|
|
"onbuild": true,
|
2015-06-04 09:47:55 -04:00
|
|
|
"label": true,
|
|
|
|
"maintainer": true,
|
2014-10-24 17:11:00 -04:00
|
|
|
}
|
|
|
|
|
2015-04-16 14:11:26 -04:00
|
|
|
type Config struct {
|
|
|
|
DockerfileName string
|
|
|
|
RemoteURL string
|
|
|
|
RepoName string
|
|
|
|
SuppressOutput bool
|
|
|
|
NoCache bool
|
|
|
|
Remove bool
|
|
|
|
ForceRemove bool
|
|
|
|
Pull bool
|
|
|
|
Memory int64
|
|
|
|
MemorySwap int64
|
|
|
|
CpuShares int64
|
2015-04-08 04:58:59 -04:00
|
|
|
CpuPeriod int64
|
2015-04-23 20:54:08 -04:00
|
|
|
CpuQuota int64
|
2015-04-16 14:11:26 -04:00
|
|
|
CpuSetCpus string
|
|
|
|
CpuSetMems string
|
2015-03-26 19:14:31 -04:00
|
|
|
CgroupParent string
|
2015-06-23 15:58:17 -04:00
|
|
|
AuthConfigs map[string]cliconfig.AuthConfig
|
2015-04-16 14:11:26 -04:00
|
|
|
|
2015-04-16 17:26:33 -04:00
|
|
|
Stdout io.Writer
|
|
|
|
Context io.ReadCloser
|
2015-04-16 14:11:26 -04:00
|
|
|
// When closed, the job has been cancelled.
|
|
|
|
// Note: not all jobs implement cancellation.
|
|
|
|
// See Job.Cancel() and Job.WaitCancelled()
|
|
|
|
cancelled chan struct{}
|
|
|
|
cancelOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
// When called, causes the Job.WaitCancelled channel to unblock.
|
|
|
|
func (b *Config) Cancel() {
|
|
|
|
b.cancelOnce.Do(func() {
|
|
|
|
close(b.cancelled)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a channel which is closed ("never blocks") when the job is cancelled.
|
|
|
|
func (b *Config) WaitCancelled() <-chan struct{} {
|
|
|
|
return b.cancelled
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:26:33 -04:00
|
|
|
func NewBuildConfig() *Config {
|
|
|
|
return &Config{
|
2015-06-23 15:58:17 -04:00
|
|
|
AuthConfigs: map[string]cliconfig.AuthConfig{},
|
|
|
|
cancelled: make(chan struct{}),
|
2015-04-16 14:11:26 -04:00
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:23:48 -04:00
|
|
|
func Build(d *daemon.Daemon, buildConfig *Config) error {
|
2014-08-11 11:44:31 -04:00
|
|
|
var (
|
2015-04-16 14:11:26 -04:00
|
|
|
repoName string
|
|
|
|
tag string
|
|
|
|
context io.ReadCloser
|
2014-08-11 11:44:31 -04:00
|
|
|
)
|
2015-04-04 10:54:43 -04:00
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
2014-09-11 10:42:17 -04:00
|
|
|
|
2015-04-16 14:11:26 -04:00
|
|
|
repoName, tag = parsers.ParseRepositoryTag(buildConfig.RepoName)
|
2014-10-02 11:37:13 -04:00
|
|
|
if repoName != "" {
|
2015-04-16 17:26:33 -04:00
|
|
|
if err := registry.ValidateRepositoryName(repoName); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-10-02 11:37:13 -04:00
|
|
|
}
|
|
|
|
if len(tag) > 0 {
|
2015-05-11 13:23:32 -04:00
|
|
|
if err := tags.ValidateTagName(tag); err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-10-02 11:37:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
|
2015-04-16 14:11:26 -04:00
|
|
|
if buildConfig.RemoteURL == "" {
|
2015-04-16 17:26:33 -04:00
|
|
|
context = ioutil.NopCloser(buildConfig.Context)
|
2015-04-16 14:11:26 -04:00
|
|
|
} else if urlutil.IsGitURL(buildConfig.RemoteURL) {
|
2015-04-23 13:19:34 -04:00
|
|
|
root, err := utils.GitClone(buildConfig.RemoteURL)
|
2014-08-11 11:44:31 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
c, err := archive.Tar(root, archive.Uncompressed)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
context = c
|
2015-04-16 14:11:26 -04:00
|
|
|
} else if urlutil.IsURL(buildConfig.RemoteURL) {
|
|
|
|
f, err := httputils.Download(buildConfig.RemoteURL)
|
2014-08-11 11:44:31 -04:00
|
|
|
if err != nil {
|
2015-04-04 10:54:43 -04:00
|
|
|
return fmt.Errorf("Error downloading remote context %s: %v", buildConfig.RemoteURL, err)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
defer f.Body.Close()
|
2015-04-04 10:54:43 -04:00
|
|
|
ct := f.Header.Get("Content-Type")
|
|
|
|
clen := int(f.ContentLength)
|
|
|
|
contentType, bodyReader, err := inspectResponse(ct, f.Body, clen)
|
|
|
|
|
|
|
|
defer bodyReader.Close()
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
if err != nil {
|
2015-04-04 10:54:43 -04:00
|
|
|
return fmt.Errorf("Error detecting content type for remote %s: %v", buildConfig.RemoteURL, err)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
2015-04-04 10:54:43 -04:00
|
|
|
if contentType == httputils.MimeTypes.TextPlain {
|
|
|
|
dockerFile, err := ioutil.ReadAll(bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-17 13:25:36 -05:00
|
|
|
|
2015-04-04 10:54:43 -04:00
|
|
|
// When we're downloading just a Dockerfile put it in
|
|
|
|
// the default name - don't allow the client to move/specify it
|
|
|
|
buildConfig.DockerfileName = api.DefaultDockerfileName
|
2015-02-17 13:25:36 -05:00
|
|
|
|
2015-04-04 10:54:43 -04:00
|
|
|
c, err := archive.Generate(buildConfig.DockerfileName, string(dockerFile))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
context = c
|
|
|
|
} else {
|
|
|
|
// Pass through - this is a pre-packaged context, presumably
|
|
|
|
// with a Dockerfile with the right name inside it.
|
|
|
|
prCfg := progressreader.Config{
|
|
|
|
In: bodyReader,
|
|
|
|
Out: buildConfig.Stdout,
|
|
|
|
Formatter: sf,
|
|
|
|
Size: clen,
|
|
|
|
NewLines: true,
|
|
|
|
ID: "Downloading context",
|
|
|
|
Action: buildConfig.RemoteURL,
|
|
|
|
}
|
|
|
|
context = progressreader.New(prCfg)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 10:54:43 -04:00
|
|
|
defer context.Close()
|
2014-08-11 11:44:31 -04:00
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
builder := &Builder{
|
2015-04-16 17:26:33 -04:00
|
|
|
Daemon: d,
|
2015-03-17 22:18:41 -04:00
|
|
|
OutStream: &streamformatter.StdoutFormater{
|
2015-04-16 14:11:26 -04:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 11:44:31 -04:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2015-03-17 22:18:41 -04:00
|
|
|
ErrStream: &streamformatter.StderrFormater{
|
2015-04-16 14:11:26 -04:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 11:44:31 -04:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2015-04-16 14:11:26 -04:00
|
|
|
Verbose: !buildConfig.SuppressOutput,
|
|
|
|
UtilizeCache: !buildConfig.NoCache,
|
|
|
|
Remove: buildConfig.Remove,
|
|
|
|
ForceRemove: buildConfig.ForceRemove,
|
|
|
|
Pull: buildConfig.Pull,
|
|
|
|
OutOld: buildConfig.Stdout,
|
2014-08-11 11:44:31 -04:00
|
|
|
StreamFormatter: sf,
|
2015-06-23 15:58:17 -04:00
|
|
|
AuthConfigs: buildConfig.AuthConfigs,
|
2015-04-16 14:11:26 -04:00
|
|
|
dockerfileName: buildConfig.DockerfileName,
|
|
|
|
cpuShares: buildConfig.CpuShares,
|
2015-04-08 04:58:59 -04:00
|
|
|
cpuPeriod: buildConfig.CpuPeriod,
|
2015-04-23 20:54:08 -04:00
|
|
|
cpuQuota: buildConfig.CpuQuota,
|
2015-04-16 14:11:26 -04:00
|
|
|
cpuSetCpus: buildConfig.CpuSetCpus,
|
|
|
|
cpuSetMems: buildConfig.CpuSetMems,
|
2015-03-26 19:14:31 -04:00
|
|
|
cgroupParent: buildConfig.CgroupParent,
|
2015-04-16 14:11:26 -04:00
|
|
|
memory: buildConfig.Memory,
|
|
|
|
memorySwap: buildConfig.MemorySwap,
|
|
|
|
cancelled: buildConfig.WaitCancelled(),
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
id, err := builder.Run(context)
|
2014-08-11 11:44:31 -04:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if repoName != "" {
|
2015-04-16 17:26:33 -04:00
|
|
|
return d.Repositories().Tag(repoName, tag, id, true)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
2015-01-09 16:07:00 -05:00
|
|
|
|
2015-04-21 17:23:48 -04:00
|
|
|
func BuildFromConfig(d *daemon.Daemon, c *runconfig.Config, changes []string) (*runconfig.Config, error) {
|
2015-02-05 06:26:05 -05:00
|
|
|
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
|
2015-01-09 16:07:00 -05:00
|
|
|
if err != nil {
|
2015-04-16 17:26:33 -04:00
|
|
|
return nil, err
|
2015-01-09 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
2014-10-24 17:11:00 -04:00
|
|
|
// ensure that the commands are valid
|
|
|
|
for _, n := range ast.Children {
|
|
|
|
if !validCommitCommands[n.Value] {
|
2015-04-16 17:26:33 -04:00
|
|
|
return nil, fmt.Errorf("%s is not a valid change command", n.Value)
|
2014-10-24 17:11:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:07:00 -05:00
|
|
|
builder := &Builder{
|
2015-04-16 17:26:33 -04:00
|
|
|
Daemon: d,
|
|
|
|
Config: c,
|
2015-01-09 16:07:00 -05:00
|
|
|
OutStream: ioutil.Discard,
|
|
|
|
ErrStream: ioutil.Discard,
|
|
|
|
disableCommit: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := range ast.Children {
|
2014-10-24 17:11:00 -04:00
|
|
|
if err := builder.dispatch(i, n); err != nil {
|
2015-04-16 17:26:33 -04:00
|
|
|
return nil, err
|
2015-01-09 16:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:26:33 -04:00
|
|
|
return builder.Config, nil
|
|
|
|
}
|
|
|
|
|
2015-06-20 06:40:37 -04:00
|
|
|
type BuilderCommitConfig struct {
|
|
|
|
Pause bool
|
|
|
|
Repo string
|
|
|
|
Tag string
|
|
|
|
Author string
|
|
|
|
Comment string
|
|
|
|
Changes []string
|
|
|
|
Config *runconfig.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func Commit(name string, d *daemon.Daemon, c *BuilderCommitConfig) (string, error) {
|
2015-04-16 17:26:33 -04:00
|
|
|
container, err := d.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-01-09 16:07:00 -05:00
|
|
|
}
|
2015-04-16 17:26:33 -04:00
|
|
|
|
2015-06-06 12:41:42 -04:00
|
|
|
if c.Config == nil {
|
|
|
|
c.Config = &runconfig.Config{}
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:23:48 -04:00
|
|
|
newConfig, err := BuildFromConfig(d, c.Config, c.Changes)
|
2015-04-16 17:26:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runconfig.Merge(newConfig, container.Config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-06-20 06:40:37 -04:00
|
|
|
commitCfg := &daemon.ContainerCommitConfig{
|
|
|
|
Pause: c.Pause,
|
|
|
|
Repo: c.Repo,
|
|
|
|
Tag: c.Tag,
|
|
|
|
Author: c.Author,
|
|
|
|
Comment: c.Comment,
|
|
|
|
Config: newConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := d.Commit(container, commitCfg)
|
2015-04-16 17:26:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img.ID, nil
|
2015-01-09 16:07:00 -05:00
|
|
|
}
|
2015-04-04 10:54:43 -04:00
|
|
|
|
|
|
|
// inspectResponse looks into the http response data at r to determine whether its
|
|
|
|
// content-type is on the list of acceptable content types for remote build contexts.
|
|
|
|
// This function returns:
|
|
|
|
// - a string representation of the detected content-type
|
|
|
|
// - an io.Reader for the response body
|
|
|
|
// - an error value which will be non-nil either when something goes wrong while
|
|
|
|
// reading bytes from r or when the detected content-type is not acceptable.
|
|
|
|
func inspectResponse(ct string, r io.ReadCloser, clen int) (string, io.ReadCloser, error) {
|
|
|
|
plen := clen
|
|
|
|
if plen <= 0 || plen > maxPreambleLength {
|
|
|
|
plen = maxPreambleLength
|
|
|
|
}
|
|
|
|
|
|
|
|
preamble := make([]byte, plen, plen)
|
|
|
|
rlen, err := r.Read(preamble)
|
|
|
|
if rlen == 0 {
|
|
|
|
return ct, r, errors.New("Empty response")
|
|
|
|
}
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return ct, r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
preambleR := bytes.NewReader(preamble)
|
|
|
|
bodyReader := ioutil.NopCloser(io.MultiReader(preambleR, r))
|
|
|
|
// Some web servers will use application/octet-stream as the default
|
|
|
|
// content type for files without an extension (e.g. 'Dockerfile')
|
|
|
|
// so if we receive this value we better check for text content
|
|
|
|
contentType := ct
|
|
|
|
if len(ct) == 0 || ct == httputils.MimeTypes.OctetStream {
|
|
|
|
contentType, _, err = httputils.DetectContentType(preamble)
|
|
|
|
if err != nil {
|
|
|
|
return contentType, bodyReader, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType = selectAcceptableMIME(contentType)
|
|
|
|
var cterr error
|
|
|
|
if len(contentType) == 0 {
|
|
|
|
cterr = fmt.Errorf("unsupported Content-Type %q", ct)
|
|
|
|
contentType = ct
|
|
|
|
}
|
|
|
|
|
|
|
|
return contentType, bodyReader, cterr
|
|
|
|
}
|