2016-02-02 21:38:38 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-01-12 19:50:49 +00:00
|
|
|
"encoding/json"
|
2019-01-09 23:06:58 +00:00
|
|
|
"io"
|
2016-02-05 01:36:31 +00:00
|
|
|
"os"
|
2016-02-02 21:38:38 +00:00
|
|
|
"strings"
|
2019-01-26 04:08:01 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2016-02-02 21:38:38 +00:00
|
|
|
|
2019-01-26 04:08:01 +00:00
|
|
|
"github.com/armon/circbuf"
|
2018-09-24 18:37:45 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2019-01-09 23:06:58 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-01-05 00:08:47 +00:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2016-02-02 21:38:38 +00:00
|
|
|
|
2018-12-07 02:13:45 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/executor"
|
2018-11-30 09:59:23 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
2016-02-02 21:38:38 +00:00
|
|
|
)
|
|
|
|
|
2019-01-26 04:08:01 +00:00
|
|
|
const (
|
|
|
|
// circleBufferSize is the size of the in memory ring buffer used for
|
|
|
|
// go-plugin logging to stderr. When the buffer exceeds this size before
|
|
|
|
// flushing it will begin overwriting data
|
|
|
|
circleBufferSize = 64 * 1024
|
|
|
|
)
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
type ExecutorPluginCommand struct {
|
2016-02-02 21:38:38 +00:00
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
func (e *ExecutorPluginCommand) Help() string {
|
2016-02-02 21:38:38 +00:00
|
|
|
helpText := `
|
|
|
|
This is a command used by Nomad internally to launch an executor plugin"
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
func (e *ExecutorPluginCommand) Synopsis() string {
|
2016-02-02 21:38:38 +00:00
|
|
|
return "internal - launch an executor plugin"
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
func (e *ExecutorPluginCommand) Run(args []string) int {
|
2017-01-12 19:50:49 +00:00
|
|
|
if len(args) != 1 {
|
|
|
|
e.Ui.Error("json configuration not provided")
|
2016-02-05 01:36:31 +00:00
|
|
|
return 1
|
|
|
|
}
|
2019-01-09 23:06:58 +00:00
|
|
|
|
2017-01-12 19:50:49 +00:00
|
|
|
config := args[0]
|
2018-11-29 13:59:53 +00:00
|
|
|
var executorConfig executor.ExecutorConfig
|
2017-01-12 19:50:49 +00:00
|
|
|
if err := json.Unmarshal([]byte(config), &executorConfig); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
2019-01-09 23:06:58 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile(executorConfig.LogFile, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
|
2016-02-05 01:36:31 +00:00
|
|
|
if err != nil {
|
|
|
|
e.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2019-01-09 23:06:58 +00:00
|
|
|
|
2019-01-26 04:08:01 +00:00
|
|
|
// If the client detatches from go-plugin it will block on logging to stderr.
|
|
|
|
// This buffered writer will never block on write, and instead buffer the
|
|
|
|
// writes to a ring buffer.
|
|
|
|
bufferedStderrW := newCircbufWriter(os.Stderr)
|
|
|
|
|
2019-01-09 23:06:58 +00:00
|
|
|
// Tee the logs to stderr and the file so that they are streamed to the
|
|
|
|
// client
|
2019-01-26 04:08:01 +00:00
|
|
|
out := io.MultiWriter(f, bufferedStderrW)
|
2019-01-09 23:06:58 +00:00
|
|
|
|
|
|
|
// Create the logger
|
|
|
|
logger := log.New(&log.LoggerOptions{
|
|
|
|
Level: hclog.LevelFromString(executorConfig.LogLevel),
|
|
|
|
JSONFormat: true,
|
|
|
|
Output: out,
|
|
|
|
})
|
|
|
|
|
2016-02-02 21:38:38 +00:00
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
2018-11-30 09:59:23 +00:00
|
|
|
HandshakeConfig: base.Handshake,
|
2018-11-29 13:59:53 +00:00
|
|
|
Plugins: executor.GetPluginMap(
|
2019-01-09 23:06:58 +00:00
|
|
|
logger,
|
2018-09-24 18:37:45 +00:00
|
|
|
executorConfig.FSIsolation,
|
|
|
|
),
|
2018-12-05 16:03:56 +00:00
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
2019-01-09 23:06:58 +00:00
|
|
|
Logger: logger,
|
2016-02-02 21:38:38 +00:00
|
|
|
})
|
|
|
|
return 0
|
|
|
|
}
|
2019-01-26 04:08:01 +00:00
|
|
|
|
|
|
|
type circbufWriter struct {
|
|
|
|
buf *circbuf.Buffer
|
|
|
|
err error
|
|
|
|
bufLock sync.Mutex
|
|
|
|
wr io.Writer
|
|
|
|
flushCh chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCircbufWriter(w io.Writer) *circbufWriter {
|
|
|
|
buf, _ := circbuf.NewBuffer(circleBufferSize)
|
|
|
|
c := &circbufWriter{
|
|
|
|
buf: buf,
|
|
|
|
wr: w,
|
|
|
|
flushCh: make(chan struct{}),
|
|
|
|
}
|
|
|
|
go c.flushLoop()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circbufWriter) Write(p []byte) (nn int, err error) {
|
|
|
|
if c.err != nil {
|
|
|
|
return nn, c.err
|
|
|
|
}
|
|
|
|
c.bufLock.Lock()
|
|
|
|
nn, err = c.buf.Write(p)
|
|
|
|
c.bufLock.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case c.flushCh <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return nn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circbufWriter) Close() error {
|
|
|
|
var err error
|
|
|
|
if wc, ok := c.wr.(io.WriteCloser); ok {
|
|
|
|
err = wc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
close(c.flushCh)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circbufWriter) flushLoop() {
|
|
|
|
timer := time.NewTimer(time.Millisecond * 100)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-c.flushCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.err = c.flush()
|
|
|
|
case <-timer.C:
|
|
|
|
c.err = c.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *circbufWriter) flush() error {
|
|
|
|
c.bufLock.Lock()
|
|
|
|
b := c.buf.Bytes()
|
|
|
|
c.buf.Reset()
|
|
|
|
c.bufLock.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if len(b) > 0 {
|
|
|
|
_, err = c.wr.Write(b)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|