2018-10-16 02:37:58 +00:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
2018-11-13 01:09:27 +00:00
|
|
|
"context"
|
2018-10-16 02:37:58 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-11-04 15:11:34 +00:00
|
|
|
"runtime"
|
2018-10-16 02:37:58 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul-template/signals"
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2018-11-04 15:11:34 +00:00
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2018-10-16 02:37:58 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/drivers/shared/eventer"
|
2018-12-05 16:04:18 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/executor/structs"
|
2018-10-16 02:37:58 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/utils"
|
2018-11-29 13:59:53 +00:00
|
|
|
pexecutor "github.com/hashicorp/nomad/plugins/executor"
|
2018-09-28 17:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared"
|
2018-10-16 02:37:58 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-10-31 17:52:15 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/loader"
|
2018-11-21 00:30:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-10-16 02:37:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// pluginName is the name of the plugin
|
|
|
|
pluginName = "exec"
|
|
|
|
|
|
|
|
// fingerprintPeriod is the interval at which the driver will send fingerprint responses
|
|
|
|
fingerprintPeriod = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-02 12:39:18 +00:00
|
|
|
// PluginID is the exec plugin metadata registered in the plugin
|
2018-10-31 17:52:15 +00:00
|
|
|
// catalog.
|
|
|
|
PluginID = loader.PluginID{
|
|
|
|
Name: pluginName,
|
|
|
|
PluginType: base.PluginTypeDriver,
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:39:18 +00:00
|
|
|
// PluginConfig is the exec driver factory function registered in the
|
|
|
|
// plugin catalog.
|
|
|
|
PluginConfig = &loader.InternalPluginConfig{
|
|
|
|
Config: map[string]interface{}{},
|
|
|
|
Factory: func(l hclog.Logger) interface{} { return NewExecDriver(l) },
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:37:58 +00:00
|
|
|
// pluginInfo is the response returned for the PluginInfo RPC
|
|
|
|
pluginInfo = &base.PluginInfoResponse{
|
|
|
|
Type: base.PluginTypeDriver,
|
|
|
|
PluginApiVersion: "0.0.1",
|
|
|
|
PluginVersion: "0.1.0",
|
|
|
|
Name: pluginName,
|
|
|
|
}
|
|
|
|
|
|
|
|
// configSpec is the hcl specification returned by the ConfigSchema RPC
|
2018-10-31 17:52:15 +00:00
|
|
|
configSpec = hclspec.NewObject(map[string]*hclspec.Spec{})
|
2018-10-16 02:37:58 +00:00
|
|
|
|
|
|
|
// taskConfigSpec is the hcl specification for the driver config section of
|
|
|
|
// a task within a job. It is returned in the TaskConfigSchema RPC
|
|
|
|
taskConfigSpec = hclspec.NewObject(map[string]*hclspec.Spec{
|
|
|
|
"command": hclspec.NewAttr("command", "string", true),
|
|
|
|
"args": hclspec.NewAttr("args", "list(string)", false),
|
|
|
|
})
|
|
|
|
|
|
|
|
// capabilities is returned by the Capabilities RPC and indicates what
|
|
|
|
// optional features this driver supports
|
|
|
|
capabilities = &drivers.Capabilities{
|
|
|
|
SendSignals: true,
|
|
|
|
Exec: true,
|
|
|
|
FSIsolation: cstructs.FSIsolationChroot,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
// Driver fork/execs tasks using many of the underlying OS's isolation
|
2018-10-16 02:37:58 +00:00
|
|
|
// features where configured.
|
2018-11-04 15:11:34 +00:00
|
|
|
type Driver struct {
|
2018-10-16 02:37:58 +00:00
|
|
|
// eventer is used to handle multiplexing of TaskEvents calls such that an
|
|
|
|
// event can be broadcast to all callers
|
|
|
|
eventer *eventer.Eventer
|
|
|
|
|
2018-10-17 03:00:26 +00:00
|
|
|
// nomadConfig is the client config from nomad
|
2018-10-30 01:34:34 +00:00
|
|
|
nomadConfig *base.ClientDriverConfig
|
2018-10-17 03:00:26 +00:00
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
// tasks is the in memory datastore mapping taskIDs to driverHandles
|
2018-10-16 02:37:58 +00:00
|
|
|
tasks *taskStore
|
|
|
|
|
|
|
|
// ctx is the context for the driver. It is passed to other subsystems to
|
|
|
|
// coordinate shutdown
|
|
|
|
ctx context.Context
|
|
|
|
|
|
|
|
// signalShutdown is called when the driver is shutting down and cancels the
|
|
|
|
// ctx passed to any subsystems
|
|
|
|
signalShutdown context.CancelFunc
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
// logger will log to the Nomad agent
|
2018-10-16 02:37:58 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskConfig is the driver configuration of a task within a job
|
|
|
|
type TaskConfig struct {
|
2018-11-04 15:11:34 +00:00
|
|
|
Command string `codec:"command"`
|
|
|
|
Args []string `codec:"args"`
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskState is the state which is encoded in the handle returned in
|
|
|
|
// StartTask. This information is needed to rebuild the task state and handler
|
|
|
|
// during recovery.
|
|
|
|
type TaskState struct {
|
2018-09-28 17:09:01 +00:00
|
|
|
ReattachConfig *shared.ReattachConfig
|
2018-10-16 02:37:58 +00:00
|
|
|
TaskConfig *drivers.TaskConfig
|
|
|
|
Pid int
|
|
|
|
StartedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecDriver returns a new DrivePlugin implementation
|
|
|
|
func NewExecDriver(logger hclog.Logger) drivers.DriverPlugin {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
logger = logger.Named(pluginName)
|
2018-11-04 15:11:34 +00:00
|
|
|
return &Driver{
|
2018-10-16 02:37:58 +00:00
|
|
|
eventer: eventer.NewEventer(ctx, logger),
|
|
|
|
tasks: newTaskStore(),
|
|
|
|
ctx: ctx,
|
|
|
|
signalShutdown: cancel,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) PluginInfo() (*base.PluginInfoResponse, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
return pluginInfo, nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) ConfigSchema() (*hclspec.Spec, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
return configSpec, nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) SetConfig(_ []byte, cfg *base.ClientAgentConfig) error {
|
2018-10-19 03:32:17 +00:00
|
|
|
if cfg != nil {
|
|
|
|
d.nomadConfig = cfg.Driver
|
|
|
|
}
|
2018-10-16 02:37:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) Shutdown(ctx context.Context) error {
|
2018-10-16 02:37:58 +00:00
|
|
|
d.signalShutdown()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) TaskConfigSchema() (*hclspec.Spec, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
return taskConfigSpec, nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) Capabilities() (*drivers.Capabilities, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
return capabilities, nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
ch := make(chan *drivers.Fingerprint)
|
|
|
|
go d.handleFingerprint(ctx, ch)
|
|
|
|
return ch, nil
|
|
|
|
|
|
|
|
}
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) handleFingerprint(ctx context.Context, ch chan<- *drivers.Fingerprint) {
|
2018-10-16 02:37:58 +00:00
|
|
|
defer close(ch)
|
|
|
|
ticker := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
ticker.Reset(fingerprintPeriod)
|
|
|
|
ch <- d.buildFingerprint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return &drivers.Fingerprint{
|
|
|
|
Health: drivers.HealthStateUndetected,
|
|
|
|
HealthDescription: "exec driver unsupported on client OS",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := &drivers.Fingerprint{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: map[string]*pstructs.Attribute{},
|
2018-11-04 15:11:34 +00:00
|
|
|
Health: drivers.HealthStateHealthy,
|
|
|
|
HealthDescription: "healthy",
|
|
|
|
}
|
2018-10-16 02:37:58 +00:00
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
mount, err := fingerprint.FindCgroupMountpointDir()
|
2018-10-16 02:37:58 +00:00
|
|
|
if err != nil {
|
2018-11-04 15:11:34 +00:00
|
|
|
fp.Health = drivers.HealthStateUnhealthy
|
|
|
|
fp.HealthDescription = "failed to discover cgroup mount point"
|
|
|
|
d.logger.Warn(fp.HealthDescription, "error", err)
|
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
if mount == "" {
|
|
|
|
fp.Health = drivers.HealthStateUnhealthy
|
2018-11-07 21:52:08 +00:00
|
|
|
fp.HealthDescription = "requires cgroup"
|
2018-11-04 15:11:34 +00:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:53:11 +00:00
|
|
|
if !utils.IsUnixRoot() {
|
2018-11-04 15:11:34 +00:00
|
|
|
fp.Health = drivers.HealthStateUnhealthy
|
2018-11-07 21:52:08 +00:00
|
|
|
fp.HealthDescription = "requires root"
|
2018-11-04 15:11:34 +00:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
2018-11-21 00:30:39 +00:00
|
|
|
fp.Attributes["driver.exec"] = pstructs.NewBoolAttribute(true)
|
2018-11-04 15:11:34 +00:00
|
|
|
return fp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
|
|
|
|
if handle == nil {
|
|
|
|
return fmt.Errorf("handle cannot be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If already attached to handle there's nothing to recover.
|
|
|
|
if _, ok := d.tasks.Get(handle.Config.ID); ok {
|
|
|
|
d.logger.Trace("nothing to recover; task already exists",
|
|
|
|
"task_id", handle.Config.ID,
|
|
|
|
"task_name", handle.Config.Name,
|
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle doesn't already exist, try to reattach
|
|
|
|
var taskState TaskState
|
|
|
|
if err := handle.GetDriverState(&taskState); err != nil {
|
|
|
|
d.logger.Error("failed to decode task state from handle", "error", err, "task_id", handle.Config.ID)
|
|
|
|
return fmt.Errorf("failed to decode task state from handle: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
// Create client for reattached executor
|
2018-09-28 17:09:01 +00:00
|
|
|
plugRC, err := shared.ReattachConfigToGoPlugin(taskState.ReattachConfig)
|
2018-10-16 02:37:58 +00:00
|
|
|
if err != nil {
|
2018-11-04 15:11:34 +00:00
|
|
|
d.logger.Error("failed to build ReattachConfig from task state", "error", err, "task_id", handle.Config.ID)
|
|
|
|
return fmt.Errorf("failed to build ReattachConfig from task state: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pluginConfig := &plugin.ClientConfig{
|
|
|
|
Reattach: plugRC,
|
|
|
|
}
|
|
|
|
|
|
|
|
exec, pluginClient, err := utils.CreateExecutorWithConfig(pluginConfig, os.Stderr)
|
|
|
|
if err != nil {
|
2018-11-04 15:11:34 +00:00
|
|
|
d.logger.Error("failed to reattach to executor", "error", err, "task_id", handle.Config.ID)
|
|
|
|
return fmt.Errorf("failed to reattach to executor: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
h := &taskHandle{
|
2018-10-16 02:37:58 +00:00
|
|
|
exec: exec,
|
|
|
|
pid: taskState.Pid,
|
|
|
|
pluginClient: pluginClient,
|
2018-11-04 15:11:34 +00:00
|
|
|
taskConfig: taskState.TaskConfig,
|
2018-10-16 02:37:58 +00:00
|
|
|
procState: drivers.TaskStateRunning,
|
|
|
|
startedAt: taskState.StartedAt,
|
2018-11-04 15:11:34 +00:00
|
|
|
exitResult: &drivers.ExitResult{},
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.tasks.Set(taskState.TaskConfig.ID, h)
|
|
|
|
|
|
|
|
go h.run()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
if _, ok := d.tasks.Get(cfg.ID); ok {
|
2018-11-04 15:11:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var driverConfig TaskConfig
|
|
|
|
if err := cfg.DecodeDriverConfig(&driverConfig); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("failed to decode driver config: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
d.logger.Info("starting task", "driver_cfg", hclog.Fmt("%+v", driverConfig))
|
2018-10-16 02:37:58 +00:00
|
|
|
handle := drivers.NewTaskHandle(pluginName)
|
|
|
|
handle.Config = cfg
|
|
|
|
|
|
|
|
pluginLogFile := filepath.Join(cfg.TaskDir().Dir, "executor.out")
|
2018-11-29 13:59:53 +00:00
|
|
|
executorConfig := &pexecutor.ExecutorConfig{
|
2018-10-16 02:37:58 +00:00
|
|
|
LogFile: pluginLogFile,
|
|
|
|
LogLevel: "debug",
|
|
|
|
FSIsolation: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: best way to pass port ranges in from client config
|
2018-10-17 03:00:26 +00:00
|
|
|
exec, pluginClient, err := utils.CreateExecutor(os.Stderr, hclog.Debug, d.nomadConfig, executorConfig)
|
2018-10-16 02:37:58 +00:00
|
|
|
if err != nil {
|
2018-11-04 15:11:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to create executor: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 16:04:18 +00:00
|
|
|
execCmd := &structs.ExecCommand{
|
2018-10-16 02:37:58 +00:00
|
|
|
Cmd: driverConfig.Command,
|
|
|
|
Args: driverConfig.Args,
|
|
|
|
Env: cfg.EnvList(),
|
|
|
|
User: cfg.User,
|
|
|
|
ResourceLimits: true,
|
2018-11-09 03:14:27 +00:00
|
|
|
Resources: toExecResources(cfg.Resources),
|
|
|
|
TaskDir: cfg.TaskDir().Dir,
|
|
|
|
StdoutPath: cfg.StdoutPath,
|
|
|
|
StderrPath: cfg.StderrPath,
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := exec.Launch(execCmd)
|
|
|
|
if err != nil {
|
|
|
|
pluginClient.Kill()
|
2018-11-04 15:11:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to launch command with executor: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
h := &taskHandle{
|
2018-10-16 02:37:58 +00:00
|
|
|
exec: exec,
|
|
|
|
pid: ps.Pid,
|
|
|
|
pluginClient: pluginClient,
|
2018-11-04 15:11:34 +00:00
|
|
|
taskConfig: cfg,
|
2018-10-16 02:37:58 +00:00
|
|
|
procState: drivers.TaskStateRunning,
|
|
|
|
startedAt: time.Now().Round(time.Millisecond),
|
|
|
|
logger: d.logger,
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
driverState := TaskState{
|
2018-09-28 17:09:01 +00:00
|
|
|
ReattachConfig: shared.ReattachConfigFromGoPlugin(pluginClient.ReattachConfig()),
|
2018-10-16 02:37:58 +00:00
|
|
|
Pid: ps.Pid,
|
|
|
|
TaskConfig: cfg,
|
|
|
|
StartedAt: h.startedAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handle.SetDriverState(&driverState); err != nil {
|
|
|
|
d.logger.Error("failed to start task, error setting driver state", "error", err)
|
|
|
|
exec.Shutdown("", 0)
|
|
|
|
pluginClient.Kill()
|
2018-11-04 15:11:34 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to set driver state: %v", err)
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.tasks.Set(cfg.ID, h)
|
|
|
|
go h.run()
|
|
|
|
return handle, nil, nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 16:04:18 +00:00
|
|
|
func toExecResources(resources *drivers.Resources) *structs.Resources {
|
2018-11-09 03:14:27 +00:00
|
|
|
if resources == nil || resources.NomadResources == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-05 16:04:18 +00:00
|
|
|
return &structs.Resources{
|
2018-11-09 03:14:27 +00:00
|
|
|
CPU: resources.NomadResources.CPU,
|
|
|
|
MemoryMB: resources.NomadResources.MemoryMB,
|
|
|
|
DiskMB: resources.NomadResources.DiskMB,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) WaitTask(ctx context.Context, taskID string) (<-chan *drivers.ExitResult, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return nil, drivers.ErrTaskNotFound
|
|
|
|
}
|
2018-11-04 15:11:34 +00:00
|
|
|
|
|
|
|
ch := make(chan *drivers.ExitResult)
|
2018-10-16 02:37:58 +00:00
|
|
|
go d.handleWait(ctx, handle, ch)
|
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) handleWait(ctx context.Context, handle *taskHandle, ch chan *drivers.ExitResult) {
|
2018-10-16 02:37:58 +00:00
|
|
|
defer close(ch)
|
2018-11-04 15:11:34 +00:00
|
|
|
var result *drivers.ExitResult
|
2018-12-05 16:04:18 +00:00
|
|
|
ps, err := handle.exec.Wait(ctx)
|
2018-11-04 15:11:34 +00:00
|
|
|
if err != nil {
|
|
|
|
result = &drivers.ExitResult{
|
|
|
|
Err: fmt.Errorf("executor: error waiting on process: %v", err),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = &drivers.ExitResult{
|
|
|
|
ExitCode: ps.ExitCode,
|
|
|
|
Signal: ps.Signal,
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 02:37:58 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
return
|
2018-11-04 15:11:34 +00:00
|
|
|
case ch <- result:
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) error {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := handle.exec.Shutdown(signal, timeout); err != nil {
|
|
|
|
if handle.pluginClient.Exited() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("executor Shutdown failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) DestroyTask(taskID string, force bool) error {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if handle.IsRunning() && !force {
|
|
|
|
return fmt.Errorf("cannot destroy running task")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !handle.pluginClient.Exited() {
|
|
|
|
if handle.IsRunning() {
|
|
|
|
if err := handle.exec.Shutdown("", 0); err != nil {
|
|
|
|
handle.logger.Error("destroying executor failed", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handle.pluginClient.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
d.tasks.Delete(taskID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return nil, drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
return handle.TaskStatus(), nil
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return nil, drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
return handle.exec.Stats()
|
2018-10-16 02:37:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
return d.eventer.TaskEvents(ctx)
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) SignalTask(taskID string, signal string) error {
|
2018-10-16 02:37:58 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := os.Interrupt
|
|
|
|
if s, ok := signals.SignalLookup[signal]; ok {
|
2018-11-04 15:11:34 +00:00
|
|
|
d.logger.Warn("signal to send to task unknown, using SIGINT", "signal", signal, "task_id", handle.taskConfig.ID)
|
2018-10-16 02:37:58 +00:00
|
|
|
sig = s
|
|
|
|
}
|
|
|
|
return handle.exec.Signal(sig)
|
|
|
|
}
|
|
|
|
|
2018-11-04 15:11:34 +00:00
|
|
|
func (d *Driver) ExecTask(taskID string, cmd []string, timeout time.Duration) (*drivers.ExecTaskResult, error) {
|
2018-10-16 02:37:58 +00:00
|
|
|
if len(cmd) == 0 {
|
|
|
|
return nil, fmt.Errorf("error cmd must have atleast one value")
|
|
|
|
}
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
|
|
|
if !ok {
|
|
|
|
return nil, drivers.ErrTaskNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if len(cmd) > 1 {
|
|
|
|
args = cmd[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
out, exitCode, err := handle.exec.Exec(time.Now().Add(timeout), cmd[0], args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &drivers.ExecTaskResult{
|
|
|
|
Stdout: out,
|
|
|
|
ExitResult: &drivers.ExitResult{
|
|
|
|
ExitCode: exitCode,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|