Support for --cleanup flag

The --cleanup flag will cause watchtower to automatically remove the old
image after a container is restart with a new image.
pull/1/head
Brian DeHamer 9 years ago
parent b8ba80df2d
commit dd80aa4a0d

@ -49,6 +49,7 @@ docker run --rm centurylink/watchtower --help
* `--host, -h` - Docker daemon socket to connect to. Defaults to "unix:///var/run/docker.sock" but can be pointed at a remote Docker host by specifying a TCP endpoint as "tcp://hostname:port". The host value can also be provided by setting the `DOCKER_HOST` environment variable. * `--host, -h` - Docker daemon socket to connect to. Defaults to "unix:///var/run/docker.sock" but can be pointed at a remote Docker host by specifying a TCP endpoint as "tcp://hostname:port". The host value can also be provided by setting the `DOCKER_HOST` environment variable.
* `--interval, -i` - Poll interval (in seconds). This value controls how frequently watchtower will poll for new images. Defaults to 300 seconds (5 minutes). * `--interval, -i` - Poll interval (in seconds). This value controls how frequently watchtower will poll for new images. Defaults to 300 seconds (5 minutes).
* `--no-pull` - Do not pull new images. When this flag is specified, watchtower will not attempt to pull new images from the registry. Instead it will only monitor the local image cache for changes. Use this option if you are building new images directly on the Docker host without pushing them to a registry. * `--no-pull` - Do not pull new images. When this flag is specified, watchtower will not attempt to pull new images from the registry. Instead it will only monitor the local image cache for changes. Use this option if you are building new images directly on the Docker host without pushing them to a registry.
* `--cleanup` - Remove old images after updating. When this flag is specified, watchtower will remove the old image after restarting a container with a new image. Use this option to prevent the accumulation of orphaned images on your system as containers are updated.
* `--tls` - Use TLS when connecting to the Docker socket but do NOT verify the server's certificate. If you are connecting a TCP Docker socket protected by TLS you'll need to use either this flag or the `--tlsverify` flag (described below). The `--tlsverify` flag is preferred as it will cause the server's certificate to be verified before a connection is made. * `--tls` - Use TLS when connecting to the Docker socket but do NOT verify the server's certificate. If you are connecting a TCP Docker socket protected by TLS you'll need to use either this flag or the `--tlsverify` flag (described below). The `--tlsverify` flag is preferred as it will cause the server's certificate to be verified before a connection is made.
* `--tlsverify` - Use TLS when connecting to the Docker socket and verify the server's certificate. If you are connecting a TCP Docker socket protected by TLS you'll need to use either this flag or the `--tls` flag (describe above). * `--tlsverify` - Use TLS when connecting to the Docker socket and verify the server's certificate. If you are connecting a TCP Docker socket protected by TLS you'll need to use either this flag or the `--tls` flag (describe above).
* `--tlscacert` - Trust only certificates signed by this CA. Used in conjunction with the `--tlsverify` flag to identify the CA certificate which should be used to verify the identity of the server. The value for this flag can be either the fully-qualified path to the *.pem* file containing the CA certificate or a string containing the CA certificate itself. Defaults to "/etc/ssl/docker/ca.pem". * `--tlscacert` - Trust only certificates signed by this CA. Used in conjunction with the `--tlsverify` flag to identify the CA certificate which should be used to verify the identity of the server. The value for this flag can be either the fully-qualified path to the *.pem* file containing the CA certificate or a string containing the CA certificate itself. Defaults to "/etc/ssl/docker/ca.pem".

