2015-07-21 18:04:41 +02:00
|
|
|
package actions
|
2015-07-13 23:41:04 +02:00
|
|
|
|
|
|
|
import (
|
2020-08-18 20:55:35 +02:00
|
|
|
"errors"
|
2019-07-22 10:20:11 +02:00
|
|
|
"github.com/containrrr/watchtower/internal/util"
|
2019-07-21 20:14:28 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/container"
|
2020-01-12 00:35:25 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/lifecycle"
|
2021-01-06 23:28:32 +02:00
|
|
|
metrics2 "github.com/containrrr/watchtower/pkg/metrics"
|
2020-01-12 00:35:25 +02:00
|
|
|
"github.com/containrrr/watchtower/pkg/sorter"
|
|
|
|
"github.com/containrrr/watchtower/pkg/types"
|
2019-04-04 23:08:44 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-07-13 23:41:04 +02:00
|
|
|
)
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// Update looks at the running Docker containers to see if any of the images
|
|
|
|
// used to start those containers have been updated. If a change is detected in
|
|
|
|
// any of the images, the associated containers are stopped and restarted with
|
|
|
|
// the new image.
|
2021-01-06 23:28:32 +02:00
|
|
|
func Update(client container.Client, params types.UpdateParams) (*metrics2.Metric, error) {
|
2017-04-12 18:32:00 +02:00
|
|
|
log.Debug("Checking containers for updated images")
|
2021-01-06 23:28:32 +02:00
|
|
|
metric := &metrics2.Metric{}
|
|
|
|
staleCount := 0
|
2015-07-22 23:58:16 +02:00
|
|
|
|
2020-01-12 00:35:25 +02:00
|
|
|
if params.LifecycleHooks {
|
|
|
|
lifecycle.ExecutePreChecks(client, params)
|
|
|
|
}
|
2019-09-15 16:58:46 +02:00
|
|
|
|
2019-04-07 15:52:56 +02:00
|
|
|
containers, err := client.ListContainers(params.Filter)
|
2015-07-13 23:41:04 +02:00
|
|
|
if err != nil {
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil, err
|
2015-07-13 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
staleCheckFailed := 0
|
|
|
|
|
2020-08-18 20:55:35 +02:00
|
|
|
for i, targetContainer := range containers {
|
|
|
|
stale, err := client.IsContainerStale(targetContainer)
|
2020-10-03 22:00:02 +02:00
|
|
|
if stale && !params.NoRestart && !params.MonitorOnly && !targetContainer.IsMonitorOnly() && !targetContainer.HasImageInfo() {
|
2020-08-18 20:55:35 +02:00
|
|
|
err = errors.New("no available image info")
|
|
|
|
}
|
2015-07-22 00:41:58 +02:00
|
|
|
if err != nil {
|
2020-08-18 20:55:35 +02:00
|
|
|
log.Infof("Unable to update container %q: %v. Proceeding to next.", containers[i].Name(), err)
|
2016-10-13 19:14:41 +02:00
|
|
|
stale = false
|
2021-01-06 23:28:32 +02:00
|
|
|
staleCheckFailed++
|
|
|
|
metric.Failed++
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2015-07-22 00:41:58 +02:00
|
|
|
containers[i].Stale = stale
|
2021-01-06 23:28:32 +02:00
|
|
|
|
|
|
|
if stale {
|
|
|
|
staleCount++
|
|
|
|
}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2015-07-13 23:41:04 +02:00
|
|
|
|
2020-01-12 00:35:25 +02:00
|
|
|
containers, err = sorter.SortByDependencies(containers)
|
2021-01-06 23:28:32 +02:00
|
|
|
metric.Scanned = len(containers)
|
2015-07-14 22:51:12 +02:00
|
|
|
if err != nil {
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil, err
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2015-07-13 23:41:04 +02:00
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
checkDependencies(containers)
|
2015-07-13 23:41:04 +02:00
|
|
|
|
2020-10-03 22:00:02 +02:00
|
|
|
containersToUpdate := []container.Container{}
|
|
|
|
if !params.MonitorOnly {
|
|
|
|
for i := len(containers) - 1; i >= 0; i-- {
|
|
|
|
if !containers[i].IsMonitorOnly() {
|
|
|
|
containersToUpdate = append(containersToUpdate, containers[i])
|
|
|
|
}
|
2020-01-12 00:35:25 +02:00
|
|
|
}
|
2019-04-07 15:52:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 22:35:46 +02:00
|
|
|
if params.RollingRestart {
|
2021-01-06 23:28:32 +02:00
|
|
|
metric.Failed += performRollingRestart(containersToUpdate, client, params)
|
2020-08-21 22:35:46 +02:00
|
|
|
} else {
|
2021-01-06 23:28:32 +02:00
|
|
|
metric.Failed += stopContainersInReversedOrder(containersToUpdate, client, params)
|
|
|
|
metric.Failed += restartContainersInSortedOrder(containersToUpdate, client, params)
|
2020-08-21 22:35:46 +02:00
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
|
|
|
|
metric.Updated = staleCount - (metric.Failed - staleCheckFailed)
|
|
|
|
|
2020-01-12 00:35:25 +02:00
|
|
|
if params.LifecycleHooks {
|
|
|
|
lifecycle.ExecutePostChecks(client, params)
|
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
return metric, nil
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
func performRollingRestart(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
2020-08-21 22:35:46 +02:00
|
|
|
cleanupImageIDs := make(map[string]bool)
|
2021-01-06 23:28:32 +02:00
|
|
|
failed := 0
|
2020-08-21 22:35:46 +02:00
|
|
|
|
|
|
|
for i := len(containers) - 1; i >= 0; i-- {
|
|
|
|
if containers[i].Stale {
|
2021-01-06 23:28:32 +02:00
|
|
|
if err := stopStaleContainer(containers[i], client, params); err != nil {
|
|
|
|
failed++
|
|
|
|
}
|
|
|
|
if err := restartStaleContainer(containers[i], client, params); err != nil {
|
|
|
|
failed++
|
|
|
|
}
|
2020-12-20 19:16:32 +02:00
|
|
|
cleanupImageIDs[containers[i].ImageID()] = true
|
2020-08-21 22:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cleanup {
|
|
|
|
cleanupImages(client, cleanupImageIDs)
|
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
return failed
|
2020-08-21 22:35:46 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
func stopContainersInReversedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
|
|
|
failed := 0
|
2015-07-14 22:51:12 +02:00
|
|
|
for i := len(containers) - 1; i >= 0; i-- {
|
2021-01-06 23:28:32 +02:00
|
|
|
if err := stopStaleContainer(containers[i], client, params); err != nil {
|
|
|
|
failed++
|
|
|
|
}
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
return failed
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
func stopStaleContainer(container container.Container, client container.Client, params types.UpdateParams) error {
|
2019-07-22 10:20:11 +02:00
|
|
|
if container.IsWatchtower() {
|
|
|
|
log.Debugf("This is the watchtower container %s", container.Name())
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2019-07-22 10:20:11 +02:00
|
|
|
if !container.Stale {
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
2020-01-12 00:35:25 +02:00
|
|
|
if params.LifecycleHooks {
|
2020-01-03 19:51:32 +02:00
|
|
|
if err := lifecycle.ExecutePreUpdateCommand(client, container); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
log.Info("Skipping container as the pre-update command failed")
|
2021-01-06 23:28:32 +02:00
|
|
|
return err
|
2020-01-03 19:51:32 +02:00
|
|
|
}
|
2020-01-12 00:35:25 +02:00
|
|
|
}
|
2020-04-24 13:45:24 +02:00
|
|
|
|
2019-07-27 01:37:16 +02:00
|
|
|
if err := client.StopContainer(container, params.Timeout); err != nil {
|
2019-07-22 10:20:11 +02:00
|
|
|
log.Error(err)
|
2021-01-06 23:28:32 +02:00
|
|
|
return err
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams) int {
|
2020-01-11 01:28:27 +02:00
|
|
|
imageIDs := make(map[string]bool)
|
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
failed := 0
|
|
|
|
|
|
|
|
for _, c := range containers {
|
|
|
|
if !c.Stale {
|
2019-07-22 10:20:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
if err := restartStaleContainer(c, client, params); err != nil {
|
|
|
|
failed++
|
|
|
|
}
|
|
|
|
imageIDs[c.ImageID()] = true
|
2019-10-13 22:46:06 +02:00
|
|
|
}
|
2020-08-21 22:35:46 +02:00
|
|
|
|
2019-10-13 22:46:06 +02:00
|
|
|
if params.Cleanup {
|
2020-08-21 22:35:46 +02:00
|
|
|
cleanupImages(client, imageIDs)
|
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
|
|
|
|
return failed
|
2020-08-21 22:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupImages(client container.Client, imageIDs map[string]bool) {
|
|
|
|
for imageID := range imageIDs {
|
|
|
|
if err := client.RemoveImageByID(imageID); err != nil {
|
|
|
|
log.Error(err)
|
2019-10-13 22:46:06 +02:00
|
|
|
}
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-21 00:54:18 +02:00
|
|
|
|
2021-01-06 23:28:32 +02:00
|
|
|
func restartStaleContainer(container container.Container, client container.Client, params types.UpdateParams) error {
|
2019-07-22 10:20:11 +02:00
|
|
|
// Since we can't shutdown a watchtower container immediately, we need to
|
|
|
|
// start the new one while the old one is still running. This prevents us
|
|
|
|
// from re-using the same container name so we first rename the current
|
|
|
|
// instance so that the new one can adopt the old name.
|
|
|
|
if container.IsWatchtower() {
|
|
|
|
if err := client.RenameContainer(container, util.RandName()); err != nil {
|
|
|
|
log.Error(err)
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil
|
2019-07-22 10:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 20:24:27 +02:00
|
|
|
|
2019-07-22 10:20:11 +02:00
|
|
|
if !params.NoRestart {
|
2019-07-27 01:37:16 +02:00
|
|
|
if newContainerID, err := client.StartContainer(container); err != nil {
|
2019-07-22 10:20:11 +02:00
|
|
|
log.Error(err)
|
2021-01-06 23:28:32 +02:00
|
|
|
return err
|
2019-07-27 01:37:16 +02:00
|
|
|
} else if container.Stale && params.LifecycleHooks {
|
2020-01-12 00:35:25 +02:00
|
|
|
lifecycle.ExecutePostUpdateCommand(client, newContainerID)
|
2015-07-13 23:41:04 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-06 23:28:32 +02:00
|
|
|
return nil
|
2015-07-13 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 18:04:41 +02:00
|
|
|
func checkDependencies(containers []container.Container) {
|
2015-07-13 23:41:04 +02:00
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
for i, parent := range containers {
|
2019-07-27 01:37:16 +02:00
|
|
|
if parent.ToRestart() {
|
2015-07-14 22:51:12 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-07-13 23:41:04 +02:00
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
LinkLoop:
|
|
|
|
for _, linkName := range parent.Links() {
|
|
|
|
for _, child := range containers {
|
2019-07-27 01:37:16 +02:00
|
|
|
if child.Name() == linkName && child.ToRestart() {
|
|
|
|
containers[i].Linked = true
|
2015-07-14 22:51:12 +02:00
|
|
|
break LinkLoop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-13 23:41:04 +02:00
|
|
|
}
|
2020-01-03 19:51:32 +02:00
|
|
|
}
|