add a monitor only flag

pull/241/head^2
Simon Aronsson 6 years ago
parent 438c90f821
commit d29b6cf93e

@ -12,14 +12,22 @@ var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
) )
type UpdateParams struct {
Filter container.Filter
Cleanup bool
NoRestart bool
Timeout time.Duration
MonitorOnly bool
}
// Update looks at the running Docker containers to see if any of the images // 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 // 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, filter container.Filter, cleanup bool, noRestart bool, timeout time.Duration) error { func Update(client container.Client, params UpdateParams) error {
log.Debug("Checking containers for updated images") log.Debug("Checking containers for updated images")
containers, err := client.ListContainers(filter) containers, err := client.ListContainers(params.Filter)
if err != nil { if err != nil {
return err return err
} }
@ -27,7 +35,8 @@ func Update(client container.Client, filter container.Filter, cleanup bool, noRe
for i, container := range containers { for i, container := range containers {
stale, err := client.IsContainerStale(container) stale, err := client.IsContainerStale(container)
if err != nil { if err != nil {
log.Infof("Unable to update container %s, err='%s'. Proceeding to next.", containers[i].Name(), err) log.Infof("Unable to update container %s. Proceeding to next.", containers[i].Name())
log.Debug(err)
stale = false stale = false
} }
containers[i].Stale = stale containers[i].Stale = stale
@ -40,6 +49,10 @@ func Update(client container.Client, filter container.Filter, cleanup bool, noRe
checkDependencies(containers) checkDependencies(containers)
if params.MonitorOnly {
return nil
}
// Stop stale containers in reverse order // Stop stale containers in reverse order
for i := len(containers) - 1; i >= 0; i-- { for i := len(containers) - 1; i >= 0; i-- {
container := containers[i] container := containers[i]
@ -50,7 +63,7 @@ func Update(client container.Client, filter container.Filter, cleanup bool, noRe
} }
if container.Stale { if container.Stale {
if err := client.StopContainer(container, timeout); err != nil { if err := client.StopContainer(container, params.Timeout); err != nil {
log.Error(err) log.Error(err)
} }
} }
@ -70,13 +83,13 @@ func Update(client container.Client, filter container.Filter, cleanup bool, noRe
} }
} }
if !noRestart { if !params.NoRestart {
if err := client.StartContainer(container); err != nil { if err := client.StartContainer(container); err != nil {
log.Error(err) log.Error(err)
} }
} }
if cleanup { if params.Cleanup {
client.RemoveImage(container) client.RemoveImage(container)
} }
} }

@ -29,6 +29,7 @@ var (
scheduleSpec string scheduleSpec string
cleanup bool cleanup bool
noRestart bool noRestart bool
monitorOnly bool
enableLabel bool enableLabel bool
notifier *notifications.Notifier notifier *notifications.Notifier
timeout time.Duration timeout time.Duration
@ -171,6 +172,11 @@ func main() {
Usage: "The MSTeams notifier will try to extract log entry fields as MSTeams message facts", Usage: "The MSTeams notifier will try to extract log entry fields as MSTeams message facts",
EnvVar: "WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA", EnvVar: "WATCHTOWER_NOTIFICATION_MSTEAMS_USE_LOG_DATA",
}, },
cli.BoolFlag{
Name: "monitor-only",
Usage: "Will only monitor for new images, not update the containers",
EnvVar: "WATCHTOWER_MONITOR_ONLY",
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -196,6 +202,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")
monitorOnly = c.GlobalBool("monitor-only")
timeout = c.GlobalDuration("stop-timeout") timeout = c.GlobalDuration("stop-timeout")
if timeout < 0 { if timeout < 0 {
log.Fatal("Please specify a positive value for timeout value.") log.Fatal("Please specify a positive value for timeout value.")
@ -234,7 +241,14 @@ 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, filter, cleanup, noRestart, timeout); err != nil { updateParams := actions.UpdateParams{
Filter: filter,
Cleanup: cleanup,
NoRestart: noRestart,
Timeout: timeout,
MonitorOnly: monitorOnly,
}
if err := actions.Update(client, updateParams); err != nil {
log.Println(err) log.Println(err)
} }
notifier.SendNotification() notifier.SendNotification()

Loading…
Cancel
Save