package container import ( "fmt" "strings" "time" log "github.com/Sirupsen/logrus" "github.com/samalba/dockerclient" ) const ( defaultStopSignal = "SIGTERM" signalLabel = "com.centurylinklabs.watchtower.stop-signal" ) type Filter func(Container) bool type Client interface { ListContainers(Filter) ([]Container, error) StopContainer(Container, time.Duration) error StartContainer(Container) error RenameContainer(Container, string) error IsContainerStale(Container) (bool, error) } func NewClient(dockerHost string, pullImages bool) Client { docker, err := dockerclient.NewDockerClient(dockerHost, nil) if err != nil { log.Fatalf("Error instantiating Docker client: %s", err) } return DockerClient{api: docker, pullImages: pullImages} } type DockerClient struct { 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 } c := Container{containerInfo: containerInfo, imageInfo: imageInfo} if fn(c) { cs = append(cs, c) } } return cs, nil } func (client DockerClient) StopContainer(c Container, timeout time.Duration) error { signal := defaultStopSignal if sig, ok := c.containerInfo.Config.Labels[signalLabel]; ok { signal = sig } log.Infof("Stopping %s (%s) with %s", c.Name(), c.containerInfo.Id, signal) if err := client.api.KillContainer(c.containerInfo.Id, signal); err != nil { return err } // Wait for container to exit, but proceed anyway after the timeout elapses client.waitForStop(c, timeout) log.Debugf("Removing container %s", c.containerInfo.Id) return client.api.RemoveContainer(c.containerInfo.Id, true, false) } func (client DockerClient) StartContainer(c Container) error { config := c.runtimeConfig() hostConfig := c.hostConfig() name := c.Name() log.Infof("Starting %s", name) newContainerID, err := client.api.CreateContainer(config, name) if err != nil { return err } log.Debugf("Starting container %s (%s)", name, newContainerID) return client.api.StartContainer(newContainerID, hostConfig) } func (client DockerClient) RenameContainer(c Container, newName string) error { log.Debugf("Renaming container %s (%s) to %s", c.Name(), c.containerInfo.Id, newName) return client.api.RenameContainer(c.containerInfo.Id, newName) } func (client DockerClient) IsContainerStale(c Container) (bool, error) { containerInfo := c.containerInfo oldImageInfo := c.imageInfo imageName := containerInfo.Config.Image if client.pullImages { if !strings.Contains(imageName, ":") { imageName = fmt.Sprintf("%s:latest", imageName) } log.Debugf("Pulling %s for %s", imageName, c.Name()) 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) return true, nil } return false, nil } 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) } } }