2015-08-20 23:50:28 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-12-23 00:10:30 +00:00
|
|
|
"encoding/json"
|
2015-08-29 23:20:07 +00:00
|
|
|
"fmt"
|
2015-12-23 00:10:30 +00:00
|
|
|
"log"
|
2016-10-07 19:37:52 +00:00
|
|
|
"os"
|
2016-02-02 21:38:38 +00:00
|
|
|
"os/exec"
|
2016-02-03 02:54:04 +00:00
|
|
|
"path/filepath"
|
2015-08-29 23:20:07 +00:00
|
|
|
"time"
|
2015-08-20 23:50:28 +00:00
|
|
|
|
2016-02-09 00:27:31 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2016-02-02 21:38:38 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2016-02-09 18:17:33 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2016-02-05 00:03:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
2016-06-12 03:15:50 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2016-02-02 22:36:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
2016-04-09 22:38:42 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/fields"
|
2016-02-03 02:54:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-08-20 23:50:28 +00:00
|
|
|
)
|
|
|
|
|
2016-04-01 01:11:27 +00:00
|
|
|
const (
|
|
|
|
// The key populated in Node Attributes to indicate the presence of the Exec
|
|
|
|
// driver
|
|
|
|
execDriverAttr = "driver.exec"
|
|
|
|
)
|
|
|
|
|
2015-09-25 23:49:14 +00:00
|
|
|
// ExecDriver fork/execs tasks using as many of the underlying OS's isolation
|
|
|
|
// features.
|
2015-08-20 23:50:28 +00:00
|
|
|
type ExecDriver struct {
|
2015-09-10 01:06:23 +00:00
|
|
|
DriverContext
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
2016-01-27 06:23:02 +00:00
|
|
|
|
2015-11-14 04:22:49 +00:00
|
|
|
type ExecDriverConfig struct {
|
2016-03-15 17:53:20 +00:00
|
|
|
Command string `mapstructure:"command"`
|
|
|
|
Args []string `mapstructure:"args"`
|
2015-11-14 02:09:42 +00:00
|
|
|
}
|
2015-08-20 23:50:28 +00:00
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
// execHandle is returned from Start/Open as a handle to the PID
|
|
|
|
type execHandle struct {
|
2016-02-08 21:48:26 +00:00
|
|
|
pluginClient *plugin.Client
|
|
|
|
executor executor.Executor
|
2016-06-12 03:15:50 +00:00
|
|
|
isolationConfig *dstructs.IsolationConfig
|
2016-02-08 21:48:26 +00:00
|
|
|
userPid int
|
2016-12-03 01:04:07 +00:00
|
|
|
taskDir *allocdir.TaskDir
|
2016-02-08 21:48:26 +00:00
|
|
|
killTimeout time.Duration
|
2016-03-03 17:21:21 +00:00
|
|
|
maxKillTimeout time.Duration
|
2016-02-08 21:48:26 +00:00
|
|
|
logger *log.Logger
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh chan *dstructs.WaitResult
|
2016-02-08 21:48:26 +00:00
|
|
|
doneCh chan struct{}
|
2016-02-25 03:06:30 +00:00
|
|
|
version string
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 23:50:28 +00:00
|
|
|
// NewExecDriver is used to create a new exec driver
|
2015-09-10 01:06:23 +00:00
|
|
|
func NewExecDriver(ctx *DriverContext) Driver {
|
2015-11-05 21:46:02 +00:00
|
|
|
return &ExecDriver{DriverContext: *ctx}
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 23:15:09 +00:00
|
|
|
// Validate is used to validate the driver configuration
|
2016-04-08 20:19:43 +00:00
|
|
|
func (d *ExecDriver) Validate(config map[string]interface{}) error {
|
2016-04-09 22:38:42 +00:00
|
|
|
fd := &fields.FieldData{
|
|
|
|
Raw: config,
|
|
|
|
Schema: map[string]*fields.FieldSchema{
|
|
|
|
"command": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"args": &fields.FieldSchema{
|
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-08 20:19:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 22:06:23 +00:00
|
|
|
func (d *ExecDriver) Abilities() DriverAbilities {
|
|
|
|
return DriverAbilities{
|
|
|
|
SendSignals: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func (d *ExecDriver) FSIsolation() cstructs.FSIsolation {
|
|
|
|
return cstructs.FSIsolationChroot
|
|
|
|
}
|
|
|
|
|
2016-01-27 06:23:02 +00:00
|
|
|
func (d *ExecDriver) Periodic() (bool, time.Duration) {
|
|
|
|
return true, 15 * time.Second
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func (d *ExecDriver) Prestart(ctx *ExecContext, task *structs.Task) error {
|
2016-11-30 00:39:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
|
2016-02-03 02:54:04 +00:00
|
|
|
var driverConfig ExecDriverConfig
|
|
|
|
if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-23 18:45:03 +00:00
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
// Get the command to be ran
|
|
|
|
command := driverConfig.Command
|
2016-02-23 18:19:40 +00:00
|
|
|
if err := validateCommand(command, "args"); err != nil {
|
|
|
|
return nil, err
|
2016-02-03 02:54:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
|
|
|
}
|
2017-01-06 00:02:20 +00:00
|
|
|
pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out")
|
2016-02-03 02:54:04 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2016-02-05 01:36:31 +00:00
|
|
|
Cmd: exec.Command(bin, "executor", pluginLogFile),
|
2015-09-15 20:11:56 +00:00
|
|
|
}
|
2015-09-16 14:13:54 +00:00
|
|
|
|
2016-02-08 21:29:53 +00:00
|
|
|
exec, pluginClient, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
|
2016-02-02 21:38:38 +00:00
|
|
|
if err != nil {
|
2016-02-03 02:54:04 +00:00
|
|
|
return nil, err
|
2015-09-25 23:49:14 +00:00
|
|
|
}
|
2016-02-05 00:03:17 +00:00
|
|
|
executorCtx := &executor.ExecutorContext{
|
2016-12-03 01:04:07 +00:00
|
|
|
TaskEnv: d.taskEnv,
|
|
|
|
Driver: "exec",
|
|
|
|
AllocID: ctx.AllocID,
|
|
|
|
LogDir: ctx.TaskDir.LogDir,
|
|
|
|
TaskDir: ctx.TaskDir.Dir,
|
|
|
|
Task: task,
|
2016-02-03 02:54:04 +00:00
|
|
|
}
|
2016-10-12 18:35:29 +00:00
|
|
|
if err := exec.SetContext(executorCtx); err != nil {
|
|
|
|
pluginClient.Kill()
|
|
|
|
return nil, fmt.Errorf("failed to set executor context: %v", err)
|
|
|
|
}
|
2016-03-23 11:57:31 +00:00
|
|
|
|
2016-10-12 18:35:29 +00:00
|
|
|
execCmd := &executor.ExecCommand{
|
2016-03-17 09:53:31 +00:00
|
|
|
Cmd: command,
|
|
|
|
Args: driverConfig.Args,
|
|
|
|
FSIsolation: true,
|
|
|
|
ResourceLimits: true,
|
2016-03-23 11:57:31 +00:00
|
|
|
User: getExecutorUser(task),
|
2016-10-12 18:35:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := exec.LaunchCmd(execCmd)
|
2016-02-02 21:38:38 +00:00
|
|
|
if err != nil {
|
2016-02-04 01:22:21 +00:00
|
|
|
pluginClient.Kill()
|
2016-03-19 19:18:10 +00:00
|
|
|
return nil, err
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
2016-10-12 18:35:29 +00:00
|
|
|
|
2016-02-06 02:07:06 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.exec: started process via plugin with pid: %v", ps.Pid)
|
2015-08-29 23:20:07 +00:00
|
|
|
|
|
|
|
// Return a driver handle
|
2016-03-03 17:21:21 +00:00
|
|
|
maxKill := d.DriverContext.config.MaxKillTimeout
|
2015-08-29 23:20:07 +00:00
|
|
|
h := &execHandle{
|
2016-02-08 21:48:26 +00:00
|
|
|
pluginClient: pluginClient,
|
|
|
|
userPid: ps.Pid,
|
|
|
|
executor: exec,
|
|
|
|
isolationConfig: ps.IsolationConfig,
|
2016-03-03 17:21:21 +00:00
|
|
|
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
|
|
|
|
maxKillTimeout: maxKill,
|
2016-02-08 21:48:26 +00:00
|
|
|
logger: d.logger,
|
2016-02-25 03:06:30 +00:00
|
|
|
version: d.config.Version,
|
2016-02-08 21:48:26 +00:00
|
|
|
doneCh: make(chan struct{}),
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
2016-03-24 22:39:10 +00:00
|
|
|
if err := exec.SyncServices(consulContext(d.config, "")); err != nil {
|
2016-03-24 21:53:53 +00:00
|
|
|
d.logger.Printf("[ERR] driver.exec: error registering services with consul for task: %q: %v", task.Name, err)
|
2016-03-23 20:19:45 +00:00
|
|
|
}
|
2015-08-29 23:20:07 +00:00
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 00:10:30 +00:00
|
|
|
type execId struct {
|
2016-02-25 03:06:30 +00:00
|
|
|
Version string
|
2016-02-08 21:48:26 +00:00
|
|
|
KillTimeout time.Duration
|
2016-03-03 17:21:21 +00:00
|
|
|
MaxKillTimeout time.Duration
|
2016-02-08 21:48:26 +00:00
|
|
|
UserPid int
|
2016-06-12 03:15:50 +00:00
|
|
|
IsolationConfig *dstructs.IsolationConfig
|
2016-02-09 20:59:05 +00:00
|
|
|
PluginConfig *PluginReattachConfig
|
2015-12-23 00:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
2015-12-23 00:10:30 +00:00
|
|
|
id := &execId{}
|
|
|
|
if err := json.Unmarshal([]byte(handleID), id); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:38:38 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2016-02-05 18:49:54 +00:00
|
|
|
Reattach: id.PluginConfig.PluginConfig(),
|
2016-02-02 21:38:38 +00:00
|
|
|
}
|
2016-02-09 00:08:29 +00:00
|
|
|
exec, client, err := createExecutor(pluginConfig, d.config.LogOutput, d.config)
|
2015-09-15 20:45:48 +00:00
|
|
|
if err != nil {
|
2016-02-09 00:27:31 +00:00
|
|
|
merrs := new(multierror.Error)
|
|
|
|
merrs.Errors = append(merrs.Errors, err)
|
2016-02-24 23:57:58 +00:00
|
|
|
d.logger.Println("[ERR] driver.exec: error connecting to plugin so destroying plugin pid and user pid")
|
2016-02-04 21:53:30 +00:00
|
|
|
if e := destroyPlugin(id.PluginConfig.Pid, id.UserPid); e != nil {
|
2016-02-09 00:27:31 +00:00
|
|
|
merrs.Errors = append(merrs.Errors, fmt.Errorf("error destroying plugin and userpid: %v", e))
|
2016-02-04 21:53:30 +00:00
|
|
|
}
|
2016-02-08 21:48:26 +00:00
|
|
|
if id.IsolationConfig != nil {
|
2016-04-19 00:20:11 +00:00
|
|
|
ePid := pluginConfig.Reattach.Pid
|
2016-07-10 06:45:33 +00:00
|
|
|
if e := executor.ClientCleanup(id.IsolationConfig, ePid); e != nil {
|
2016-02-09 00:27:31 +00:00
|
|
|
merrs.Errors = append(merrs.Errors, fmt.Errorf("destroying cgroup failed: %v", e))
|
2016-02-08 21:48:26 +00:00
|
|
|
}
|
2016-02-04 21:53:30 +00:00
|
|
|
}
|
2016-02-09 02:51:11 +00:00
|
|
|
return nil, fmt.Errorf("error connecting to plugin: %v", merrs.ErrorOrNil())
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 05:05:02 +00:00
|
|
|
ver, _ := exec.Version()
|
|
|
|
d.logger.Printf("[DEBUG] driver.exec : version of executor: %v", ver.Version)
|
2015-08-29 23:20:07 +00:00
|
|
|
// Return a driver handle
|
|
|
|
h := &execHandle{
|
2016-02-08 21:48:26 +00:00
|
|
|
pluginClient: client,
|
2016-02-09 00:08:29 +00:00
|
|
|
executor: exec,
|
2016-02-08 21:48:26 +00:00
|
|
|
userPid: id.UserPid,
|
|
|
|
isolationConfig: id.IsolationConfig,
|
|
|
|
logger: d.logger,
|
2016-02-25 03:06:30 +00:00
|
|
|
version: id.Version,
|
2016-02-08 21:48:26 +00:00
|
|
|
killTimeout: id.KillTimeout,
|
2016-03-03 17:21:21 +00:00
|
|
|
maxKillTimeout: id.MaxKillTimeout,
|
2016-02-08 21:48:26 +00:00
|
|
|
doneCh: make(chan struct{}),
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
2016-03-24 22:39:10 +00:00
|
|
|
if err := exec.SyncServices(consulContext(d.config, "")); err != nil {
|
|
|
|
d.logger.Printf("[ERR] driver.exec: error registering services with consul: %v", err)
|
|
|
|
}
|
2015-08-29 23:20:07 +00:00
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *execHandle) ID() string {
|
2015-12-23 00:10:30 +00:00
|
|
|
id := execId{
|
2016-02-25 03:06:30 +00:00
|
|
|
Version: h.version,
|
2016-02-08 21:48:26 +00:00
|
|
|
KillTimeout: h.killTimeout,
|
2016-03-03 17:21:21 +00:00
|
|
|
MaxKillTimeout: h.maxKillTimeout,
|
2016-02-09 20:59:05 +00:00
|
|
|
PluginConfig: NewPluginReattachConfig(h.pluginClient.ReattachConfig()),
|
2016-02-08 21:48:26 +00:00
|
|
|
UserPid: h.userPid,
|
|
|
|
IsolationConfig: h.isolationConfig,
|
2015-12-23 00:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(id)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.exec: failed to marshal ID to JSON: %s", err)
|
|
|
|
}
|
|
|
|
return string(data)
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:15:50 +00:00
|
|
|
func (h *execHandle) WaitCh() chan *dstructs.WaitResult {
|
2015-08-23 23:49:48 +00:00
|
|
|
return h.waitCh
|
|
|
|
}
|
|
|
|
|
2015-08-29 23:20:07 +00:00
|
|
|
func (h *execHandle) Update(task *structs.Task) error {
|
2016-02-04 03:43:44 +00:00
|
|
|
// Store the updated kill timeout.
|
2016-03-03 17:21:21 +00:00
|
|
|
h.killTimeout = GetKillTimeout(task.KillTimeout, h.maxKillTimeout)
|
2016-03-17 09:53:31 +00:00
|
|
|
h.executor.UpdateTask(task)
|
2016-02-04 03:43:44 +00:00
|
|
|
|
2015-08-29 23:20:07 +00:00
|
|
|
// Update is not possible
|
2015-08-23 23:49:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-08-29 23:20:07 +00:00
|
|
|
|
2016-10-07 19:37:52 +00:00
|
|
|
func (h *execHandle) Signal(s os.Signal) error {
|
2016-10-10 18:46:27 +00:00
|
|
|
return h.executor.Signal(s)
|
2016-10-07 19:37:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 23:20:07 +00:00
|
|
|
func (h *execHandle) Kill() error {
|
2016-02-09 03:31:57 +00:00
|
|
|
if err := h.executor.ShutDown(); err != nil {
|
2016-02-17 05:00:49 +00:00
|
|
|
if h.pluginClient.Exited() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-09 03:31:57 +00:00
|
|
|
return fmt.Errorf("executor Shutdown failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-29 23:20:07 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
2015-12-23 00:10:30 +00:00
|
|
|
case <-time.After(h.killTimeout):
|
2016-02-04 20:40:48 +00:00
|
|
|
if h.pluginClient.Exited() {
|
2016-12-03 01:04:07 +00:00
|
|
|
break
|
2016-02-04 20:40:48 +00:00
|
|
|
}
|
2016-02-09 03:31:57 +00:00
|
|
|
if err := h.executor.Exit(); err != nil {
|
|
|
|
return fmt.Errorf("executor Exit failed: %v", err)
|
|
|
|
}
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
2016-12-03 01:04:07 +00:00
|
|
|
return nil
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:06:01 +00:00
|
|
|
func (h *execHandle) Stats() (*cstructs.TaskResourceUsage, error) {
|
|
|
|
return h.executor.Stats()
|
|
|
|
}
|
|
|
|
|
2015-08-29 23:20:07 +00:00
|
|
|
func (h *execHandle) run() {
|
2016-11-04 21:58:55 +00:00
|
|
|
ps, werr := h.executor.Wait()
|
2015-08-29 23:20:07 +00:00
|
|
|
close(h.doneCh)
|
2016-02-09 18:17:33 +00:00
|
|
|
|
|
|
|
// If the exitcode is 0 and we had an error that means the plugin didn't
|
|
|
|
// connect and doesn't know the state of the user process so we are killing
|
|
|
|
// the user process so that when we create a new executor on restarting the
|
|
|
|
// new user process doesn't have collisions with resources that the older
|
|
|
|
// user pid might be holding onto.
|
2016-11-04 21:58:55 +00:00
|
|
|
if ps.ExitCode == 0 && werr != nil {
|
2016-02-09 18:17:33 +00:00
|
|
|
if h.isolationConfig != nil {
|
2016-04-19 00:20:11 +00:00
|
|
|
ePid := h.pluginClient.ReattachConfig().Pid
|
2016-07-10 06:45:33 +00:00
|
|
|
if e := executor.ClientCleanup(h.isolationConfig, ePid); e != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.exec: destroying resource container failed: %v", e)
|
2016-02-09 18:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 21:58:55 +00:00
|
|
|
|
2016-03-23 20:19:45 +00:00
|
|
|
// Remove services
|
|
|
|
if err := h.executor.DeregisterServices(); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.exec: failed to deregister services: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:58:55 +00:00
|
|
|
// Exit the executor
|
2016-03-18 19:04:11 +00:00
|
|
|
if err := h.executor.Exit(); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.exec: error destroying executor: %v", err)
|
|
|
|
}
|
2016-02-03 19:54:54 +00:00
|
|
|
h.pluginClient.Kill()
|
2016-11-04 21:58:55 +00:00
|
|
|
|
|
|
|
// Send the results
|
|
|
|
h.waitCh <- dstructs.NewWaitResult(ps.ExitCode, ps.Signal, werr)
|
|
|
|
close(h.waitCh)
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|