Re-apply based on new go flags package (#336)

pull/350/head^2
Zois Pagoulatos 5 years ago committed by Simon Aronsson
parent 9eca883f17
commit baf5e50051

@ -34,6 +34,7 @@ var _ = Describe("the actions package", func() {
client = mockClient{ client = mockClient{
api: dockerClient, api: dockerClient,
pullImages: false, pullImages: false,
removeVolumes: false,
TestData: &TestData{}, TestData: &TestData{},
} }
}) })
@ -64,6 +65,7 @@ var _ = Describe("the actions package", func() {
client = mockClient{ client = mockClient{
api: dockerClient, api: dockerClient,
pullImages: false, pullImages: false,
removeVolumes: false,
TestData: &TestData{ TestData: &TestData{
NameOfContainerToKeep: "test-container-02", NameOfContainerToKeep: "test-container-02",
Containers: []container.Container{ Containers: []container.Container{
@ -91,6 +93,7 @@ var _ = Describe("the actions package", func() {
client = mockClient{ client = mockClient{
api: dockerClient, api: dockerClient,
pullImages: false, pullImages: false,
removeVolumes: false,
TestData: &TestData{ TestData: &TestData{
Containers: []container.Container{ Containers: []container.Container{
createMockContainer( createMockContainer(
@ -137,6 +140,7 @@ type mockClient struct {
TestData *TestData TestData *TestData
api cli.CommonAPIClient api cli.CommonAPIClient
pullImages bool pullImages bool
removeVolumes bool
} }
type TestData struct { type TestData struct {

@ -1,17 +1,18 @@
package cmd package cmd
import ( import (
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/containrrr/watchtower/actions" "github.com/containrrr/watchtower/actions"
"github.com/containrrr/watchtower/container" "github.com/containrrr/watchtower/container"
"github.com/containrrr/watchtower/internal/flags" "github.com/containrrr/watchtower/internal/flags"
"github.com/containrrr/watchtower/notifications" "github.com/containrrr/watchtower/notifications"
"github.com/robfig/cron" "github.com/robfig/cron"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -92,9 +93,11 @@ func PreRun(cmd *cobra.Command, args []string) {
noPull, _ := f.GetBool("no-pull") noPull, _ := f.GetBool("no-pull")
includeStopped, _ := f.GetBool("include-stopped") includeStopped, _ := f.GetBool("include-stopped")
removeVolumes, _ := f.GetBool("remove-volumes")
client = container.NewClient( client = container.NewClient(
!noPull, !noPull,
includeStopped, includeStopped,
removeVolumes,
) )
notifier = notifications.NewNotifier(cmd) notifier = notifications.NewNotifier(cmd)
@ -176,5 +179,3 @@ func runUpdatesWithNotifications(filter container.Filter) {
} }
notifier.SendNotification() notifier.SendNotification()
} }

@ -2,11 +2,12 @@ package container
import ( import (
"fmt" "fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"io/ioutil" "io/ioutil"
"time" "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"
"github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client" 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_HOST the docker-engine host to send api requests to
// * DOCKER_TLS_VERIFY whether to verify tls certificates // * DOCKER_TLS_VERIFY whether to verify tls certificates
// * DOCKER_API_VERSION the minimum docker api version to work with // * 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) cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv)
if err != nil { if err != nil {
@ -45,6 +46,7 @@ func NewClient(pullImages bool, includeStopped bool) Client {
return dockerClient{ return dockerClient{
api: cli, api: cli,
pullImages: pullImages, pullImages: pullImages,
removeVolumes: removeVolumes,
includeStopped: includeStopped, includeStopped: includeStopped,
} }
} }
@ -52,6 +54,7 @@ func NewClient(pullImages bool, includeStopped bool) Client {
type dockerClient struct { type dockerClient struct {
api dockerclient.CommonAPIClient api dockerclient.CommonAPIClient
pullImages bool pullImages bool
removeVolumes bool
includeStopped bool includeStopped bool
} }
@ -131,7 +134,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
} else { } else {
log.Debugf("Removing container %s", c.ID()) 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 return err
} }
} }

@ -47,6 +47,16 @@ Environment Variable: WATCHTOWER_CLEANUP
Default: false 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 ## Debug
Enable debug mode with verbose logging. Enable debug mode with verbose logging.

@ -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 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 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/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/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 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= 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.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 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 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= 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= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

@ -1,11 +1,12 @@
package flags package flags
import ( import (
"os"
"time"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"os"
"time"
) )
// RegisterDockerFlags that are used directly by the docker api client // RegisterDockerFlags that are used directly by the docker api client
@ -52,6 +53,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
viper.GetBool("WATCHTOWER_CLEANUP"), viper.GetBool("WATCHTOWER_CLEANUP"),
"remove previously used images after updating") "remove previously used images after updating")
flags.BoolP(
"remove-volumes",
"",
viper.GetBool("WATCHTOWER_REMOVE_VOLUMES"),
"remove attached volumes before updating")
flags.BoolP( flags.BoolP(
"label-enable", "label-enable",
"e", "e",
@ -64,7 +71,6 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
viper.GetBool("WATCHTOWER_DEBUG"), viper.GetBool("WATCHTOWER_DEBUG"),
"enable debug mode with verbose logging") "enable debug mode with verbose logging")
flags.BoolP( flags.BoolP(
"monitor-only", "monitor-only",
"m", "m",
@ -253,7 +259,6 @@ func ReadFlags(cmd *cobra.Command) (bool, bool, bool, time.Duration) {
return cleanup, noRestart, monitorOnly, timeout return cleanup, noRestart, monitorOnly, timeout
} }
func setEnvOptStr(env string, opt string) error { func setEnvOptStr(env string, opt string) error {
if opt == "" || opt == os.Getenv(env) { if opt == "" || opt == os.Getenv(env) {
return nil return nil
@ -271,4 +276,3 @@ func setEnvOptBool(env string, opt bool) error {
} }
return nil return nil
} }

Loading…
Cancel
Save