mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
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:
parent
b8ba80df2d
commit
dd80aa4a0d
10 changed files with 117 additions and 7 deletions
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -20,6 +20,10 @@ func CheckPrereqs(client container.Client) error {
|
|||
// Iterate over all containers execept the last one
|
||||
for _, c := range containers[0 : len(containers)-1] {
|
||||
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("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)
|
||||
client.AssertExpectations(t)
|
||||
|
@ -61,7 +94,7 @@ func TestCheckPrereqs_OnlyOneContainer(t *testing.T) {
|
|||
client := &mockclient.MockClient{}
|
||||
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, nil)
|
||||
|
||||
err := CheckPrereqs(client)
|
||||
err := CheckPrereqs(client, false)
|
||||
|
||||
assert.NoError(t, err)
|
||||
client.AssertExpectations(t)
|
||||
|
@ -73,7 +106,7 @@ func TestCheckPrereqs_ListError(t *testing.T) {
|
|||
client := &mockclient.MockClient{}
|
||||
client.On("ListContainers", mock.AnythingOfType("container.Filter")).Return(cs, errors.New("oops"))
|
||||
|
||||
err := CheckPrereqs(client)
|
||||
err := CheckPrereqs(client, false)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.EqualError(t, err, "oops")
|
||||
|
|
|
@ -15,7 +15,7 @@ var (
|
|||
|
||||
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")
|
||||
|
||||
containers, err := client.ListContainers(allContainersFilter)
|
||||
|
@ -70,6 +70,10 @@ func Update(client container.Client) error {
|
|||
if err := client.StartContainer(container); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
if cleanup {
|
||||
client.RemoveImage(container)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue