2015-07-21 16:04:41 +00:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2019-04-20 16:44:41 +02:00
|
|
|
"fmt"
|
2021-04-18 18:37:35 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/types"
|
2015-07-21 16:04:41 +00:00
|
|
|
"sort"
|
2019-04-20 16:44:41 +02:00
|
|
|
"time"
|
|
|
|
|
2020-05-02 21:29:44 +03:00
|
|
|
"github.com/containrrr/watchtower/pkg/filters"
|
|
|
|
"github.com/containrrr/watchtower/pkg/sorter"
|
2015-07-21 16:04:41 +00:00
|
|
|
|
2019-04-06 14:27:50 +02: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
|
|
|
)
|
|
|
|
|
2021-04-18 18:37:35 +02:00
|
|
|
// CheckForSanity makes sure everything is sane before starting
|
|
|
|
func CheckForSanity(client container.Client, filter types.Filter, rollingRestarts bool) error {
|
|
|
|
log.Debug("Making sure everything is sane before starting")
|
|
|
|
|
|
|
|
if rollingRestarts {
|
|
|
|
containers, err := client.ListContainers(filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, c := range containers {
|
|
|
|
if len(c.Links()) > 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%q is depending on at least one other container. This is not compatible with rolling restarts",
|
|
|
|
c.Name(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
// CheckForMultipleWatchtowerInstances will ensure that there are not multiple instances of the
|
|
|
|
// watchtower running simultaneously. If multiple watchtower containers are detected, this function
|
2020-08-21 15:13:47 -03:00
|
|
|
// 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 {
|
|
|
|
containers, err := client.ListContainers(filters.FilterByScope(scope, filters.WatchtowerContainersFilter))
|
2019-04-20 16:44:41 +02:00
|
|
|
|
2015-07-21 16:04:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
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
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
func cleanupExcessWatchtowers(containers []container.Container, client container.Client, cleanup bool) error {
|
|
|
|
var stopErrors int
|
2015-07-31 18:24:27 +00:00
|
|
|
|
2020-01-11 23:35:25 +01:00
|
|
|
sort.Sort(sorter.ByCreated(containers))
|
2019-04-20 16:44:41 +02:00
|
|
|
allContainersExceptLast := containers[0 : len(containers)-1]
|
|
|
|
|
|
|
|
for _, c := range allContainersExceptLast {
|
2020-05-02 21:29:44 +03:00
|
|
|
if err := client.StopContainer(c, 10*time.Minute); err != nil {
|
2019-04-20 16:44:41 +02:00
|
|
|
// logging the original here as we're just returning a count
|
2021-04-18 18:37:35 +02:00
|
|
|
log.WithError(err).Error("Could not stop a previous watchtower instance.")
|
2019-04-20 16:44:41 +02:00
|
|
|
stopErrors++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-22 12:10:57 +02:00
|
|
|
if cleanup {
|
2020-01-11 00:28:27 +01:00
|
|
|
if err := client.RemoveImageByID(c.ImageID()); err != nil {
|
2021-04-18 18:37:35 +02:00
|
|
|
log.WithError(err).Warning("Could not cleanup watchtower images, possibly because of other watchtowers instances in other scopes.")
|
2015-07-31 18:24:27 +00:00
|
|
|
}
|
2015-07-21 16:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 11:12:54 +01:00
|
|
|
if stopErrors > 0 {
|
|
|
|
return fmt.Errorf("%d errors while stopping watchtower containers", stopErrors)
|
2019-04-20 16:44:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:12:54 +01:00
|
|
|
return nil
|
2019-04-20 16:44:41 +02:00
|
|
|
}
|