@ -8,7 +8,7 @@ import (
func watchtowerContainersFilter(c container.Container) bool { return c.IsWatchtower() } func watchtowerContainersFilter(c container.Container) bool { return c.IsWatchtower() }
func CheckPrereqs(client container.Client) error { func CheckPrereqs(client container.Client, cleanup bool) error {
containers, err := client.ListContainers(watchtowerContainersFilter) containers, err := client.ListContainers(watchtowerContainersFilter)
if err != nil { if err != nil {
return err return err
@ -20,6 +20,10 @@ func CheckPrereqs(client container.Client) error {
// Iterate over all containers execept the last one // Iterate over all containers execept the last one
for _, c := range containers[0 : len(containers)-1] { for _, c := range containers[0 : len(containers)-1] {
client.StopContainer(c, 60) client.StopContainer(c, 60)
if cleanup {
client.RemoveImage(c)
}
} }
} }

@ -38,7 +38,40 @@ func TestCheckPrereqs_Success(t *testing.T) {
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil) client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
client.On("StopContainer", c2, time.Duration(60)).Return(nil) client.On("StopContainer", c2, time.Duration(60)).Return(nil)
err := CheckPrereqs(client) err := CheckPrereqs(client, false)
assert.NoError(t, err)
client.AssertExpectations(t)
}
func TestCheckPrereqs_WithCleanup(t *testing.T) {
cc := &dockerclient.ContainerConfig{
Labels: map[string]string{"com.centurylinklabs.watchtower": "true"},
}
c1 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c1",
Config: cc,
Created: "2015-07-01T12:00:01.000000000Z",
},
nil,
)
c2 := *container.NewContainer(
&dockerclient.ContainerInfo{
Name: "c2",
Config: cc,
Created: "2015-07-01T12:00:00.000000000Z",
},
nil,
)
cs := []container.Container{c1, c2}
client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
client.On("StopContainer", c2, time.Duration(60)).Return(nil)
client.On("RemoveImage", c2).Return(nil)
err := CheckPrereqs(client, true)
assert.NoError(t, err) assert.NoError(t, err)
client.AssertExpectations(t) client.AssertExpectations(t)
@ -61,7 +94,7 @@ func TestCheckPrereqs_OnlyOneContainer(t *testing.T) {
client := &mockclient.MockClient{} client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil) client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
err := CheckPrereqs(client) err := CheckPrereqs(client, false)
assert.NoError(t, err) assert.NoError(t, err)
client.AssertExpectations(t) client.AssertExpectations(t)
@ -73,7 +106,7 @@ func TestCheckPrereqs_ListError(t *testing.T) {
client := &mockclient.MockClient{} client := &mockclient.MockClient{}
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, errors.New("oops")) client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, errors.New("oops"))
err := CheckPrereqs(client) err := CheckPrereqs(client, false)
assert.Error(t, err) assert.Error(t, err)
assert.EqualError(t, err, "oops") assert.EqualError(t, err, "oops")

@ -15,7 +15,7 @@ var (
func allContainersFilter(container.Container) bool { return true } func allContainersFilter(container.Container) bool { return true }
func Update(client container.Client) error { func Update(client container.Client, cleanup bool) error {
log.Info("Checking containers for updated images") log.Info("Checking containers for updated images")
containers, err := client.ListContainers(allContainersFilter) containers, err := client.ListContainers(allContainersFilter)
@ -70,6 +70,10 @@ func Update(client container.Client) error {
if err := client.StartContainer(container); err != nil { if err := client.StartContainer(container); err != nil {
log.Error(err) log.Error(err)
} }
if cleanup {
client.RemoveImage(container)
}
} }
} }

@ -21,6 +21,7 @@ type Client interface {
StartContainer(Container) error StartContainer(Container) error
RenameContainer(Container, string) error RenameContainer(Container, string) error
IsContainerStale(Container) (bool, error) IsContainerStale(Container) (bool, error)
RemoveImage(Container) error
} }
func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client { func NewClient(dockerHost string, tlsConfig *tls.Config, pullImages bool) Client {
@ -147,6 +148,13 @@ func (client DockerClient) IsContainerStale(c Container) (bool, error) {
return false, 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 { func (client DockerClient) waitForStop(c Container, waitTime time.Duration) error {
timeout := time.After(waitTime) timeout := time.After(waitTime)

@ -399,3 +399,38 @@ func TestIsContainerStale_InspectImageError(t *testing.T) {
assert.EqualError(t, err, "uh-oh") assert.EqualError(t, err, "uh-oh")
api.AssertExpectations(t) api.AssertExpectations(t)
} }
func TestRemoveImage_Success(t *testing.T) {
c := Container{
imageInfo: &dockerclient.ImageInfo{
Id: "abc123",
},
}
api := mockclient.NewMockClient()
api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, nil)
client := DockerClient{api: api}
err := client.RemoveImage(c)
assert.NoError(t, err)
api.AssertExpectations(t)
}
func TestRemoveImage_Error(t *testing.T) {
c := Container{
imageInfo: &dockerclient.ImageInfo{
Id: "abc123",
},
}
api := mockclient.NewMockClient()
api.On("RemoveImage", "abc123").Return([]*dockerclient.ImageDelete{}, errors.New("oops"))
client := DockerClient{api: api}
err := client.RemoveImage(c)
assert.Error(t, err)
assert.EqualError(t, err, "oops")
api.AssertExpectations(t)
}

@ -34,6 +34,10 @@ func (c Container) Name() string {
return c.containerInfo.Name return c.containerInfo.Name
} }
func (c Container) ImageID() string {
return c.imageInfo.Id
}
func (c Container) ImageName() string { func (c Container) ImageName() string {
imageName := c.containerInfo.Config.Image imageName := c.containerInfo.Config.Image

@ -23,6 +23,16 @@ func TestName(t *testing.T) {
assert.Equal(t, "foo", c.Name()) assert.Equal(t, "foo", c.Name())
} }
func TestImageID(t *testing.T) {
c := Container{
imageInfo: &dockerclient.ImageInfo{
Id: "foo",
},
}
assert.Equal(t, "foo", c.ImageID())
}
func TestImageName_Tagged(t *testing.T) { func TestImageName_Tagged(t *testing.T) {
c := Container{ c := Container{
containerInfo: &dockerclient.ContainerInfo{ containerInfo: &dockerclient.ContainerInfo{

@ -35,3 +35,8 @@ func (m *MockClient) IsContainerStale(c container.Container) (bool, error) {
args := m.Called(c) args := m.Called(c)
return args.Bool(0), args.Error(1) return args.Bool(0), args.Error(1)
} }
func (m *MockClient) RemoveImage(c container.Container) error {
args := m.Called(c)
return args.Error(0)
}

@ -22,6 +22,7 @@ var (
wg sync.WaitGroup wg sync.WaitGroup
client container.Client client container.Client
pollInterval time.Duration pollInterval time.Duration
cleanup bool
) )
func init() { func init() {
@ -56,6 +57,10 @@ func main() {
Name: "no-pull", Name: "no-pull",
Usage: "do not pull new images", Usage: "do not pull new images",
}, },
cli.BoolFlag{
Name: "cleanup",
Usage: "remove old images after updating",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "tls", Name: "tls",
Usage: "use TLS; implied by --tlsverify", Usage: "use TLS; implied by --tlsverify",
@ -97,6 +102,7 @@ func before(c *cli.Context) error {
} }
pollInterval = time.Duration(c.Int("interval")) * time.Second pollInterval = time.Duration(c.Int("interval")) * time.Second
cleanup = c.GlobalBool("cleanup")
// Set-up container client // Set-up container client
tls, err := tlsConfig(c) tls, err := tlsConfig(c)
@ -111,13 +117,13 @@ func before(c *cli.Context) error {
} }
func start(*cli.Context) { func start(*cli.Context) {
if err := actions.CheckPrereqs(client); err != nil { if err := actions.CheckPrereqs(client, cleanup); err != nil {
log.Fatal(err) log.Fatal(err)
} }
for { for {
wg.Add(1) wg.Add(1)
if err := actions.Update(client); err != nil { if err := actions.Update(client, cleanup); err != nil {
fmt.Println(err) fmt.Println(err)
} }
wg.Done() wg.Done()

Loading…
Cancel
Save