2015-11-03 20:47:48 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2015-11-05 17:58:57 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-11-03 20:47:48 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-11-05 01:20:52 +00:00
|
|
|
"os/exec"
|
2015-11-05 17:58:57 +00:00
|
|
|
"path/filepath"
|
2015-11-06 18:42:49 +00:00
|
|
|
"runtime"
|
2015-11-03 20:47:48 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-11-05 17:58:57 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/spawn"
|
2015-11-03 20:47:48 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-14 06:07:13 +00:00
|
|
|
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/driver/structs"
|
2015-11-03 20:47:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BasicExecutor should work everywhere, and as a result does not include
|
|
|
|
// any resource restrictions or runas capabilities.
|
|
|
|
type BasicExecutor struct {
|
2016-01-11 17:58:26 +00:00
|
|
|
*ExecutorContext
|
2015-11-05 17:58:57 +00:00
|
|
|
cmd exec.Cmd
|
|
|
|
spawn *spawn.Spawner
|
|
|
|
taskName string
|
|
|
|
taskDir string
|
|
|
|
allocDir string
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 17:58:26 +00:00
|
|
|
func NewBasicExecutor(ctx *ExecutorContext) Executor {
|
|
|
|
return &BasicExecutor{ExecutorContext: ctx}
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) Limit(resources *structs.Resources) error {
|
|
|
|
if resources == nil {
|
|
|
|
return errNoResources
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) ConfigureTaskDir(taskName string, alloc *allocdir.AllocDir) error {
|
|
|
|
taskDir, ok := alloc.TaskDirs[taskName]
|
|
|
|
if !ok {
|
2015-11-21 00:01:27 +00:00
|
|
|
return fmt.Errorf("Couldn't find task directory for task %v", taskName)
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
2015-11-05 01:20:52 +00:00
|
|
|
e.cmd.Dir = taskDir
|
2015-11-05 17:58:57 +00:00
|
|
|
|
|
|
|
e.taskDir = taskDir
|
|
|
|
e.taskName = taskName
|
|
|
|
e.allocDir = alloc.AllocDir
|
2015-11-03 20:47:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) Start() error {
|
|
|
|
// Parse the commands arguments and replace instances of Nomad environment
|
|
|
|
// variables.
|
2016-01-11 17:58:26 +00:00
|
|
|
e.cmd.Path = e.taskEnv.ReplaceEnv(e.cmd.Path)
|
|
|
|
e.cmd.Args = e.taskEnv.ParseAndReplace(e.cmd.Args)
|
2016-01-20 20:00:20 +00:00
|
|
|
e.cmd.Env = e.taskEnv.Build().EnvList()
|
2015-11-03 20:47:48 +00:00
|
|
|
|
2015-11-05 17:58:57 +00:00
|
|
|
spawnState := filepath.Join(e.allocDir, fmt.Sprintf("%s_%s", e.taskName, "exit_status"))
|
|
|
|
e.spawn = spawn.NewSpawner(spawnState)
|
|
|
|
e.spawn.SetCommand(&e.cmd)
|
|
|
|
e.spawn.SetLogs(&spawn.Logs{
|
|
|
|
Stdout: filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", e.taskName)),
|
|
|
|
Stderr: filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stderr", e.taskName)),
|
|
|
|
Stdin: os.DevNull,
|
|
|
|
})
|
|
|
|
|
|
|
|
return e.spawn.Spawn(nil)
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 17:58:57 +00:00
|
|
|
func (e *BasicExecutor) Open(id string) error {
|
|
|
|
var spawn spawn.Spawner
|
|
|
|
dec := json.NewDecoder(strings.NewReader(id))
|
|
|
|
if err := dec.Decode(&spawn); err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse id: %v", err)
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 17:58:57 +00:00
|
|
|
// Setup the executor.
|
|
|
|
e.spawn = &spawn
|
2015-11-06 19:23:27 +00:00
|
|
|
return e.spawn.Valid()
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 06:07:13 +00:00
|
|
|
func (e *BasicExecutor) Wait() *cstructs.WaitResult {
|
|
|
|
return e.spawn.Wait()
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) ID() (string, error) {
|
2015-11-05 17:58:57 +00:00
|
|
|
if e.spawn == nil {
|
|
|
|
return "", fmt.Errorf("Process was never started")
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buffer)
|
|
|
|
if err := enc.Encode(e.spawn); err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to serialize id: %v", err)
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
2015-11-05 17:58:57 +00:00
|
|
|
|
|
|
|
return buffer.String(), nil
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) Shutdown() error {
|
2015-11-05 17:58:57 +00:00
|
|
|
proc, err := os.FindProcess(e.spawn.UserPid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to find user processes %v: %v", e.spawn.UserPid, err)
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:42:49 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return proc.Kill()
|
|
|
|
}
|
|
|
|
|
2015-11-05 17:58:57 +00:00
|
|
|
return proc.Signal(os.Interrupt)
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) ForceStop() error {
|
2015-11-05 17:58:57 +00:00
|
|
|
proc, err := os.FindProcess(e.spawn.UserPid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to find user processes %v: %v", e.spawn.UserPid, err)
|
|
|
|
}
|
|
|
|
|
2016-01-21 22:09:04 +00:00
|
|
|
if err := proc.Kill(); err != nil && err.Error() != "os: process already finished" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-11-03 20:47:48 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 01:20:52 +00:00
|
|
|
func (e *BasicExecutor) Command() *exec.Cmd {
|
2015-11-03 20:47:48 +00:00
|
|
|
return &e.cmd
|
|
|
|
}
|