Skip to content

Commit

Permalink
Merge pull request #4788 from hashicorp/r-clientv2-exec
Browse files Browse the repository at this point in the history
drivers/exec: add exec implementation
  • Loading branch information
nickethier authored Oct 17, 2018
2 parents 6fd19f0 + 832a214 commit e7f6419
Show file tree
Hide file tree
Showing 11 changed files with 1,099 additions and 35 deletions.
60 changes: 30 additions & 30 deletions client/driver/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ func (e *UniversalExecutor) Launch(command *ExecCommand) (*ProcessState, error)
e.childCmd.Stderr = stderr

// Look up the binary path and make it executable
absPath, err := e.lookupBin(command.Cmd)
absPath, err := lookupBin(command.TaskDir, command.Cmd)
if err != nil {
return nil, err
}

if err := e.makeExecutable(absPath); err != nil {
if err := makeExecutable(absPath); err != nil {
return nil, err
}

Expand Down Expand Up @@ -476,18 +476,42 @@ func (e *UniversalExecutor) Shutdown(signal string, grace time.Duration) error {
return nil
}

// Signal sends the passed signal to the task
func (e *UniversalExecutor) Signal(s os.Signal) error {
if e.childCmd.Process == nil {
return fmt.Errorf("Task not yet run")
}

e.logger.Debug("sending signal to PID", "signal", s, "pid", e.childCmd.Process.Pid)
err := e.childCmd.Process.Signal(s)
if err != nil {
e.logger.Error("sending signal failed", "signal", s, "error", err)
return err
}

return nil
}

func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
pidStats, err := e.pidCollector.pidStats()
if err != nil {
return nil, err
}
return aggregatedResourceUsage(e.systemCpuStats, pidStats), nil
}

// lookupBin looks for path to the binary to run by looking for the binary in
// the following locations, in-order: task/local/, task/, based on host $PATH.
// The return path is absolute.
func (e *UniversalExecutor) lookupBin(bin string) (string, error) {
func lookupBin(taskDir string, bin string) (string, error) {
// Check in the local directory
local := filepath.Join(e.commandCfg.TaskDir, allocdir.TaskLocal, bin)
local := filepath.Join(taskDir, allocdir.TaskLocal, bin)
if _, err := os.Stat(local); err == nil {
return local, nil
}

// Check at the root of the task's directory
root := filepath.Join(e.commandCfg.TaskDir, bin)
root := filepath.Join(taskDir, bin)
if _, err := os.Stat(root); err == nil {
return root, nil
}
Expand All @@ -501,7 +525,7 @@ func (e *UniversalExecutor) lookupBin(bin string) (string, error) {
}

// makeExecutable makes the given file executable for root,group,others.
func (e *UniversalExecutor) makeExecutable(binPath string) error {
func makeExecutable(binPath string) error {
if runtime.GOOS == "windows" {
return nil
}
Expand All @@ -524,27 +548,3 @@ func (e *UniversalExecutor) makeExecutable(binPath string) error {
}
return nil
}

// Signal sends the passed signal to the task
func (e *UniversalExecutor) Signal(s os.Signal) error {
if e.childCmd.Process == nil {
return fmt.Errorf("Task not yet run")
}

e.logger.Debug("sending signal to PID", "signal", s, "pid", e.childCmd.Process.Pid)
err := e.childCmd.Process.Signal(s)
if err != nil {
e.logger.Error("sending signal failed", "signal", s, "error", err)
return err
}

return nil
}

func (e *UniversalExecutor) Stats() (*cstructs.TaskResourceUsage, error) {
pidStats, err := e.pidCollector.pidStats()
if err != nil {
return nil, err
}
return aggregatedResourceUsage(e.systemCpuStats, pidStats), nil
}
25 changes: 24 additions & 1 deletion client/driver/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (l *LibcontainerExecutor) Launch(command *ExecCommand) (*ProcessState, erro
return nil, fmt.Errorf("unable to find the nomad binary: %v", err)
}

if command.Resources == nil {
command.Resources = &Resources{}
}

l.command = command

// Move to the root cgroup until process is started
Expand Down Expand Up @@ -129,7 +133,26 @@ func (l *LibcontainerExecutor) Launch(command *ExecCommand) (*ProcessState, erro
}
l.container = container

combined := append([]string{command.Cmd}, command.Args...)
// Look up the binary path and make it executable
absPath, err := lookupBin(command.TaskDir, command.Cmd)
if err != nil {
return nil, err
}

if err := makeExecutable(absPath); err != nil {
return nil, err
}

path := absPath

// Determine the path to run as it may have to be relative to the chroot.
rel, err := filepath.Rel(command.TaskDir, path)
if err != nil {
return nil, fmt.Errorf("failed to determine relative path base=%q target=%q: %v", command.TaskDir, path, err)
}
path = rel

combined := append([]string{path}, command.Args...)
stdout, err := command.Stdout()
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit e7f6419

Please sign in to comment.