2014-08-28 23:40:31 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2017-02-08 21:56:58 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2014-08-28 23:40:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EventCommand is a Command implementation that is used to
|
|
|
|
// fire new events
|
|
|
|
type EventCommand struct {
|
2017-02-08 21:56:58 +00:00
|
|
|
base.Command
|
2014-08-28 23:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EventCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: consul event [options] [payload]
|
|
|
|
|
|
|
|
Dispatches a custom user event across a datacenter. An event must provide
|
|
|
|
a name, but a payload is optional. Events support filtering using
|
|
|
|
regular expressions on node name, service, and tag definitions.
|
|
|
|
|
2017-02-08 21:56:58 +00:00
|
|
|
` + c.Command.Help()
|
|
|
|
|
2014-08-28 23:40:31 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EventCommand) Run(args []string) int {
|
2017-02-08 21:56:58 +00:00
|
|
|
var name, node, service, tag string
|
|
|
|
|
|
|
|
f := c.Command.NewFlagSet(c)
|
|
|
|
f.StringVar(&name, "name", "",
|
|
|
|
"Name of the event.")
|
|
|
|
f.StringVar(&node, "node", "",
|
|
|
|
"Regular expression to filter on node names.")
|
|
|
|
f.StringVar(&service, "service", "",
|
2017-02-10 01:49:17 +00:00
|
|
|
"Regular expression to filter on service instances.")
|
2017-02-08 21:56:58 +00:00
|
|
|
f.StringVar(&tag, "tag", "",
|
|
|
|
"Regular expression to filter on service tags. Must be used with -service.")
|
|
|
|
|
|
|
|
if err := c.Command.Parse(args); err != nil {
|
2014-08-28 23:40:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for a name
|
|
|
|
if 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
|
|
|
|
if node != "" {
|
|
|
|
if _, err := regexp.Compile(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if service != "" {
|
|
|
|
if _, err := regexp.Compile(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tag != "" {
|
|
|
|
if _, err := regexp.Compile(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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tag != "" && 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-02-08 21:56:58 +00:00
|
|
|
args = f.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-02-08 21:56:58 +00:00
|
|
|
client, err := c.Command.HTTPClient()
|
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()
|
|
|
|
params := &consulapi.UserEvent{
|
|
|
|
Name: name,
|
|
|
|
Payload: payload,
|
|
|
|
NodeFilter: node,
|
|
|
|
ServiceFilter: service,
|
|
|
|
TagFilter: tag,
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *EventCommand) Synopsis() string {
|
|
|
|
return "Fire a new event"
|
|
|
|
}
|