2014-08-11 15:44:31 +00:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2015-01-09 21:07:00 +00:00
|
|
|
"bytes"
|
2015-03-25 07:44:12 +00:00
|
|
|
"fmt"
|
2014-08-11 15:44:31 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-02-05 11:26:05 +00:00
|
|
|
"strings"
|
2015-04-16 18:11:26 +00:00
|
|
|
"sync"
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2014-09-11 14:42:17 +00:00
|
|
|
"github.com/docker/docker/api"
|
2015-01-09 21:07:00 +00:00
|
|
|
"github.com/docker/docker/builder/parser"
|
2015-04-22 12:06:58 +00:00
|
|
|
"github.com/docker/docker/cliconfig"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/daemon"
|
2014-10-02 15:37:13 +00:00
|
|
|
"github.com/docker/docker/graph"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-03-29 21:17:23 +00:00
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-03-18 02:18:41 +00:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2014-11-24 23:47:42 +00:00
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
2014-08-11 15:44:31 +00:00
|
|
|
"github.com/docker/docker/registry"
|
2015-01-09 21:07:00 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-04-23 17:19:34 +00:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-11 15:44:31 +00:00
|
|
|
)
|
|
|
|
|
2015-02-05 11:26:05 +00:00
|
|
|
// whitelist of commands allowed for a commit/import
|
2014-10-24 21:11:00 +00:00
|
|
|
var validCommitCommands = map[string]bool{
|
|
|
|
"entrypoint": true,
|
|
|
|
"cmd": true,
|
|
|
|
"user": true,
|
|
|
|
"workdir": true,
|
|
|
|
"env": true,
|
|
|
|
"volume": true,
|
|
|
|
"expose": true,
|
|
|
|
"onbuild": true,
|
|
|
|
}
|
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
type Config struct {
|
|
|
|
DockerfileName string
|
|
|
|
RemoteURL string
|
|
|
|
RepoName string
|
|
|
|
SuppressOutput bool
|
|
|
|
NoCache bool
|
|
|
|
Remove bool
|
|
|
|
ForceRemove bool
|
|
|
|
Pull bool
|
|
|
|
JSONFormat bool
|
|
|
|
Memory int64
|
|
|
|
MemorySwap int64
|
|
|
|
CpuShares int64
|
2015-04-08 08:58:59 +00:00
|
|
|
CpuPeriod int64
|
2015-04-24 00:54:08 +00:00
|
|
|
CpuQuota int64
|
2015-04-16 18:11:26 +00:00
|
|
|
CpuSetCpus string
|
|
|
|
CpuSetMems string
|
2015-03-26 23:14:31 +00:00
|
|
|
CgroupParent string
|
2015-04-22 12:06:58 +00:00
|
|
|
AuthConfig *cliconfig.AuthConfig
|
|
|
|
ConfigFile *cliconfig.ConfigFile
|
2015-04-16 18:11:26 +00:00
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
Stdout io.Writer
|
|
|
|
Context io.ReadCloser
|
2015-04-16 18:11:26 +00: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 21:26:33 +00:00
|
|
|
func NewBuildConfig() *Config {
|
|
|
|
return &Config{
|
2015-04-22 12:06:58 +00:00
|
|
|
AuthConfig: &cliconfig.AuthConfig{},
|
|
|
|
ConfigFile: &cliconfig.ConfigFile{},
|
2015-04-16 21:26:33 +00:00
|
|
|
cancelled: make(chan struct{}),
|
2015-04-16 18:11:26 +00:00
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 21:23:48 +00:00
|
|
|
func Build(d *daemon.Daemon, buildConfig *Config) error {
|
2014-08-11 15:44:31 +00:00
|
|
|
var (
|
2015-04-16 18:11:26 +00:00
|
|
|
repoName string
|
|
|
|
tag string
|
|
|
|
context io.ReadCloser
|
2014-08-11 15:44:31 +00:00
|
|
|
)
|
2014-09-11 14:42:17 +00:00
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
repoName, tag = parsers.ParseRepositoryTag(buildConfig.RepoName)
|
2014-10-02 15:37:13 +00:00
|
|
|
if repoName != "" {
|
2015-04-16 21:26:33 +00:00
|
|
|
if err := registry.ValidateRepositoryName(repoName); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-02 15:37:13 +00:00
|
|
|
}
|
|
|
|
if len(tag) > 0 {
|
|
|
|
if err := graph.ValidateTagName(tag); err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-10-02 15:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
if buildConfig.RemoteURL == "" {
|
2015-04-16 21:26:33 +00:00
|
|
|
context = ioutil.NopCloser(buildConfig.Context)
|
2015-04-16 18:11:26 +00:00
|
|
|
} else if urlutil.IsGitURL(buildConfig.RemoteURL) {
|
2015-04-23 17:19:34 +00:00
|
|
|
root, err := utils.GitClone(buildConfig.RemoteURL)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
c, err := archive.Tar(root, archive.Uncompressed)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
context = c
|
2015-04-16 18:11:26 +00:00
|
|
|
} else if urlutil.IsURL(buildConfig.RemoteURL) {
|
|
|
|
f, err := httputils.Download(buildConfig.RemoteURL)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
defer f.Body.Close()
|
|
|
|
dockerFile, err := ioutil.ReadAll(f.Body)
|
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-02-17 18:25:36 +00:00
|
|
|
|
|
|
|
// When we're downloading just a Dockerfile put it in
|
|
|
|
// the default name - don't allow the client to move/specify it
|
2015-04-16 18:11:26 +00:00
|
|
|
buildConfig.DockerfileName = api.DefaultDockerfileName
|
2015-02-17 18:25:36 +00:00
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
c, err := archive.Generate(buildConfig.DockerfileName, string(dockerFile))
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
context = c
|
|
|
|
}
|
|
|
|
defer context.Close()
|
|
|
|
|
2015-04-16 18:11:26 +00:00
|
|
|
sf := streamformatter.NewStreamFormatter(buildConfig.JSONFormat)
|
2014-08-11 15:44:31 +00:00
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
builder := &Builder{
|
2015-04-16 21:26:33 +00:00
|
|
|
Daemon: d,
|
2015-03-18 02:18:41 +00:00
|
|
|
OutStream: &streamformatter.StdoutFormater{
|
2015-04-16 18:11:26 +00:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 15:44:31 +00:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2015-03-18 02:18:41 +00:00
|
|
|
ErrStream: &streamformatter.StderrFormater{
|
2015-04-16 18:11:26 +00:00
|
|
|
Writer: buildConfig.Stdout,
|
2014-08-11 15:44:31 +00:00
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
2015-04-16 18:11:26 +00:00
|
|
|
Verbose: !buildConfig.SuppressOutput,
|
|
|
|
UtilizeCache: !buildConfig.NoCache,
|
|
|
|
Remove: buildConfig.Remove,
|
|
|
|
ForceRemove: buildConfig.ForceRemove,
|
|
|
|
Pull: buildConfig.Pull,
|
|
|
|
OutOld: buildConfig.Stdout,
|
2014-08-11 15:44:31 +00:00
|
|
|
StreamFormatter: sf,
|
2015-04-16 18:11:26 +00:00
|
|
|
AuthConfig: buildConfig.AuthConfig,
|
|
|
|
ConfigFile: buildConfig.ConfigFile,
|
|
|
|
dockerfileName: buildConfig.DockerfileName,
|
|
|
|
cpuShares: buildConfig.CpuShares,
|
2015-04-08 08:58:59 +00:00
|
|
|
cpuPeriod: buildConfig.CpuPeriod,
|
2015-04-24 00:54:08 +00:00
|
|
|
cpuQuota: buildConfig.CpuQuota,
|
2015-04-16 18:11:26 +00:00
|
|
|
cpuSetCpus: buildConfig.CpuSetCpus,
|
|
|
|
cpuSetMems: buildConfig.CpuSetMems,
|
2015-03-26 23:14:31 +00:00
|
|
|
cgroupParent: buildConfig.CgroupParent,
|
2015-04-16 18:11:26 +00:00
|
|
|
memory: buildConfig.Memory,
|
|
|
|
memorySwap: buildConfig.MemorySwap,
|
|
|
|
cancelled: buildConfig.WaitCancelled(),
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 19:25:44 +00:00
|
|
|
id, err := builder.Run(context)
|
2014-08-11 15:44:31 +00:00
|
|
|
if err != nil {
|
2015-03-25 07:44:12 +00:00
|
|
|
return err
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if repoName != "" {
|
2015-04-16 21:26:33 +00:00
|
|
|
return d.Repositories().Tag(repoName, tag, id, true)
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-03-25 07:44:12 +00:00
|
|
|
return nil
|
2014-08-11 15:44:31 +00:00
|
|
|
}
|
2015-01-09 21:07:00 +00:00
|
|
|
|
2015-04-21 21:23:48 +00:00
|
|
|
func BuildFromConfig(d *daemon.Daemon, c *runconfig.Config, changes []string) (*runconfig.Config, error) {
|
2015-02-05 11:26:05 +00:00
|
|
|
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
|
2015-01-09 21:07:00 +00:00
|
|
|
if err != nil {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 21:11:00 +00:00
|
|
|
// ensure that the commands are valid
|
|
|
|
for _, n := range ast.Children {
|
|
|
|
if !validCommitCommands[n.Value] {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, fmt.Errorf("%s is not a valid change command", n.Value)
|
2014-10-24 21:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 21:07:00 +00:00
|
|
|
builder := &Builder{
|
2015-04-16 21:26:33 +00:00
|
|
|
Daemon: d,
|
|
|
|
Config: c,
|
2015-01-09 21:07:00 +00:00
|
|
|
OutStream: ioutil.Discard,
|
|
|
|
ErrStream: ioutil.Discard,
|
|
|
|
disableCommit: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, n := range ast.Children {
|
2014-10-24 21:11:00 +00:00
|
|
|
if err := builder.dispatch(i, n); err != nil {
|
2015-04-16 21:26:33 +00:00
|
|
|
return nil, err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 21:26:33 +00:00
|
|
|
return builder.Config, nil
|
|
|
|
}
|
|
|
|
|
2015-04-21 21:23:48 +00:00
|
|
|
func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (string, error) {
|
2015-04-16 21:26:33 +00:00
|
|
|
container, err := d.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|
2015-04-16 21:26:33 +00:00
|
|
|
|
2015-04-21 21:23:48 +00:00
|
|
|
newConfig, err := BuildFromConfig(d, c.Config, c.Changes)
|
2015-04-16 21:26:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := runconfig.Merge(newConfig, container.Config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
img, err := d.Commit(container, c.Repo, c.Tag, c.Comment, c.Author, c.Pause, newConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img.ID, nil
|
2015-01-09 21:07:00 +00:00
|
|
|
}
|