2015-07-21 18:04:41 +02:00
|
|
|
package container
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
import (
|
2015-07-16 22:25:01 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-07-17 20:56:09 +02:00
|
|
|
"time"
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-07-14 22:51:12 +02:00
|
|
|
"github.com/samalba/dockerclient"
|
|
|
|
)
|
|
|
|
|
2015-07-21 18:04:41 +02:00
|
|
|
const (
|
|
|
|
defaultStopSignal = "SIGTERM"
|
|
|
|
signalLabel = "com.centurylinklabs.watchtower.stop-signal"
|
|
|
|
)
|
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
type Filter func(Container) bool
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2015-07-14 22:51:12 +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)
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:00 +02:00
|
|
|
func NewClient(dockerHost string, pullImages bool) Client {
|
2015-07-21 21:37:18 +02:00
|
|
|
docker, err := dockerclient.NewDockerClient(dockerHost, nil)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Fatalf("Error instantiating Docker client: %s", err)
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 01:29:00 +02:00
|
|
|
return DockerClient{api: docker, pullImages: pullImages}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type DockerClient struct {
|
2015-07-22 01:29:00 +02:00
|
|
|
api dockerclient.Client
|
|
|
|
pullImages bool
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
func (client DockerClient) ListContainers(fn Filter) ([]Container, error) {
|
2015-07-14 22:51:12 +02:00
|
|
|
cs := []Container{}
|
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debug("Retrieving running containers")
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
runningContainers, err := client.api.ListContainers(false, false, "")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, runningContainer := range runningContainers {
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debugf("Inspecting container %s (%s)", runningContainer.Names[0], runningContainer.Id)
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
containerInfo, err := client.api.InspectContainer(runningContainer.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debugf("Inspecting image %s (%s)", containerInfo.Config.Image, containerInfo.Image)
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
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
|
|
|
}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cs, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
func (client DockerClient) StopContainer(c Container, timeout time.Duration) error {
|
2015-07-21 18:04:41 +02:00
|
|
|
signal := defaultStopSignal
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-21 18:04:41 +02:00
|
|
|
if sig, ok := c.containerInfo.Config.Labels[signalLabel]; ok {
|
2015-07-14 22:51:12 +02:00
|
|
|
signal = sig
|
|
|
|
}
|
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Infof("Stopping %s (%s) with %s", c.Name(), c.containerInfo.Id, signal)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
if err := client.api.KillContainer(c.containerInfo.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-22 23:58:16 +02:00
|
|
|
log.Debugf("Removing container %s", c.containerInfo.Id)
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
return client.api.RemoveContainer(c.containerInfo.Id, true, false)
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
func (client DockerClient) StartContainer(c Container) error {
|
2015-07-14 22:51:12 +02:00
|
|
|
config := c.runtimeConfig()
|
|
|
|
hostConfig := c.hostConfig()
|
2015-07-21 00:54:18 +02:00
|
|
|
name := c.Name()
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Infof("Starting %s", name)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-21 23:40:22 +02:00
|
|
|
newContainerID, err := client.api.CreateContainer(config, name)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debugf("Starting container %s (%s)", name, newContainerID)
|
|
|
|
|
2015-07-21 23:40:22 +02:00
|
|
|
return client.api.StartContainer(newContainerID, hostConfig)
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
func (client DockerClient) RenameContainer(c Container, newName string) error {
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.containerInfo.Id, newName)
|
2015-07-21 00:54:18 +02:00
|
|
|
return client.api.RenameContainer(c.containerInfo.Id, newName)
|
|
|
|
}
|
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
func (client DockerClient) IsContainerStale(c Container) (bool, error) {
|
|
|
|
containerInfo := c.containerInfo
|
|
|
|
oldImageInfo := c.imageInfo
|
|
|
|
imageName := containerInfo.Config.Image
|
|
|
|
|
2015-07-22 01:29:00 +02:00
|
|
|
if client.pullImages {
|
2015-07-22 00:41:58 +02:00
|
|
|
if !strings.Contains(imageName, ":") {
|
|
|
|
imageName = fmt.Sprintf("%s:latest", imageName)
|
|
|
|
}
|
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
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 {
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Infof("Found new %s image (%s)", imageName, newImageInfo.Id)
|
2015-07-22 00:41:58 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:54:18 +02:00
|
|
|
func (client DockerClient) waitForStop(c Container, waitTime time.Duration) error {
|
|
|
|
timeout := time.After(waitTime)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
if ci, err := client.api.InspectContainer(c.containerInfo.Id); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ci.State.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|