Added noUpdate run parameter, just pull new image without updating container

This commit is contained in:
Krystian Matti 2018-01-02 15:47:39 +01:00
parent 82a2df38ed
commit 338b54a00e
2 changed files with 40 additions and 31 deletions

View file

@ -34,7 +34,7 @@ func containerFilter(names []string) container.Filter {
// used to start those containers have been updated. If a change is detected in // 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 // any of the images, the associated containers are stopped and restarted with
// the new image. // the new image.
func Update(client container.Client, names []string, cleanup bool, noRestart bool) error { func Update(client container.Client, names []string, cleanup bool, noRestart bool, noUpdate bool) error {
log.Debug("Checking containers for updated images") log.Debug("Checking containers for updated images")
containers, err := client.ListContainers(containerFilter(names)) containers, err := client.ListContainers(containerFilter(names))
@ -59,43 +59,45 @@ func Update(client container.Client, names []string, cleanup bool, noRestart boo
checkDependencies(containers) checkDependencies(containers)
// Stop stale containers in reverse order if !noUpdate {
for i := len(containers) - 1; i >= 0; i-- { // Stop stale containers in reverse order
container := containers[i] for i := len(containers) - 1; i >= 0; i-- {
container := containers[i]
if container.IsWatchtower() {
continue
}
if container.Stale {
if err := client.StopContainer(container, waitTime); err != nil {
log.Error(err)
}
}
}
// Restart stale containers in sorted order
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 container.IsWatchtower() {
if err := client.RenameContainer(container, randName()); err != nil { continue
log.Error(err)
continue
}
} }
if !noRestart { if container.Stale {
if err := client.StartContainer(container); err != nil { if err := client.StopContainer(container, waitTime); err != nil {
log.Error(err) log.Error(err)
} }
} }
}
if cleanup { // Restart stale containers in sorted order
client.RemoveImage(container) 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 !noRestart {
if err := client.StartContainer(container); err != nil {
log.Error(err)
}
}
if cleanup {
client.RemoveImage(container)
}
} }
} }
} }

View file

@ -29,6 +29,7 @@ var (
scheduleSpec string scheduleSpec string
cleanup bool cleanup bool
noRestart bool noRestart bool
noUpdate bool
notifier *notifications.Notifier notifier *notifications.Notifier
) )
@ -76,6 +77,11 @@ func main() {
Usage: "remove old images after updating", Usage: "remove old images after updating",
EnvVar: "WATCHTOWER_CLEANUP", EnvVar: "WATCHTOWER_CLEANUP",
}, },
cli.BoolFlag{
Name: "no-update",
Usage: "do not update container",
EnvVar: "WATCHTOWER_NO_UPDATE",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "tlsverify", Name: "tlsverify",
Usage: "use TLS and verify the remote", Usage: "use TLS and verify the remote",
@ -146,6 +152,7 @@ func before(c *cli.Context) error {
cleanup = c.GlobalBool("cleanup") cleanup = c.GlobalBool("cleanup")
noRestart = c.GlobalBool("no-restart") noRestart = c.GlobalBool("no-restart")
noUpdate = c.GlobalBool("no-update")
// configure environment vars for client // configure environment vars for client
err := envConfig(c) err := envConfig(c)
@ -177,7 +184,7 @@ func start(c *cli.Context) error {
case v := <- tryLockSem: case v := <- tryLockSem:
defer func() { tryLockSem <- v }() defer func() { tryLockSem <- v }()
notifier.StartNotification() notifier.StartNotification()
if err := actions.Update(client, names, cleanup, noRestart); err != nil { if err := actions.Update(client, names, cleanup, noRestart, noUpdate); err != nil {
log.Println(err) log.Println(err)
} }
notifier.SendNotification() notifier.SendNotification()