mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
Port client lib from samalba/dockerclient to docker/docker/client
This commit is contained in:
parent
25f1fee8e2
commit
99ed959155
4 changed files with 74 additions and 61 deletions
|
@ -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
|
||||
|
|
|
@ -4,7 +4,8 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/samalba/dockerclient"
|
||||
"github.com/docker/docker/api/types"
|
||||
dockercontainer "github.com/docker/docker/api/types/container"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -15,7 +16,7 @@ const (
|
|||
|
||||
// NewContainer returns a new Container instance instantiated with the
|
||||
// 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{
|
||||
containerInfo: containerInfo,
|
||||
imageInfo: imageInfo,
|
||||
|
@ -26,13 +27,13 @@ func NewContainer(containerInfo *dockerclient.ContainerInfo, imageInfo *dockercl
|
|||
type Container struct {
|
||||
Stale bool
|
||||
|
||||
containerInfo *dockerclient.ContainerInfo
|
||||
imageInfo *dockerclient.ImageInfo
|
||||
containerInfo *types.ContainerJSON
|
||||
imageInfo *types.ImageInspect
|
||||
}
|
||||
|
||||
// ID returns the Docker container ID.
|
||||
func (c Container) ID() string {
|
||||
return c.containerInfo.Id
|
||||
return c.containerInfo.ID
|
||||
}
|
||||
|
||||
// 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
|
||||
// container.
|
||||
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
|
||||
|
@ -109,7 +110,7 @@ func (c Container) StopSignal() string {
|
|||
// running container with the ContainerConfig from the image that container was
|
||||
// started from. This function returns a ContainerConfig which contains just
|
||||
// the options overridden at runtime.
|
||||
func (c Container) runtimeConfig() *dockerclient.ContainerConfig {
|
||||
func (c Container) runtimeConfig() *dockercontainer.Config {
|
||||
config := c.containerInfo.Config
|
||||
imageConfig := c.imageInfo.Config
|
||||
|
||||
|
@ -135,7 +136,7 @@ func (c Container) runtimeConfig() *dockerclient.ContainerConfig {
|
|||
|
||||
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 {
|
||||
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
|
||||
// re-submitted to the Docker create API.
|
||||
func (c Container) hostConfig() *dockerclient.HostConfig {
|
||||
func (c Container) hostConfig() *dockercontainer.HostConfig {
|
||||
hostConfig := c.containerInfo.HostConfig
|
||||
|
||||
for i, link := range hostConfig.Links {
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"github.com/docker/go-connections/nat"
|
||||
)
|
||||
|
||||
func sliceEqual(s1, s2 []string) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
|
@ -62,3 +66,15 @@ func structMapSubtract(m1, m2 map[string]struct{}) map[string]struct{} {
|
|||
|
||||
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
|
||||
}
|
||||
|
|
4
main.go
4
main.go
|
@ -115,12 +115,12 @@ func before(c *cli.Context) error {
|
|||
cleanup = c.GlobalBool("cleanup")
|
||||
|
||||
// Set-up container client
|
||||
tls, err := tlsConfig(c)
|
||||
_, err := tlsConfig(c)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
handleSignals()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue