mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Move the docker build URL form client to server, prepare for GIT support
This commit is contained in:
parent
01f446e908
commit
a11e61677c
3 changed files with 53 additions and 27 deletions
47
api.go
47
api.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/dotcloud/docker/utils"
|
"github.com/dotcloud/docker/utils"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -674,31 +675,51 @@ func postBuild(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
|
||||||
if err := r.ParseMultipartForm(4096); err != nil {
|
if err := r.ParseMultipartForm(4096); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
remote := r.FormValue("t")
|
remoteURL := r.FormValue("remote")
|
||||||
|
repoName := r.FormValue("t")
|
||||||
tag := ""
|
tag := ""
|
||||||
if strings.Contains(remote, ":") {
|
if strings.Contains(repoName, ":") {
|
||||||
remoteParts := strings.Split(remote, ":")
|
remoteParts := strings.Split(repoName, ":")
|
||||||
tag = remoteParts[1]
|
tag = remoteParts[1]
|
||||||
remote = remoteParts[0]
|
repoName = remoteParts[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
dockerfile, _, err := r.FormFile("Dockerfile")
|
var dockerfile, context io.ReadCloser
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
context, _, err := r.FormFile("Context")
|
if remoteURL == "" {
|
||||||
if err != nil {
|
d, _, err := r.FormFile("Dockerfile")
|
||||||
if err != http.ErrMissingFile {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
} else {
|
||||||
|
dockerfile = d
|
||||||
|
}
|
||||||
|
c, _, err := r.FormFile("Context")
|
||||||
|
if err != nil {
|
||||||
|
if err != http.ErrMissingFile {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
context = c
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if utils.IsGIT(remoteURL) {
|
||||||
|
return fmt.Errorf("Builder from git is not yet supported")
|
||||||
|
} else if utils.IsURL(remoteURL) {
|
||||||
|
f, err := utils.Download(remoteURL, ioutil.Discard)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
dockerfile = f.Body
|
||||||
|
}
|
||||||
|
defer f.Body.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b := NewBuildFile(srv, utils.NewWriteFlusher(w))
|
b := NewBuildFile(srv, utils.NewWriteFlusher(w))
|
||||||
if id, err := b.Build(dockerfile, context); err != nil {
|
if id, err := b.Build(dockerfile, context); err != nil {
|
||||||
fmt.Fprintf(w, "Error build: %s\n", err)
|
fmt.Fprintf(w, "Error build: %s\n", err)
|
||||||
} else if remote != "" {
|
} else if repoName != "" {
|
||||||
srv.runtime.repositories.Set(remote, tag, id, false)
|
srv.runtime.repositories.Set(repoName, tag, id, false)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
29
commands.go
29
commands.go
|
@ -155,15 +155,12 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
||||||
|
|
||||||
compression := Bzip2
|
compression := Bzip2
|
||||||
|
|
||||||
|
isRemote := false
|
||||||
|
|
||||||
if cmd.Arg(0) == "-" {
|
if cmd.Arg(0) == "-" {
|
||||||
file = os.Stdin
|
file = os.Stdin
|
||||||
} else if utils.IsURL(cmd.Arg(0)) {
|
} else if utils.IsURL(cmd.Arg(0)) || utils.IsGIT(cmd.Arg(0)) {
|
||||||
f, err := utils.Download(cmd.Arg(0), ioutil.Discard)
|
isRemote = true
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Body.Close()
|
|
||||||
file = f.Body
|
|
||||||
} else {
|
} else {
|
||||||
// Send Dockerfile from arg/Dockerfile (deprecate later)
|
// Send Dockerfile from arg/Dockerfile (deprecate later)
|
||||||
f, err := os.Open(path.Join(cmd.Arg(0), "Dockerfile"))
|
f, err := os.Open(path.Join(cmd.Arg(0), "Dockerfile"))
|
||||||
|
@ -192,16 +189,20 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
||||||
io.Copy(wField, utils.ProgressReader(ioutil.NopCloser(context), -1, os.Stdout, sf.FormatProgress("Caching Context", "%v/%v (%v)"), sf))
|
io.Copy(wField, utils.ProgressReader(ioutil.NopCloser(context), -1, os.Stdout, sf.FormatProgress("Caching Context", "%v/%v (%v)"), sf))
|
||||||
multipartBody = io.MultiReader(multipartBody, boundary)
|
multipartBody = io.MultiReader(multipartBody, boundary)
|
||||||
}
|
}
|
||||||
// Create a FormFile multipart for the Dockerfile
|
if !isRemote {
|
||||||
wField, err := w.CreateFormFile("Dockerfile", "Dockerfile")
|
// Create a FormFile multipart for the Dockerfile
|
||||||
if err != nil {
|
wField, err := w.CreateFormFile("Dockerfile", "Dockerfile")
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
io.Copy(wField, file)
|
||||||
|
multipartBody = io.MultiReader(multipartBody, boundary)
|
||||||
}
|
}
|
||||||
io.Copy(wField, file)
|
|
||||||
multipartBody = io.MultiReader(multipartBody, boundary)
|
|
||||||
|
|
||||||
v := &url.Values{}
|
v := &url.Values{}
|
||||||
v.Set("t", *tag)
|
v.Set("t", *tag)
|
||||||
|
if isRemote {
|
||||||
|
v.Set("remote", cmd.Arg(0))
|
||||||
|
}
|
||||||
// Send the multipart request with correct content-type
|
// Send the multipart request with correct content-type
|
||||||
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s:%d%s?%s", cli.host, cli.port, "/build", v.Encode()), multipartBody)
|
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s:%d%s?%s", cli.host, cli.port, "/build", v.Encode()), multipartBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -624,3 +624,7 @@ func (sf *StreamFormatter) Used() bool {
|
||||||
func IsURL(str string) bool {
|
func IsURL(str string) bool {
|
||||||
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
|
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsGIT(str string) bool {
|
||||||
|
return strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "github.com/")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue