2015-08-20 23:50:28 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-08-29 23:20:07 +00:00
|
|
|
"fmt"
|
2015-09-22 23:32:05 +00:00
|
|
|
"runtime"
|
2015-08-29 23:20:07 +00:00
|
|
|
"strings"
|
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
|
|
|
|
2015-08-25 23:21:29 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-09-15 21:03:03 +00:00
|
|
|
"github.com/hashicorp/nomad/client/executor"
|
2015-08-20 23:50:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ExecDriver is the simplest possible driver. It literally just
|
|
|
|
// fork/execs tasks. It should probably not be used for most things,
|
|
|
|
// but is useful for testing purposes or for very simple tasks.
|
|
|
|
type ExecDriver struct {
|
2015-09-10 01:06:23 +00:00
|
|
|
DriverContext
|
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 {
|
2015-09-15 21:03:03 +00:00
|
|
|
cmd executor.Executor
|
2015-08-23 23:49:48 +00:00
|
|
|
waitCh chan error
|
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
|
2015-09-10 01:06:23 +00:00
|
|
|
func NewExecDriver(ctx *DriverContext) Driver {
|
|
|
|
return &ExecDriver{*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) {
|
2015-09-22 23:32:05 +00:00
|
|
|
// Only enable if we are root when running on non-windows systems.
|
|
|
|
if runtime.GOOS != "windows" && syscall.Geteuid() != 0 {
|
|
|
|
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-08-29 23:20:07 +00:00
|
|
|
// Get the command
|
|
|
|
command, ok := task.Config["command"]
|
|
|
|
if !ok || command == "" {
|
|
|
|
return nil, fmt.Errorf("missing command for exec driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for arguments
|
|
|
|
argRaw, ok := task.Config["args"]
|
|
|
|
var args []string
|
|
|
|
if ok {
|
|
|
|
args = strings.Split(argRaw, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the command
|
2015-09-15 21:03:03 +00:00
|
|
|
cmd := executor.Command(command, args...)
|
2015-09-20 01:47:04 +00:00
|
|
|
if err := cmd.Limit(task.Resources); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to constrain resources: %s", err)
|
2015-09-15 20:11:56 +00:00
|
|
|
}
|
2015-09-16 14:13:54 +00:00
|
|
|
|
2015-09-20 01:47:04 +00:00
|
|
|
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{
|
2015-09-15 20:11:56 +00:00
|
|
|
cmd: cmd,
|
2015-08-29 23:20:07 +00:00
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan error, 1),
|
|
|
|
}
|
|
|
|
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
|
2015-09-20 01:47:04 +00:00
|
|
|
cmd, err := executor.OpenId(handleID)
|
2015-09-15 20:45:48 +00:00
|
|
|
if err != nil {
|
2015-09-20 01:47:04 +00:00
|
|
|
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{
|
2015-09-15 20:11:56 +00:00
|
|
|
cmd: cmd,
|
2015-08-29 23:20:07 +00:00
|
|
|
doneCh: make(chan struct{}),
|
|
|
|
waitCh: make(chan error, 1),
|
|
|
|
}
|
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *execHandle) ID() string {
|
2015-09-20 01:47:04 +00:00
|
|
|
id, _ := h.cmd.ID()
|
|
|
|
return id
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *execHandle) WaitCh() chan error {
|
|
|
|
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 {
|
2015-09-15 20:11:56 +00:00
|
|
|
h.cmd.Shutdown()
|
2015-08-29 23:20:07 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
return nil
|
|
|
|
case <-time.After(5 * time.Second):
|
2015-09-15 20:11:56 +00:00
|
|
|
return h.cmd.ForceStop()
|
2015-08-29 23:20:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *execHandle) run() {
|
2015-09-15 20:11:56 +00:00
|
|
|
err := h.cmd.Wait()
|
2015-08-29 23:20:07 +00:00
|
|
|
close(h.doneCh)
|
|
|
|
if err != nil {
|
|
|
|
h.waitCh <- err
|
|
|
|
}
|
|
|
|
close(h.waitCh)
|
|
|
|
}
|