2016-08-22 16:34:24 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2017-02-01 00:43:57 +00:00
|
|
|
"context"
|
2016-09-16 00:24:09 +00:00
|
|
|
"encoding/json"
|
2016-08-22 16:34:24 +00:00
|
|
|
"errors"
|
2016-09-16 00:24:09 +00:00
|
|
|
"fmt"
|
2018-01-31 20:13:57 +00:00
|
|
|
"io"
|
2016-08-22 16:34:24 +00:00
|
|
|
"log"
|
2016-10-07 19:37:52 +00:00
|
|
|
"os"
|
2017-01-13 01:21:54 +00:00
|
|
|
"strconv"
|
2017-12-06 23:40:04 +00:00
|
|
|
"strings"
|
2016-08-22 16:34:24 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
2018-01-31 20:13:57 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/logging"
|
2016-08-22 16:34:24 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2018-01-31 22:03:55 +00:00
|
|
|
const (
|
|
|
|
// ShutdownPeriodicAfter is a config key that can be used during tests to
|
|
|
|
// "stop" a previously-functioning driver, allowing for testing of periodic
|
|
|
|
// drivers and fingerprinters
|
|
|
|
ShutdownPeriodicAfter = "test.shutdown_periodic_after"
|
|
|
|
|
|
|
|
// ShutdownPeriodicDuration is a config option that can be used during tests
|
|
|
|
// to "stop" a previously functioning driver after the specified duration
|
|
|
|
// (specified in seconds) for testing of periodic drivers and fingerprinters.
|
|
|
|
ShutdownPeriodicDuration = "test.shutdown_periodic_duration"
|
2018-01-25 16:30:15 +00:00
|
|
|
|
|
|
|
mockDriverName = "driver.mock_driver"
|
2018-01-31 22:03:55 +00:00
|
|
|
)
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// MockDriverConfig is the driver configuration for the MockDriver
|
|
|
|
type MockDriverConfig struct {
|
|
|
|
|
2016-10-29 00:49:46 +00:00
|
|
|
// StartErr specifies the error that should be returned when starting the
|
|
|
|
// mock driver.
|
|
|
|
StartErr string `mapstructure:"start_error"`
|
|
|
|
|
|
|
|
// StartErrRecoverable marks the error returned is recoverable
|
|
|
|
StartErrRecoverable bool `mapstructure:"start_error_recoverable"`
|
|
|
|
|
2017-07-04 19:24:27 +00:00
|
|
|
// StartBlockFor specifies a duration in which to block before returning
|
|
|
|
StartBlockFor time.Duration `mapstructure:"start_block_for"`
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// KillAfter is the duration after which the mock driver indicates the task
|
|
|
|
// has exited after getting the initial SIGINT signal
|
|
|
|
KillAfter time.Duration `mapstructure:"kill_after"`
|
|
|
|
|
|
|
|
// RunFor is the duration for which the fake task runs for. After this
|
|
|
|
// period the MockDriver responds to the task running indicating that the
|
|
|
|
// task has terminated
|
|
|
|
RunFor time.Duration `mapstructure:"run_for"`
|
|
|
|
|
|
|
|
// ExitCode is the exit code with which the MockDriver indicates the task
|
|
|
|
// has exited
|
|
|
|
ExitCode int `mapstructure:"exit_code"`
|
|
|
|
|
|
|
|
// ExitSignal is the signal with which the MockDriver indicates the task has
|
|
|
|
// been killed
|
|
|
|
ExitSignal int `mapstructure:"exit_signal"`
|
|
|
|
|
|
|
|
// ExitErrMsg is the error message that the task returns while exiting
|
|
|
|
ExitErrMsg string `mapstructure:"exit_err_msg"`
|
2016-10-10 21:49:37 +00:00
|
|
|
|
|
|
|
// SignalErr is the error message that the task returns if signalled
|
|
|
|
SignalErr string `mapstructure:"signal_error"`
|
2017-12-06 23:40:04 +00:00
|
|
|
|
|
|
|
// DriverIP will be returned as the DriverNetwork.IP from Start()
|
|
|
|
DriverIP string `mapstructure:"driver_ip"`
|
|
|
|
|
|
|
|
// DriverAdvertise will be returned as DriverNetwork.AutoAdvertise from
|
|
|
|
// Start().
|
|
|
|
DriverAdvertise bool `mapstructure:"driver_advertise"`
|
|
|
|
|
|
|
|
// DriverPortMap will parse a label:number pair and return it in
|
|
|
|
// DriverNetwork.PortMap from Start().
|
|
|
|
DriverPortMap string `mapstructure:"driver_port_map"`
|
2018-01-31 20:13:57 +00:00
|
|
|
|
|
|
|
// StdoutString is the string that should be sent to stdout
|
|
|
|
StdoutString string `mapstructure:"stdout_string"`
|
|
|
|
|
|
|
|
// StdoutRepeat is the number of times the output should be sent.
|
|
|
|
StdoutRepeat int `mapstructure:"stdout_repeat"`
|
|
|
|
|
|
|
|
// StdoutRepeatDur is the duration between repeated outputs.
|
|
|
|
StdoutRepeatDur time.Duration `mapstructure:"stdout_repeat_duration"`
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MockDriver is a driver which is used for testing purposes
|
|
|
|
type MockDriver struct {
|
|
|
|
DriverContext
|
2018-01-26 19:31:37 +00:00
|
|
|
|
2017-01-13 01:21:54 +00:00
|
|
|
cleanupFailNum int
|
2018-01-31 22:03:55 +00:00
|
|
|
|
|
|
|
// shutdownFingerprintTime is the time up to which the driver will be up
|
|
|
|
shutdownFingerprintTime time.Time
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMockDriver is a factory method which returns a new Mock Driver
|
|
|
|
func NewMockDriver(ctx *DriverContext) Driver {
|
2018-01-26 19:31:37 +00:00
|
|
|
md := &MockDriver{DriverContext: *ctx}
|
|
|
|
|
|
|
|
// if the shutdown configuration options are set, start the timer here.
|
|
|
|
// This config option defaults to false
|
2018-01-31 22:03:55 +00:00
|
|
|
if ctx.config != nil && ctx.config.ReadBoolDefault(ShutdownPeriodicAfter, false) {
|
|
|
|
duration, err := ctx.config.ReadInt(ShutdownPeriodicDuration)
|
2018-01-26 19:31:37 +00:00
|
|
|
if err != nil {
|
2018-02-28 18:21:27 +00:00
|
|
|
errMsg := fmt.Sprintf("unable to read config option for shutdown_periodic_duration %v, got err %s", duration, err.Error())
|
2018-01-26 19:31:37 +00:00
|
|
|
panic(errMsg)
|
|
|
|
}
|
2018-01-31 22:03:55 +00:00
|
|
|
md.shutdownFingerprintTime = time.Now().Add(time.Second * time.Duration(duration))
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return md
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 22:06:23 +00:00
|
|
|
func (d *MockDriver) Abilities() DriverAbilities {
|
|
|
|
return DriverAbilities{
|
|
|
|
SendSignals: false,
|
2017-04-13 16:52:16 +00:00
|
|
|
Exec: true,
|
2016-10-19 22:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func (d *MockDriver) FSIsolation() cstructs.FSIsolation {
|
|
|
|
return cstructs.FSIsolationNone
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:08:49 +00:00
|
|
|
func (d *MockDriver) Prestart(*ExecContext, *structs.Task) (*PrestartResponse, error) {
|
2017-01-10 21:24:45 +00:00
|
|
|
return nil, nil
|
2016-11-30 00:39:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// Start starts the mock driver
|
2017-06-09 17:29:41 +00:00
|
|
|
func (m *MockDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse, error) {
|
2016-08-22 16:34:24 +00:00
|
|
|
var driverConfig MockDriverConfig
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &driverConfig,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(task.Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-04 19:24:27 +00:00
|
|
|
if driverConfig.StartBlockFor != 0 {
|
|
|
|
time.Sleep(driverConfig.StartBlockFor)
|
|
|
|
}
|
|
|
|
|
2016-10-29 00:49:46 +00:00
|
|
|
if driverConfig.StartErr != "" {
|
|
|
|
return nil, structs.NewRecoverableError(errors.New(driverConfig.StartErr), driverConfig.StartErrRecoverable)
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:40:04 +00:00
|
|
|
// Create the driver network
|
|
|
|
net := &cstructs.DriverNetwork{
|
|
|
|
IP: driverConfig.DriverIP,
|
|
|
|
AutoAdvertise: driverConfig.DriverAdvertise,
|
|
|
|
}
|
|
|
|
if raw := driverConfig.DriverPortMap; len(raw) > 0 {
|
|
|
|
parts := strings.Split(raw, ":")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("malformed port map: %q", raw)
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("malformed port map: %q -- error: %v", raw, err)
|
|
|
|
}
|
|
|
|
net.PortMap = map[string]int{parts[0]: port}
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
h := mockDriverHandle{
|
2018-01-31 20:13:57 +00:00
|
|
|
ctx: ctx,
|
|
|
|
task: task,
|
|
|
|
taskName: task.Name,
|
|
|
|
runFor: driverConfig.RunFor,
|
|
|
|
killAfter: driverConfig.KillAfter,
|
|
|
|
killTimeout: task.KillTimeout,
|
|
|
|
exitCode: driverConfig.ExitCode,
|
|
|
|
exitSignal: driverConfig.ExitSignal,
|
|
|
|
stdoutString: driverConfig.StdoutString,
|
|
|
|
stdoutRepeat: driverConfig.StdoutRepeat,
|
|
|
|
stdoutRepeatDur: driverConfig.StdoutRepeatDur,
|
|
|
|
logger: m.logger,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
if driverConfig.ExitErrMsg != "" {
|
|
|
|
h.exitErr = errors.New(driverConfig.ExitErrMsg)
|
|
|
|
}
|
2016-10-10 21:49:37 +00:00
|
|
|
if driverConfig.SignalErr != "" {
|
|
|
|
h.signalErr = fmt.Errorf(driverConfig.SignalErr)
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
m.logger.Printf("[DEBUG] driver.mock: starting task %q", task.Name)
|
|
|
|
go h.run()
|
2017-12-06 23:40:04 +00:00
|
|
|
|
|
|
|
return &StartResponse{Handle: &h, Network: net}, nil
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 00:46:08 +00:00
|
|
|
// Cleanup deletes all keys except for Config.Options["cleanup_fail_on"] for
|
|
|
|
// Config.Options["cleanup_fail_num"] times. For failures it will return a
|
|
|
|
// recoverable error.
|
|
|
|
func (m *MockDriver) Cleanup(ctx *ExecContext, res *CreatedResources) error {
|
2017-01-31 18:51:32 +00:00
|
|
|
if res == nil {
|
|
|
|
panic("Cleanup should not be called with nil *CreatedResources")
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:46:08 +00:00
|
|
|
var err error
|
|
|
|
failn, _ := strconv.Atoi(m.config.Options["cleanup_fail_num"])
|
|
|
|
failk := m.config.Options["cleanup_fail_on"]
|
|
|
|
for k := range res.Resources {
|
|
|
|
if k == failk && m.cleanupFailNum < failn {
|
|
|
|
m.cleanupFailNum++
|
|
|
|
err = structs.NewRecoverableError(fmt.Errorf("mock_driver failure on %q call %d/%d", k, m.cleanupFailNum, failn), true)
|
|
|
|
} else {
|
|
|
|
delete(res.Resources, k)
|
|
|
|
}
|
2017-01-13 01:21:54 +00:00
|
|
|
}
|
2017-01-14 00:46:08 +00:00
|
|
|
return err
|
2017-01-13 01:21:54 +00:00
|
|
|
}
|
2017-01-10 21:24:45 +00:00
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// Validate validates the mock driver configuration
|
|
|
|
func (m *MockDriver) Validate(map[string]interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fingerprint fingerprints a node and returns if MockDriver is enabled
|
2018-01-24 14:09:53 +00:00
|
|
|
func (m *MockDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
|
2018-01-26 19:31:37 +00:00
|
|
|
switch {
|
2018-01-31 22:03:55 +00:00
|
|
|
// If the driver is configured to shut down after a period of time, and the
|
|
|
|
// current time is after the time which the node should shut down, simulate
|
|
|
|
// driver failure
|
|
|
|
case !m.shutdownFingerprintTime.IsZero() && time.Now().After(m.shutdownFingerprintTime):
|
2018-01-25 16:30:15 +00:00
|
|
|
resp.RemoveAttribute(mockDriverName)
|
2018-01-26 19:31:37 +00:00
|
|
|
default:
|
2018-01-25 16:30:15 +00:00
|
|
|
resp.AddAttribute(mockDriverName, "1")
|
2018-01-31 22:03:55 +00:00
|
|
|
resp.Detected = true
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
2018-01-31 20:13:57 +00:00
|
|
|
// When testing, poll for updates
|
|
|
|
func (m *MockDriver) Periodic() (bool, time.Duration) {
|
|
|
|
return true, 500 * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2018-01-25 16:30:15 +00:00
|
|
|
// HealthCheck implements the interface for HealthCheck, and indicates the current
|
|
|
|
// health status of the mock driver.
|
|
|
|
func (m *MockDriver) HealthCheck(req *cstructs.HealthCheckRequest, resp *cstructs.HealthCheckResponse) error {
|
|
|
|
switch {
|
|
|
|
case !m.shutdownFingerprintTime.IsZero() && time.Now().After(m.shutdownFingerprintTime):
|
|
|
|
notHealthy := &structs.DriverInfo{
|
|
|
|
Healthy: false,
|
|
|
|
HealthDescription: "not running",
|
|
|
|
UpdateTime: time.Now(),
|
|
|
|
}
|
|
|
|
resp.AddDriverInfo("mock_driver", notHealthy)
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
healthy := &structs.DriverInfo{
|
|
|
|
Healthy: true,
|
|
|
|
HealthDescription: "running",
|
|
|
|
UpdateTime: time.Now(),
|
|
|
|
}
|
|
|
|
resp.AddDriverInfo("mock_driver", healthy)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHealthCheckInterval implements the interface for HealthCheck and indicates
|
|
|
|
// that mock driver should be checked periodically. Returns a boolean
|
2018-03-01 22:25:34 +00:00
|
|
|
// indicating if it should be checked, and the duration at which to do this
|
2018-01-25 16:30:15 +00:00
|
|
|
// check.
|
|
|
|
func (m *MockDriver) GetHealthCheckInterval(req *cstructs.HealthCheckIntervalRequest, resp *cstructs.HealthCheckIntervalResponse) error {
|
|
|
|
resp.Eligible = true
|
|
|
|
resp.Period = 1 * time.Second
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// MockDriverHandle is a driver handler which supervises a mock task
|
|
|
|
type mockDriverHandle struct {
|
2018-01-31 20:13:57 +00:00
|
|
|
ctx *ExecContext
|
|
|
|
task *structs.Task
|
|
|
|
taskName string
|
|
|
|
runFor time.Duration
|
|
|
|
killAfter time.Duration
|
|
|
|
killTimeout time.Duration
|
|
|
|
exitCode int
|
|
|
|
exitSignal int
|
|
|
|
exitErr error
|
|
|
|
signalErr error
|
|
|
|
logger *log.Logger
|
|
|
|
stdoutString string
|
|
|
|
stdoutRepeat int
|
|
|
|
stdoutRepeatDur time.Duration
|
|
|
|
waitCh chan *dstructs.WaitResult
|
|
|
|
doneCh chan struct{}
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
2016-09-16 00:24:09 +00:00
|
|
|
type mockDriverID struct {
|
|
|
|
TaskName string
|
|
|
|
RunFor time.Duration
|
|
|
|
KillAfter time.Duration
|
|
|
|
KillTimeout time.Duration
|
|
|
|
ExitCode int
|
|
|
|
ExitSignal int
|
|
|
|
ExitErr error
|
2016-10-10 21:49:37 +00:00
|
|
|
SignalErr error
|
2016-09-16 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
func (h *mockDriverHandle) ID() string {
|
2016-09-16 00:24:09 +00:00
|
|
|
id := mockDriverID{
|
|
|
|
TaskName: h.taskName,
|
|
|
|
RunFor: h.runFor,
|
|
|
|
KillAfter: h.killAfter,
|
2017-07-23 02:04:36 +00:00
|
|
|
KillTimeout: h.killTimeout,
|
2016-09-16 00:24:09 +00:00
|
|
|
ExitCode: h.exitCode,
|
|
|
|
ExitSignal: h.exitSignal,
|
|
|
|
ExitErr: h.exitErr,
|
2016-10-10 21:49:37 +00:00
|
|
|
SignalErr: h.signalErr,
|
2016-09-16 00:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(id)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.mock_driver: failed to marshal ID to JSON: %s", err)
|
|
|
|
}
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open re-connects the driver to the running task
|
|
|
|
func (m *MockDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
|
|
|
id := &mockDriverID{}
|
|
|
|
if err := json.Unmarshal([]byte(handleID), id); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := mockDriverHandle{
|
|
|
|
taskName: id.TaskName,
|
|
|
|
runFor: id.RunFor,
|
|
|
|
killAfter: id.KillAfter,
|
|
|
|
killTimeout: id.KillTimeout,
|
|
|
|
exitCode: id.ExitCode,
|
|
|
|
exitSignal: id.ExitSignal,
|
|
|
|
exitErr: id.ExitErr,
|
2016-10-10 21:49:37 +00:00
|
|
|
signalErr: id.SignalErr,
|
2016-09-16 00:24:09 +00:00
|
|
|
logger: m.logger,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
|
|
|
}
|
|
|
|
|
|
|
|
go h.run()
|
|
|
|
return &h, nil
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *mockDriverHandle) WaitCh() chan *dstructs.WaitResult {
|
|
|
|
return h.waitCh
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:43:57 +00:00
|
|
|
func (h *mockDriverHandle) Exec(ctx context.Context, cmd string, args []string) ([]byte, int, error) {
|
|
|
|
h.logger.Printf("[DEBUG] driver.mock: Exec(%q, %q)", cmd, args)
|
|
|
|
return []byte(fmt.Sprintf("Exec(%q, %q)", cmd, args)), 0, nil
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// TODO Implement when we need it.
|
|
|
|
func (h *mockDriverHandle) Update(task *structs.Task) error {
|
2017-07-23 02:04:36 +00:00
|
|
|
h.killTimeout = task.KillTimeout
|
2016-08-22 16:34:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-30 02:27:06 +00:00
|
|
|
// TODO Implement when we need it.
|
|
|
|
func (d *mockDriverHandle) Network() *cstructs.DriverNetwork {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 19:37:52 +00:00
|
|
|
// TODO Implement when we need it.
|
|
|
|
func (h *mockDriverHandle) Signal(s os.Signal) error {
|
2016-10-10 21:49:37 +00:00
|
|
|
return h.signalErr
|
2016-10-07 19:37:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
// Kill kills a mock task
|
|
|
|
func (h *mockDriverHandle) Kill() error {
|
2018-02-20 01:14:54 +00:00
|
|
|
h.logger.Printf("[DEBUG] driver.mock: killing task %q after %s or kill timeout: %v", h.taskName, h.killAfter, h.killTimeout)
|
2016-08-22 16:34:24 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
case <-time.After(h.killAfter):
|
2017-12-06 23:40:04 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
// already closed
|
|
|
|
default:
|
|
|
|
close(h.doneCh)
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
case <-time.After(h.killTimeout):
|
|
|
|
h.logger.Printf("[DEBUG] driver.mock: terminating task %q", h.taskName)
|
2017-12-06 23:40:04 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
// already closed
|
|
|
|
default:
|
|
|
|
close(h.doneCh)
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Implement when we need it.
|
|
|
|
func (h *mockDriverHandle) Stats() (*cstructs.TaskResourceUsage, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run waits for the configured amount of time and then indicates the task has
|
|
|
|
// terminated
|
|
|
|
func (h *mockDriverHandle) run() {
|
2018-01-31 20:13:57 +00:00
|
|
|
// Setup logging output
|
|
|
|
if h.stdoutString != "" {
|
|
|
|
go h.handleLogging()
|
|
|
|
}
|
|
|
|
|
2016-08-22 16:34:24 +00:00
|
|
|
timer := time.NewTimer(h.runFor)
|
|
|
|
defer timer.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
2017-12-06 23:40:04 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
// already closed
|
|
|
|
default:
|
|
|
|
close(h.doneCh)
|
|
|
|
}
|
2016-08-22 16:34:24 +00:00
|
|
|
case <-h.doneCh:
|
|
|
|
h.logger.Printf("[DEBUG] driver.mock: finished running task %q", h.taskName)
|
|
|
|
h.waitCh <- dstructs.NewWaitResult(h.exitCode, h.exitSignal, h.exitErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 19:31:37 +00:00
|
|
|
|
2018-01-31 20:13:57 +00:00
|
|
|
// handleLogging handles logging stdout messages
|
|
|
|
func (h *mockDriverHandle) handleLogging() {
|
|
|
|
if h.stdoutString == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a log rotator
|
|
|
|
logFileSize := int64(h.task.LogConfig.MaxFileSizeMB * 1024 * 1024)
|
|
|
|
lro, err := logging.NewFileRotator(h.ctx.TaskDir.LogDir, fmt.Sprintf("%v.stdout", h.taskName),
|
|
|
|
h.task.LogConfig.MaxFiles, logFileSize, h.logger)
|
|
|
|
if err != nil {
|
|
|
|
h.exitErr = err
|
|
|
|
close(h.doneCh)
|
|
|
|
h.logger.Printf("[ERR] mock_driver: failed to setup file rotator: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer lro.Close()
|
|
|
|
|
|
|
|
// Do initial write to stdout.
|
|
|
|
if _, err := io.WriteString(lro, h.stdoutString); err != nil {
|
|
|
|
h.exitErr = err
|
|
|
|
close(h.doneCh)
|
|
|
|
h.logger.Printf("[ERR] mock_driver: failed to write to stdout: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < h.stdoutRepeat; i++ {
|
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
return
|
|
|
|
case <-time.After(h.stdoutRepeatDur):
|
|
|
|
if _, err := io.WriteString(lro, h.stdoutString); err != nil {
|
|
|
|
h.exitErr = err
|
|
|
|
close(h.doneCh)
|
|
|
|
h.logger.Printf("[ERR] mock_driver: failed to write to stdout: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|