1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-15 09:14:13 +02:00
watchtower/container/client.go

176 lines
4.1 KiB
Go
Raw Normal View History

2015-07-21 18:04:41 +02:00
package container
import (
"crypto/tls"
"fmt"
2015-07-17 20:56:09 +02:00
"time"
log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient"
)
2015-07-21 18:04:41 +02:00
const (
defaultStopSignal = "SIGTERM"
)
2015-07-22 00:41:58 +02:00
type Filter func(Container) bool
2015-07-21 00:54:18 +02:00
type Client interface {
2015-07-22 00:41:58 +02:00
ListContainers(Filter) ([]Container, error)
StopContainer(Container, time.Duration) error
StartContainer(Container) error
RenameContainer(Container, string) error
IsContainerStale(Container) (bool, error)
RemoveImage(Container) error
}
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
docker, err := dockerclient.NewDockerClient(dockerHost, tlsConfig)
if err != nil {
log.Fatalf("Error instantiating Docker client: %s", err)
}
return dockerClient{api: docker, pullImages: pullImages}
}
type dockerClient struct {
2015-07-22 01:29:00 +02:00
api dockerclient.Client
pullImages bool
}
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
cs := []Container{}
log.Debug("Retrieving running containers")
runningContainers, err := client.api.ListContainers(false, false, "")
if err != nil {
return nil, err
}
for _, runningContainer := range runningContainers {
log.Debugf("Inspecting container %s (%s)", runningContainer.Names[0], runningContainer.Id)
containerInfo, err := client.api.InspectContainer(runningContainer.Id)
if err != nil {
return nil, err
}
log.Debugf("Inspecting image %s (%s)", containerInfo.Config.Image, containerInfo.Image)
imageInfo, err := client.api.InspectImage(containerInfo.Image)
if err != nil {
return nil, err
}
2015-07-21 00:54:18 +02:00
c := Container{containerInfo: containerInfo, imageInfo: imageInfo}
if fn(c) {
2015-07-21 23:40:22 +02:00
cs = append(cs, c)
2015-07-21 00:54:18 +02:00
}
}
return cs, nil
}
func (client dockerClient) StopContainer(c Container, timeout time.Duration) error {
2015-07-23 00:52:22 +02:00
signal := c.StopSignal()
if signal == "" {
signal = defaultStopSignal
}
2015-07-23 00:52:22 +02:00
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
2015-07-23 00:52:22 +02:00
if err := client.api.KillContainer(c.ID(), signal); err != nil {
return err
}
2015-07-21 18:04:41 +02:00
// Wait for container to exit, but proceed anyway after the timeout elapses
2015-07-21 00:54:18 +02:00
client.waitForStop(c, timeout)
2015-07-17 20:56:09 +02:00
2015-07-23 00:52:22 +02:00
log.Debugf("Removing container %s", c.ID())
if err := client.api.RemoveContainer(c.ID(), true, false); err != nil {
return err
}
// Wait for container to be removed. In this case an error is a good thing
if err := client.waitForStop(c, timeout); err == nil {
return fmt.Errorf("Container %s (%s) could not be removed", c.Name(), c.ID())
}
return nil
}
func (client dockerClient) StartContainer(c Container) error {
config := c.runtimeConfig()
hostConfig := c.hostConfig()
2015-07-21 00:54:18 +02:00
name := c.Name()
log.Infof("Starting %s", name)
2015-07-21 23:40:22 +02:00
newContainerID, err := client.api.CreateContainer(config, name)
if err != nil {
return err
}
log.Debugf("Starting container %s (%s)", name, newContainerID)
2015-07-21 23:40:22 +02:00
return client.api.StartContainer(newContainerID, hostConfig)
}
2015-07-21 00:54:18 +02:00
func (client dockerClient) RenameContainer(c Container, newName string) error {
2015-07-23 00:52:22 +02:00
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.ID(), newName)
return client.api.RenameContainer(c.ID(), newName)
2015-07-21 00:54:18 +02:00
}
func (client dockerClient) IsContainerStale(c Container) (bool, error) {
2015-07-22 00:41:58 +02:00
oldImageInfo := c.imageInfo
2015-07-23 00:52:22 +02:00
imageName := c.ImageName()
2015-07-22 00:41:58 +02:00
2015-07-22 01:29:00 +02:00
if client.pullImages {
log.Debugf("Pulling %s for %s", imageName, c.Name())
2015-07-22 00:41:58 +02:00
if err := client.api.PullImage(imageName, nil); err != nil {
return false, err
}
}
newImageInfo, err := client.api.InspectImage(imageName)
if err != nil {
return false, err
}
if newImageInfo.Id != oldImageInfo.Id {
log.Infof("Found new %s image (%s)", imageName, newImageInfo.Id)
2015-07-22 00:41:58 +02:00
return true, nil
}
return false, nil
}
func (client dockerClient) RemoveImage(c Container) error {
imageID := c.ImageID()
log.Infof("Removing image %s", imageID)
_, err := client.api.RemoveImage(imageID)
return err
}
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
2015-07-21 00:54:18 +02:00
timeout := time.After(waitTime)
for {
select {
case <-timeout:
return nil
default:
2015-07-23 00:52:22 +02:00
if ci, err := client.api.InspectContainer(c.ID()); err != nil {
2015-07-21 00:54:18 +02:00
return err
} else if !ci.State.Running {
return nil
}
}
time.Sleep(1 * time.Second)
2015-07-21 00:54:18 +02:00
}
}