watchtower/internal/actions/check.go

71 lines
2.1 KiB
Go
Raw Normal View History

2015-07-21 16:04:41 +00:00
package actions
import (
"fmt"
2015-07-21 16:04:41 +00:00
"sort"
"time"
"github.com/containrrr/watchtower/pkg/filters"
"github.com/containrrr/watchtower/pkg/sorter"
"github.com/sirupsen/logrus"
2015-07-21 16:04:41 +00:00
log "github.com/sirupsen/logrus"
2019-07-21 20:14:28 +02:00
"github.com/containrrr/watchtower/pkg/container"
2015-07-21 16:04:41 +00:00
)
// CheckForMultipleWatchtowerInstances 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. This behaviour can be bypassed
// if a scope UID is defined.
func CheckForMultipleWatchtowerInstances(client container.Client, cleanup bool, scope string) error {
awaitDockerClient()
containers, err := client.ListContainers(filters.FilterByScope(scope, filters.WatchtowerContainersFilter))
2015-07-21 16:04:41 +00:00
if err != nil {
log.Fatal(err)
2015-07-21 16:04:41 +00:00
return err
}
if len(containers) <= 1 {
log.Debug("There are no additional watchtower containers")
return nil
}
log.Info("Found multiple running watchtower instances. Cleaning up.")
return cleanupExcessWatchtowers(containers, client, cleanup)
}
2015-07-21 16:04:41 +00:00
func cleanupExcessWatchtowers(containers []container.Container, client container.Client, cleanup bool) error {
var stopErrors int
sort.Sort(sorter.ByCreated(containers))
allContainersExceptLast := containers[0 : len(containers)-1]
for _, c := range allContainersExceptLast {
if err := client.StopContainer(c, 10*time.Minute); err != nil {
// logging the original here as we're just returning a count
logrus.WithError(err).Error("Could not stop a previous watchtower instance.")
stopErrors++
continue
}
2019-07-22 12:10:57 +02:00
if cleanup {
if err := client.RemoveImageByID(c.ImageID()); err != nil {
logrus.WithError(err).Warning("Could not cleanup watchtower images, possibly because of other watchtowers instances in other scopes.")
}
2015-07-21 16:04:41 +00:00
}
}
if stopErrors > 0 {
return fmt.Errorf("%d errors while stopping watchtower containers", stopErrors)
}
return nil
}
func awaitDockerClient() {
2019-08-25 06:43:03 -04:00
log.Debug("Sleeping for a second to ensure the docker api client has been properly initialized.")
time.Sleep(1 * time.Second)
2015-07-21 16:04:41 +00:00
}