open-nomad/client/driver/driver.go

183 lines
5 KiB
Go
Raw Normal View History

2015-08-20 23:50:28 +00:00
package driver
import (
"fmt"
"log"
"path/filepath"
2015-08-23 22:36:06 +00:00
"sync"
"time"
2015-08-20 23:50:28 +00:00
"github.com/hashicorp/nomad/client/allocdir"
"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"
cstructs "github.com/hashicorp/nomad/client/driver/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{
"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
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
f := factory(ctx)
2015-08-20 23:50:28 +00:00
return f, nil
}
// Factory is used to instantiate a new Driver
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)
}
// 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 {
taskName string
config *config.Config
logger *log.Logger
node *structs.Node
2016-01-11 17:58:26 +00:00
taskEnv *env.TaskEnvironment
}
// 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 {
return &DriverContext{
taskName: taskName,
config: config,
node: node,
logger: logger,
2016-01-11 17:58:26 +00:00
taskEnv: taskEnv,
}
}
// KillTimeout returns the timeout that should be used for the task between
// signaling and killing the task.
func (d *DriverContext) KillTimeout(task *structs.Task) time.Duration {
max := d.config.MaxKillTimeout.Nanoseconds()
desired := task.KillTimeout.Nanoseconds()
2016-02-09 03:31:57 +00:00
// Make the minimum time between signal and kill, 1 second.
if desired == 0 {
desired = (1 * time.Second).Nanoseconds()
}
if desired < max {
2016-02-09 03:46:46 +00:00
return time.Duration(desired)
}
return d.config.MaxKillTimeout
}
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 *cstructs.WaitResult
2015-08-23 23:49:48 +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
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
// AllocDir contains information about the alloc directory structure.
AllocDir *allocdir.AllocDir
// Alloc ID
AllocID string
2015-08-23 22:36:06 +00:00
}
// NewExecContext is used to create a new execution context
func NewExecContext(alloc *allocdir.AllocDir, allocID string) *ExecContext {
return &ExecContext{AllocDir: alloc, AllocID: allocID}
2015-08-23 22:36:06 +00:00
}
2016-01-11 17:58:26 +00:00
// GetTaskEnv converts the alloc dir, the node and task configuration into a
// TaskEnvironment.
2016-01-11 17:58:26 +00:00
func GetTaskEnv(alloc *allocdir.AllocDir, node *structs.Node, task *structs.Task) (*env.TaskEnvironment, error) {
env := env.NewTaskEnvironment(node).
SetMeta(task.Meta).
SetEnvvars(task.Env)
if alloc != nil {
env.SetAllocDir(alloc.SharedDir)
taskdir, ok := alloc.TaskDirs[task.Name]
if !ok {
2016-01-11 17:58:26 +00:00
return nil, fmt.Errorf("failed to get task directory for task %q", task.Name)
}
env.SetTaskLocalDir(filepath.Join(taskdir, allocdir.TaskLocal))
}
2015-09-23 06:11:55 +00:00
if task.Resources != nil {
env.SetMemLimit(task.Resources.MemoryMB)
env.SetCpuLimit(task.Resources.CPU)
env.SetNetworks(task.Resources.Networks)
2015-09-23 06:11:55 +00:00
}
2016-01-11 17:58:26 +00:00
return env.Build(), nil
}
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
}