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-02-02 21:38:38 +00:00
|
|
|
"os/exec"
|
2016-02-03 02:54:04 +00:00
|
|
|
"path/filepath"
|
2015-09-22 23:32:05 +00:00
|
|
|
"syscall"
|
2015-08-29 23:20:07 +00:00
|
|
|
"time"
|
2015-08-20 23:50:28 +00:00
|
|
|
|
2016-02-02 21:38:38 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2016-02-03 02:54:04 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-08-25 23:21:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-11-05 00:53:27 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
2016-02-02 21:38:38 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/plugins"
|
2015-11-17 00:23:03 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2016-02-03 02:54:04 +00:00
|
|
|
"github.com/hashicorp/nomad/client/getter"
|
2016-02-02 22:36:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/discover"
|
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
|
|
|
)
|
|
|
|
|
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 {
|
2015-11-18 23:16:42 +00:00
|
|
|
ArtifactSource string `mapstructure:"artifact_source"`
|
|
|
|
Checksum string `mapstructure:"checksum"`
|
|
|
|
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-02 21:38:38 +00:00
|
|
|
pluginClient *plugin.Client
|
|
|
|
executor plugins.Executor
|
|
|
|
cmd executor.Executor
|
|
|
|
killTimeout time.Duration
|
|
|
|
logger *log.Logger
|
|
|
|
waitCh chan *cstructs.WaitResult
|
|
|
|
doneCh chan struct{}
|
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
|
|
|
}
|
|
|
|
|
2015-08-25 23:21:29 +00:00
|
|
|
func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
2016-01-27 06:23:02 +00:00
|
|
|
// Only enable if cgroups are available and we are root
|
2016-01-29 13:34:29 +00:00
|
|
|
if _, ok := node.Attributes["unique.cgroup.mountpoint"]; !ok {
|
2016-01-27 06:23:02 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling")
|
2015-10-29 00:22:04 +00:00
|
|
|
return false, nil
|
|
|
|
} else if syscall.Geteuid() != 0 {
|
2015-09-23 00:36:44 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
|
2015-09-22 23:32:05 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-08-20 23:53:43 +00:00
|
|
|
node.Attributes["driver.exec"] = "1"
|
2015-08-20 23:50:28 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2015-08-23 23:49:48 +00:00
|
|
|
|
2016-01-27 06:23:02 +00:00
|
|
|
func (d *ExecDriver) Periodic() (bool, time.Duration) {
|
|
|
|
return true, 15 * time.Second
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
// Get the command to be ran
|
|
|
|
command := driverConfig.Command
|
|
|
|
if command == "" {
|
|
|
|
return nil, fmt.Errorf("missing command for exec driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a location to download the artifact.
|
|
|
|
taskDir, ok := ctx.AllocDir.TaskDirs[d.DriverContext.taskName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Could not find task directory for task: %v", d.DriverContext.taskName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an artificat is specified and attempt to download it
|
|
|
|
source, ok := task.Config["artifact_source"]
|
|
|
|
if ok && source != "" {
|
|
|
|
// Proceed to download an artifact to be executed.
|
|
|
|
_, err := getter.GetArtifact(
|
|
|
|
filepath.Join(taskDir, allocdir.TaskLocal),
|
|
|
|
driverConfig.ArtifactSource,
|
|
|
|
driverConfig.Checksum,
|
|
|
|
d.logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|
2016-02-03 02:54:04 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2016-02-02 21:38:38 +00:00
|
|
|
HandshakeConfig: plugins.HandshakeConfig,
|
|
|
|
Plugins: plugins.PluginMap,
|
2016-02-02 22:36:11 +00:00
|
|
|
Cmd: exec.Command(bin, "executor"),
|
2015-09-15 20:11:56 +00:00
|
|
|
}
|
2015-09-16 14:13:54 +00:00
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
executor, pluginClient, err := d.executor(pluginConfig)
|
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-03 02:54:04 +00:00
|
|
|
executorCtx := &plugins.ExecutorContext{
|
|
|
|
TaskEnv: d.taskEnv,
|
|
|
|
AllocDir: ctx.AllocDir,
|
|
|
|
Task: task,
|
|
|
|
}
|
|
|
|
ps, err := executor.LaunchCmd(&plugins.ExecCommand{Cmd: command, Args: driverConfig.Args}, executorCtx)
|
2016-02-02 21:38:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error starting process via the plugin: %v", err)
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
2016-02-02 21:38:38 +00:00
|
|
|
d.logger.Printf("DIPTANU Started process via plugin: %#v", ps)
|
2015-08-29 23:20:07 +00:00
|
|
|
|
|
|
|
// Return a driver handle
|
|
|
|
h := &execHandle{
|
2016-02-03 02:54:04 +00:00
|
|
|
pluginClient: pluginClient,
|
|
|
|
executor: executor,
|
2016-02-02 21:38:38 +00:00
|
|
|
//cmd: cmd,
|
2015-12-23 00:10:30 +00:00
|
|
|
killTimeout: d.DriverContext.KillTimeout(task),
|
|
|
|
logger: d.logger,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
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-02 21:38:38 +00:00
|
|
|
//ExecutorId string
|
|
|
|
KillTimeout time.Duration
|
|
|
|
PluginConfig *plugin.ReattachConfig
|
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-03 02:54:04 +00:00
|
|
|
bin, err := discover.NomadExecutable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:38:38 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
|
|
|
HandshakeConfig: plugins.HandshakeConfig,
|
|
|
|
Plugins: plugins.PluginMap,
|
2016-02-03 02:54:04 +00:00
|
|
|
Cmd: exec.Command(bin, "executor"),
|
2016-02-02 21:38:38 +00:00
|
|
|
Reattach: id.PluginConfig,
|
|
|
|
}
|
|
|
|
executor, client, err := d.executor(pluginConfig)
|
2015-09-15 20:45:48 +00:00
|
|
|
if err != nil {
|
2016-02-02 21:38:38 +00:00
|
|
|
return nil, fmt.Errorf("error connecting to plugin: %v", err)
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a driver handle
|
|
|
|
h := &execHandle{
|
2016-02-02 21:38:38 +00:00
|
|
|
pluginClient: client,
|
|
|
|
executor: executor,
|
|
|
|
logger: d.logger,
|
|
|
|
killTimeout: id.KillTimeout,
|
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-02 21:38:38 +00:00
|
|
|
func (d *ExecDriver) executor(config *plugin.ClientConfig) (plugins.Executor, *plugin.Client, error) {
|
|
|
|
executorClient := plugin.NewClient(config)
|
|
|
|
rpcClient, err := executorClient.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("error creating rpc client for executor plugin: %v", err)
|
|
|
|
}
|
2016-02-03 02:54:04 +00:00
|
|
|
rpcClient.SyncStreams(d.config.LogOutput, d.config.LogOutput)
|
2016-02-02 21:38:38 +00:00
|
|
|
|
|
|
|
raw, err := rpcClient.Dispense("executor")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to dispense the executor plugin: %v", err)
|
|
|
|
}
|
|
|
|
executorPlugin := raw.(plugins.Executor)
|
|
|
|
return executorPlugin, executorClient, 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-02 21:38:38 +00:00
|
|
|
KillTimeout: h.killTimeout,
|
|
|
|
PluginConfig: h.pluginClient.ReattachConfig(),
|
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
|
|
|
}
|
|
|
|
|
2015-11-14 06:07:13 +00:00
|
|
|
func (h *execHandle) WaitCh() chan *cstructs.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.
|
|
|
|
h.killTimeout = task.KillTimeout
|
|
|
|
|
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
|
|
|
|
|
|
|
func (h *execHandle) Kill() error {
|
2016-02-02 21:38:38 +00:00
|
|
|
h.executor.ShutDown()
|
2015-08-29 23:20:07 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
return nil
|
2015-12-23 00:10:30 +00:00
|
|
|
case <-time.After(h.killTimeout):
|
2016-02-03 02:54:04 +00:00
|
|
|
err := h.executor.Exit()
|
2016-02-02 21:38:38 +00:00
|
|
|
return err
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *execHandle) run() {
|
2016-02-02 21:38:38 +00:00
|
|
|
ps, err := h.executor.Wait()
|
2015-08-29 23:20:07 +00:00
|
|
|
close(h.doneCh)
|
2016-02-02 21:38:38 +00:00
|
|
|
h.waitCh <- &cstructs.WaitResult{ExitCode: ps.ExitCode, Signal: 0, Err: err}
|
2015-08-29 23:20:07 +00:00
|
|
|
close(h.waitCh)
|
2016-02-03 19:54:54 +00:00
|
|
|
h.pluginClient.Kill()
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|