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.
This commit is contained in:
Brian DeHamer 2015-07-31 18:24:27 +00:00
parent b8ba80df2d
commit dd80aa4a0d
10 changed files with 117 additions and 7 deletions

View file

@ -21,6 +21,7 @@ type Client interface {
StartContainer(Container) error
RenameContainer(Container, string) error
IsContainerStale(Container) (bool, error)
RemoveImage(Container) error
}
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
}
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 {
timeout := time.After(waitTime)

View file

@ -399,3 +399,38 @@ func TestIsContainerStale_InspectImageError(t *testing.T) {
assert.EqualError(t, err, "uh-oh")
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)
}

View file

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

View file

@ -23,6 +23,16 @@ func TestName(t *testing.T) {
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) {
c := Container{
containerInfo: &dockerclient.ContainerInfo{

View file

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