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

This commit is contained in:
Ross Cadogan 2016-10-13 21:34:24 +01:00
parent 25f1fee8e2
commit 99ed959155
4 changed files with 74 additions and 61 deletions

View file

@ -1,13 +1,14 @@
package container
import (
"crypto/tls"
"fmt"
"os"
"time"
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 (
@ -17,6 +18,7 @@ const (
var username = os.Getenv("REPO_USER")
var password = os.Getenv("REPO_PASS")
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
// results from a call to the ListContainers() method on the Client.
@ -31,48 +33,57 @@ type Client interface {
RenameContainer(Container, string) error
IsContainerStale(Container) (bool, error)
RemoveImage(Container) error
RegistryLogin(string) error
}
// NewClient returns a new Client instance which can be used to interact with
// the Docker API.
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig)
func NewClient(dockerHost string, pullImages bool) Client {
cli, err := dockerclient.NewClient(dockerHost, api_version, nil, nil)
if err != nil {
log.Fatalf("Error instantiating Docker client: %s", err)
}
return dockerClient{api: docker, pullImages: pullImages}
return dockerClient{api: cli, pullImages: pullImages}
}
type dockerClient struct {
api dockerclient.Client
api *dockerclient.Client
pullImages bool
}
func (client dockerClient) RegistryLogin(registryURL string) error {
log.Debug("Login not implemented")
return nil
}
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
cs := []Container{}
bg := context.Background()
log.Debug("Retrieving running containers")
runningContainers, err := client.api.ListContainers(false, false, "")
runningContainers, err := client.api.ContainerList(
bg,
types.ContainerListOptions{})
if err != nil {
return nil, err
}
for _, runningContainer := range runningContainers {
containerInfo, err := client.api.InspectContainer(runningContainer.Id)
containerInfo, err := client.api.ContainerInspect(bg, runningContainer.ID)
if err != nil {
return nil, err
}
imageInfo, err := client.api.InspectImage(containerInfo.Image)
imageInfo, _, err := client.api.ImageInspectWithRaw(bg, containerInfo.Image)
if err != nil {
return nil, err
}
c := Container{containerInfo: containerInfo, imageInfo: imageInfo}
if fn(c) {
c := Container{containerInfo: &containerInfo, imageInfo: &imageInfo}
if (fn(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 {
bg := context.Background()
signal := c.StopSignal()
if signal == "" {
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)
if err := client.api.KillContainer(c.ID(), signal); err != nil {
if err := client.api.ContainerKill(bg, c.ID(), signal); err != nil {
return err
}
@ -97,7 +109,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
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
}
@ -110,72 +122,55 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
}
func (client dockerClient) StartContainer(c Container) error {
bg := context.Background();
config := c.runtimeConfig()
hostConfig := c.hostConfig()
name := c.Name()
log.Infof("Starting %s", 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)
}
creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name)
if err != nil {
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 {
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) {
bg := context.Background()
oldImageInfo := c.imageInfo
imageName := c.ImageName()
if client.pullImages {
log.Debugf("Pulling %s for %s", imageName, c.Name())
if username != "" && password != "" && email != "" {
auth := dockerclient.AuthConfig{
Username: username,
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
}
// Note: ImagePullOptions below can take a RegistryAuth arg if 401 on private registry
closer, err := client.api.ImagePull(bg, imageName, types.ImagePullOptions{})
if (err != nil) {
log.Debugf("Error pulling image %s, %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)
return false, err
}
closer.Close()
}
}
newImageInfo, err := client.api.InspectImage(imageName)
newImageInfo, _, err := client.api.ImageInspectWithRaw(bg, imageName)
if err != nil {
return false, err
}
if newImageInfo.Id != oldImageInfo.Id {
log.Infof("Found new %s image (%s)", imageName, newImageInfo.Id)
if newImageInfo.ID != oldImageInfo.ID {
log.Infof("Found new %s image (%s)", imageName, newImageInfo.ID)
return true, nil
}
@ -185,11 +180,12 @@ func (client dockerClient) IsContainerStale(c Container) (bool, error) {
func (client dockerClient) RemoveImage(c Container) error {
imageID := c.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
}
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
bg := context.Background()
timeout := time.After(waitTime)
for {
@ -197,7 +193,7 @@ func (client dockerClient) waitForStop(c Container, waitTime time.Duration) erro
case <-timeout:
return nil
default:
if ci, err := client.api.InspectContainer(c.ID()); err != nil {
if ci, err := client.api.ContainerInspect(bg, c.ID()); err != nil {
return err
} else if !ci.State.Running {
return nil