mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
add ps style filtering of containers
This commit is contained in:
parent
265ae80099
commit
86bd046deb
2 changed files with 29 additions and 4 deletions
|
|
@ -3,10 +3,12 @@ package container
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
"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"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
|
@ -37,19 +39,20 @@ 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) Client {
|
func NewClient(pullImages bool, filtersSlice []string) Client {
|
||||||
cli, err := dockerclient.NewEnvClient()
|
cli, err := dockerclient.NewEnvClient()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error instantiating Docker client: %s", err)
|
log.Fatalf("Error instantiating Docker client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return dockerClient{api: cli, pullImages: pullImages}
|
return dockerClient{api: cli, pullImages: pullImages, rawFilters: filtersSlice}
|
||||||
}
|
}
|
||||||
|
|
||||||
type dockerClient struct {
|
type dockerClient struct {
|
||||||
api *dockerclient.Client
|
api *dockerclient.Client
|
||||||
pullImages bool
|
pullImages bool
|
||||||
|
rawFilters []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
||||||
|
|
@ -58,9 +61,25 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
|
||||||
|
|
||||||
log.Debug("Retrieving running containers")
|
log.Debug("Retrieving running containers")
|
||||||
|
|
||||||
|
listOptions := types.ContainerListOptions{}
|
||||||
|
|
||||||
|
if len(client.rawFilters) != 0 {
|
||||||
|
// try to construct filters from the string slice
|
||||||
|
filters := filters.NewArgs()
|
||||||
|
for _, filter := range client.rawFilters {
|
||||||
|
// split by first equals sign
|
||||||
|
filterParts := strings.SplitN(filter, "=", 2)
|
||||||
|
// now add the filter
|
||||||
|
filters.Add(filterParts[0], filterParts[1])
|
||||||
|
}
|
||||||
|
listOptions.All = true
|
||||||
|
listOptions.Filters = filters
|
||||||
|
}
|
||||||
|
|
||||||
runningContainers, err := client.api.ContainerList(
|
runningContainers, err := client.api.ContainerList(
|
||||||
bg,
|
bg,
|
||||||
types.ContainerListOptions{})
|
listOptions,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
main.go
8
main.go
|
|
@ -27,6 +27,7 @@ var (
|
||||||
scheduleSpec string
|
scheduleSpec string
|
||||||
cleanup bool
|
cleanup bool
|
||||||
noRestart bool
|
noRestart bool
|
||||||
|
filters []string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -82,6 +83,10 @@ func main() {
|
||||||
Name: "debug",
|
Name: "debug",
|
||||||
Usage: "enable debug mode with verbose logging",
|
Usage: "enable debug mode with verbose logging",
|
||||||
},
|
},
|
||||||
|
cli.StringSliceFlag{
|
||||||
|
Name: "ps-filter",
|
||||||
|
Usage: "docker ps filters",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := app.Run(os.Args); err != nil {
|
if err := app.Run(os.Args); err != nil {
|
||||||
|
|
@ -96,6 +101,7 @@ func before(c *cli.Context) error {
|
||||||
|
|
||||||
pollingSet := c.IsSet("interval")
|
pollingSet := c.IsSet("interval")
|
||||||
cronSet := c.IsSet("schedule")
|
cronSet := c.IsSet("schedule")
|
||||||
|
filters = c.GlobalStringSlice("ps-filter")
|
||||||
|
|
||||||
if pollingSet && cronSet {
|
if pollingSet && cronSet {
|
||||||
log.Fatal("Only schedule or interval can be defined, not both.")
|
log.Fatal("Only schedule or interval can be defined, not both.")
|
||||||
|
|
@ -114,7 +120,7 @@ func before(c *cli.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
client = container.NewClient(!c.GlobalBool("no-pull"))
|
client = container.NewClient(!c.GlobalBool("no-pull"), filters)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue