mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
Fix notifications and old instance cleanup (#748)
Co-authored-by: Simon Aronsson <simme@arcticbit.se>
This commit is contained in:
parent
06e705d538
commit
40ab6fd5ba
7 changed files with 123 additions and 53 deletions
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue