From 6c507433e8b9f5fc264861c81dd83cf605bbd651 Mon Sep 17 00:00:00 2001 From: Simon Aronsson Date: Mon, 22 Jul 2019 10:20:11 +0200 Subject: [PATCH] refactor: split out more code into separate files --- internal/actions/update.go | 107 ++++++++++++++---------------- internal/actions/update_params.go | 15 +++++ internal/util/rand_name.go | 15 +++++ 3 files changed, 78 insertions(+), 59 deletions(-) create mode 100644 internal/actions/update_params.go create mode 100644 internal/util/rand_name.go diff --git a/internal/actions/update.go b/internal/actions/update.go index 6827687..a3cf928 100644 --- a/internal/actions/update.go +++ b/internal/actions/update.go @@ -1,27 +1,11 @@ package actions import ( - "math/rand" - "time" - + "github.com/containrrr/watchtower/internal/util" "github.com/containrrr/watchtower/pkg/container" - t "github.com/containrrr/watchtower/pkg/types" log "github.com/sirupsen/logrus" ) -var ( - letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") -) - -// UpdateParams contains all different options available to alter the behavior of the Update func -type UpdateParams struct { - Filter t.Filter - Cleanup bool - NoRestart bool - Timeout time.Duration - MonitorOnly bool -} - // 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 @@ -55,49 +39,64 @@ func Update(client container.Client, params UpdateParams) error { return nil } - // Stop stale containers in reverse order + stopContainersInReversedOrder(containers, client, params) + restartContainersInSortedOrder(containers, client, params) + + return nil +} + +func stopContainersInReversedOrder(containers []container.Container, client container.Client, params UpdateParams) { for i := len(containers) - 1; i >= 0; i-- { - container := containers[i] + stopStaleContainer(containers[i], client, params) + } +} - if container.IsWatchtower() { - log.Debugf("This is the watchtower container %s", containers[i].Name()) - continue - } +func stopStaleContainer(container container.Container, client container.Client, params UpdateParams) { + if container.IsWatchtower() { + log.Debugf("This is the watchtower container %s", container.Name()) + return + } - if container.Stale { - if err := client.StopContainer(container, params.Timeout); err != nil { - log.Error(err) - } - } + if !container.Stale { + return } - // Restart stale containers in sorted order + err := client.StopContainer(container, params.Timeout) + if err != nil { + log.Error(err) + } +} + +func restartContainersInSortedOrder(containers []container.Container, client container.Client, params UpdateParams) { for _, container := range containers { - if container.Stale { - // 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, randName()); err != nil { - log.Error(err) - continue - } - } + if !container.Stale { + continue + } + restartStaleContainer(container, client, params) + } +} - if !params.NoRestart { - if err := client.StartContainer(container); err != nil { - log.Error(err) - } - } +func restartStaleContainer(container container.Container, client container.Client, params 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.Cleanup { - client.RemoveImage(container) - } + if !params.NoRestart { + if err := client.StartContainer(container); err != nil { + log.Error(err) } } - return nil + if params.Cleanup { + client.RemoveImage(container) + } } func checkDependencies(containers []container.Container) { @@ -118,13 +117,3 @@ func checkDependencies(containers []container.Container) { } } } - -// Generates a random, 32-character, Docker-compatible container name. -func randName() string { - b := make([]rune, 32) - for i := range b { - b[i] = letters[rand.Intn(len(letters))] - } - - return string(b) -} diff --git a/internal/actions/update_params.go b/internal/actions/update_params.go new file mode 100644 index 0000000..851f23e --- /dev/null +++ b/internal/actions/update_params.go @@ -0,0 +1,15 @@ +package actions + +import ( + t "github.com/containrrr/watchtower/pkg/types" + "time" +) + +// UpdateParams contains all different options available to alter the behavior of the Update func +type UpdateParams struct { + Filter t.Filter + Cleanup bool + NoRestart bool + Timeout time.Duration + MonitorOnly bool +} diff --git a/internal/util/rand_name.go b/internal/util/rand_name.go new file mode 100644 index 0000000..76f6a3f --- /dev/null +++ b/internal/util/rand_name.go @@ -0,0 +1,15 @@ +package util + +import "math/rand" + +var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + +// RandName Generates a random, 32-character, Docker-compatible container name. +func RandName() string { + b := make([]rune, 32) + for i := range b { + b[i] = letters[rand.Intn(len(letters))] + } + + return string(b) +}