2015-08-20 23:50:28 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-08-23 22:36:06 +00:00
|
|
|
"sync"
|
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{
|
|
|
|
"exec": NewExecDriver,
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDriver is used to instantiate and return a new driver
|
|
|
|
// given the name and a logger
|
|
|
|
func NewDriver(name string, logger *log.Logger) (Driver, error) {
|
|
|
|
// Lookup the factory function
|
|
|
|
factory, ok := BuiltinDrivers[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unknown driver '%s'", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Instantiate the driver
|
|
|
|
f := factory(logger)
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Factory is used to instantiate a new Driver
|
|
|
|
type Factory func(*log.Logger) Driver
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecContext is used to create a new execution context
|
|
|
|
func NewExecContext() *ExecContext {
|
|
|
|
ctx := &ExecContext{}
|
|
|
|
return ctx
|
|
|
|
}
|