2015-08-20 23:50:28 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-10-15 21:40:08 +00:00
|
|
|
"path/filepath"
|
2015-08-23 22:36:06 +00:00
|
|
|
"sync"
|
2015-08-20 23:50:28 +00:00
|
|
|
|
2015-09-21 21:13:17 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-09-08 19:43:02 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2015-09-27 00:56:14 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/environment"
|
2015-08-20 23:50:28 +00:00
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2015-08-23 23:49:48 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-08-20 23:50:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// BuiltinDrivers contains the built in registered drivers
|
|
|
|
// which are available for allocation handling
|
|
|
|
var BuiltinDrivers = map[string]Factory{
|
2015-10-08 18:34:42 +00:00
|
|
|
"docker": NewDockerDriver,
|
|
|
|
"exec": NewExecDriver,
|
|
|
|
"raw_exec": NewRawExecDriver,
|
|
|
|
"java": NewJavaDriver,
|
|
|
|
"qemu": NewQemuDriver,
|
|
|
|
"rkt": NewRktDriver,
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDriver is used to instantiate and return a new driver
|
|
|
|
// given the name and a logger
|
2015-09-10 01:06:23 +00:00
|
|
|
func NewDriver(name string, ctx *DriverContext) (Driver, error) {
|
2015-08-20 23:50:28 +00:00
|
|
|
// Lookup the factory function
|
|
|
|
factory, ok := BuiltinDrivers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown driver '%s'", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the driver
|
2015-09-10 01:06:23 +00:00
|
|
|
f := factory(ctx)
|
2015-08-20 23:50:28 +00:00
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is used to instantiate a new Driver
|
2015-09-10 01:06:23 +00:00
|
|
|
type Factory func(*DriverContext) Driver
|
2015-08-20 23:50:28 +00:00
|
|
|
|
|
|
|
// Driver is used for execution of tasks. This allows Nomad
|
|
|
|
// to support many pluggable implementations of task drivers.
|
|
|
|
// Examples could include LXC, Docker, Qemu, etc.
|
|
|
|
type Driver interface {
|
|
|
|
// Drivers must support the fingerprint interface for detection
|
|
|
|
fingerprint.Fingerprint
|
2015-08-23 23:49:48 +00:00
|
|
|
|
|
|
|
// Start is used to being task execution
|
|
|
|
Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error)
|
|
|
|
|
|
|
|
// Open is used to re-open a handle to a task
|
|
|
|
Open(ctx *ExecContext, handleID string) (DriverHandle, error)
|
|
|
|
}
|
|
|
|
|
2015-09-10 01:06:23 +00:00
|
|
|
// DriverContext 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 DriverContext struct {
|
2015-09-21 21:13:17 +00:00
|
|
|
taskName string
|
|
|
|
config *config.Config
|
|
|
|
logger *log.Logger
|
|
|
|
node *structs.Node
|
2015-09-10 01:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDriverContext initializes a new DriverContext with the specified fields.
|
|
|
|
// This enables other packages to create DriverContexts but keeps the fields
|
|
|
|
// private to the driver. If we want to change this later we can gorename all of
|
|
|
|
// the fields in DriverContext.
|
2015-09-21 21:13:17 +00:00
|
|
|
func NewDriverContext(taskName string, config *config.Config, node *structs.Node, logger *log.Logger) *DriverContext {
|
2015-09-10 01:06:23 +00:00
|
|
|
return &DriverContext{
|
2015-09-21 21:13:17 +00:00
|
|
|
taskName: taskName,
|
|
|
|
config: config,
|
|
|
|
node: node,
|
|
|
|
logger: logger,
|
2015-09-10 01:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
// DriverHandle is an opaque handle into a driver used for task
|
|
|
|
// manipulation
|
|
|
|
type DriverHandle interface {
|
|
|
|
// Returns an opaque handle that can be used to re-open the handle
|
|
|
|
ID() string
|
|
|
|
|
|
|
|
// WaitCh is used to return a channel used wait for task completion
|
|
|
|
WaitCh() chan error
|
|
|
|
|
2015-08-29 22:46:10 +00:00
|
|
|
// Update is used to update the task if possible
|
|
|
|
Update(task *structs.Task) error
|
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
// Kill is used to stop the task
|
|
|
|
Kill() error
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
2015-08-23 22:36:06 +00:00
|
|
|
|
|
|
|
// ExecContext is shared between drivers within an allocation
|
|
|
|
type ExecContext struct {
|
|
|
|
sync.Mutex
|
2015-08-31 00:35:58 +00:00
|
|
|
|
2015-09-21 21:13:17 +00:00
|
|
|
// AllocDir contains information about the alloc directory structure.
|
2015-09-23 04:56:29 +00:00
|
|
|
AllocDir *allocdir.AllocDir
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecContext is used to create a new execution context
|
2015-09-25 23:49:14 +00:00
|
|
|
func NewExecContext(alloc *allocdir.AllocDir) *ExecContext {
|
|
|
|
return &ExecContext{AllocDir: alloc}
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2015-09-26 22:37:48 +00:00
|
|
|
// TaskEnvironmentVariables converts exec context and task configuration into a
|
2015-09-27 00:56:14 +00:00
|
|
|
// TaskEnvironment.
|
|
|
|
func TaskEnvironmentVariables(ctx *ExecContext, task *structs.Task) environment.TaskEnvironment {
|
|
|
|
env := environment.NewTaskEnivornment()
|
|
|
|
env.SetMeta(task.Meta)
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2015-09-27 00:56:14 +00:00
|
|
|
if ctx.AllocDir != nil {
|
2015-10-08 02:00:05 +00:00
|
|
|
env.SetAllocDir(ctx.AllocDir.SharedDir)
|
2015-10-15 21:40:08 +00:00
|
|
|
taskdir, ok := ctx.AllocDir.TaskDirs[task.Name]
|
|
|
|
if !ok {
|
|
|
|
// TODO: Update this to return an error
|
|
|
|
}
|
|
|
|
|
|
|
|
env.SetTaskLocalDir(filepath.Join(taskdir, allocdir.TaskLocal))
|
2015-09-27 00:56:14 +00:00
|
|
|
}
|
2015-09-23 06:11:55 +00:00
|
|
|
|
|
|
|
if task.Resources != nil {
|
2015-09-27 00:56:14 +00:00
|
|
|
env.SetMemLimit(task.Resources.MemoryMB)
|
|
|
|
env.SetCpuLimit(task.Resources.CPU)
|
2015-09-23 06:11:55 +00:00
|
|
|
|
|
|
|
if len(task.Resources.Networks) > 0 {
|
|
|
|
network := task.Resources.Networks[0]
|
2015-09-27 00:56:14 +00:00
|
|
|
env.SetTaskIp(network.IP)
|
|
|
|
env.SetPorts(network.MapDynamicPorts())
|
2015-09-23 06:11:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2015-10-01 11:22:26 +00:00
|
|
|
if task.Env != nil {
|
|
|
|
env.SetEnvvars(task.Env)
|
|
|
|
}
|
|
|
|
|
2015-09-23 05:33:29 +00:00
|
|
|
return env
|
|
|
|
}
|