Port client lib from samalba/dockerclient to docker/docker/client

pull/26/head
Ross Cadogan 8 years ago
parent 25f1fee8e2
commit 99ed959155

@ -1,13 +1,14 @@
package container package container
import ( import (
"crypto/tls"
"fmt" "fmt"
"os" "os"
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient" dockerclient "github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"golang.org/x/net/context"
) )
const ( const (
@ -17,6 +18,7 @@ const (
var username = os.Getenv("REPO_USER") var username = os.Getenv("REPO_USER")
var password = os.Getenv("REPO_PASS") var password = os.Getenv("REPO_PASS")
var email = os.Getenv("REPO_EMAIL") var email = os.Getenv("REPO_EMAIL")
var api_version = "1.24"
// A Filter is a prototype for a function that can be used to filter the // A Filter is a prototype for a function that can be used to filter the
// results from a call to the ListContainers() method on the Client. // results from a call to the ListContainers() method on the Client.
@ -31,48 +33,57 @@ type Client interface {
RenameContainer(Container, string) error RenameContainer(Container, string) error
IsContainerStale(Container) (bool, error) IsContainerStale(Container) (bool, error)
RemoveImage(Container) error RemoveImage(Container) error
RegistryLogin(string) error
} }
// NewClient returns a new Client instance which can be used to interact with // NewClient returns a new Client instance which can be used to interact with
// the Docker API. // the Docker API.
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client { func NewClient(dockerHost string, pullImages bool) Client {
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig) cli, err := dockerclient.NewClient(dockerHost, api_version, nil, nil)
if err != nil { if err != nil {
log.Fatalf("Error instantiating Docker client: %s", err) log.Fatalf("Error instantiating Docker client: %s", err)
} }
return dockerClient{api: docker, pullImages: pullImages} return dockerClient{api: cli, pullImages: pullImages}
} }
type dockerClient struct { type dockerClient struct {
api dockerclient.Client api *dockerclient.Client
pullImages bool pullImages bool
} }
func (client dockerClient) RegistryLogin(registryURL string) error {
log.Debug("Login not implemented")
return nil
}
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) { func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
cs := []Container{} cs := []Container{}
bg := context.Background()
log.Debug("Retrieving running containers") log.Debug("Retrieving running containers")
runningContainers, err := client.api.ListContainers(false, false, "") runningContainers, err := client.api.ContainerList(
bg,
types.ContainerListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, runningContainer := range runningContainers { for _, runningContainer := range runningContainers {
containerInfo, err := client.api.InspectContainer(runningContainer.Id) containerInfo, err := client.api.ContainerInspect(bg, runningContainer.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
imageInfo, err := client.api.InspectImage(containerInfo.Image) imageInfo, _, err := client.api.ImageInspectWithRaw(bg, containerInfo.Image)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := Container{containerInfo: containerInfo, imageInfo: imageInfo} c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo}
if fn(c) { if (fn(c)) {
cs = append(cs, c) cs = append(cs, c)
} }
} }
@ -81,6 +92,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
} }
func (client dockerClient) StopContainer(c Container, timeout time.Duration) error { func (client dockerClient) StopContainer(c Container, timeout time.Duration) error {
bg := context.Background()
signal := c.StopSignal() signal := c.StopSignal()
if signal == "" { if signal == "" {
signal = defaultStopSignal signal = defaultStopSignal
@ -88,7 +100,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal) log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
if err := client.api.KillContainer(c.ID(), signal); err != nil { if err := client.api.ContainerKill(bg, c.ID(), signal); err != nil {
return err return err
} }
@ -97,7 +109,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
log.Debugf("Removing container %s", c.ID()) log.Debugf("Removing container %s", c.ID())
if err := client.api.RemoveContainer(c.ID(), true, false); err != nil { if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: false}); err != nil {
return err return err
} }
@ -110,72 +122,55 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
} }
func (client dockerClient) StartContainer(c Container) error { func (client dockerClient) StartContainer(c Container) error {
bg := context.Background();
config := c.runtimeConfig() config := c.runtimeConfig()
hostConfig := c.hostConfig() hostConfig := c.hostConfig()
name := c.Name() name := c.Name()
log.Infof("Starting %s", name) log.Infof("Starting %s", name)
creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name)
var err error
var newContainerID string
if username != "" && password != "" && email != "" {
auth := dockerclient.AuthConfig{
Username: username,
Password: password,
Email: email,
}
newContainerID, err = client.api.CreateContainer(config, name, &auth)
} else {
newContainerID, err = client.api.CreateContainer(config, name, nil)
}
if err != nil { if err != nil {
return err return err
} }
log.Debugf("Starting container %s (%s)", name, newContainerID) log.Debugf("Starting container %s (%s)", name, creation.ID)
return client.api.StartContainer(newContainerID, hostConfig) return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{})
} }
func (client dockerClient) RenameContainer(c Container, newName string) error { func (client dockerClient) RenameContainer(c Container, newName string) error {
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName) log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName)
return client.api.RenameContainer(c.ID(), newName) //return client.api.ContainerRename(c.ID(), newName)
// no op
return nil
} }
func (client dockerClient) IsContainerStale(c Container) (bool, error) { func (client dockerClient) IsContainerStale(c Container) (bool, error) {
bg := context.Background()
oldImageInfo := c.imageInfo oldImageInfo := c.imageInfo
imageName := c.ImageName() imageName := c.ImageName()
if client.pullImages { if client.pullImages {
log.Debugf("Pulling %s for %s", imageName, c.Name()) log.Debugf("Pulling %s for %s", imageName, c.Name())
if username != "" && password != "" && email != "" { // Note: ImagePullOptions below can take a RegistryAuth arg if 401 on private registry
auth := dockerclient.AuthConfig{ closer, err := client.api.ImagePull(bg, imageName, types.ImagePullOptions{})
Username: username, if (err != nil) {
Password: password,
Email: email,
}
if err := client.api.PullImage(imageName, &auth); err != nil {
log.Debugf("Error pulling image %s with authentication, %s", imageName, err)
return false, err
}
} else {
if err := client.api.PullImage(imageName, nil); err != nil {
log.Debugf("Error pulling image %s, %s", imageName, err) log.Debugf("Error pulling image %s, %s", imageName, err)
return false, err return false, err
} } else {
closer.Close()
} }
} }
newImageInfo, err := client.api.InspectImage(imageName) newImageInfo, _, err := client.api.ImageInspectWithRaw(bg, imageName)
if err != nil { if err != nil {
return false, err return false, err
} }
if newImageInfo.Id != oldImageInfo.Id { if newImageInfo.ID != oldImageInfo.ID {
log.Infof("Found new %s image (%s)", imageName, newImageInfo.Id) log.Infof("Found new %s image (%s)", imageName, newImageInfo.ID)
return true, nil return true, nil
} }
@ -185,11 +180,12 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
func (client dockerClient) RemoveImage(c Container) error { func (client dockerClient) RemoveImage(c Container) error {
imageID := c.ImageID() imageID := c.ImageID()
log.Infof("Removing image %s", imageID) log.Infof("Removing image %s", imageID)
_, err := client.api.RemoveImage(imageID, true) _, err := client.api.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{Force: true})
return err return err
} }
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error { func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
bg := context.Background()
timeout := time.After(waitTime) timeout := time.After(waitTime)
for { for {
@ -197,7 +193,7 @@ func (client dockerClient) waitForStop(c Container, waitTime time.Duration) erro
case <-timeout: case <-timeout:
return nil return nil
default: default:
if ci, err := client.api.InspectContainer(c.ID()); err != nil { if ci, err := client.api.ContainerInspect(bg, c.ID()); err != nil {
return err return err
} else if !ci.State.Running { } else if !ci.State.Running {
return nil return nil

@ -4,7 +4,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/samalba/dockerclient" "github.com/docker/docker/api/types"
dockercontainer "github.com/docker/docker/api/types/container"
) )
const ( const (
@ -15,7 +16,7 @@ const (
// NewContainer returns a new Container instance instantiated with the // NewContainer returns a new Container instance instantiated with the
// specified ContainerInfo and ImageInfo structs. // specified ContainerInfo and ImageInfo structs.
func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockerclient.ImageInfo) *Container { func NewContainer(containerInfo *types.ContainerJSON, imageInfo *types.ImageInspect) *Container {
return &Container{ return &Container{
containerInfo: containerInfo, containerInfo: containerInfo,
imageInfo: imageInfo, imageInfo: imageInfo,
@ -26,13 +27,13 @@ func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockercl
type Container struct { type Container struct {
Stale bool Stale bool
containerInfo *dockerclient.ContainerInfo containerInfo *types.ContainerJSON
imageInfo *dockerclient.ImageInfo imageInfo *types.ImageInspect
} }
// ID returns the Docker container ID. // ID returns the Docker container ID.
func (c Container) ID() string { func (c Container) ID() string {
return c.containerInfo.Id return c.containerInfo.ID
} }
// Name returns the Docker container name. // Name returns the Docker container name.
@ -43,7 +44,7 @@ func (c Container) Name() string {
// ImageID returns the ID of the Docker image that was used to start the // ImageID returns the ID of the Docker image that was used to start the
// container. // container.
func (c Container) ImageID() string { func (c Container) ImageID() string {
return c.imageInfo.Id return c.imageInfo.ID
} }
// ImageName returns the name of the Docker image that was used to start the // ImageName returns the name of the Docker image that was used to start the
@ -109,7 +110,7 @@ func (c Container) StopSignal() string {
// running container with the ContainerConfig from the image that container was // running container with the ContainerConfig from the image that container was
// started from. This function returns a ContainerConfig which contains just // started from. This function returns a ContainerConfig which contains just
// the options overridden at runtime. // the options overridden at runtime.
func (c Container) runtimeConfig() *dockerclient.ContainerConfig { func (c Container) runtimeConfig() *dockercontainer.Config {
config := c.containerInfo.Config config := c.containerInfo.Config
imageConfig := c.imageInfo.Config imageConfig := c.imageInfo.Config
@ -135,7 +136,7 @@ func (c Container) runtimeConfig() *dockerclient.ContainerConfig {
config.Volumes = structMapSubtract(config.Volumes, imageConfig.Volumes) config.Volumes = structMapSubtract(config.Volumes, imageConfig.Volumes)
config.ExposedPorts = structMapSubtract(config.ExposedPorts, imageConfig.ExposedPorts) config.ExposedPorts = structMapPortSubtract(config.ExposedPorts, imageConfig.ExposedPorts)
for p := range c.containerInfo.HostConfig.PortBindings { for p := range c.containerInfo.HostConfig.PortBindings {
config.ExposedPorts[p] = struct{}{} config.ExposedPorts[p] = struct{}{}
} }
@ -146,7 +147,7 @@ func (c Container) runtimeConfig() *dockerclient.ContainerConfig {
// Any links in the HostConfig need to be re-written before they can be // Any links in the HostConfig need to be re-written before they can be
// re-submitted to the Docker create API. // re-submitted to the Docker create API.
func (c Container) hostConfig() *dockerclient.HostConfig { func (c Container) hostConfig() *dockercontainer.HostConfig {
hostConfig := c.containerInfo.HostConfig hostConfig := c.containerInfo.HostConfig
for i, link := range hostConfig.Links { for i, link := range hostConfig.Links {

@ -1,5 +1,9 @@
package container package container
import (
"github.com/docker/go-connections/nat"
)
func sliceEqual(s1, s2 []string) bool { func sliceEqual(s1, s2 []string) bool {
if len(s1) != len(s2) { if len(s1) != len(s2) {
return false return false
@ -62,3 +66,15 @@ func structMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} {
return m return m
} }
func structMapPortSubtract(m1, m2 map[nat.Port]struct{}) map[nat.Port]struct{} {
m := map[nat.Port]struct{}{}
for k1, v1 := range m1 {
if _, ok := m2[k1]; !ok {
m[k1] = v1
}
}
return m
}

@ -115,12 +115,12 @@ func before(c *cli.Context) error {
cleanup = c.GlobalBool("cleanup") cleanup = c.GlobalBool("cleanup")
// Set-up container client // Set-up container client
tls, err := tlsConfig(c) _, err := tlsConfig(c)
if err != nil { if err != nil {
return err return err
} }
client = container.NewClient(c.GlobalString("host"), tls, !c.GlobalBool("no-pull")) client = container.NewClient(c.GlobalString("host"), !c.GlobalBool("no-pull"))
login(c) login(c)
handleSignals() handleSignals()

Loading…
Cancel
Save