1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-15 09:14:13 +02:00
watchtower/internal/actions/update.go

144 lines
3.8 KiB
Go
Raw Normal View History

2015-07-21 18:04:41 +02:00
package actions
import (
"github.com/containrrr/watchtower/internal/util"
2019-07-21 20:14:28 +02:00
"github.com/containrrr/watchtower/pkg/container"
"github.com/containrrr/watchtower/pkg/lifecycle"
"github.com/containrrr/watchtower/pkg/sorter"
"github.com/containrrr/watchtower/pkg/types"
log "github.com/sirupsen/logrus"
)
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.
func Update(client container.Client, params types.UpdateParams) error {
log.Debug("Checking containers for updated images")
if params.LifecycleHooks {
lifecycle.ExecutePreChecks(client, params)
}
2019-04-07 15:52:56 +02:00
containers, err := client.ListContainers(params.Filter)
if err != nil {
return err
}
2015-07-22 00:41:58 +02:00
for i, container := range containers {
stale, err := client.IsContainerStale(container)
if err != nil {
2019-04-07 15:52:56 +02:00
log.Infof("Unable to update container %s. Proceeding to next.", containers[i].Name())
log.Debug(err)
2016-10-13 19:14:41 +02:00
stale = false
}
2015-07-22 00:41:58 +02:00
containers[i].Stale = stale
}
containers, err = sorter.SortByDependencies(containers)
if err != nil {
return err
}
checkDependencies(containers)
2019-04-07 15:52:56 +02:00
if params.MonitorOnly {
if params.LifecycleHooks {
lifecycle.ExecutePostChecks(client, params)
}
2019-04-07 15:52:56 +02:00
return nil
}
stopContainersInReversedOrder(containers, client, params)
restartContainersInSortedOrder(containers, client, params)
if params.LifecycleHooks {
lifecycle.ExecutePostChecks(client, params)
}
return nil
}
func stopContainersInReversedOrder(containers []container.Container, client container.Client, params types.UpdateParams) {
for i := len(containers) - 1; i >= 0; i-- {
stopStaleContainer(containers[i], client, params)
}
}
2015-07-21 00:54:18 +02:00
func stopStaleContainer(container container.Container, client container.Client, params types.UpdateParams) {
if container.IsWatchtower() {
log.Debugf("This is the watchtower container %s", container.Name())
return
}
2015-07-21 00:54:18 +02:00
if !container.Stale {
return
}
if params.LifecycleHooks {
lifecycle.ExecutePreUpdateCommand(client, container)
}
if err := client.StopContainer(container, params.Timeout); err != nil {
log.Error(err)
}
}
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params types.UpdateParams) {
imageIDs := make(map[string]bool)
for _, container := range containers {
if !container.Stale {
continue
}
restartStaleContainer(container, client, params)
imageIDs[container.ImageID()] = true
}
if params.Cleanup {
for imageID := range imageIDs {
if err := client.RemoveImageByID(imageID); err != nil {
log.Error(err)
}
}
}
}
2015-07-21 00:54:18 +02:00
func restartStaleContainer(container container.Container, client container.Client, params types.UpdateParams) {
// 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)
return
}
}
if !params.NoRestart {
if newContainerID, err := client.StartContainer(container); err != nil {
log.Error(err)
} else if container.Stale && params.LifecycleHooks {
lifecycle.ExecutePostUpdateCommand(client, newContainerID)
}
}
}
2015-07-21 18:04:41 +02:00
func checkDependencies(containers []container.Container) {
for i, parent := range containers {
if parent.ToRestart() {
continue
}
LinkLoop:
for _, linkName := range parent.Links() {
for _, child := range containers {
if child.Name() == linkName && child.ToRestart() {
containers[i].Linked = true
break LinkLoop
}
}
}
}
}