From 2d8507ca313a3d726798cb07ac21e86ca4a79ab2 Mon Sep 17 00:00:00 2001 From: Zois Pagoulatos Date: Wed, 13 Nov 2019 11:16:37 +0100 Subject: [PATCH] Add --revive-stopped flag to start stopped containers after an update (#403) * Add --revive-stopped flag to start stopped containers after an update * Update arguments.md --- cmd/root.go | 2 ++ docs/arguments.md | 10 ++++++++++ internal/flags/flags.go | 8 +++++++- pkg/container/client.go | 6 ++++-- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ea00786..453196a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -93,11 +93,13 @@ func PreRun(cmd *cobra.Command, args []string) { noPull, _ := f.GetBool("no-pull") includeStopped, _ := f.GetBool("include-stopped") + reviveStopped, _ := f.GetBool("revive-stopped") removeVolumes, _ := f.GetBool("remove-volumes") client = container.NewClient( !noPull, includeStopped, + reviveStopped, removeVolumes, ) diff --git a/docs/arguments.md b/docs/arguments.md index db51c95..dc2c0ee 100644 --- a/docs/arguments.md +++ b/docs/arguments.md @@ -97,6 +97,16 @@ Environment Variable: WATCHTOWER_INCLUDE_STOPPED Default: false ``` +## Revive stopped +Start any stopped containers that have had their image updated. This argument is only usable with the `--include-stopped` argument. + +``` + Argument: --revive-stopped +Environment Variable: WATCHTOWER_REVIVE_STOPPED + Type: Boolean + Default: false +``` + ## Poll interval Poll interval (in seconds). This value controls how frequently watchtower will poll for new images. diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 6e9ea55..a8f330b 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -94,6 +94,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { viper.GetBool("WATCHTOWER_INCLUDE_STOPPED"), "Will also include created and exited containers") + flags.BoolP( + "revive-stopped", + "", + viper.GetBool("WATCHTOWER_REVIVE_STOPPED"), + "Will also start stopped containers that were updated, if include-stopped is active") + flags.BoolP( "enable-lifecycle-hooks", "", @@ -128,7 +134,7 @@ func RegisterNotificationFlags(rootCmd *cobra.Command) { "", viper.GetString("WATCHTOWER_NOTIFICATION_EMAIL_TO"), "Address to send notification emails to") - + flags.IntP( "notification-email-delay", "", diff --git a/pkg/container/client.go b/pkg/container/client.go index 0dc22db..5877eb4 100644 --- a/pkg/container/client.go +++ b/pkg/container/client.go @@ -38,7 +38,7 @@ type Client interface { // * DOCKER_HOST the docker-engine host to send api requests to // * DOCKER_TLS_VERIFY whether to verify tls certificates // * DOCKER_API_VERSION the minimum docker api version to work with -func NewClient(pullImages bool, includeStopped bool, removeVolumes bool) Client { +func NewClient(pullImages bool, includeStopped bool, reviveStopped bool, removeVolumes bool) Client { cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv) if err != nil { @@ -50,6 +50,7 @@ func NewClient(pullImages bool, includeStopped bool, removeVolumes bool) Client pullImages: pullImages, removeVolumes: removeVolumes, includeStopped: includeStopped, + reviveStopped: reviveStopped, } } @@ -58,6 +59,7 @@ type dockerClient struct { pullImages bool removeVolumes bool includeStopped bool + reviveStopped bool } func (client dockerClient) ListContainers(fn t.Filter) ([]Container, error) { @@ -203,7 +205,7 @@ func (client dockerClient) StartContainer(c Container) (string, error) { } - if !c.IsRunning() { + if !c.IsRunning() && !client.reviveStopped { return createdContainer.ID, nil }