2014-08-11 11:44:31 -04:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
2015-01-09 16:07:00 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-08-11 11:44:31 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-02-05 06:26:05 -05:00
|
|
|
"strings"
|
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"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/daemon"
|
|
|
|
"github.com/docker/docker/engine"
|
2014-10-02 11:37:13 -04:00
|
|
|
"github.com/docker/docker/graph"
|
2014-09-30 02:23:36 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
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"
|
2014-08-11 11:44:31 -04:00
|
|
|
"github.com/docker/docker/utils"
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
type BuilderJob struct {
|
|
|
|
Engine *engine.Engine
|
|
|
|
Daemon *daemon.Daemon
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuilderJob) Install() {
|
|
|
|
b.Engine.Register("build", b.CmdBuild)
|
2015-01-09 16:07:00 -05:00
|
|
|
b.Engine.Register("build_config", b.CmdBuildConfig)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuilderJob) CmdBuild(job *engine.Job) engine.Status {
|
|
|
|
if len(job.Args) != 0 {
|
|
|
|
return job.Errorf("Usage: %s\n", job.Name)
|
|
|
|
}
|
|
|
|
var (
|
2014-09-11 10:42:17 -04:00
|
|
|
dockerfileName = job.Getenv("dockerfile")
|
2014-08-11 11:44:31 -04:00
|
|
|
remoteURL = job.Getenv("remote")
|
|
|
|
repoName = job.Getenv("t")
|
|
|
|
suppressOutput = job.GetenvBool("q")
|
|
|
|
noCache = job.GetenvBool("nocache")
|
|
|
|
rm = job.GetenvBool("rm")
|
|
|
|
forceRm = job.GetenvBool("forcerm")
|
2014-11-21 12:51:32 -05:00
|
|
|
pull = job.GetenvBool("pull")
|
2014-08-11 11:44:31 -04:00
|
|
|
authConfig = ®istry.AuthConfig{}
|
|
|
|
configFile = ®istry.ConfigFile{}
|
|
|
|
tag string
|
|
|
|
context io.ReadCloser
|
|
|
|
)
|
2014-09-11 10:42:17 -04:00
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
job.GetenvJson("authConfig", authConfig)
|
|
|
|
job.GetenvJson("configFile", configFile)
|
2014-10-02 11:37:13 -04:00
|
|
|
|
2014-08-11 11:44:31 -04:00
|
|
|
repoName, tag = parsers.ParseRepositoryTag(repoName)
|
2014-10-02 11:37:13 -04:00
|
|
|
if repoName != "" {
|
2014-10-06 21:54:52 -04:00
|
|
|
if err := registry.ValidateRepositoryName(repoName); err != nil {
|
2014-10-02 11:37:13 -04:00
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
if len(tag) > 0 {
|
|
|
|
if err := graph.ValidateTagName(tag); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 11:44:31 -04:00
|
|
|
|
|
|
|
if remoteURL == "" {
|
|
|
|
context = ioutil.NopCloser(job.Stdin)
|
2014-11-24 18:47:42 -05:00
|
|
|
} else if urlutil.IsGitURL(remoteURL) {
|
|
|
|
if !urlutil.IsGitTransport(remoteURL) {
|
2014-08-11 11:44:31 -04:00
|
|
|
remoteURL = "https://" + remoteURL
|
|
|
|
}
|
|
|
|
root, err := ioutil.TempDir("", "docker-build-git")
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
|
|
|
if output, err := exec.Command("git", "clone", "--recursive", remoteURL, root).CombinedOutput(); err != nil {
|
|
|
|
return job.Errorf("Error trying to use git: %s (%s)", err, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := archive.Tar(root, archive.Uncompressed)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
context = c
|
2014-11-24 18:47:42 -05:00
|
|
|
} else if urlutil.IsURL(remoteURL) {
|
2014-08-11 11:44:31 -04:00
|
|
|
f, err := utils.Download(remoteURL)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
defer f.Body.Close()
|
|
|
|
dockerFile, err := ioutil.ReadAll(f.Body)
|
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
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
|
|
|
|
dockerfileName = api.DefaultDockerfileName
|
|
|
|
|
2014-09-11 10:42:17 -04:00
|
|
|
c, err := archive.Generate(dockerfileName, string(dockerFile))
|
2014-08-11 11:44:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
context = c
|
|
|
|
}
|
|
|
|
defer context.Close()
|
|
|
|
|
|
|
|
sf := utils.NewStreamFormatter(job.GetenvBool("json"))
|
|
|
|
|
2014-08-26 15:25:44 -04:00
|
|
|
builder := &Builder{
|
2014-08-11 11:44:31 -04:00
|
|
|
Daemon: b.Daemon,
|
|
|
|
Engine: b.Engine,
|
|
|
|
OutStream: &utils.StdoutFormater{
|
|
|
|
Writer: job.Stdout,
|
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
|
|
|
ErrStream: &utils.StderrFormater{
|
|
|
|
Writer: job.Stdout,
|
|
|
|
StreamFormatter: sf,
|
|
|
|
},
|
|
|
|
Verbose: !suppressOutput,
|
|
|
|
UtilizeCache: !noCache,
|
|
|
|
Remove: rm,
|
|
|
|
ForceRemove: forceRm,
|
2014-11-21 12:51:32 -05:00
|
|
|
Pull: pull,
|
2014-08-11 11:44:31 -04:00
|
|
|
OutOld: job.Stdout,
|
|
|
|
StreamFormatter: sf,
|
|
|
|
AuthConfig: authConfig,
|
|
|
|
AuthConfigFile: configFile,
|
2014-09-11 10:42:17 -04:00
|
|
|
dockerfileName: dockerfileName,
|
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 {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if repoName != "" {
|
2014-09-30 04:57:17 -04:00
|
|
|
b.Daemon.Repositories().Set(repoName, tag, id, true)
|
2014-08-11 11:44:31 -04:00
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|
2015-01-09 16:07:00 -05:00
|
|
|
|
|
|
|
func (b *BuilderJob) CmdBuildConfig(job *engine.Job) engine.Status {
|
|
|
|
if len(job.Args) != 0 {
|
|
|
|
return job.Errorf("Usage: %s\n", job.Name)
|
|
|
|
}
|
|
|
|
|
2014-10-24 17:11:00 -04:00
|
|
|
var (
|
2015-02-05 06:26:05 -05:00
|
|
|
changes = job.GetenvList("changes")
|
2015-01-09 16:07:00 -05:00
|
|
|
newConfig runconfig.Config
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := job.GetenvJson("config", &newConfig); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-24 17:11:00 -04:00
|
|
|
// ensure that the commands are valid
|
|
|
|
for _, n := range ast.Children {
|
|
|
|
if !validCommitCommands[n.Value] {
|
|
|
|
return job.Errorf("%s is not a valid change command", n.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 16:07:00 -05:00
|
|
|
builder := &Builder{
|
|
|
|
Daemon: b.Daemon,
|
|
|
|
Engine: b.Engine,
|
|
|
|
Config: &newConfig,
|
|
|
|
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 {
|
|
|
|
return job.Error(err)
|
2015-01-09 16:07:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewEncoder(job.Stdout).Encode(builder.Config); err != nil {
|
|
|
|
return job.Error(err)
|
|
|
|
}
|
|
|
|
return engine.StatusOK
|
|
|
|
}
|