-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for 'watches' #298
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5ee737b
agent: Adding watches config
armon 2b07355
watch: First pass at query parsing
armon 66edf00
watch: Testing plan execution
armon 68a8291
watch: test key watch
armon 970c606
watch: Support for key prefix
armon 00358ba
watch: service watcher
armon 8d70128
watch: node watcher
armon f3c8873
watch: supporting service watch
armon 5aefdf0
watch: support checks watch
armon 723352f
watch: support parameter exemption
armon ee4a1a9
watch: Set the ACL token
armon ad40ddf
watch: Remove DSL in place of JSON
armon e877753
agent: Changing to use nested JSON for watches
armon 4b547a4
agent: First pass at agent-based watches
armon 46a96d9
agent: Refactor script invoke
armon dc5dee5
command/watch: First pass at command
armon 0cd9faf
command/watch: Adding tests
armon 94615b0
watch: Fixing bug with null keys
armon dce716f
website: Document watches
armon 54316d2
website: Minor cleanups
armon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package agent | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/armon/circbuf" | ||
"github.com/hashicorp/consul/watch" | ||
) | ||
|
||
const ( | ||
// Limit the size of a watch handlers's output to the | ||
// last WatchBufSize. Prevents an enormous buffer | ||
// from being captured | ||
WatchBufSize = 4 * 1024 // 4KB | ||
) | ||
|
||
// verifyWatchHandler does the pre-check for our handler configuration | ||
func verifyWatchHandler(params interface{}) error { | ||
if params == nil { | ||
return fmt.Errorf("Must provide watch handler") | ||
} | ||
_, ok := params.(string) | ||
if !ok { | ||
return fmt.Errorf("Watch handler must be a string") | ||
} | ||
return nil | ||
} | ||
|
||
// makeWatchHandler returns a handler for the given watch | ||
func makeWatchHandler(logOutput io.Writer, params interface{}) watch.HandlerFunc { | ||
script := params.(string) | ||
logger := log.New(logOutput, "", log.LstdFlags) | ||
fn := func(idx uint64, data interface{}) { | ||
// Create the command | ||
cmd, err := ExecScript(script) | ||
if err != nil { | ||
logger.Printf("[ERR] agent: Failed to setup watch: %v", err) | ||
return | ||
} | ||
cmd.Env = append(os.Environ(), | ||
"CONSUL_INDEX="+strconv.FormatUint(idx, 10), | ||
) | ||
|
||
// Collect the output | ||
output, _ := circbuf.NewBuffer(WatchBufSize) | ||
cmd.Stdout = output | ||
cmd.Stderr = output | ||
|
||
// Setup the input | ||
var inp bytes.Buffer | ||
enc := json.NewEncoder(&inp) | ||
if err := enc.Encode(data); err != nil { | ||
logger.Printf("[ERR] agent: Failed to encode data for watch '%s': %v", script, err) | ||
return | ||
} | ||
cmd.Stdin = &inp | ||
|
||
// Run the handler | ||
if err := cmd.Run(); err != nil { | ||
logger.Printf("[ERR] agent: Failed to invoke watch handler '%s': %v", script, err) | ||
} | ||
|
||
// Get the output, add a message about truncation | ||
outputStr := string(output.Bytes()) | ||
if output.TotalWritten() > output.Size() { | ||
outputStr = fmt.Sprintf("Captured %d of %d bytes\n...\n%s", | ||
output.Size(), output.TotalWritten(), outputStr) | ||
} | ||
|
||
// Log the output | ||
logger.Printf("[DEBUG] agent: watch handler '%s' output: %s", script, outputStr) | ||
} | ||
return fn | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
never returns error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not currently, but it forces code upstream to check for a possible error