2015-07-21 18:04:41 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2019-04-20 16:44:41 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-07-21 18:04:41 +02:00
|
|
|
"sort"
|
2019-04-20 16:44:41 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-05-02 20:29:44 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/filters"
|
|
|
|
"github.com/containrrr/watchtower/pkg/sorter"
|
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
"github.com/opencontainers/runc/Godeps/_workspace/src/github.com/Sirupsen/logrus"
|
2015-07-21 18:04:41 +02: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 18:04:41 +02:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// will stop and remove all but the most recently started container.
|
|
|
|
func CheckForMultipleWatchtowerInstances(client container.Client, cleanup bool) error {
|
|
|
|
awaitDockerClient()
|
2020-01-12 00:35:25 +02:00
|
|
|
containers, err := client.ListContainers(filters.WatchtowerContainersFilter)
|
2019-04-20 16:44:41 +02:00
|
|
|
|
2015-07-21 18:04:41 +02:00
|
|
|
if err != nil {
|
2019-04-20 16:44:41 +02:00
|
|
|
log.Fatal(err)
|
2015-07-21 18:04:41 +02:00
|
|
|
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 18:04:41 +02:00
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
func cleanupExcessWatchtowers(containers []container.Container, client container.Client, cleanup bool) error {
|
|
|
|
var cleanupErrors int
|
|
|
|
var stopErrors int
|
2015-07-31 20:24:27 +02:00
|
|
|
|
2020-01-12 00:35:25 +02: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 20:29:44 +02: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
|
|
|
|
logrus.Error(err)
|
|
|
|
stopErrors++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-22 12:10:57 +02:00
|
|
|
if cleanup {
|
2020-01-11 01:28:27 +02:00
|
|
|
if err := client.RemoveImageByID(c.ImageID()); err != nil {
|
2019-04-20 16:44:41 +02:00
|
|
|
// logging the original here as we're just returning a count
|
|
|
|
logrus.Error(err)
|
|
|
|
cleanupErrors++
|
2015-07-31 20:24:27 +02:00
|
|
|
}
|
2015-07-21 18:04:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 16:44:41 +02:00
|
|
|
return createErrorIfAnyHaveOccurred(stopErrors, cleanupErrors)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createErrorIfAnyHaveOccurred(c int, i int) error {
|
|
|
|
if c == 0 && i == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
func awaitDockerClient() {
|
2019-08-25 12:43:03 +02:00
|
|
|
log.Debug("Sleeping for a second to ensure the docker api client has been properly initialized.")
|
2019-04-20 16:44:41 +02:00
|
|
|
time.Sleep(1 * time.Second)
|
2015-07-21 18:04:41 +02:00
|
|
|
}
|