Fix notifications and old instance cleanup (#748)

Co-authored-by: Simon Aronsson <simme@arcticbit.se>
This commit is contained in:
nils måsén 2021-01-10 11:12:54 +01:00 committed by GitHub
parent 06e705d538
commit 40ab6fd5ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 123 additions and 53 deletions

View file

@ -1,15 +1,12 @@
package actions
import (
"errors"
"fmt"
"sort"
"strings"
"time"
"github.com/containrrr/watchtower/pkg/filters"
"github.com/containrrr/watchtower/pkg/sorter"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
@ -40,7 +37,6 @@ func CheckForMultipleWatchtowerInstances(client container.Client, cleanup bool,
}
func cleanupExcessWatchtowers(containers []container.Container, client container.Client, cleanup bool) error {
var cleanupErrors int
var stopErrors int
sort.Sort(sorter.ByCreated(containers))
@ -49,37 +45,23 @@ func cleanupExcessWatchtowers(containers []container.Container, client container
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.Error(err)
logrus.WithError(err).Error("Could not stop a previous watchtower instance.")
stopErrors++
continue
}
if cleanup {
if err := client.RemoveImageByID(c.ImageID()); err != nil {
// logging the original here as we're just returning a count
logrus.Error(err)
cleanupErrors++
logrus.WithError(err).Warning("Could not cleanup watchtower images, possibly because of other watchtowers instances in other scopes.")
}
}
}
return createErrorIfAnyHaveOccurred(stopErrors, cleanupErrors)
}
func createErrorIfAnyHaveOccurred(c int, i int) error {
if c == 0 && i == 0 {
return nil
if stopErrors > 0 {
return fmt.Errorf("%d errors while stopping watchtower containers", stopErrors)
}
var output strings.Builder
if c > 0 {
output.WriteString(fmt.Sprintf("%d errors while stopping containers", c))
}
if i > 0 {
output.WriteString(fmt.Sprintf("%d errors while cleaning up images", c))
}
return errors.New(output.String())
return nil
}
func awaitDockerClient() {