2018-09-30 03:05:14 +00:00
|
|
|
package rawexec
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul-template/signals"
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2018-10-04 19:08:20 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-10-02 19:40:27 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/eventer"
|
2018-09-26 14:42:02 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/utils"
|
2018-09-28 17:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared"
|
2018-09-19 19:56:50 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/loader"
|
2018-09-19 19:56:50 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2018-10-15 21:08:12 +00:00
|
|
|
const (
|
|
|
|
// pluginName is the name of the plugin
|
|
|
|
pluginName = "raw_exec"
|
|
|
|
|
|
|
|
// fingerprintPeriod is the interval at which the driver will send fingerprint responses
|
|
|
|
fingerprintPeriod = 30 * time.Second
|
|
|
|
)
|
|
|
|
|
2018-10-15 17:58:09 +00:00
|
|
|
var (
|
2018-10-15 21:08:12 +00:00
|
|
|
// PluginID is the rawexec plugin metadata registered in the plugin
|
|
|
|
// catalog.
|
2018-10-15 17:58:09 +00:00
|
|
|
PluginID = loader.PluginID{
|
2018-10-04 19:08:20 +00:00
|
|
|
Name: pluginName,
|
|
|
|
PluginType: base.PluginTypeDriver,
|
2018-10-15 17:58:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-15 21:08:12 +00:00
|
|
|
// PluginConfig is the rawexec factory function registered in the
|
|
|
|
// plugin catalog.
|
2018-10-15 17:58:09 +00:00
|
|
|
PluginConfig = &loader.InternalPluginConfig{
|
2018-10-04 19:08:20 +00:00
|
|
|
Config: map[string]interface{}{},
|
|
|
|
Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) },
|
2018-10-15 17:58:09 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
// PluginLoader maps pre-0.9 client driver options to post-0.9 plugin options.
|
2018-10-15 17:58:09 +00:00
|
|
|
func PluginLoader(opts map[string]string) (map[string]interface{}, error) {
|
|
|
|
conf := map[string]interface{}{}
|
|
|
|
if v, err := strconv.ParseBool(opts["driver.raw_exec.enable"]); err == nil {
|
|
|
|
conf["enabled"] = v
|
|
|
|
}
|
|
|
|
if v, err := strconv.ParseBool(opts["driver.raw_exec.no_cgroups"]); err == nil {
|
|
|
|
conf["no_cgroups"] = v
|
|
|
|
}
|
|
|
|
return conf, nil
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 19:56:50 +00:00
|
|
|
var (
|
2018-09-30 03:05:14 +00:00
|
|
|
// pluginInfo is the response returned for the PluginInfo RPC
|
2018-09-26 14:42:02 +00:00
|
|
|
pluginInfo = &base.PluginInfoResponse{
|
|
|
|
Type: base.PluginTypeDriver,
|
2018-09-19 19:56:50 +00:00
|
|
|
PluginApiVersion: "0.0.1",
|
|
|
|
PluginVersion: "0.1.0",
|
|
|
|
Name: pluginName,
|
|
|
|
}
|
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// configSpec is the hcl specification returned by the ConfigSchema RPC
|
2018-09-19 19:56:50 +00:00
|
|
|
configSpec = hclspec.NewObject(map[string]*hclspec.Spec{
|
|
|
|
"enabled": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("enabled", "bool", false),
|
|
|
|
hclspec.NewLiteral("false"),
|
|
|
|
),
|
|
|
|
"no_cgroups": hclspec.NewDefault(
|
|
|
|
hclspec.NewAttr("no_cgroups", "bool", false),
|
|
|
|
hclspec.NewLiteral("false"),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2018-09-30 03:05:14 +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
|
2018-09-19 19:56:50 +00:00
|
|
|
taskConfigSpec = hclspec.NewObject(map[string]*hclspec.Spec{
|
|
|
|
"command": hclspec.NewAttr("command", "string", true),
|
2018-10-04 19:08:20 +00:00
|
|
|
"args": hclspec.NewAttr("args", "list(string)", false),
|
2018-09-19 19:56:50 +00:00
|
|
|
})
|
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// capabilities is returned by the Capabilities RPC and indicates what
|
|
|
|
// optional features this driver supports
|
2018-09-26 14:42:02 +00:00
|
|
|
capabilities = &drivers.Capabilities{
|
2018-09-19 19:56:50 +00:00
|
|
|
SendSignals: true,
|
|
|
|
Exec: true,
|
2018-10-04 19:08:20 +00:00
|
|
|
FSIsolation: cstructs.FSIsolationNone,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
// Driver is a privileged version of the exec driver. It provides no
|
2018-09-19 19:56:50 +00:00
|
|
|
// resource isolation and just fork/execs. The Exec driver should be preferred
|
|
|
|
// and this should only be used when explicitly needed.
|
2018-10-31 00:16:22 +00:00
|
|
|
type Driver struct {
|
2018-09-30 03:05:14 +00:00
|
|
|
// eventer is used to handle multiplexing of TaskEvents calls such that an
|
|
|
|
// event can be broadcast to all callers
|
2018-10-02 19:40:27 +00:00
|
|
|
eventer *eventer.Eventer
|
2018-09-30 03:05:14 +00:00
|
|
|
|
|
|
|
// config is the driver configuration set by the SetConfig RPC
|
2018-09-19 19:56:50 +00:00
|
|
|
config *Config
|
|
|
|
|
2018-10-17 02:21:15 +00:00
|
|
|
// nomadConfig is the client config from nomad
|
2018-10-30 01:34:34 +00:00
|
|
|
nomadConfig *base.ClientDriverConfig
|
2018-10-17 02:21:15 +00:00
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// tasks is the in memory datastore mapping taskIDs to rawExecDriverHandles
|
|
|
|
tasks *taskStore
|
|
|
|
|
|
|
|
// ctx is the context for the driver. It is passed to other subsystems to
|
|
|
|
// coordinate shutdown
|
|
|
|
ctx context.Context
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// signalShutdown is called when the driver is shutting down and cancels the
|
|
|
|
// ctx passed to any subsystems
|
|
|
|
signalShutdown context.CancelFunc
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
// logger will log to the Nomad agent
|
2018-09-19 19:56:50 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// Config is the driver configuration set by the SetConfig RPC call
|
2018-09-19 19:56:50 +00:00
|
|
|
type Config struct {
|
2018-09-26 05:12:58 +00:00
|
|
|
// NoCgroups tracks whether we should use a cgroup to manage the process
|
2018-09-19 19:56:50 +00:00
|
|
|
// tree
|
2018-10-17 00:14:21 +00:00
|
|
|
NoCgroups bool `codec:"no_cgroups"`
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-26 05:12:58 +00:00
|
|
|
// Enabled is set to true to enable the raw_exec driver
|
2018-10-17 00:14:21 +00:00
|
|
|
Enabled bool `codec:"enabled"`
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 03:05:14 +00:00
|
|
|
// TaskConfig is the driver configuration of a task within a job
|
2018-09-19 19:56:50 +00:00
|
|
|
type TaskConfig struct {
|
2018-10-17 00:14:21 +00:00
|
|
|
Command string `codec:"command"`
|
|
|
|
Args []string `codec:"args"`
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
// TaskState is the state which is encoded in the handle returned in
|
2018-09-30 03:05:14 +00:00
|
|
|
// StartTask. This information is needed to rebuild the task state and handler
|
|
|
|
// during recovery.
|
2018-10-31 00:16:22 +00:00
|
|
|
type TaskState struct {
|
2018-09-28 17:09:01 +00:00
|
|
|
ReattachConfig *shared.ReattachConfig
|
2018-09-30 03:05:14 +00:00
|
|
|
TaskConfig *drivers.TaskConfig
|
|
|
|
Pid int
|
|
|
|
StartedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRawExecDriver returns a new DriverPlugin implementation
|
2018-09-26 14:42:02 +00:00
|
|
|
func NewRawExecDriver(logger hclog.Logger) drivers.DriverPlugin {
|
2018-09-30 03:05:14 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
logger = logger.Named(pluginName)
|
2018-10-31 00:16:22 +00:00
|
|
|
return &Driver{
|
2018-10-31 18:54:08 +00:00
|
|
|
eventer: eventer.NewEventer(ctx, logger),
|
|
|
|
config: &Config{},
|
|
|
|
tasks: newTaskStore(),
|
|
|
|
ctx: ctx,
|
|
|
|
signalShutdown: cancel,
|
|
|
|
logger: logger,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) PluginInfo() (*base.PluginInfoResponse, error) {
|
2018-09-19 19:56:50 +00:00
|
|
|
return pluginInfo, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) ConfigSchema() (*hclspec.Spec, error) {
|
2018-09-19 19:56:50 +00:00
|
|
|
return configSpec, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) SetConfig(data []byte, cfg *base.ClientAgentConfig) error {
|
2018-09-19 19:56:50 +00:00
|
|
|
var config Config
|
2018-09-26 14:42:02 +00:00
|
|
|
if err := base.MsgPackDecode(data, &config); err != nil {
|
2018-09-19 19:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
d.config = &config
|
2018-10-19 03:32:17 +00:00
|
|
|
if cfg != nil {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.nomadConfig = cfg.Driver
|
2018-10-19 03:32:17 +00:00
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) Shutdown(ctx context.Context) error {
|
|
|
|
d.signalShutdown()
|
2018-09-19 19:56:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) TaskConfigSchema() (*hclspec.Spec, error) {
|
2018-09-19 19:56:50 +00:00
|
|
|
return taskConfigSpec, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) Capabilities() (*drivers.Capabilities, error) {
|
2018-09-19 19:56:50 +00:00
|
|
|
return capabilities, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint, error) {
|
2018-09-26 14:42:02 +00:00
|
|
|
ch := make(chan *drivers.Fingerprint)
|
2018-10-31 18:54:08 +00:00
|
|
|
go d.handleFingerprint(ctx, ch)
|
2018-09-26 05:12:58 +00:00
|
|
|
return ch, nil
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) handleFingerprint(ctx context.Context, ch chan<- *drivers.Fingerprint) {
|
2018-10-11 19:42:08 +00:00
|
|
|
defer close(ch)
|
2018-09-19 19:56:50 +00:00
|
|
|
ticker := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2018-10-31 18:54:08 +00:00
|
|
|
case <-d.ctx.Done():
|
2018-09-30 03:05:14 +00:00
|
|
|
return
|
2018-09-19 19:56:50 +00:00
|
|
|
case <-ticker.C:
|
2018-10-31 18:54:08 +00:00
|
|
|
ticker.Reset(fingerprintPeriod)
|
|
|
|
ch <- d.buildFingerprint()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
2018-09-26 14:42:02 +00:00
|
|
|
var health drivers.HealthState
|
2018-09-19 19:56:50 +00:00
|
|
|
var desc string
|
2018-09-26 05:12:58 +00:00
|
|
|
attrs := map[string]string{}
|
2018-10-31 18:54:08 +00:00
|
|
|
if d.config.Enabled {
|
2018-09-26 14:42:02 +00:00
|
|
|
health = drivers.HealthStateHealthy
|
2018-10-31 00:16:22 +00:00
|
|
|
desc = "ready"
|
2018-09-26 05:12:58 +00:00
|
|
|
attrs["driver.raw_exec"] = "1"
|
2018-09-19 19:56:50 +00:00
|
|
|
} else {
|
2018-09-26 14:42:02 +00:00
|
|
|
health = drivers.HealthStateUndetected
|
2018-10-31 00:16:22 +00:00
|
|
|
desc = "disabled"
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-09-30 03:05:14 +00:00
|
|
|
|
|
|
|
return &drivers.Fingerprint{
|
2018-10-31 00:16:22 +00:00
|
|
|
Attributes: attrs,
|
2018-09-19 19:56:50 +00:00
|
|
|
Health: health,
|
|
|
|
HealthDescription: desc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
|
2018-09-30 03:05:14 +00:00
|
|
|
if handle == nil {
|
2018-10-18 20:39:02 +00:00
|
|
|
return fmt.Errorf("handle cannot be nil")
|
2018-09-30 03:05:14 +00:00
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-10-18 20:39:02 +00:00
|
|
|
// 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
|
2018-10-31 00:16:22 +00:00
|
|
|
var taskState TaskState
|
2018-09-30 03:05:14 +00:00
|
|
|
if err := handle.GetDriverState(&taskState); err != nil {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Error("failed to decode task state from handle", "error", err, "task_id", handle.Config.ID)
|
2018-09-30 03:05:14 +00:00
|
|
|
return fmt.Errorf("failed to decode task state from handle: %v", err)
|
2018-09-25 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 17:09:01 +00:00
|
|
|
plugRC, err := shared.ReattachConfigToGoPlugin(taskState.ReattachConfig)
|
2018-09-25 18:12:58 +00:00
|
|
|
if err != nil {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Error("failed to build ReattachConfig from task state", "error", err, "task_id", handle.Config.ID)
|
2018-09-30 03:05:14 +00:00
|
|
|
return fmt.Errorf("failed to build ReattachConfig from task state: %v", err)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2018-09-25 18:12:58 +00:00
|
|
|
Reattach: plugRC,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 20:39:02 +00:00
|
|
|
// Create client for reattached executor
|
2018-09-19 19:56:50 +00:00
|
|
|
exec, pluginClient, err := utils.CreateExecutorWithConfig(pluginConfig, os.Stderr)
|
|
|
|
if err != nil {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Error("failed to reattach to executor", "error", err, "task_id", handle.Config.ID)
|
2018-09-30 03:05:14 +00:00
|
|
|
return fmt.Errorf("failed to reattach to executor: %v", err)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
h := &taskHandle{
|
2018-09-19 19:56:50 +00:00
|
|
|
exec: exec,
|
|
|
|
pid: taskState.Pid,
|
|
|
|
pluginClient: pluginClient,
|
2018-10-31 18:54:08 +00:00
|
|
|
taskConfig: taskState.TaskConfig,
|
2018-09-26 14:42:02 +00:00
|
|
|
procState: drivers.TaskStateRunning,
|
2018-09-19 19:56:50 +00:00
|
|
|
startedAt: taskState.StartedAt,
|
2018-09-26 14:42:02 +00:00
|
|
|
exitResult: &drivers.ExitResult{},
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
d.tasks.Set(taskState.TaskConfig.ID, h)
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
go h.run()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstructs.DriverNetwork, error) {
|
|
|
|
if _, ok := d.tasks.Get(cfg.ID); ok {
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var driverConfig TaskConfig
|
|
|
|
if err := cfg.DecodeDriverConfig(&driverConfig); err != nil {
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to decode driver config: %v", err)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Info("starting task", "driver_cfg", hclog.Fmt("%+v", driverConfig))
|
2018-09-26 14:42:02 +00:00
|
|
|
handle := drivers.NewTaskHandle(pluginName)
|
2018-09-19 19:56:50 +00:00
|
|
|
handle.Config = cfg
|
|
|
|
|
|
|
|
pluginLogFile := filepath.Join(cfg.TaskDir().Dir, "executor.out")
|
|
|
|
executorConfig := &dstructs.ExecutorConfig{
|
|
|
|
LogFile: pluginLogFile,
|
|
|
|
LogLevel: "debug",
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
exec, pluginClient, err := utils.CreateExecutor(os.Stderr, hclog.Debug, d.nomadConfig, executorConfig)
|
2018-09-19 19:56:50 +00:00
|
|
|
if err != nil {
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to create executor: %v", err)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
execCmd := &executor.ExecCommand{
|
2018-09-26 19:33:25 +00:00
|
|
|
Cmd: driverConfig.Command,
|
|
|
|
Args: driverConfig.Args,
|
|
|
|
Env: cfg.EnvList(),
|
|
|
|
User: cfg.User,
|
2018-10-31 18:54:08 +00:00
|
|
|
BasicProcessCgroup: !d.config.NoCgroups,
|
2018-09-25 18:12:58 +00:00
|
|
|
TaskDir: cfg.TaskDir().Dir,
|
2018-09-26 19:33:25 +00:00
|
|
|
StdoutPath: cfg.StdoutPath,
|
|
|
|
StderrPath: cfg.StderrPath,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := exec.Launch(execCmd)
|
|
|
|
if err != nil {
|
|
|
|
pluginClient.Kill()
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to launch command with executor: %v", err)
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
h := &taskHandle{
|
2018-09-19 19:56:50 +00:00
|
|
|
exec: exec,
|
|
|
|
pid: ps.Pid,
|
|
|
|
pluginClient: pluginClient,
|
2018-10-31 18:54:08 +00:00
|
|
|
taskConfig: cfg,
|
2018-09-26 14:42:02 +00:00
|
|
|
procState: drivers.TaskStateRunning,
|
2018-09-25 18:12:58 +00:00
|
|
|
startedAt: time.Now().Round(time.Millisecond),
|
2018-10-31 18:54:08 +00:00
|
|
|
logger: d.logger,
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 00:16:22 +00:00
|
|
|
driverState := TaskState{
|
2018-09-28 17:09:01 +00:00
|
|
|
ReattachConfig: shared.ReattachConfigFromGoPlugin(pluginClient.ReattachConfig()),
|
2018-09-19 19:56:50 +00:00
|
|
|
Pid: ps.Pid,
|
|
|
|
TaskConfig: cfg,
|
|
|
|
StartedAt: h.startedAt,
|
2018-09-25 18:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := handle.SetDriverState(&driverState); err != nil {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Error("failed to start task, error setting driver state", "error", err)
|
2018-09-25 18:12:58 +00:00
|
|
|
exec.Shutdown("", 0)
|
|
|
|
pluginClient.Kill()
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil, nil, fmt.Errorf("failed to set driver state: %v", err)
|
2018-09-25 18:12:58 +00:00
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
d.tasks.Set(cfg.ID, h)
|
2018-09-19 19:56:50 +00:00
|
|
|
go h.run()
|
2018-10-04 19:08:20 +00:00
|
|
|
return handle, nil, nil
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) WaitTask(ctx context.Context, taskID string) (<-chan *drivers.ExitResult, error) {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return nil, drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-09-30 03:05:14 +00:00
|
|
|
|
|
|
|
ch := make(chan *drivers.ExitResult)
|
2018-10-31 18:54:08 +00:00
|
|
|
go d.handleWait(ctx, handle, ch)
|
2018-09-19 19:56:50 +00:00
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) handleWait(ctx context.Context, handle *taskHandle, ch chan *drivers.ExitResult) {
|
2018-09-19 19:56:50 +00:00
|
|
|
defer close(ch)
|
2018-09-26 14:42:02 +00:00
|
|
|
var result *drivers.ExitResult
|
2018-09-19 19:56:50 +00:00
|
|
|
ps, err := handle.exec.Wait()
|
|
|
|
if err != nil {
|
2018-09-26 14:42:02 +00:00
|
|
|
result = &drivers.ExitResult{
|
2018-09-19 19:56:50 +00:00
|
|
|
Err: fmt.Errorf("executor: error waiting on process: %v", err),
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-26 14:42:02 +00:00
|
|
|
result = &drivers.ExitResult{
|
2018-09-19 19:56:50 +00:00
|
|
|
ExitCode: ps.ExitCode,
|
|
|
|
Signal: ps.Signal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2018-10-31 18:54:08 +00:00
|
|
|
case <-d.ctx.Done():
|
2018-09-30 03:05:14 +00:00
|
|
|
return
|
2018-09-19 19:56:50 +00:00
|
|
|
case ch <- result:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) error {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-25 18:12:58 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return drivers.ErrTaskNotFound
|
2018-09-25 18:12:58 +00:00
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
if err := handle.exec.Shutdown(signal, timeout); err != nil {
|
|
|
|
if handle.pluginClient.Exited() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("executor Shutdown failed: %v", err)
|
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
return nil
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) DestroyTask(taskID string, force bool) error {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 18:12:58 +00:00
|
|
|
if handle.IsRunning() && !force {
|
|
|
|
return fmt.Errorf("cannot destroy running task")
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:56:50 +00:00
|
|
|
if !handle.pluginClient.Exited() {
|
2018-09-25 18:12:58 +00:00
|
|
|
if handle.IsRunning() {
|
|
|
|
if err := handle.exec.Shutdown("", 0); err != nil {
|
|
|
|
handle.logger.Error("destroying executor failed", "err", err)
|
|
|
|
}
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-09-25 18:12:58 +00:00
|
|
|
|
|
|
|
handle.pluginClient.Kill()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
d.tasks.Delete(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) InspectTask(taskID string) (*drivers.TaskStatus, error) {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return nil, drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
return handle.TaskStatus(), nil
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) TaskStats(taskID string) (*cstructs.TaskResourceUsage, error) {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return nil, drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
return handle.exec.Stats()
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) TaskEvents(ctx context.Context) (<-chan *drivers.TaskEvent, error) {
|
|
|
|
return d.eventer.TaskEvents(ctx)
|
2018-09-30 03:05:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) SignalTask(taskID string, signal string) error {
|
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sig := os.Interrupt
|
|
|
|
if s, ok := signals.SignalLookup[signal]; ok {
|
2018-10-31 18:54:08 +00:00
|
|
|
d.logger.Warn("signal to send to task unknown, using SIGINT", "signal", signal, "task_id", handle.taskConfig.ID)
|
2018-09-19 19:56:50 +00:00
|
|
|
sig = s
|
|
|
|
}
|
|
|
|
return handle.exec.Signal(sig)
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:54:08 +00:00
|
|
|
func (d *Driver) ExecTask(taskID string, cmd []string, timeout time.Duration) (*drivers.ExecTaskResult, error) {
|
2018-09-19 19:56:50 +00:00
|
|
|
if len(cmd) == 0 {
|
2018-10-31 14:30:13 +00:00
|
|
|
return nil, fmt.Errorf("error cmd must have at least one value")
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
2018-10-31 18:54:08 +00:00
|
|
|
handle, ok := d.tasks.Get(taskID)
|
2018-09-19 19:56:50 +00:00
|
|
|
if !ok {
|
2018-09-26 14:42:02 +00:00
|
|
|
return nil, drivers.ErrTaskNotFound
|
2018-09-19 19:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 14:30:13 +00:00
|
|
|
out, exitCode, err := handle.exec.Exec(time.Now().Add(timeout), cmd[0], cmd[1:])
|
2018-09-19 19:56:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:42:02 +00:00
|
|
|
return &drivers.ExecTaskResult{
|
2018-09-19 19:56:50 +00:00
|
|
|
Stdout: out,
|
2018-09-26 14:42:02 +00:00
|
|
|
ExitResult: &drivers.ExitResult{
|
2018-09-19 19:56:50 +00:00
|
|
|
ExitCode: exitCode,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|