2016-02-05 00:03:17 +00:00
|
|
|
package executor
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExecutorContext struct {
|
2016-02-04 00:09:17 +00:00
|
|
|
TaskEnv *env.TaskEnvironment
|
|
|
|
AllocDir *allocdir.AllocDir
|
2016-02-04 18:09:52 +00:00
|
|
|
TaskName string
|
|
|
|
TaskResources *structs.Resources
|
2016-02-04 00:09:17 +00:00
|
|
|
FSIsolation bool
|
|
|
|
ResourceLimits bool
|
|
|
|
UnprivilegedUser bool
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExecCommand struct {
|
|
|
|
Cmd string
|
|
|
|
Args []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProcessState struct {
|
|
|
|
Pid int
|
|
|
|
ExitCode int
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type Executor interface {
|
|
|
|
LaunchCmd(command *ExecCommand, ctx *ExecutorContext) (*ProcessState, error)
|
|
|
|
Wait() (*ProcessState, error)
|
|
|
|
ShutDown() error
|
|
|
|
Exit() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type UniversalExecutor struct {
|
|
|
|
cmd exec.Cmd
|
|
|
|
ctx *ExecutorContext
|
|
|
|
|
2016-02-04 00:26:10 +00:00
|
|
|
taskDir string
|
|
|
|
groups *cgroupConfig.Cgroup
|
|
|
|
exitState *ProcessState
|
|
|
|
processExited chan interface{}
|
2016-02-04 00:03:43 +00:00
|
|
|
|
|
|
|
logger *log.Logger
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewExecutor(logger *log.Logger) Executor {
|
2016-02-04 00:26:10 +00:00
|
|
|
return &UniversalExecutor{logger: logger, processExited: make(chan interface{})}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) LaunchCmd(command *ExecCommand, ctx *ExecutorContext) (*ProcessState, error) {
|
|
|
|
e.ctx = ctx
|
2016-02-04 19:51:43 +00:00
|
|
|
|
2016-02-04 00:03:43 +00:00
|
|
|
if err := e.configureTaskDir(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := e.configureIsolation(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-04 00:09:17 +00:00
|
|
|
if e.ctx.UnprivilegedUser {
|
|
|
|
if err := e.runAs("nobody"); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 18:09:52 +00:00
|
|
|
stdoPath := filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stdout", ctx.TaskName))
|
2016-02-04 00:03:43 +00:00
|
|
|
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
|
|
|
|
|
2016-02-04 18:09:52 +00:00
|
|
|
stdePath := filepath.Join(e.taskDir, allocdir.TaskLocal, fmt.Sprintf("%v.stderr", ctx.TaskName))
|
2016-02-04 00:03:43 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
e.cmd.Env = ctx.TaskEnv.EnvList()
|
|
|
|
|
2016-02-04 19:51:43 +00:00
|
|
|
e.cmd.Path = ctx.TaskEnv.ReplaceEnv(command.Cmd)
|
2016-02-04 20:21:06 +00:00
|
|
|
e.cmd.Args = append([]string{e.cmd.Path}, ctx.TaskEnv.ParseAndReplace(command.Args)...)
|
2016-02-04 19:51:43 +00:00
|
|
|
if filepath.Base(command.Cmd) == command.Cmd {
|
|
|
|
if lp, err := exec.LookPath(command.Cmd); err != nil {
|
|
|
|
} else {
|
|
|
|
e.cmd.Path = lp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 00:03:43 +00:00
|
|
|
if err := e.cmd.Start(); err != nil {
|
|
|
|
return nil, fmt.Errorf("error starting command: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
e.applyLimits()
|
2016-02-04 00:26:10 +00:00
|
|
|
go e.wait()
|
2016-02-04 00:03:43 +00:00
|
|
|
return &ProcessState{Pid: e.cmd.Process.Pid, ExitCode: -1, Time: time.Now()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) Wait() (*ProcessState, error) {
|
2016-02-04 00:26:10 +00:00
|
|
|
<-e.processExited
|
|
|
|
return e.exitState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) wait() {
|
2016-02-04 18:21:33 +00:00
|
|
|
defer close(e.processExited)
|
2016-02-04 00:03:43 +00:00
|
|
|
err := e.cmd.Wait()
|
|
|
|
if err == nil {
|
2016-02-04 00:26:10 +00:00
|
|
|
e.exitState = &ProcessState{Pid: 0, ExitCode: 0, Time: time.Now()}
|
|
|
|
return
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
exitCode := 1
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
|
|
|
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
|
|
|
|
exitCode = status.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 00:09:17 +00:00
|
|
|
if e.ctx.FSIsolation {
|
2016-02-04 00:03:43 +00:00
|
|
|
e.removeChrootMounts()
|
|
|
|
}
|
2016-02-04 00:09:17 +00:00
|
|
|
if e.ctx.ResourceLimits {
|
2016-02-04 00:03:43 +00:00
|
|
|
e.destroyCgroup()
|
|
|
|
}
|
2016-02-04 00:26:10 +00:00
|
|
|
e.exitState = &ProcessState{Pid: 0, ExitCode: exitCode, Time: time.Now()}
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) Exit() error {
|
2016-02-04 18:09:52 +00:00
|
|
|
e.logger.Printf("[INFO] Exiting plugin for task %q", e.ctx.TaskName)
|
2016-02-04 21:22:38 +00:00
|
|
|
if e.cmd.Process == nil {
|
|
|
|
return fmt.Errorf("executor.exit error: no process found")
|
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-02-04 00:09:17 +00:00
|
|
|
if e.ctx.FSIsolation {
|
2016-02-04 00:03:43 +00:00
|
|
|
e.removeChrootMounts()
|
|
|
|
}
|
2016-02-04 00:09:17 +00:00
|
|
|
if e.ctx.ResourceLimits {
|
2016-02-04 00:03:43 +00:00
|
|
|
e.destroyCgroup()
|
|
|
|
}
|
2016-02-04 20:40:48 +00:00
|
|
|
if err = proc.Kill(); err != nil {
|
|
|
|
e.logger.Printf("[DEBUG] executor.exit error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) ShutDown() error {
|
2016-02-04 21:22:38 +00:00
|
|
|
if e.cmd.Process == nil {
|
|
|
|
return fmt.Errorf("executor.shutdown error: no process found")
|
|
|
|
}
|
2016-02-04 00:03:43 +00:00
|
|
|
proc, err := os.FindProcess(e.cmd.Process.Pid)
|
|
|
|
if err != nil {
|
2016-02-04 20:40:48 +00:00
|
|
|
return fmt.Errorf("executor.shutdown error: %v", err)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return proc.Kill()
|
|
|
|
}
|
2016-02-04 20:40:48 +00:00
|
|
|
if err = proc.Signal(os.Interrupt); err != nil {
|
|
|
|
return fmt.Errorf("executor.shutdown error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UniversalExecutor) configureTaskDir() error {
|
2016-02-04 18:09:52 +00:00
|
|
|
taskDir, ok := e.ctx.AllocDir.TaskDirs[e.ctx.TaskName]
|
2016-02-04 00:03:43 +00:00
|
|
|
e.taskDir = taskDir
|
|
|
|
if !ok {
|
2016-02-04 18:09:52 +00:00
|
|
|
return fmt.Errorf("Couldn't find task directory for task %v", e.ctx.TaskName)
|
2016-02-04 00:03:43 +00:00
|
|
|
}
|
|
|
|
e.cmd.Dir = taskDir
|
|
|
|
return nil
|
|
|
|
}
|