watchtower/actions/check.go

37 lines
897 B
Go
Raw Normal View History

2015-07-21 16:04:41 +00:00
package actions
import (
"sort"
log "github.com/sirupsen/logrus"
2019-04-03 21:02:05 +02:00
"github.com/containrrr/watchtower/container"
2015-07-21 16:04:41 +00:00
)
2015-07-31 22:23:17 +00:00
// 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 {
2018-03-02 17:22:42 +01:00
containers, err := client.ListContainers(container.WatchtowerContainersFilter)
2015-07-21 16:04:41 +00:00
if err != nil {
return err
}
if len(containers) > 1 {
log.Info("Found multiple running watchtower instances. Cleaning up")
2015-07-21 16:04:41 +00:00
sort.Sort(container.ByCreated(containers))
// Iterate over all containers execept the last one
for _, c := range containers[0 : len(containers)-1] {
2015-07-21 22:41:58 +00:00
client.StopContainer(c, 60)
if cleanup {
client.RemoveImage(c)
}
2015-07-21 16:04:41 +00:00
}
}
return nil
}