open-nomad/client/driver/exec.go

168 lines
4.2 KiB
Go
Raw Normal View History

2015-08-20 23:50:28 +00:00
package driver
import (
2015-08-29 23:20:07 +00:00
"fmt"
"path/filepath"
"runtime"
"syscall"
2015-08-29 23:20:07 +00:00
"time"
2015-08-20 23:50:28 +00:00
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/executor"
2015-11-17 00:23:03 +00:00
cstructs "github.com/hashicorp/nomad/client/driver/structs"
2015-11-05 21:46:02 +00:00
"github.com/hashicorp/nomad/client/fingerprint"
2015-11-03 21:16:17 +00:00
"github.com/hashicorp/nomad/client/getter"
2015-08-20 23:50:28 +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 {
DriverContext
2015-11-05 21:46:02 +00:00
fingerprint.StaticFingerprinter
2015-08-20 23:50:28 +00:00
}
2015-11-14 04:22:49 +00:00
type ExecDriverConfig struct {
ArtifactSource string `mapstructure:"artifact_source"`
Checksum string `mapstructure:"checksum"`
Command string `mapstructure:"command"`
Args []string `mapstructure:"args"`
}
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 {
cmd executor.Executor
waitCh chan *cstructs.WaitResult
2015-08-29 23:20:07 +00:00
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
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
}
func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Only enable if we are root on linux.
if runtime.GOOS != "linux" {
d.logger.Printf("[DEBUG] driver.exec: only available on linux, disabling")
return false, nil
} else if syscall.Geteuid() != 0 {
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
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
func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
2015-11-14 04:22:49 +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 == "" {
2015-08-29 23:20:07 +00:00
return nil, fmt.Errorf("missing command for exec driver")
}
2015-11-03 21:16:17 +00:00
// 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.
2015-11-03 21:16:17 +00:00
_, err := getter.GetArtifact(
filepath.Join(taskDir, allocdir.TaskLocal),
driverConfig.ArtifactSource,
driverConfig.Checksum,
2015-11-03 21:16:17 +00:00
d.logger,
)
if err != nil {
return nil, err
}
}
2015-09-26 22:37:48 +00:00
// Get the environment variables.
envVars := TaskEnvironmentVariables(ctx, task)
2015-08-29 23:20:07 +00:00
// Setup the command
cmd := executor.Command(command, driverConfig.Args...)
if err := cmd.Limit(task.Resources); err != nil {
return nil, fmt.Errorf("failed to constrain resources: %s", err)
}
2015-09-16 14:13:54 +00:00
2015-09-23 05:36:10 +00:00
// Populate environment variables
cmd.Command().Env = envVars.List()
2015-09-25 23:49:14 +00:00
if err := cmd.ConfigureTaskDir(d.taskName, ctx.AllocDir); err != nil {
return nil, fmt.Errorf("failed to configure task directory: %v", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start command: %v", err)
2015-08-29 23:20:07 +00:00
}
// Return a driver handle
h := &execHandle{
cmd: cmd,
2015-08-29 23:20:07 +00:00
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
}
func (d *ExecDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
2015-08-29 23:20:07 +00:00
// Find the process
cmd, err := executor.OpenId(handleID)
2015-09-15 20:45:48 +00:00
if err != nil {
return nil, fmt.Errorf("failed to open ID %v: %v", handleID, err)
2015-08-29 23:20:07 +00:00
}
// Return a driver handle
h := &execHandle{
cmd: cmd,
2015-08-29 23:20:07 +00:00
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
}
func (h *execHandle) ID() string {
id, _ := h.cmd.ID()
return id
2015-08-23 23:49:48 +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 {
// 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 {
h.cmd.Shutdown()
2015-08-29 23:20:07 +00:00
select {
case <-h.doneCh:
return nil
case <-time.After(5 * time.Second):
return h.cmd.ForceStop()
2015-08-29 23:20:07 +00:00
}
}
func (h *execHandle) run() {
res := h.cmd.Wait()
2015-08-29 23:20:07 +00:00
close(h.doneCh)
h.waitCh <- res
2015-08-29 23:20:07 +00:00
close(h.waitCh)
}