2014-08-21 20:09:13 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-10-04 23:48:00 +00:00
|
|
|
"os/exec"
|
2014-08-21 20:09:13 +00:00
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
// makeWatchHandler returns a handler for the given watch
|
2017-10-04 23:48:00 +00:00
|
|
|
func makeWatchHandler(logOutput io.Writer, handler interface{}) watch.HandlerFunc {
|
|
|
|
var args []string
|
|
|
|
var script string
|
|
|
|
|
|
|
|
// Figure out whether to run in shell or raw subprocess mode
|
|
|
|
switch h := handler.(type) {
|
|
|
|
case string:
|
|
|
|
script = h
|
|
|
|
case []string:
|
|
|
|
args = h
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown handler type %T", handler))
|
|
|
|
}
|
|
|
|
|
2014-08-21 20:09:13 +00:00
|
|
|
logger := log.New(logOutput, "", log.LstdFlags)
|
|
|
|
fn := func(idx uint64, data interface{}) {
|
|
|
|
// Create the command
|
2017-10-04 23:48:00 +00:00
|
|
|
var cmd *exec.Cmd
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
cmd, err = ExecSubprocess(args)
|
|
|
|
} else {
|
|
|
|
cmd, err = ExecScript(script)
|
|
|
|
}
|
2014-08-21 21:28:16 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Printf("[ERR] agent: Failed to setup watch: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-10-04 23:48:00 +00:00
|
|
|
|
2014-08-21 20:09:13 +00:00
|
|
|
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 {
|
2017-10-04 23:48:00 +00:00
|
|
|
logger.Printf("[ERR] agent: Failed to encode data for watch '%v': %v", handler, err)
|
2014-08-21 20:09:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd.Stdin = &inp
|
|
|
|
|
|
|
|
// Run the handler
|
|
|
|
if err := cmd.Run(); err != nil {
|
2017-10-04 23:48:00 +00:00
|
|
|
logger.Printf("[ERR] agent: Failed to run watch handler '%v': %v", handler, err)
|
2014-08-21 20:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-10-04 23:48:00 +00:00
|
|
|
logger.Printf("[DEBUG] agent: watch handler '%v' output: %s", handler, outputStr)
|
2014-08-21 20:09:13 +00:00
|
|
|
}
|
|
|
|
return fn
|
|
|
|
}
|