2014-08-11 11:44:31 -04:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2015-01-09 16:07:00 -05:00
|
|
|
"bytes"
|
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-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-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-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-04-22 08:06:58 -04:00
|
|
|
AuthConfig *cliconfig.AuthConfig
|
|
|
|
ConfigFile *cliconfig.ConfigFile
|
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-04-22 08:06:58 -04:00
|
|
|
AuthConfig: &cliconfig.AuthConfig{},
|
|
|
|
ConfigFile: &cliconfig.ConfigFile{},
|
2015-04-16 17:26:33 -04:00
|
|
|
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
|
|
|
)
|
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-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
defer f.Body.Close()
|
|
|
|
dockerFile, err := ioutil.ReadAll(f.Body)
|
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
2015-02-17 13:25:36 -05: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 14:11:26 -04:00
|
|
|
buildConfig.DockerfileName = api.DefaultDockerfileName
|
2015-02-17 13:25:36 -05:00
|
|
|
|
2015-04-16 14:11:26 -04:00
|
|
|
c, err := archive.Generate(buildConfig.DockerfileName, string(dockerFile))
|
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
|
|
|
}
|
|
|
|
context = c
|
|
|
|
}
|
|
|
|
defer context.Close()
|
|
|
|
|
2015-05-12 14:18:54 -04:00
|
|
|
sf := streamformatter.NewJSONStreamFormatter()
|
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-04-16 14:11:26 -04:00
|
|
|
AuthConfig: buildConfig.AuthConfig,
|
|
|
|
ConfigFile: buildConfig.ConfigFile,
|
|
|
|
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-04-21 17:23:48 -04:00
|
|
|
func Commit(d *daemon.Daemon, name string, c *daemon.ContainerCommitConfig) (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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 16:07:00 -05:00
|
|
|
}
|