2018-10-12 18:31:33 +00:00
|
|
|
package mock
|
|
|
|
|
|
|
|
import (
|
2018-10-12 23:56:13 +00:00
|
|
|
"context"
|
2018-10-12 18:31:33 +00:00
|
|
|
"io"
|
2018-10-15 21:08:12 +00:00
|
|
|
"sync"
|
2018-10-12 18:31:33 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/nomad/client/lib/fifo"
|
2019-01-04 22:04:45 +00:00
|
|
|
bstructs "github.com/hashicorp/nomad/plugins/base/structs"
|
2018-10-12 18:31:33 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
2018-10-31 18:52:51 +00:00
|
|
|
// taskHandle supervises a mock task
|
|
|
|
type taskHandle struct {
|
2018-10-12 18:31:33 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
|
|
|
|
runFor time.Duration
|
2019-01-04 22:04:45 +00:00
|
|
|
pluginExitAfter time.Duration
|
2018-10-12 18:31:33 +00:00
|
|
|
killAfter time.Duration
|
2018-10-12 23:56:13 +00:00
|
|
|
waitCh chan struct{}
|
2018-10-12 18:31:33 +00:00
|
|
|
exitCode int
|
|
|
|
exitSignal int
|
|
|
|
exitErr error
|
|
|
|
signalErr error
|
|
|
|
stdoutString string
|
|
|
|
stdoutRepeat int
|
|
|
|
stdoutRepeatDur time.Duration
|
|
|
|
|
2018-10-31 18:52:51 +00:00
|
|
|
taskConfig *drivers.TaskConfig
|
2018-10-15 21:08:12 +00:00
|
|
|
|
|
|
|
// stateLock guards the procState field
|
2018-10-31 18:52:51 +00:00
|
|
|
stateLock sync.RWMutex
|
2018-10-15 21:08:12 +00:00
|
|
|
procState drivers.TaskState
|
|
|
|
|
2018-10-12 18:31:33 +00:00
|
|
|
startedAt time.Time
|
|
|
|
completedAt time.Time
|
|
|
|
exitResult *drivers.ExitResult
|
2018-10-12 23:56:13 +00:00
|
|
|
|
|
|
|
// Calling kill closes killCh if it is not already closed
|
|
|
|
kill context.CancelFunc
|
|
|
|
killCh <-chan struct{}
|
2019-01-05 00:08:47 +00:00
|
|
|
|
|
|
|
// Recovered is set to true if the handle was created while being recovered
|
|
|
|
Recovered bool
|
2018-10-12 18:31:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:52:51 +00:00
|
|
|
func (h *taskHandle) TaskStatus() *drivers.TaskStatus {
|
|
|
|
h.stateLock.RLock()
|
|
|
|
defer h.stateLock.RUnlock()
|
|
|
|
|
|
|
|
return &drivers.TaskStatus{
|
|
|
|
ID: h.taskConfig.ID,
|
|
|
|
Name: h.taskConfig.Name,
|
|
|
|
State: h.procState,
|
|
|
|
StartedAt: h.startedAt,
|
|
|
|
CompletedAt: h.completedAt,
|
|
|
|
ExitResult: h.exitResult,
|
|
|
|
DriverAttributes: map[string]string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *taskHandle) IsRunning() bool {
|
2018-10-15 21:08:12 +00:00
|
|
|
h.stateLock.Lock()
|
|
|
|
defer h.stateLock.Unlock()
|
|
|
|
return h.procState == drivers.TaskStateRunning
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:52:51 +00:00
|
|
|
func (h *taskHandle) run() {
|
2018-10-15 21:08:12 +00:00
|
|
|
defer func() {
|
|
|
|
h.stateLock.Lock()
|
|
|
|
h.procState = drivers.TaskStateExited
|
|
|
|
h.stateLock.Unlock()
|
|
|
|
|
|
|
|
h.completedAt = time.Now()
|
|
|
|
close(h.waitCh)
|
|
|
|
}()
|
|
|
|
|
|
|
|
h.stateLock.Lock()
|
|
|
|
h.procState = drivers.TaskStateRunning
|
|
|
|
h.stateLock.Unlock()
|
2018-10-12 23:56:13 +00:00
|
|
|
|
|
|
|
errCh := make(chan error, 1)
|
2018-10-12 18:31:33 +00:00
|
|
|
|
|
|
|
// Setup logging output
|
2019-01-05 00:08:47 +00:00
|
|
|
go h.handleLogging(errCh)
|
2018-10-12 18:31:33 +00:00
|
|
|
|
|
|
|
timer := time.NewTimer(h.runFor)
|
|
|
|
defer timer.Stop()
|
2018-10-12 23:56:13 +00:00
|
|
|
|
2019-01-04 22:04:45 +00:00
|
|
|
var pluginExitTimer <-chan time.Time
|
|
|
|
if h.pluginExitAfter != 0 {
|
|
|
|
timer := time.NewTimer(h.pluginExitAfter)
|
|
|
|
defer timer.Stop()
|
|
|
|
pluginExitTimer = timer.C
|
|
|
|
}
|
|
|
|
|
2018-10-12 23:56:13 +00:00
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
h.logger.Debug("run_for time elapsed; exiting", "run_for", h.runFor)
|
|
|
|
case <-h.killCh:
|
|
|
|
h.logger.Debug("killed; exiting")
|
2019-01-04 22:04:45 +00:00
|
|
|
case <-pluginExitTimer:
|
|
|
|
h.logger.Debug("exiting plugin")
|
|
|
|
h.exitResult = &drivers.ExitResult{
|
|
|
|
Err: bstructs.ErrPluginShutdown,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2018-10-12 23:56:13 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
h.logger.Error("error running mock task; exiting", "error", err)
|
|
|
|
h.exitResult = &drivers.ExitResult{
|
|
|
|
Err: err,
|
2018-10-12 18:31:33 +00:00
|
|
|
}
|
2018-10-12 23:56:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.exitResult = &drivers.ExitResult{
|
|
|
|
ExitCode: h.exitCode,
|
|
|
|
Signal: h.exitSignal,
|
|
|
|
Err: h.exitErr,
|
2018-10-12 18:31:33 +00:00
|
|
|
}
|
2018-10-12 23:56:13 +00:00
|
|
|
return
|
2018-10-12 18:31:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:52:51 +00:00
|
|
|
func (h *taskHandle) handleLogging(errCh chan<- error) {
|
2019-04-01 19:56:43 +00:00
|
|
|
stdout, err := fifo.OpenWriter(h.taskConfig.StdoutPath)
|
2018-10-12 18:31:33 +00:00
|
|
|
if err != nil {
|
2018-10-12 23:56:13 +00:00
|
|
|
h.logger.Error("failed to write to stdout", "error", err)
|
|
|
|
errCh <- err
|
2018-10-12 18:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-01 19:56:43 +00:00
|
|
|
stderr, err := fifo.OpenWriter(h.taskConfig.StderrPath)
|
2019-01-05 00:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logger.Error("failed to write to stderr", "error", err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stderr.Close()
|
|
|
|
|
|
|
|
if h.stdoutString == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-05 05:22:29 +00:00
|
|
|
if _, err := io.WriteString(stdout, h.stdoutString); err != nil {
|
|
|
|
h.logger.Error("failed to write to stdout", "error", err)
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
2018-10-12 18:31:33 +00:00
|
|
|
|
|
|
|
for i := 0; i < h.stdoutRepeat; i++ {
|
|
|
|
select {
|
2018-10-12 23:56:13 +00:00
|
|
|
case <-h.waitCh:
|
|
|
|
h.logger.Warn("exiting before done writing output", "i", i, "total", h.stdoutRepeat)
|
2018-10-12 18:31:33 +00:00
|
|
|
return
|
|
|
|
case <-time.After(h.stdoutRepeatDur):
|
|
|
|
if _, err := io.WriteString(stdout, h.stdoutString); err != nil {
|
|
|
|
h.logger.Error("failed to write to stdout", "error", err)
|
2018-10-12 23:56:13 +00:00
|
|
|
errCh <- err
|
2018-10-12 18:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|