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"
|
2016-01-11 17:58:26 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
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-11-14 06:07:13 +00:00
|
|
|
|
2016-06-12 03:15:50 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/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)
|
2016-04-08 20:19:43 +00:00
|
|
|
|
|
|
|
// Drivers must validate their configuration
|
|
|
|
Validate(map[string]interface{}) error
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
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
|
2016-01-11 17:58:26 +00:00
|
|
|
taskEnv *env.TaskEnvironment
|
2015-09-10 01:06:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 22:38:42 +00:00
|
|
|
// NewEmptyDriverContext returns a DriverContext with all fields set to their
|
|
|
|
// zero value.
|
|
|
|
func NewEmptyDriverContext() *DriverContext {
|
|
|
|
return &DriverContext{
|
|
|
|
taskName: "",
|
|
|
|
config: nil,
|
|
|
|
node: nil,
|
|
|
|
logger: nil,
|
|
|
|
taskEnv: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2016-01-11 17:58:26 +00:00
|
|
|
func NewDriverContext(taskName string, config *config.Config, node *structs.Node,
|
|
|
|
logger *log.Logger, taskEnv *env.TaskEnvironment) *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,
|
2016-01-11 17:58:26 +00:00
|
|
|
taskEnv: taskEnv,
|
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
|
2016-06-12 03:15:50 +00:00
|
|
|
WaitCh() chan *dstructs.WaitResult
|
2015-08-23 23:49:48 +00:00
|
|
|
|
2016-02-04 03:43:44 +00:00
|
|
|
// Update is used to update the task if possible and update task related
|
|
|
|
// configurations.
|
2015-08-29 22:46:10 +00:00
|
|
|
Update(task *structs.Task) error
|
|
|
|
|
2015-08-23 23:49:48 +00:00
|
|
|
// Kill is used to stop the task
|
|
|
|
Kill() error
|
2016-04-28 23:06:01 +00:00
|
|
|
|
2016-05-19 14:41:11 +00:00
|
|
|
// Stats returns aggregated stats of the driver
|
2016-04-28 23:06:01 +00:00
|
|
|
Stats() (*cstructs.TaskResourceUsage, 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-11-06 02:31:39 +00:00
|
|
|
|
|
|
|
// Alloc ID
|
|
|
|
AllocID string
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecContext is used to create a new execution context
|
2015-11-06 02:31:39 +00:00
|
|
|
func NewExecContext(alloc *allocdir.AllocDir, allocID string) *ExecContext {
|
|
|
|
return &ExecContext{AllocDir: alloc, AllocID: allocID}
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
// GetTaskEnv converts the alloc dir, the node, task and alloc into a
|
2015-09-27 00:56:14 +00:00
|
|
|
// TaskEnvironment.
|
2016-03-02 00:08:21 +00:00
|
|
|
func GetTaskEnv(allocDir *allocdir.AllocDir, node *structs.Node,
|
|
|
|
task *structs.Task, alloc *structs.Allocation) (*env.TaskEnvironment, error) {
|
|
|
|
|
2016-03-25 00:39:09 +00:00
|
|
|
tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup)
|
2016-01-11 17:58:26 +00:00
|
|
|
env := env.NewTaskEnvironment(node).
|
2016-03-25 00:39:09 +00:00
|
|
|
SetTaskMeta(task.Meta).
|
|
|
|
SetTaskGroupMeta(tg.Meta).
|
|
|
|
SetJobMeta(alloc.Job.Meta).
|
2016-03-02 00:08:21 +00:00
|
|
|
SetEnvvars(task.Env).
|
|
|
|
SetTaskName(task.Name)
|
2016-01-11 17:58:26 +00:00
|
|
|
|
2016-03-02 00:08:21 +00:00
|
|
|
if allocDir != nil {
|
|
|
|
env.SetAllocDir(allocDir.SharedDir)
|
|
|
|
taskdir, ok := allocDir.TaskDirs[task.Name]
|
2015-10-15 21:40:08 +00:00
|
|
|
if !ok {
|
2016-01-11 17:58:26 +00:00
|
|
|
return nil, fmt.Errorf("failed to get task directory for task %q", task.Name)
|
2015-10-15 21:40:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2016-03-02 00:08:21 +00:00
|
|
|
env.SetMemLimit(task.Resources.MemoryMB).
|
|
|
|
SetCpuLimit(task.Resources.CPU).
|
|
|
|
SetNetworks(task.Resources.Networks)
|
|
|
|
}
|
|
|
|
|
|
|
|
if alloc != nil {
|
2016-03-10 02:09:51 +00:00
|
|
|
env.SetAlloc(alloc)
|
2015-09-23 06:11:55 +00:00
|
|
|
}
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2016-01-11 17:58:26 +00:00
|
|
|
return env.Build(), nil
|
2015-09-23 05:33:29 +00:00
|
|
|
}
|
2015-11-20 05:29:37 +00:00
|
|
|
|
|
|
|
func mapMergeStrInt(maps ...map[string]int) map[string]int {
|
|
|
|
out := map[string]int{}
|
|
|
|
for _, in := range maps {
|
|
|
|
for key, val := range in {
|
|
|
|
out[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapMergeStrStr(maps ...map[string]string) map[string]string {
|
|
|
|
out := map[string]string{}
|
|
|
|
for _, in := range maps {
|
|
|
|
for key, val := range in {
|
|
|
|
out[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|