mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
6856a6b110
This is part of the ongoing effort to remove the deprecated server/ package, and generally cleanup and simplify the codebase. Signed-off-by: Solomon Hykes <solomon@docker.com>
100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
// DEPRECATION NOTICE. PLEASE DO NOT ADD ANYTHING TO THIS FILE.
|
|
//
|
|
// For additional commments see server/server.go
|
|
//
|
|
package server
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/archive"
|
|
"github.com/docker/docker/builder"
|
|
"github.com/docker/docker/engine"
|
|
"github.com/docker/docker/pkg/parsers"
|
|
"github.com/docker/docker/registry"
|
|
"github.com/docker/docker/utils"
|
|
)
|
|
|
|
func (srv *Server) Build(job *engine.Job) engine.Status {
|
|
if len(job.Args) != 0 {
|
|
return job.Errorf("Usage: %s\n", job.Name)
|
|
}
|
|
var (
|
|
remoteURL = job.Getenv("remote")
|
|
repoName = job.Getenv("t")
|
|
suppressOutput = job.GetenvBool("q")
|
|
noCache = job.GetenvBool("nocache")
|
|
rm = job.GetenvBool("rm")
|
|
forceRm = job.GetenvBool("forcerm")
|
|
authConfig = ®istry.AuthConfig{}
|
|
configFile = ®istry.ConfigFile{}
|
|
tag string
|
|
context io.ReadCloser
|
|
)
|
|
job.GetenvJson("authConfig", authConfig)
|
|
job.GetenvJson("configFile", configFile)
|
|
repoName, tag = parsers.ParseRepositoryTag(repoName)
|
|
|
|
if remoteURL == "" {
|
|
context = ioutil.NopCloser(job.Stdin)
|
|
} else if utils.IsGIT(remoteURL) {
|
|
if !strings.HasPrefix(remoteURL, "git://") {
|
|
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
|
|
} else if utils.IsURL(remoteURL) {
|
|
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)
|
|
}
|
|
c, err := archive.Generate("Dockerfile", string(dockerFile))
|
|
if err != nil {
|
|
return job.Error(err)
|
|
}
|
|
context = c
|
|
}
|
|
defer context.Close()
|
|
|
|
sf := utils.NewStreamFormatter(job.GetenvBool("json"))
|
|
b := builder.NewBuildFile(srv.daemon, srv.Eng,
|
|
&utils.StdoutFormater{
|
|
Writer: job.Stdout,
|
|
StreamFormatter: sf,
|
|
},
|
|
&utils.StderrFormater{
|
|
Writer: job.Stdout,
|
|
StreamFormatter: sf,
|
|
},
|
|
!suppressOutput, !noCache, rm, forceRm, job.Stdout, sf, authConfig, configFile)
|
|
id, err := b.Build(context)
|
|
if err != nil {
|
|
return job.Error(err)
|
|
}
|
|
if repoName != "" {
|
|
srv.daemon.Repositories().Set(repoName, tag, id, false)
|
|
}
|
|
return engine.StatusOK
|
|
}
|