242 lines
6.5 KiB
Go
242 lines
6.5 KiB
Go
package driver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/hashicorp/go-plugin"
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
"github.com/hashicorp/nomad/client/config"
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
|
"github.com/hashicorp/nomad/client/driver/plugins"
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
"github.com/hashicorp/nomad/client/getter"
|
|
"github.com/hashicorp/nomad/helper/discover"
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
"github.com/mitchellh/mapstructure"
|
|
)
|
|
|
|
// ExecDriver fork/execs tasks using as many of the underlying OS's isolation
|
|
// features.
|
|
type ExecDriver struct {
|
|
DriverContext
|
|
}
|
|
|
|
type ExecDriverConfig struct {
|
|
ArtifactSource string `mapstructure:"artifact_source"`
|
|
Checksum string `mapstructure:"checksum"`
|
|
Command string `mapstructure:"command"`
|
|
Args []string `mapstructure:"args"`
|
|
}
|
|
|
|
// execHandle is returned from Start/Open as a handle to the PID
|
|
type execHandle struct {
|
|
pluginClient *plugin.Client
|
|
executor plugins.Executor
|
|
cmd executor.Executor
|
|
killTimeout time.Duration
|
|
logger *log.Logger
|
|
waitCh chan *cstructs.WaitResult
|
|
doneCh chan struct{}
|
|
}
|
|
|
|
// NewExecDriver is used to create a new exec driver
|
|
func NewExecDriver(ctx *DriverContext) Driver {
|
|
return &ExecDriver{DriverContext: *ctx}
|
|
}
|
|
|
|
func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
|
// Only enable if cgroups are available and we are root
|
|
if _, ok := node.Attributes["unique.cgroup.mountpoint"]; !ok {
|
|
d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling")
|
|
return false, nil
|
|
} else if syscall.Geteuid() != 0 {
|
|
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
|
|
return false, nil
|
|
}
|
|
|
|
node.Attributes["driver.exec"] = "1"
|
|
return true, nil
|
|
}
|
|
|
|
func (d *ExecDriver) Periodic() (bool, time.Duration) {
|
|
return true, 15 * time.Second
|
|
}
|
|
|
|
func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
|
|
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
|
|
}
|
|
}
|
|
|
|
bin, err := discover.NomadExecutable()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
|
}
|
|
pluginConfig := &plugin.ClientConfig{
|
|
HandshakeConfig: plugins.HandshakeConfig,
|
|
Plugins: plugins.PluginMap,
|
|
Cmd: exec.Command(bin, "executor"),
|
|
}
|
|
|
|
executor, pluginClient, err := d.executor(pluginConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
executorCtx := &plugins.ExecutorContext{
|
|
TaskEnv: d.taskEnv,
|
|
AllocDir: ctx.AllocDir,
|
|
Task: task,
|
|
}
|
|
ps, err := executor.LaunchCmd(&plugins.ExecCommand{Cmd: command, Args: driverConfig.Args}, executorCtx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error starting process via the plugin: %v", err)
|
|
}
|
|
d.logger.Printf("DIPTANU Started process via plugin: %#v", ps)
|
|
|
|
// Return a driver handle
|
|
h := &execHandle{
|
|
pluginClient: pluginClient,
|
|
executor: executor,
|
|
//cmd: cmd,
|
|
killTimeout: d.DriverContext.KillTimeout(task),
|
|
logger: d.logger,
|
|
doneCh: make(chan struct{}),
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
|
}
|
|
go h.run()
|
|
return h, nil
|
|
}
|
|
|
|
type execId struct {
|
|
//ExecutorId string
|
|
KillTimeout time.Duration
|
|
PluginConfig *plugin.ReattachConfig
|
|
}
|
|
|
|
func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
|
id := &execId{}
|
|
if err := json.Unmarshal([]byte(handleID), id); err != nil {
|
|
return nil, fmt.Errorf("Failed to parse handle '%s': %v", handleID, err)
|
|
}
|
|
|
|
bin, err := discover.NomadExecutable()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
|
|
}
|
|
|
|
pluginConfig := &plugin.ClientConfig{
|
|
HandshakeConfig: plugins.HandshakeConfig,
|
|
Plugins: plugins.PluginMap,
|
|
Cmd: exec.Command(bin, "executor"),
|
|
Reattach: id.PluginConfig,
|
|
}
|
|
executor, client, err := d.executor(pluginConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error connecting to plugin: %v", err)
|
|
}
|
|
|
|
// Return a driver handle
|
|
h := &execHandle{
|
|
pluginClient: client,
|
|
executor: executor,
|
|
logger: d.logger,
|
|
killTimeout: id.KillTimeout,
|
|
doneCh: make(chan struct{}),
|
|
waitCh: make(chan *cstructs.WaitResult, 1),
|
|
}
|
|
go h.run()
|
|
return h, nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
rpcClient.SyncStreams(d.config.LogOutput, d.config.LogOutput)
|
|
|
|
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
|
|
}
|
|
|
|
func (h *execHandle) ID() string {
|
|
id := execId{
|
|
KillTimeout: h.killTimeout,
|
|
PluginConfig: h.pluginClient.ReattachConfig(),
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (h *execHandle) WaitCh() chan *cstructs.WaitResult {
|
|
return h.waitCh
|
|
}
|
|
|
|
func (h *execHandle) Update(task *structs.Task) error {
|
|
// Store the updated kill timeout.
|
|
h.killTimeout = task.KillTimeout
|
|
|
|
// Update is not possible
|
|
return nil
|
|
}
|
|
|
|
func (h *execHandle) Kill() error {
|
|
h.executor.ShutDown()
|
|
select {
|
|
case <-h.doneCh:
|
|
return nil
|
|
case <-time.After(h.killTimeout):
|
|
err := h.executor.Exit()
|
|
return err
|
|
}
|
|
}
|
|
|
|
func (h *execHandle) run() {
|
|
ps, err := h.executor.Wait()
|
|
close(h.doneCh)
|
|
h.waitCh <- &cstructs.WaitResult{ExitCode: ps.ExitCode, Signal: 0, Err: err}
|
|
close(h.waitCh)
|
|
h.pluginClient.Kill()
|
|
}
|