mirror of https://github.com/containrrr/watchtower
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			85 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Go
		
	
			
		
		
	
	
			85 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Go
		
	
package actions
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
	"github.com/containrrr/watchtower/pkg/types"
 | 
						|
	"sort"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"github.com/containrrr/watchtower/pkg/filters"
 | 
						|
	"github.com/containrrr/watchtower/pkg/sorter"
 | 
						|
 | 
						|
	log "github.com/sirupsen/logrus"
 | 
						|
 | 
						|
	"github.com/containrrr/watchtower/pkg/container"
 | 
						|
)
 | 
						|
 | 
						|
// 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
 | 
						|
}
 | 
						|
 | 
						|
// 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 {
 | 
						|
	containers, err := client.ListContainers(filters.FilterByScope(scope, filters.WatchtowerContainersFilter))
 | 
						|
 | 
						|
	if err != nil {
 | 
						|
		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)
 | 
						|
}
 | 
						|
 | 
						|
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
 | 
						|
			log.WithError(err).Error("Could not stop a previous watchtower instance.")
 | 
						|
			stopErrors++
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		if cleanup {
 | 
						|
			if err := client.RemoveImageByID(c.ImageID()); err != nil {
 | 
						|
				log.WithError(err).Warning("Could not cleanup watchtower images, possibly because of other watchtowers instances in other scopes.")
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	if stopErrors > 0 {
 | 
						|
		return fmt.Errorf("%d errors while stopping watchtower containers", stopErrors)
 | 
						|
	}
 | 
						|
 | 
						|
	return nil
 | 
						|
}
 |