godep: update dockerclient.

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2015-05-06 22:52:30 -07:00
parent f49fc12ff3
commit 1a82c9ce7d
6 changed files with 96 additions and 13 deletions

2
Godeps/Godeps.json generated
View File

@ -60,7 +60,7 @@
}, },
{ {
"ImportPath": "github.com/samalba/dockerclient", "ImportPath": "github.com/samalba/dockerclient",
"Rev": "8b9427265e82fddde577648b986fbe78d816b333" "Rev": "5e5020b90dd4657c33d446356556481182d2d66b"
}, },
{ {
"ImportPath": "github.com/samuel/go-zookeeper/zk", "ImportPath": "github.com/samuel/go-zookeeper/zk",

View File

@ -4,7 +4,9 @@ Docker client library in Go
Well maintained docker client library. Well maintained docker client library.
Example: # How to use it?
Here is an example showing how to use it:
```go ```go
package main package main
@ -25,7 +27,7 @@ func main() {
docker, _ := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil) docker, _ := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
// Get only running containers // Get only running containers
containers, err := docker.ListContainers(false) containers, err := docker.ListContainers(false, false, "")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -41,14 +43,19 @@ func main() {
} }
// Create a container // Create a container
containerConfig := &dockerclient.ContainerConfig{Image: "ubuntu:12.04", Cmd: []string{"bash"}} containerConfig := &dockerclient.ContainerConfig{
containerId, err := docker.CreateContainer(containerConfig) Image: "ubuntu:14.04",
Cmd: []string{"bash"},
AttachStdin: true,
Tty: true}
containerId, err := docker.CreateContainer(containerConfig, "foobar")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// Start the container // Start the container
err = docker.StartContainer(containerId) hostConfig := &dockerclient.HostConfig{}
err = docker.StartContainer(containerId, hostConfig)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -58,6 +65,19 @@ func main() {
// Listen to events // Listen to events
docker.StartMonitorEvents(eventCallback, nil) docker.StartMonitorEvents(eventCallback, nil)
// Hold the execution to look at the events coming
time.Sleep(3600 * time.Second) time.Sleep(3600 * time.Second)
} }
``` ```
# Maintainers
List of people you can ping for feedback on Pull Requests or any questions.
- [Sam Alba](https://github.com/samalba)
- [Michael Crosby](https://github.com/crosbymichael)
- [Andrea Luzzardi](https://github.com/aluzzardi)
- [Victor Vieux](https://github.com/vieux)
- [Evan Hazlett](https://github.com/ehazlett)
- [Donald Huang](https://github.com/donhcd)

View File

@ -66,7 +66,25 @@ func NewDockerClientTimeout(daemonUrl string, tlsConfig *tls.Config, timeout tim
func (client *DockerClient) doRequest(method string, path string, body []byte, headers map[string]string) ([]byte, error) { func (client *DockerClient) doRequest(method string, path string, body []byte, headers map[string]string) ([]byte, error) {
b := bytes.NewBuffer(body) b := bytes.NewBuffer(body)
req, err := http.NewRequest(method, client.URL.String()+path, b)
reader, err := client.doStreamRequest(method, path, b, headers)
if err != nil {
return nil, err
}
defer reader.Close()
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return data, nil
}
func (client *DockerClient) doStreamRequest(method string, path string, in io.Reader, headers map[string]string) (io.ReadCloser, error) {
if (method == "POST" || method == "PUT") && in == nil {
in = bytes.NewReader(nil)
}
req, err := http.NewRequest(method, client.URL.String()+path, in)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -83,18 +101,19 @@ func (client *DockerClient) doRequest(method string, path string, body []byte, h
} }
return nil, err return nil, err
} }
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
return nil, ErrNotFound return nil, ErrNotFound
} }
if resp.StatusCode >= 400 { if resp.StatusCode >= 400 {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, Error{StatusCode: resp.StatusCode, Status: resp.Status, msg: string(data)} return nil, Error{StatusCode: resp.StatusCode, Status: resp.Status, msg: string(data)}
} }
return data, nil
return resp.Body, nil
} }
func (client *DockerClient) Info() (*Info, error) { func (client *DockerClient) Info() (*Info, error) {
@ -464,3 +483,25 @@ func (client *DockerClient) RenameContainer(oldName string, newName string) erro
_, err := client.doRequest("POST", uri, nil, nil) _, err := client.doRequest("POST", uri, nil, nil)
return err return err
} }
func (client *DockerClient) ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error) {
var fromSrc string
v := &url.Values{}
if source == "" {
fromSrc = "-"
} else {
fromSrc = source
}
v.Set("fromSrc", fromSrc)
v.Set("repo", repository)
if tag != "" {
v.Set("tag", tag)
}
var in io.Reader
if fromSrc == "-" {
in = tar
}
return client.doStreamRequest("POST", "/images/create?"+v.Encode(), in, nil)
}

View File

@ -34,4 +34,5 @@ type Client interface {
PauseContainer(name string) error PauseContainer(name string) error
UnpauseContainer(name string) error UnpauseContainer(name string) error
RenameContainer(oldName string, newName string) error RenameContainer(oldName string, newName string) error
ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error)
} }

View File

@ -135,3 +135,8 @@ func (client *MockClient) RenameContainer(oldName string, newName string) error
args := client.Mock.Called(oldName, newName) args := client.Mock.Called(oldName, newName)
return args.Error(0) return args.Error(0)
} }
func (client *MockClient) ImportImage(source string, repository string, tag string, tar io.Reader) (io.ReadCloser, error) {
args := client.Mock.Called(source, repository, tag, tar)
return args.Get(0).(io.ReadCloser), args.Error(1)
}

View File

@ -46,6 +46,8 @@ type HostConfig struct {
SecurityOpt []string SecurityOpt []string
NetworkMode string NetworkMode string
RestartPolicy RestartPolicy RestartPolicy RestartPolicy
Ulimits []Ulimit
LogConfig LogConfig
} }
type ExecConfig struct { type ExecConfig struct {
@ -88,8 +90,11 @@ type ContainerInfo struct {
Running bool Running bool
Paused bool Paused bool
Restarting bool Restarting bool
OOMKilled bool
Dead bool
Pid int Pid int
ExitCode int ExitCode int
Error string // contains last known error when starting the container
StartedAt time.Time StartedAt time.Time
FinishedAt time.Time FinishedAt time.Time
Ghost bool Ghost bool
@ -256,3 +261,14 @@ type Stats struct {
MemoryStats MemoryStats `json:"memory_stats,omitempty"` MemoryStats MemoryStats `json:"memory_stats,omitempty"`
BlkioStats BlkioStats `json:"blkio_stats,omitempty"` BlkioStats BlkioStats `json:"blkio_stats,omitempty"`
} }
type Ulimit struct {
Name string `json:"name"`
Soft uint64 `json:"soft"`
Hard uint64 `json:"hard"`
}
type LogConfig struct {
Type string `json:"type"`
Config map[string]string `json:"config"`
}