refactor: split out more code into separate files

pull/350/head^2
Simon Aronsson 5 years ago
parent a425bf1024
commit 6c507433e8

@ -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,33 +39,52 @@ 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)
}
}
func stopStaleContainer(container container.Container, client container.Client, params UpdateParams) {
if container.IsWatchtower() {
log.Debugf("This is the watchtower container %s", containers[i].Name())
continue
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
}
err := client.StopContainer(container, params.Timeout)
if err != nil {
log.Error(err)
}
}
// Restart stale containers in sorted order
func restartContainersInSortedOrder(containers []container.Container, client container.Client, params UpdateParams) {
for _, container := range containers {
if container.Stale {
if !container.Stale {
continue
}
restartStaleContainer(container, client, params)
}
}
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, randName()); err != nil {
if err := client.RenameContainer(container, util.RandName()); err != nil {
log.Error(err)
continue
return
}
}
@ -95,10 +98,6 @@ func Update(client container.Client, params UpdateParams) error {
client.RemoveImage(container)
}
}
}
return nil
}
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)
}

@ -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
}

@ -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)
}
Loading…
Cancel
Save