2014-08-28 20:42:07 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
"fmt"
|
2014-08-28 20:42:07 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/structs"
|
2014-08-28 20:42:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// maxQueryTime is used to bound the limit of a blocking query
|
|
|
|
maxQueryTime = 600 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// EventFire is used to fire a new event
|
|
|
|
func (s *HTTPServer) EventFire(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Mandate a PUT request
|
|
|
|
if req.Method != "PUT" {
|
|
|
|
resp.WriteHeader(405)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:00:49 +00:00
|
|
|
// Get the datacenter
|
|
|
|
var dc string
|
|
|
|
s.parseDC(req, &dc)
|
|
|
|
|
2014-08-28 20:42:07 +00:00
|
|
|
event := &UserEvent{}
|
|
|
|
event.Name = strings.TrimPrefix(req.URL.Path, "/v1/event/fire/")
|
|
|
|
if event.Name == "" {
|
|
|
|
resp.WriteHeader(400)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Missing name")
|
2014-08-28 20:42:07 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-18 01:58:27 +00:00
|
|
|
// Get the ACL token
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2014-08-28 20:42:07 +00:00
|
|
|
// Get the filters
|
|
|
|
if filt := req.URL.Query().Get("node"); filt != "" {
|
|
|
|
event.NodeFilter = filt
|
|
|
|
}
|
|
|
|
if filt := req.URL.Query().Get("service"); filt != "" {
|
|
|
|
event.ServiceFilter = filt
|
|
|
|
}
|
|
|
|
if filt := req.URL.Query().Get("tag"); filt != "" {
|
|
|
|
event.TagFilter = filt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the payload
|
|
|
|
if req.ContentLength > 0 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, req.Body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
event.Payload = buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fire the event
|
2015-06-18 01:58:27 +00:00
|
|
|
if err := s.agent.UserEvent(dc, token, event); err != nil {
|
2015-06-19 00:34:35 +00:00
|
|
|
if strings.Contains(err.Error(), permissionDenied) {
|
|
|
|
resp.WriteHeader(403)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, permissionDenied)
|
2015-06-19 00:34:35 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
resp.WriteHeader(500)
|
|
|
|
return nil, err
|
2014-08-28 20:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the event
|
|
|
|
return event, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventList is used to retrieve the recent list of events
|
|
|
|
func (s *HTTPServer) EventList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Parse the query options, since we simulate a blocking query
|
|
|
|
var b structs.QueryOptions
|
|
|
|
if parseWait(resp, req, &b) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 03:42:37 +00:00
|
|
|
// Fetch the ACL token, if any.
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
acl, err := s.agent.resolveToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:25:53 +00:00
|
|
|
// Look for a name filter
|
|
|
|
var nameFilter string
|
|
|
|
if filt := req.URL.Query().Get("name"); filt != "" {
|
|
|
|
nameFilter = filt
|
|
|
|
}
|
|
|
|
|
2017-01-24 07:44:32 +00:00
|
|
|
// Lots of this logic is borrowed from consul/rpc.go:blockingQuery
|
2014-08-28 20:42:07 +00:00
|
|
|
// However we cannot use that directly since this code has some
|
|
|
|
// slight semantics differences...
|
|
|
|
var timeout <-chan time.Time
|
|
|
|
var notifyCh chan struct{}
|
|
|
|
|
|
|
|
// Fast path non-blocking
|
|
|
|
if b.MinQueryIndex == 0 {
|
|
|
|
goto RUN_QUERY
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict the max query time
|
|
|
|
if b.MaxQueryTime > maxQueryTime {
|
|
|
|
b.MaxQueryTime = maxQueryTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure a time limit is set if we have an index
|
|
|
|
if b.MinQueryIndex > 0 && b.MaxQueryTime == 0 {
|
|
|
|
b.MaxQueryTime = maxQueryTime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a query timeout
|
|
|
|
if b.MaxQueryTime > 0 {
|
|
|
|
timeout = time.After(b.MaxQueryTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a notification channel for changes
|
|
|
|
SETUP_NOTIFY:
|
|
|
|
if b.MinQueryIndex > 0 {
|
|
|
|
notifyCh = make(chan struct{}, 1)
|
|
|
|
s.agent.eventNotify.Wait(notifyCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
RUN_QUERY:
|
|
|
|
// Get the recent events
|
|
|
|
events := s.agent.UserEvents()
|
2014-08-28 22:25:53 +00:00
|
|
|
|
2016-12-15 03:42:37 +00:00
|
|
|
// Filter the events using the ACL, if present
|
|
|
|
if acl != nil {
|
|
|
|
for i := 0; i < len(events); i++ {
|
|
|
|
name := events[i].Name
|
|
|
|
if acl.EventRead(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.agent.logger.Printf("[DEBUG] agent: dropping event %q from result due to ACLs", name)
|
|
|
|
events = append(events[:i], events[i+1:]...)
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the events if requested
|
2014-08-28 22:25:53 +00:00
|
|
|
if nameFilter != "" {
|
2014-11-21 03:51:08 +00:00
|
|
|
for i := 0; i < len(events); i++ {
|
|
|
|
if events[i].Name != nameFilter {
|
|
|
|
events = append(events[:i], events[i+1:]...)
|
|
|
|
i--
|
2014-08-28 22:25:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the index
|
2014-08-28 20:42:07 +00:00
|
|
|
var index uint64
|
|
|
|
if len(events) == 0 {
|
2014-08-28 22:55:36 +00:00
|
|
|
// Return a non-zero index to prevent a hot query loop. This
|
|
|
|
// can be caused by a watch for example when there is no matching
|
|
|
|
// events.
|
|
|
|
index = 1
|
2014-08-28 20:42:07 +00:00
|
|
|
} else {
|
|
|
|
last := events[len(events)-1]
|
|
|
|
index = uuidToUint64(last.ID)
|
|
|
|
}
|
|
|
|
setIndex(resp, index)
|
|
|
|
|
2014-08-31 00:22:17 +00:00
|
|
|
// Check for exact match on the query value. Because
|
2014-08-28 20:42:07 +00:00
|
|
|
// the index value is not monotonic, we just ensure it is
|
|
|
|
// not an exact match.
|
|
|
|
if index > 0 && index == b.MinQueryIndex {
|
|
|
|
select {
|
|
|
|
case <-notifyCh:
|
|
|
|
goto SETUP_NOTIFY
|
|
|
|
case <-timeout:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// uuidToUint64 is a bit of a hack to generate a 64bit Consul index.
|
|
|
|
// In effect, we take our random UUID, convert it to a 128 bit number,
|
|
|
|
// then XOR the high-order and low-order 64bit's together to get the
|
|
|
|
// output. This lets us generate an index which can be used to simulate
|
|
|
|
// the blocking behavior of other catalog endpoints.
|
|
|
|
func uuidToUint64(uuid string) uint64 {
|
|
|
|
lower := uuid[0:8] + uuid[9:13] + uuid[14:18]
|
|
|
|
upper := uuid[19:23] + uuid[24:36]
|
|
|
|
lowVal, err := strconv.ParseUint(lower, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to convert " + lower)
|
|
|
|
}
|
|
|
|
highVal, err := strconv.ParseUint(upper, 16, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to convert " + upper)
|
|
|
|
}
|
|
|
|
return lowVal ^ highVal
|
|
|
|
}
|