2016-02-02 22:36:11 +00:00
|
|
|
// +build !linux
|
|
|
|
|
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
2016-02-03 02:54:04 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-02-02 22:36:11 +00:00
|
|
|
"os/exec"
|
2016-02-03 02:54:04 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2016-02-02 22:36:11 +00:00
|
|
|
"time"
|
2016-02-03 02:54:04 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2016-02-02 22:36:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BasicExecutor struct {
|
2016-02-03 02:54:04 +00:00
|
|
|
logger *log.Logger
|
|
|
|
cmd exec.Cmd
|
2016-02-02 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
func NewExecutor(logger *log.Logger) Executor {
|
|
|
|
return &BasicExecutor{logger: logger}
|
2016-02-02 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
func (e *BasicExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext) (*ProcessState, error) {
|
|
|
|
e.cmd.Path = command.Cmd
|
|
|
|
e.cmd.Args = append([]string{command.Cmd}, command.Args...)
|
|
|
|
e.cmd.Path = ctx.TaskEnv.ReplaceEnv(e.cmd.Path)
|
|
|
|
e.cmd.Args = ctx.TaskEnv.ParseAndReplace(e.cmd.Args)
|
|
|
|
|
|
|
|
if filepath.Base(command.Cmd) == command.Cmd {
|
|
|
|
if lp, err := exec.LookPath(command.Cmd); err != nil {
|
|
|
|
} else {
|
|
|
|
e.cmd.Path = lp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.configureTaskDir(ctx.Task.Name, ctx.AllocDir)
|
|
|
|
e.cmd.Env = ctx.TaskEnv.EnvList()
|
|
|
|
stdoPath := filepath.Join(e.cmd.Dir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", ctx.Task.Name))
|
|
|
|
stdo, err := os.OpenFile(stdoPath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.cmd.Stdout = stdo
|
|
|
|
|
|
|
|
stdePath := filepath.Join(e.cmd.Dir, allocdir.TaskLocal, fmt.Sprintf("%v.stderr", ctx.Task.Name))
|
|
|
|
stde, err := os.OpenFile(stdePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.cmd.Stderr = stde
|
|
|
|
if err := e.cmd.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-02 22:36:11 +00:00
|
|
|
return &ProcessState{Pid: 5, ExitCode: -1, Time: time.Now()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) Wait() (*ProcessState, error) {
|
2016-02-03 02:54:04 +00:00
|
|
|
err := e.cmd.Wait()
|
|
|
|
exitCode := 1
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &ProcessState{Pid: 0, ExitCode: exitCode, Time: time.Now()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BasicExecutor) Exit() error {
|
|
|
|
proc, err := os.FindProcess(e.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failied to find user process %v: %v", e.cmd.Process.Pid, err)
|
|
|
|
}
|
|
|
|
return proc.Kill()
|
2016-02-02 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
func (e *BasicExecutor) ShutDown() error {
|
|
|
|
proc, err := os.FindProcess(e.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return proc.Kill()
|
|
|
|
}
|
|
|
|
return proc.Signal(os.Interrupt)
|
2016-02-02 22:36:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 02:54:04 +00:00
|
|
|
func (e *BasicExecutor) configureTaskDir(taskName string, allocDir *allocdir.AllocDir) error {
|
|
|
|
taskDir, ok := allocDir.TaskDirs[taskName]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Couldn't find task directory for task %v", taskName)
|
|
|
|
}
|
|
|
|
e.cmd.Dir = taskDir
|
|
|
|
return nil
|
2016-02-02 22:36:11 +00:00
|
|
|
}
|