mirror of https://github.com/hashicorp/consul
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
252 lines
6.0 KiB
252 lines
6.0 KiB
7 years ago
|
package watch
|
||
10 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
7 years ago
|
"flag"
|
||
10 years ago
|
"fmt"
|
||
|
"os"
|
||
7 years ago
|
"os/exec"
|
||
10 years ago
|
"strconv"
|
||
|
"strings"
|
||
|
|
||
8 years ago
|
"github.com/hashicorp/consul/agent"
|
||
7 years ago
|
"github.com/hashicorp/consul/command/flags"
|
||
|
consulwatch "github.com/hashicorp/consul/watch"
|
||
|
"github.com/mitchellh/cli"
|
||
10 years ago
|
)
|
||
|
|
||
7 years ago
|
func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
|
||
|
c := &cmd{UI: ui, shutdownCh: shutdownCh}
|
||
|
c.init()
|
||
|
return c
|
||
|
}
|
||
|
|
||
|
type cmd struct {
|
||
|
UI cli.Ui
|
||
|
flags *flag.FlagSet
|
||
|
http *flags.HTTPFlags
|
||
|
usage string
|
||
|
|
||
|
shutdownCh <-chan struct{}
|
||
10 years ago
|
|
||
7 years ago
|
// flags
|
||
|
watchType string
|
||
|
key string
|
||
|
prefix string
|
||
|
service string
|
||
|
tag string
|
||
|
passingOnly string
|
||
|
state string
|
||
|
name string
|
||
|
shell bool
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
func (c *cmd) init() {
|
||
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||
|
c.flags.StringVar(&c.watchType, "type", "",
|
||
8 years ago
|
"Specifies the watch type. One of key, keyprefix, services, nodes, "+
|
||
8 years ago
|
"service, checks, or event.")
|
||
7 years ago
|
c.flags.StringVar(&c.key, "key", "",
|
||
8 years ago
|
"Specifies the key to watch. Only for 'key' type.")
|
||
7 years ago
|
c.flags.StringVar(&c.prefix, "prefix", "",
|
||
8 years ago
|
"Specifies the key prefix to watch. Only for 'keyprefix' type.")
|
||
7 years ago
|
c.flags.StringVar(&c.service, "service", "",
|
||
8 years ago
|
"Specifies the service to watch. Required for 'service' type, "+
|
||
|
"optional for 'checks' type.")
|
||
7 years ago
|
c.flags.StringVar(&c.tag, "tag", "",
|
||
8 years ago
|
"Specifies the service tag to filter on. Optional for 'service' type.")
|
||
7 years ago
|
c.flags.StringVar(&c.passingOnly, "passingonly", "",
|
||
8 years ago
|
"Specifies if only hosts passing all checks are displayed. "+
|
||
|
"Optional for 'service' type, must be one of `[true|false]`. Defaults false.")
|
||
7 years ago
|
c.flags.BoolVar(&c.shell, "shell", true,
|
||
7 years ago
|
"Use a shell to run the command (can set a custom shell via the SHELL "+
|
||
|
"environment variable).")
|
||
7 years ago
|
c.flags.StringVar(&c.state, "state", "",
|
||
8 years ago
|
"Specifies the states to watch. Optional for 'checks' type.")
|
||
7 years ago
|
c.flags.StringVar(&c.name, "name", "",
|
||
8 years ago
|
"Specifies an event name to watch. Only for 'event' type.")
|
||
7 years ago
|
|
||
7 years ago
|
c.http = &flags.HTTPFlags{}
|
||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||
|
flags.Merge(c.flags, c.http.ServerFlags())
|
||
|
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
func (c *cmd) Synopsis() string {
|
||
|
return "Watch for changes in Consul"
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
func (c *cmd) Help() string {
|
||
|
return c.usage
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
func (c *cmd) Run(args []string) int {
|
||
|
if err := c.flags.Parse(args); err != nil {
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
|
||
|
// Check for a type
|
||
7 years ago
|
if c.watchType == "" {
|
||
8 years ago
|
c.UI.Error("Watch type must be specified")
|
||
|
c.UI.Error("")
|
||
|
c.UI.Error(c.Help())
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
|
||
|
// Compile the watch parameters
|
||
|
params := make(map[string]interface{})
|
||
7 years ago
|
if c.watchType != "" {
|
||
|
params["type"] = c.watchType
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.http.Datacenter() != "" {
|
||
|
params["datacenter"] = c.http.Datacenter()
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.http.Token() != "" {
|
||
|
params["token"] = c.http.Token()
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.key != "" {
|
||
|
params["key"] = c.key
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.prefix != "" {
|
||
|
params["prefix"] = c.prefix
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.service != "" {
|
||
|
params["service"] = c.service
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.tag != "" {
|
||
|
params["tag"] = c.tag
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.http.Stale() {
|
||
|
params["stale"] = c.http.Stale()
|
||
9 years ago
|
}
|
||
7 years ago
|
if c.state != "" {
|
||
|
params["state"] = c.state
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.name != "" {
|
||
|
params["name"] = c.name
|
||
10 years ago
|
}
|
||
7 years ago
|
if c.passingOnly != "" {
|
||
|
b, err := strconv.ParseBool(c.passingOnly)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Failed to parse passingonly flag: %s", err))
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
params["passingonly"] = b
|
||
|
}
|
||
|
|
||
|
// Create the watch
|
||
7 years ago
|
wp, err := consulwatch.Parse(params)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("%s", err))
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
|
||
|
// Create and test the HTTP client
|
||
7 years ago
|
client, err := c.http.APIClient()
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
_, err = client.Agent().NodeName()
|
||
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
|
||
|
// Setup handler
|
||
10 years ago
|
|
||
|
// errExit:
|
||
|
// 0: false
|
||
|
// 1: true
|
||
|
errExit := 0
|
||
7 years ago
|
if len(c.flags.Args()) == 0 {
|
||
10 years ago
|
wp.Handler = func(idx uint64, data interface{}) {
|
||
|
defer wp.Stop()
|
||
|
buf, err := json.MarshalIndent(data, "", " ")
|
||
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error encoding output: %s", err))
|
||
10 years ago
|
errExit = 1
|
||
10 years ago
|
}
|
||
8 years ago
|
c.UI.Output(string(buf))
|
||
10 years ago
|
}
|
||
|
} else {
|
||
|
wp.Handler = func(idx uint64, data interface{}) {
|
||
7 years ago
|
doneCh := make(chan struct{})
|
||
|
defer close(doneCh)
|
||
|
logFn := func(err error) {
|
||
|
c.UI.Error(fmt.Sprintf("Warning, could not forward signal: %s", err))
|
||
|
}
|
||
|
|
||
10 years ago
|
// Create the command
|
||
|
var buf bytes.Buffer
|
||
|
var err error
|
||
7 years ago
|
var cmd *exec.Cmd
|
||
7 years ago
|
if !c.shell {
|
||
7 years ago
|
cmd, err = agent.ExecSubprocess(c.flags.Args())
|
||
7 years ago
|
} else {
|
||
7 years ago
|
cmd, err = agent.ExecScript(strings.Join(c.flags.Args(), " "))
|
||
7 years ago
|
}
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error executing handler: %s", err))
|
||
10 years ago
|
goto ERR
|
||
|
}
|
||
|
cmd.Env = append(os.Environ(),
|
||
|
"CONSUL_INDEX="+strconv.FormatUint(idx, 10),
|
||
|
)
|
||
|
|
||
|
// Encode the input
|
||
|
if err = json.NewEncoder(&buf).Encode(data); err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error encoding output: %s", err))
|
||
10 years ago
|
goto ERR
|
||
|
}
|
||
|
cmd.Stdin = &buf
|
||
|
cmd.Stdout = os.Stdout
|
||
|
cmd.Stderr = os.Stderr
|
||
|
|
||
7 years ago
|
// Run the handler.
|
||
|
if err := cmd.Start(); err != nil {
|
||
|
c.UI.Error(fmt.Sprintf("Error starting handler: %s", err))
|
||
|
goto ERR
|
||
|
}
|
||
|
|
||
|
// Set up signal forwarding.
|
||
|
agent.ForwardSignals(cmd, logFn, doneCh)
|
||
|
|
||
|
// Wait for the handler to complete.
|
||
|
if err := cmd.Wait(); err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error executing handler: %s", err))
|
||
10 years ago
|
goto ERR
|
||
|
}
|
||
|
return
|
||
|
ERR:
|
||
|
wp.Stop()
|
||
10 years ago
|
errExit = 1
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Watch for a shutdown
|
||
|
go func() {
|
||
7 years ago
|
<-c.shutdownCh
|
||
10 years ago
|
wp.Stop()
|
||
|
os.Exit(0)
|
||
|
}()
|
||
|
|
||
|
// Run the watch
|
||
7 years ago
|
if err := wp.Run(c.http.Addr()); err != nil {
|
||
8 years ago
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
||
10 years ago
|
return 1
|
||
|
}
|
||
|
|
||
10 years ago
|
return errExit
|
||
10 years ago
|
}
|
||
|
|
||
7 years ago
|
const usage = `Usage: consul watch [options] [child...]
|
||
|
|
||
|
Watches for changes in a given data view from Consul. If a child process
|
||
|
is specified, it will be invoked with the latest results on changes. Otherwise,
|
||
|
the latest values are dumped to stdout and the watch terminates.
|
||
|
|
||
|
Providing the watch type is required, and other parameters may be required
|
||
|
or supported depending on the watch type.`
|