This commit is contained in:
Alex Dadgar
2016-01-11 09:58:26 -08:00
parent 857771d286
commit b8a42786f3
18 changed files with 144 additions and 375 deletions

View File

@@ -28,6 +28,7 @@ import (
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/client/driver/env"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
)
@@ -75,9 +76,23 @@ type Executor interface {
Command() *exec.Cmd
}
// Command is a mirror of exec.Command that returns a platform-specific Executor
func Command(name string, args ...string) Executor {
executor := NewExecutor()
// ExecutorContext is a means to inject dependencies such as loggers, configs, and
// node attributes into a Driver without having to change the Driver interface
// each time we do it. Used in conjection with Factory, above.
type ExecutorContext struct {
taskEnv *env.TaskEnvironment
}
// NewExecutorContext initializes a new DriverContext with the specified fields.
func NewExecutorContext(taskEnv *env.TaskEnvironment) *ExecutorContext {
return &ExecutorContext{
taskEnv: taskEnv,
}
}
// Command returns a platform-specific Executor
func Command(ctx *ExecutorContext, name string, args ...string) Executor {
executor := NewExecutor(ctx)
SetCommand(executor, name, args)
return executor
}
@@ -98,8 +113,8 @@ func SetCommand(e Executor, name string, args []string) {
// OpenId is similar to executor.Command but will attempt to reopen with the
// passed ID.
func OpenId(id string) (Executor, error) {
executor := NewExecutor()
func OpenId(ctx *ExecutorContext, id string) (Executor, error) {
executor := NewExecutor(ctx)
err := executor.Open(id)
if err != nil {
return nil, err

View File

@@ -11,9 +11,7 @@ import (
"strings"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/driver/environment"
"github.com/hashicorp/nomad/client/driver/spawn"
"github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/nomad/structs"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
@@ -22,6 +20,7 @@ import (
// BasicExecutor should work everywhere, and as a result does not include
// any resource restrictions or runas capabilities.
type BasicExecutor struct {
*ExecutorContext
cmd exec.Cmd
spawn *spawn.Spawner
taskName string
@@ -29,8 +28,8 @@ type BasicExecutor struct {
allocDir string
}
func NewBasicExecutor() Executor {
return &BasicExecutor{}
func NewBasicExecutor(ctx *ExecutorContext) Executor {
return &BasicExecutor{ExecutorContext: ctx}
}
func (e *BasicExecutor) Limit(resources *structs.Resources) error {
@@ -56,13 +55,8 @@ func (e *BasicExecutor) ConfigureTaskDir(taskName string, alloc *allocdir.AllocD
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
}
e.cmd.Path = args.ReplaceEnv(e.cmd.Path, envVars.Map())
e.cmd.Args = args.ParseAndReplace(e.cmd.Args, envVars.Map())
e.cmd.Path = e.taskEnv.ReplaceEnv(e.cmd.Path)
e.cmd.Args = e.taskEnv.ParseAndReplace(e.cmd.Args)
spawnState := filepath.Join(e.allocDir, fmt.Sprintf("%s_%s", e.taskName, "exit_status"))
e.spawn = spawn.NewSpawner(spawnState)

View File

@@ -23,7 +23,6 @@ import (
"github.com/hashicorp/nomad/client/driver/environment"
"github.com/hashicorp/nomad/client/driver/spawn"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/helper/args"
"github.com/hashicorp/nomad/nomad/structs"
)
@@ -42,16 +41,17 @@ var (
}
)
func NewExecutor() Executor {
return NewLinuxExecutor()
func NewExecutor(ctx *ExecutorContext) Executor {
return NewLinuxExecutor(ctx)
}
func NewLinuxExecutor() Executor {
return &LinuxExecutor{}
func NewLinuxExecutor(ctx *ExecutorContext) Executor {
return &LinuxExecutor{ExecutorContext: ctx}
}
// Linux executor is designed to run on linux kernel 2.8+.
type LinuxExecutor struct {
*ExecutorContext
cmd exec.Cmd
user *user.User
@@ -161,13 +161,8 @@ func (e *LinuxExecutor) 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
}
e.cmd.Path = args.ReplaceEnv(e.cmd.Path, envVars.Map())
e.cmd.Args = args.ParseAndReplace(e.cmd.Args, envVars.Map())
e.cmd.Path = e.taskEnv.ReplaceEnv(e.cmd.Path, envVars.Map())
e.cmd.Args = e.taskEnv.ParseAndReplace(e.cmd.Args, envVars.Map())
spawnState := filepath.Join(e.allocDir, fmt.Sprintf("%s_%s", e.taskName, "exit_status"))
e.spawn = spawn.NewSpawner(spawnState)

View File

@@ -2,11 +2,13 @@
package executor
func NewExecutor() Executor {
return &UniversalExecutor{BasicExecutor{}}
func NewExecutor(ctx *ExecutorContext) Executor {
return &UniversalExecutor{
BasicExecutor: NewBasicExecutor(ctx).(*BasicExecutor),
}
}
// UniversalExecutor wraps the BasicExecutor
type UniversalExecutor struct {
BasicExecutor
*BasicExecutor
}