From acdb67bd63d1e5808222fb5f438a3d24e81ed36d Mon Sep 17 00:00:00 2001 From: Xian Chaobo Date: Wed, 8 Jul 2015 18:03:18 +0800 Subject: [PATCH] godeps update Signed-off-by: Xian Chaobo --- Godeps/Godeps.json | 2 +- .../github.com/samalba/dockerclient/auth.go | 23 +++- .../samalba/dockerclient/auth_test.go | 2 +- .../samalba/dockerclient/dockerclient.go | 103 +++++++++++++++--- .../samalba/dockerclient/engine_mock_test.go | 2 +- .../samalba/dockerclient/interface.go | 3 +- .../samalba/dockerclient/mockclient/mock.go | 9 +- .../github.com/samalba/dockerclient/types.go | 22 ++++ 8 files changed, 144 insertions(+), 22 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index b77ee0ed2a..bc9c5f7755 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -105,7 +105,7 @@ }, { "ImportPath": "github.com/samalba/dockerclient", - "Rev": "15ebe064ca62ad0d64834e7ef0d4ed8ce9d02cde" + "Rev": "2bd0abc1edafed71d634778312f534b7798d3a94" }, { "ImportPath": "github.com/samuel/go-zookeeper/zk", diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/auth.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/auth.go index 022d3ddfdb..48f5f90b65 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/auth.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/auth.go @@ -14,8 +14,25 @@ type AuthConfig struct { } // encode the auth configuration struct into base64 for the X-Registry-Auth header -func (c *AuthConfig) encode() string { +func (c *AuthConfig) encode() (string, error) { var buf bytes.Buffer - json.NewEncoder(&buf).Encode(c) - return base64.URLEncoding.EncodeToString(buf.Bytes()) + if err := json.NewEncoder(&buf).Encode(c); err != nil { + return "", err + } + return base64.URLEncoding.EncodeToString(buf.Bytes()), nil +} + +// ConfigFile holds parameters for authenticating during a BuildImage request +type ConfigFile struct { + Configs map[string]AuthConfig `json:"configs,omitempty"` + rootPath string +} + +// encode the configuration struct into base64 for the X-Registry-Config header +func (c *ConfigFile) encode() (string, error) { + var buf bytes.Buffer + if err := json.NewEncoder(&buf).Encode(c); err != nil { + return "", err + } + return base64.URLEncoding.EncodeToString(buf.Bytes()), nil } diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/auth_test.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/auth_test.go index f6eac9926e..99801b2258 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/auth_test.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/auth_test.go @@ -7,7 +7,7 @@ import ( func TestAuthEncode(t *testing.T) { a := AuthConfig{Username: "foo", Password: "password", Email: "bar@baz.com"} expected := "eyJ1c2VybmFtZSI6ImZvbyIsInBhc3N3b3JkIjoicGFzc3dvcmQiLCJlbWFpbCI6ImJhckBiYXouY29tIn0K" - got := a.encode() + got, _ := a.encode() if expected != got { t.Errorf("testAuthEncode failed. Expected [%s] got [%s]", expected, got) diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/dockerclient.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/dockerclient.go index 4fb40a33d2..e96d27468a 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/dockerclient.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/dockerclient.go @@ -235,21 +235,30 @@ func (client *DockerClient) readJSONStream(stream io.ReadCloser, decode func(*js resultChan := make(chan decodingResult) go func() { - decoder := json.NewDecoder(stream) - stopped := make(chan struct{}) + decodeChan := make(chan decodingResult) + go func() { - <-stopChan - stream.Close() - stopped <- struct{}{} + decoder := json.NewDecoder(stream) + for { + decodeResult := decode(decoder) + decodeChan <- decodeResult + if decodeResult.err != nil { + close(decodeChan) + return + } + } }() defer close(resultChan) + for { - decodeResult := decode(decoder) select { - case <-stopped: + case <-stopChan: + stream.Close() + for range decodeChan { + } return - default: + case decodeResult := <-decodeChan: resultChan <- decodeResult if decodeResult.err != nil { stream.Close() @@ -257,6 +266,7 @@ func (client *DockerClient) readJSONStream(stream io.ReadCloser, decode func(*js } } } + }() return resultChan @@ -366,13 +376,17 @@ func (client *DockerClient) StartMonitorEvents(cb Callback, ec chan error, args go func() { eventErrChan, err := client.MonitorEvents(nil, client.eventStopChan) if err != nil { - ec <- err + if ec != nil { + ec <- err + } return } for e := range eventErrChan { if e.Error != nil { - ec <- err + if ec != nil { + ec <- err + } return } cb(&e.Event, ec, args...) @@ -447,7 +461,11 @@ func (client *DockerClient) PullImage(name string, auth *AuthConfig) error { uri := fmt.Sprintf("/%s/images/create?%s", APIVersion, v.Encode()) req, err := http.NewRequest("POST", client.URL.String()+uri, nil) if auth != nil { - req.Header.Add("X-Registry-Auth", auth.encode()) + encoded_auth, err := auth.encode() + if err != nil { + return err + } + req.Header.Add("X-Registry-Auth", encoded_auth) } resp, err := client.HTTPClient.Do(req) if err != nil { @@ -521,8 +539,12 @@ func (client *DockerClient) RemoveContainer(id string, force, volumes bool) erro return err } -func (client *DockerClient) ListImages() ([]*Image, error) { - uri := fmt.Sprintf("/%s/images/json", APIVersion) +func (client *DockerClient) ListImages(all bool) ([]*Image, error) { + argAll := 0 + if all { + argAll = 1 + } + uri := fmt.Sprintf("/%s/images/json?all=%d", APIVersion, argAll) data, err := client.doRequest("GET", uri, nil, nil) if err != nil { return nil, err @@ -615,3 +637,58 @@ func (client *DockerClient) ImportImage(source string, repository string, tag st } return client.doStreamRequest("POST", "/images/create?"+v.Encode(), in, nil) } + +func (client *DockerClient) BuildImage(image *BuildImage) (io.ReadCloser, error) { + v := url.Values{} + + if image.DockerfileName != "" { + v.Set("dockerfile", image.DockerfileName) + } + if image.RepoName != "" { + v.Set("t", image.RepoName) + } + if image.RemoteURL != "" { + v.Set("remote", image.RemoteURL) + } + if image.NoCache { + v.Set("nocache", "1") + } + if image.Pull { + v.Set("pull", "1") + } + if image.Remove { + v.Set("rm", "1") + } else { + v.Set("rm", "0") + } + if image.ForceRemove { + v.Set("forcerm", "1") + } + if image.SuppressOutput { + v.Set("q", "1") + } + + v.Set("memory", strconv.FormatInt(image.Memory, 10)) + v.Set("memswap", strconv.FormatInt(image.MemorySwap, 10)) + v.Set("cpushares", strconv.FormatInt(image.CpuShares, 10)) + v.Set("cpuperiod", strconv.FormatInt(image.CpuPeriod, 10)) + v.Set("cpuquota", strconv.FormatInt(image.CpuQuota, 10)) + v.Set("cpusetcpus", image.CpuSetCpus) + v.Set("cpusetmems", image.CpuSetMems) + v.Set("cgroupparent", image.CgroupParent) + + headers := make(map[string]string) + if image.Config != nil { + encoded_config, err := image.Config.encode() + if err != nil { + return nil, err + } + headers["X-Registry-Config"] = encoded_config + } + if image.Context != nil { + headers["Content-Type"] = "application/tar" + } + + uri := fmt.Sprintf("/%s/build?%s", APIVersion, v.Encode()) + return client.doStreamRequest("POST", uri, image.Context, headers) +} diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/engine_mock_test.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/engine_mock_test.go index 114a71f22d..65d94f4c56 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/engine_mock_test.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/engine_mock_test.go @@ -98,7 +98,7 @@ func handleContainerLogs(w http.ResponseWriter, r *http.Request) { for ; i < 50; i++ { line := fmt.Sprintf("line %d", i) if getBoolValue(r.Form.Get("timestamps")) { - l := &jsonlog.JSONLog{Log: line, Created: time.Now()} + l := &jsonlog.JSONLog{Log: line, Created: time.Now().UTC()} line = fmt.Sprintf("%s %s", l.Created.Format(timeutils.RFC3339NanoFixed), line) } if i%2 == 0 && stderr { diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/interface.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/interface.go index f3d3f14a59..6e8361771f 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/interface.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/interface.go @@ -35,10 +35,11 @@ type Client interface { PullImage(name string, auth *AuthConfig) error LoadImage(reader io.Reader) error RemoveContainer(id string, force, volumes bool) error - ListImages() ([]*Image, error) + ListImages(all bool) ([]*Image, error) RemoveImage(name string) ([]*ImageDelete, error) PauseContainer(name string) error UnpauseContainer(name string) error RenameContainer(oldName string, newName string) error ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error) + BuildImage(image BuildImage) (io.ReadCloser, error) } diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/mockclient/mock.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/mockclient/mock.go index 0facc9cee8..6babeea6f1 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/mockclient/mock.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/mockclient/mock.go @@ -116,8 +116,8 @@ func (client *MockClient) RemoveContainer(id string, force, volumes bool) error return args.Error(0) } -func (client *MockClient) ListImages() ([]*dockerclient.Image, error) { - args := client.Mock.Called() +func (client *MockClient) ListImages(all bool) ([]*dockerclient.Image, error) { + args := client.Mock.Called(all) return args.Get(0).([]*dockerclient.Image), args.Error(1) } @@ -150,3 +150,8 @@ func (client *MockClient) ImportImage(source string, repository string, tag stri args := client.Mock.Called(source, repository, tag, tar) return args.Get(0).(io.ReadCloser), args.Error(1) } + +func (client *MockClient) BuildImage(image *dockerclient.BuildImage) (io.ReadCloser, error) { + args := client.Mock.Called(image) + return args.Get(0).(io.ReadCloser), args.Error(1) +} diff --git a/Godeps/_workspace/src/github.com/samalba/dockerclient/types.go b/Godeps/_workspace/src/github.com/samalba/dockerclient/types.go index 4b30770dec..e1b5527993 100644 --- a/Godeps/_workspace/src/github.com/samalba/dockerclient/types.go +++ b/Godeps/_workspace/src/github.com/samalba/dockerclient/types.go @@ -2,6 +2,7 @@ package dockerclient import ( "fmt" + "io" "time" "github.com/docker/docker/pkg/units" @@ -415,3 +416,24 @@ type LogConfig struct { Type string `json:"type"` Config map[string]string `json:"config"` } + +type BuildImage struct { + Config *ConfigFile + DockerfileName string + Context io.Reader + RemoteURL string + RepoName string + SuppressOutput bool + NoCache bool + Remove bool + ForceRemove bool + Pull bool + Memory int64 + MemorySwap int64 + CpuShares int64 + CpuPeriod int64 + CpuQuota int64 + CpuSetCpus string + CpuSetMems string + CgroupParent string +}