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"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/args"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/environment"
|
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 {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Have raw_exec use this as well.
|
|
|
|
func NewBasicExecutor() Executor {
|
|
|
|
return &BasicExecutor{}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 17:58:57 +00:00
|
|
|
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.
|
|
|
|
envVars, err := environment.ParseFromList(e.cmd.Env)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:41:42 +00:00
|
|
|
e.cmd.Path = args.ReplaceEnv(e.cmd.Path, envVars.Map())
|
2015-11-03 20:47:48 +00:00
|
|
|
combined := strings.Join(e.cmd.Args, " ")
|
|
|
|
parsed, err := args.ParseAndReplace(combined, envVars.Map())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-05 01:20:52 +00:00
|
|
|
e.cmd.Args = parsed
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return proc.Kill()
|
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
|
|
|
|
}
|