1
0
mirror of https://github.com/containrrr/watchtower.git synced 2025-01-29 18:53:51 +02:00
watchtower/actions/check.go
2018-03-03 21:58:07 +01:00

34 lines
786 B
Go

package actions
import (
"sort"
"github.com/v2tec/watchtower/container"
)
// CheckPrereqs will ensure that there are not multiple instances of the
// watchtower running simultaneously. If multiple watchtower containers are
// detected, this function will stop and remove all but the most recently
// started container.
func CheckPrereqs(client container.Client, cleanup bool) error {
containers, err := client.ListContainers(container.WatchtowerContainersFilter)
if err != nil {
return err
}
if len(containers) > 1 {
sort.Sort(container.ByCreated(containers))
// 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)
}
}
}
return nil
}