2015-07-21 16:04:41 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2019-04-06 14:27:50 +02:00
|
|
|
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.
|
2015-07-31 18:24:27 +00:00
|
|
|
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 {
|
2019-04-06 14:27:50 +02:00
|
|
|
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)
|
2015-07-31 18:24:27 +00:00
|
|
|
|
|
|
|
if cleanup {
|
|
|
|
client.RemoveImage(c)
|
|
|
|
}
|
2015-07-21 16:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|