From baf5e50051f0dc4c4d6dac77ae1503cacb51fdcf Mon Sep 17 00:00:00 2001 From: Zois Pagoulatos Date: Sun, 21 Jul 2019 18:00:56 +0200 Subject: [PATCH] Re-apply based on new go flags package (#336) --- actions/actions_suite_test.go | 24 ++++++++++++++---------- actions/update.go | 8 ++++---- cmd/root.go | 21 +++++++++++---------- container/client.go | 13 ++++++++----- docs/arguments.md | 10 ++++++++++ go.sum | 2 ++ internal/flags/flags.go | 14 +++++++++----- 7 files changed, 58 insertions(+), 34 deletions(-) diff --git a/actions/actions_suite_test.go b/actions/actions_suite_test.go index 850fd4e..fcbe461 100644 --- a/actions/actions_suite_test.go +++ b/actions/actions_suite_test.go @@ -32,9 +32,10 @@ var _ = Describe("the actions package", func() { }) BeforeEach(func() { client = mockClient{ - api: dockerClient, - pullImages: false, - TestData: &TestData{}, + api: dockerClient, + pullImages: false, + removeVolumes: false, + TestData: &TestData{}, } }) @@ -62,8 +63,9 @@ var _ = Describe("the actions package", func() { When("given multiple containers", func() { BeforeEach(func() { client = mockClient{ - api: dockerClient, - pullImages: false, + api: dockerClient, + pullImages: false, + removeVolumes: false, TestData: &TestData{ NameOfContainerToKeep: "test-container-02", Containers: []container.Container{ @@ -89,8 +91,9 @@ var _ = Describe("the actions package", func() { When("deciding whether to cleanup images", func() { BeforeEach(func() { client = mockClient{ - api: dockerClient, - pullImages: false, + api: dockerClient, + pullImages: false, + removeVolumes: false, TestData: &TestData{ Containers: []container.Container{ createMockContainer( @@ -134,9 +137,10 @@ func createMockContainer(id string, name string, image string, created time.Time } type mockClient struct { - TestData *TestData - api cli.CommonAPIClient - pullImages bool + TestData *TestData + api cli.CommonAPIClient + pullImages bool + removeVolumes bool } type TestData struct { diff --git a/actions/update.go b/actions/update.go index a18ea4c..a288729 100644 --- a/actions/update.go +++ b/actions/update.go @@ -14,10 +14,10 @@ var ( // UpdateParams contains all different options available to alter the behavior of the Update func type UpdateParams struct { - Filter container.Filter - Cleanup bool - NoRestart bool - Timeout time.Duration + Filter container.Filter + Cleanup bool + NoRestart bool + Timeout time.Duration MonitorOnly bool } diff --git a/cmd/root.go b/cmd/root.go index 2c1bdff..a64a045 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,17 +1,18 @@ package cmd import ( + "os" + "os/signal" + "strconv" + "syscall" + "time" + "github.com/containrrr/watchtower/actions" "github.com/containrrr/watchtower/container" "github.com/containrrr/watchtower/internal/flags" "github.com/containrrr/watchtower/notifications" "github.com/robfig/cron" log "github.com/sirupsen/logrus" - "os" - "os/signal" - "strconv" - "syscall" - "time" "github.com/spf13/cobra" ) @@ -32,9 +33,9 @@ var ( ) var rootCmd = &cobra.Command{ - Use: "watchtower", - Short: "Automatically updates running Docker containers", - Long: ` + Use: "watchtower", + Short: "Automatically updates running Docker containers", + Long: ` Watchtower automatically updates running Docker containers whenever a new image is released. More information available at https://github.com/containrrr/watchtower/. `, @@ -92,9 +93,11 @@ func PreRun(cmd *cobra.Command, args []string) { noPull, _ := f.GetBool("no-pull") includeStopped, _ := f.GetBool("include-stopped") + removeVolumes, _ := f.GetBool("remove-volumes") client = container.NewClient( !noPull, includeStopped, + removeVolumes, ) notifier = notifications.NewNotifier(cmd) @@ -176,5 +179,3 @@ func runUpdatesWithNotifications(filter container.Filter) { } notifier.SendNotification() } - - diff --git a/container/client.go b/container/client.go index 70a6fb1..b707d0c 100644 --- a/container/client.go +++ b/container/client.go @@ -2,11 +2,12 @@ package container import ( "fmt" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types/filters" "io/ioutil" "time" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" dockerclient "github.com/docker/docker/client" @@ -35,7 +36,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) Client { +func NewClient(pullImages bool, includeStopped bool, removeVolumes bool) Client { cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv) if err != nil { @@ -45,6 +46,7 @@ func NewClient(pullImages bool, includeStopped bool) Client { return dockerClient{ api: cli, pullImages: pullImages, + removeVolumes: removeVolumes, includeStopped: includeStopped, } } @@ -52,6 +54,7 @@ func NewClient(pullImages bool, includeStopped bool) Client { type dockerClient struct { api dockerclient.CommonAPIClient pullImages bool + removeVolumes bool includeStopped bool } @@ -71,7 +74,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) { types.ContainerListOptions{ Filters: filter, }) - + if err != nil { return nil, err } @@ -131,7 +134,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err } else { log.Debugf("Removing container %s", c.ID()) - if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: false}); err != nil { + if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: client.removeVolumes}); err != nil { return err } } diff --git a/docs/arguments.md b/docs/arguments.md index b62fd50..3631476 100644 --- a/docs/arguments.md +++ b/docs/arguments.md @@ -47,6 +47,16 @@ Environment Variable: WATCHTOWER_CLEANUP Default: false ``` +## Remove attached volumes +Removes attached volumes after updating. When this flag is specified, watchtower will remove all attached volumes from the container before restarting container with a new image. Use this option to force new volumes to be populated as containers are updated. + +``` + Argument: --remove-volumes +Environment Variable: WATCHTOWER_REMOVE_VOLUMES + Type: Boolean + Default: false +``` + ## Debug Enable debug mode with verbose logging. diff --git a/go.sum b/go.sum index c9968f6..2107c46 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U= github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= @@ -258,5 +259,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 0915141..d6b0cb1 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -1,11 +1,12 @@ package flags import ( + "os" + "time" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" - "time" ) // RegisterDockerFlags that are used directly by the docker api client @@ -52,6 +53,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { viper.GetBool("WATCHTOWER_CLEANUP"), "remove previously used images after updating") + flags.BoolP( + "remove-volumes", + "", + viper.GetBool("WATCHTOWER_REMOVE_VOLUMES"), + "remove attached volumes before updating") + flags.BoolP( "label-enable", "e", @@ -64,7 +71,6 @@ func RegisterSystemFlags(rootCmd *cobra.Command) { viper.GetBool("WATCHTOWER_DEBUG"), "enable debug mode with verbose logging") - flags.BoolP( "monitor-only", "m", @@ -253,7 +259,6 @@ func ReadFlags(cmd *cobra.Command) (bool, bool, bool, time.Duration) { return cleanup, noRestart, monitorOnly, timeout } - func setEnvOptStr(env string, opt string) error { if opt == "" || opt == os.Getenv(env) { return nil @@ -271,4 +276,3 @@ func setEnvOptBool(env string, opt bool) error { } return nil } -