2015-07-21 18:04:41 +02:00
|
|
|
package container
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
import (
|
2015-07-28 21:29:20 +02:00
|
|
|
"fmt"
|
2016-02-24 04:40:35 +02:00
|
|
|
"os"
|
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"
|
2016-10-13 22:34:24 +02:00
|
|
|
dockerclient "github.com/docker/docker/client"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"golang.org/x/net/context"
|
2015-07-14 22:51:12 +02:00
|
|
|
)
|
|
|
|
|
2015-07-21 18:04:41 +02:00
|
|
|
const (
|
|
|
|
defaultStopSignal = "SIGTERM"
|
|
|
|
)
|
|
|
|
|
2016-02-24 04:40:35 +02:00
|
|
|
var username = os.Getenv("REPO_USER")
|
|
|
|
var password = os.Getenv("REPO_PASS")
|
|
|
|
var email = os.Getenv("REPO_EMAIL")
|
2016-10-13 22:34:24 +02:00
|
|
|
var api_version = "1.24"
|
2016-02-24 03:33:29 +02:00
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// 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.
|
2015-07-22 00:41:58 +02:00
|
|
|
type Filter func(Container) bool
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// A Client is the interface through which watchtower interacts with the
|
|
|
|
// Docker API.
|
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-31 20:24:27 +02:00
|
|
|
RemoveImage(Container) error
|
2016-10-13 22:34:24 +02:00
|
|
|
RegistryLogin(string) error
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// NewClient returns a new Client instance which can be used to interact with
|
|
|
|
// the Docker API.
|
2016-10-13 22:34:24 +02:00
|
|
|
func NewClient(dockerHost string, pullImages bool) Client {
|
|
|
|
cli, err := dockerclient.NewClient(dockerHost, api_version, nil, 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
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
return dockerClient{api: cli, pullImages: pullImages}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
type dockerClient struct {
|
2016-10-13 22:34:24 +02:00
|
|
|
api *dockerclient.Client
|
2015-07-22 01:29:00 +02:00
|
|
|
pullImages bool
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
func (client dockerClient) RegistryLogin(registryURL string) error {
|
|
|
|
log.Debug("Login not implemented")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
2015-07-14 22:51:12 +02:00
|
|
|
cs := []Container{}
|
2016-10-13 22:34:24 +02:00
|
|
|
bg := context.Background()
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debug("Retrieving running containers")
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
runningContainers, err := client.api.ContainerList(
|
|
|
|
bg,
|
|
|
|
types.ContainerListOptions{})
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, runningContainer := range runningContainers {
|
2016-10-13 22:34:24 +02:00
|
|
|
containerInfo, err := client.api.ContainerInspect(bg, runningContainer.ID)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
imageInfo, _, err := client.api.ImageInspectWithRaw(bg, containerInfo.Image)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +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-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) StopContainer(c Container, timeout time.Duration) error {
|
2016-10-13 22:34:24 +02:00
|
|
|
bg := context.Background()
|
2015-07-23 00:52:22 +02:00
|
|
|
signal := c.StopSignal()
|
|
|
|
if signal == "" {
|
|
|
|
signal = defaultStopSignal
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-07-23 00:52:22 +02:00
|
|
|
log.Infof("Stopping %s (%s) with %s", c.Name(), c.ID(), signal)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
if err := client.api.ContainerKill(bg, c.ID(), signal); err != nil {
|
2015-07-14 22:51:12 +02:00
|
|
|
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())
|
2015-07-22 23:58:16 +02:00
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: false}); err != nil {
|
2015-07-28 21:29:20 +02:00
|
|
|
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
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) StartContainer(c Container) error {
|
2016-10-13 22:34:24 +02:00
|
|
|
bg := context.Background();
|
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)
|
2016-10-13 22:34:24 +02:00
|
|
|
creation, err := client.api.ContainerCreate(bg, config, hostConfig, nil, name)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
log.Debugf("Starting container %s (%s)", name, creation.ID)
|
2015-07-22 23:58:16 +02:00
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
return client.api.ContainerStart(bg, creation.ID, types.ContainerStartOptions{})
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2015-08-01 00:04:56 +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)
|
2016-10-13 22:34:24 +02:00
|
|
|
//return client.api.ContainerRename(c.ID(), newName)
|
|
|
|
// no op
|
|
|
|
return nil
|
2015-07-21 00:54:18 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) IsContainerStale(c Container) (bool, error) {
|
2016-10-13 22:34:24 +02:00
|
|
|
bg := context.Background()
|
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 {
|
2015-07-22 23:58:16 +02:00
|
|
|
log.Debugf("Pulling %s for %s", imageName, c.Name())
|
2016-02-24 04:40:35 +02:00
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
// 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
|
2016-02-24 04:40:35 +02:00
|
|
|
} else {
|
2016-10-13 22:34:24 +02:00
|
|
|
closer.Close()
|
2015-07-22 00:41:58 +02:00
|
|
|
}
|
2016-10-13 22:34:24 +02:00
|
|
|
|
2015-07-22 00:41:58 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
newImageInfo, _, err := client.api.ImageInspectWithRaw(bg, imageName)
|
2015-07-22 00:41:58 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) RemoveImage(c Container) error {
|
2015-07-31 20:24:27 +02:00
|
|
|
imageID := c.ImageID()
|
|
|
|
log.Infof("Removing image %s", imageID)
|
2016-10-13 22:34:24 +02:00
|
|
|
_, err := client.api.ImageRemove(context.Background(), imageID, types.ImageRemoveOptions{Force: true})
|
2015-07-31 20:24:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:04:56 +02:00
|
|
|
func (client dockerClient) waitForStop(c Container, waitTime time.Duration) error {
|
2016-10-13 22:34:24 +02:00
|
|
|
bg := context.Background()
|
2015-07-21 00:54:18 +02:00
|
|
|
timeout := time.After(waitTime)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
return nil
|
|
|
|
default:
|
2016-10-13 22:34:24 +02:00
|
|
|
if ci, err := client.api.ContainerInspect(bg, c.ID()); err != nil {
|
2015-07-21 00:54:18 +02:00
|
|
|
return err
|
|
|
|
} else if !ci.State.Running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 22:03:47 +02:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2015-07-21 00:54:18 +02:00
|
|
|
}
|
|
|
|
}
|