2017-10-11 12:51:22 +00:00
|
|
|
package event
|
2014-08-28 23:40:31 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-11 12:51:22 +00:00
|
|
|
"flag"
|
2014-08-28 23:40:31 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
|
2017-10-11 12:51:22 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
2014-08-28 23:40:31 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 12:51:22 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
2017-10-11 21:37:15 +00:00
|
|
|
c.init()
|
2017-10-11 12:51:22 +00:00
|
|
|
return c
|
|
|
|
}
|
2014-08-28 23:40:31 +00:00
|
|
|
|
2017-10-11 12:51:22 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-11 12:51:18 +00:00
|
|
|
name string
|
|
|
|
node string
|
|
|
|
service string
|
|
|
|
tag string
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2014-08-28 23:40:31 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 21:37:15 +00:00
|
|
|
func (c *cmd) init() {
|
2017-10-11 12:51:22 +00:00
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.StringVar(&c.name, "name", "",
|
2017-02-08 21:56:58 +00:00
|
|
|
"Name of the event.")
|
2017-10-11 12:51:22 +00:00
|
|
|
c.flags.StringVar(&c.node, "node", "",
|
2017-02-08 21:56:58 +00:00
|
|
|
"Regular expression to filter on node names.")
|
2017-10-11 12:51:22 +00:00
|
|
|
c.flags.StringVar(&c.service, "service", "",
|
2017-02-10 01:49:17 +00:00
|
|
|
"Regular expression to filter on service instances.")
|
2017-10-11 12:51:22 +00:00
|
|
|
c.flags.StringVar(&c.tag, "tag", "",
|
2017-02-08 21:56:58 +00:00
|
|
|
"Regular expression to filter on service tags. Must be used with -service.")
|
2017-10-11 12:51:22 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-10-11 12:51:18 +00:00
|
|
|
}
|
2017-02-08 21:56:58 +00:00
|
|
|
|
2017-10-11 12:51:22 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a name
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.name == "" {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Event name must be specified")
|
|
|
|
c.UI.Error("")
|
|
|
|
c.UI.Error(c.Help())
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the filters
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.node != "" {
|
|
|
|
if _, err := regexp.Compile(c.node); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to compile node filter regexp: %v", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.service != "" {
|
|
|
|
if _, err := regexp.Compile(c.service); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to compile service filter regexp: %v", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.tag != "" {
|
|
|
|
if _, err := regexp.Compile(c.tag); err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to compile tag filter regexp: %v", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.tag != "" && c.service == "" {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Cannot provide tag filter without service filter.")
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a payload
|
|
|
|
var payload []byte
|
2017-10-11 12:51:22 +00:00
|
|
|
args = c.flags.Args()
|
2014-08-28 23:40:31 +00:00
|
|
|
switch len(args) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
payload = []byte(args[0])
|
|
|
|
default:
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("Too many command line arguments.")
|
|
|
|
c.UI.Error("")
|
|
|
|
c.UI.Error(c.Help())
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and test the HTTP client
|
2017-10-11 12:51:22 +00:00
|
|
|
client, err := c.http.APIClient()
|
2014-08-28 23:40:31 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
_, err = client.Agent().NodeName()
|
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error querying Consul agent: %s", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the request
|
|
|
|
event := client.Event()
|
2017-10-11 12:51:22 +00:00
|
|
|
params := &api.UserEvent{
|
2017-10-11 12:51:18 +00:00
|
|
|
Name: c.name,
|
2014-08-28 23:40:31 +00:00
|
|
|
Payload: payload,
|
2017-10-11 12:51:18 +00:00
|
|
|
NodeFilter: c.node,
|
|
|
|
ServiceFilter: c.service,
|
|
|
|
TagFilter: c.tag,
|
2014-08-28 23:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fire the event
|
2017-02-08 21:56:58 +00:00
|
|
|
id, _, err := event.Fire(params, nil)
|
2014-08-28 23:40:31 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error firing event: %s", err))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the ID
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Output(fmt.Sprintf("Event ID: %s", id))
|
2014-08-28 23:40:31 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-11 12:51:22 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2017-10-11 12:51:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return c.help
|
2017-10-11 21:31:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
const synopsis = "Fire a new event"
|
|
|
|
const help = `
|
|
|
|
Usage: consul event [options] [payload]
|
2017-10-11 12:51:18 +00:00
|
|
|
|
|
|
|
Dispatches a custom user event across a datacenter. An event must provide
|
|
|
|
a name, but a payload is optional. Events support filtering using
|
2017-10-17 13:44:20 +00:00
|
|
|
regular expressions on node name, service, and tag definitions.
|
|
|
|
`
|