2015-08-20 23:50:28 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2017-04-12 20:26:55 +00:00
|
|
|
"context"
|
2017-04-29 22:43:23 +00:00
|
|
|
"crypto/md5"
|
2015-08-20 23:50:28 +00:00
|
|
|
"fmt"
|
2017-04-29 22:43:23 +00:00
|
|
|
"io"
|
2015-08-20 23:50:28 +00:00
|
|
|
"log"
|
2016-10-07 19:37:52 +00:00
|
|
|
"os"
|
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-08-20 23:50:28 +00:00
|
|
|
"github.com/hashicorp/nomad/client/fingerprint"
|
2018-11-28 15:41:59 +00:00
|
|
|
"github.com/hashicorp/nomad/drivers/shared/env"
|
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
|
|
|
)
|
|
|
|
|
2017-01-09 21:47:06 +00:00
|
|
|
var (
|
|
|
|
// BuiltinDrivers contains the built in registered drivers
|
|
|
|
// which are available for allocation handling
|
|
|
|
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
|
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
|
2017-05-17 00:33:50 +00:00
|
|
|
d := factory(ctx)
|
|
|
|
return d, nil
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-05-19 18:08:49 +00:00
|
|
|
// PrestartResponse is driver state returned by Driver.Prestart.
|
|
|
|
type PrestartResponse struct {
|
|
|
|
// CreatedResources by the driver.
|
|
|
|
CreatedResources *CreatedResources
|
2017-06-13 21:02:11 +00:00
|
|
|
|
|
|
|
// Network contains driver-specific network parameters such as the port
|
|
|
|
// map between the host and a container.
|
|
|
|
//
|
|
|
|
// Since the network configuration may not be fully populated by
|
|
|
|
// Prestart, it will only be used for creating an environment for
|
|
|
|
// Start. It will be overridden by the DriverNetwork returned by Start.
|
|
|
|
Network *cstructs.DriverNetwork
|
2017-05-19 18:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPrestartResponse creates a new PrestartResponse with CreatedResources
|
|
|
|
// initialized.
|
|
|
|
func NewPrestartResponse() *PrestartResponse {
|
|
|
|
return &PrestartResponse{
|
|
|
|
CreatedResources: NewCreatedResources(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
// CreatedResources is a map of resources (eg downloaded images) created by a driver
|
|
|
|
// that must be cleaned up.
|
|
|
|
type CreatedResources struct {
|
|
|
|
Resources map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCreatedResources() *CreatedResources {
|
|
|
|
return &CreatedResources{Resources: make(map[string][]string)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new resource if it doesn't already exist.
|
|
|
|
func (r *CreatedResources) Add(k, v string) {
|
|
|
|
if r.Resources == nil {
|
2017-09-26 22:26:33 +00:00
|
|
|
r.Resources = map[string][]string{k: {v}}
|
2017-01-10 21:24:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
existing, ok := r.Resources[k]
|
|
|
|
if !ok {
|
|
|
|
// Key doesn't exist, create it
|
|
|
|
r.Resources[k] = []string{v}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, item := range existing {
|
|
|
|
if item == v {
|
|
|
|
// resource exists, return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resource type exists but value did not, append it
|
|
|
|
r.Resources[k] = append(existing, v)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:41:59 +00:00
|
|
|
// Remove a resource. Return true if removed, otherwise false.
|
|
|
|
//
|
|
|
|
// Removes the entire key if the needle is the last value in the list.
|
|
|
|
func (r *CreatedResources) Remove(k, needle string) bool {
|
|
|
|
haystack := r.Resources[k]
|
|
|
|
for i, item := range haystack {
|
|
|
|
if item == needle {
|
|
|
|
r.Resources[k] = append(haystack[:i], haystack[i+1:]...)
|
|
|
|
if len(r.Resources[k]) == 0 {
|
|
|
|
delete(r.Resources, k)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-11 18:51:03 +00:00
|
|
|
// Copy returns a new deep copy of CreatedResources.
|
2017-01-14 00:46:08 +00:00
|
|
|
func (r *CreatedResources) Copy() *CreatedResources {
|
2017-01-31 17:19:59 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-14 00:46:08 +00:00
|
|
|
newr := CreatedResources{
|
|
|
|
Resources: make(map[string][]string, len(r.Resources)),
|
|
|
|
}
|
|
|
|
for k, v := range r.Resources {
|
|
|
|
newv := make([]string, len(v))
|
|
|
|
copy(newv, v)
|
|
|
|
newr.Resources[k] = newv
|
|
|
|
}
|
|
|
|
return &newr
|
|
|
|
}
|
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
// Merge another CreatedResources into this one. If the other CreatedResources
|
|
|
|
// is nil this method is a noop.
|
|
|
|
func (r *CreatedResources) Merge(o *CreatedResources) {
|
|
|
|
if o == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range o.Resources {
|
|
|
|
// New key
|
|
|
|
if len(r.Resources[k]) == 0 {
|
|
|
|
r.Resources[k] = v
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Existing key
|
|
|
|
OUTER:
|
|
|
|
for _, item := range v {
|
|
|
|
for _, existing := range r.Resources[k] {
|
|
|
|
if item == existing {
|
|
|
|
// Found it, move on
|
|
|
|
continue OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New item, append it
|
|
|
|
r.Resources[k] = append(r.Resources[k], item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 22:43:23 +00:00
|
|
|
func (r *CreatedResources) Hash() []byte {
|
|
|
|
h := md5.New()
|
|
|
|
|
|
|
|
for k, values := range r.Resources {
|
|
|
|
io.WriteString(h, k)
|
|
|
|
io.WriteString(h, "values")
|
|
|
|
for i, v := range values {
|
|
|
|
io.WriteString(h, fmt.Sprintf("%d-%v", i, v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// StartResponse is returned by Driver.Start.
|
|
|
|
type StartResponse struct {
|
|
|
|
// Handle to the driver's task executor for controlling the lifecycle
|
|
|
|
// of the task.
|
|
|
|
Handle DriverHandle
|
|
|
|
|
2017-06-13 21:02:11 +00:00
|
|
|
// Network contains driver-specific network parameters such as the port
|
|
|
|
// map between the host and a container.
|
2017-06-09 17:29:41 +00:00
|
|
|
//
|
|
|
|
// Network may be nil as not all drivers or configurations create
|
|
|
|
// networks.
|
|
|
|
Network *cstructs.DriverNetwork
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-11-30 00:39:36 +00:00
|
|
|
// Prestart prepares the task environment and performs expensive
|
2018-03-11 18:18:37 +00:00
|
|
|
// initialization steps like downloading images.
|
2017-01-10 21:24:45 +00:00
|
|
|
//
|
|
|
|
// CreatedResources may be non-nil even when an error occurs.
|
2017-05-19 18:08:49 +00:00
|
|
|
Prestart(*ExecContext, *structs.Task) (*PrestartResponse, error)
|
2016-11-30 00:39:36 +00:00
|
|
|
|
2017-06-09 17:29:41 +00:00
|
|
|
// Start is used to begin task execution. If error is nil,
|
|
|
|
// StartResponse.Handle will be the handle to the task's executor.
|
|
|
|
// StartResponse.Network may be nil if the task doesn't configure a
|
|
|
|
// network.
|
|
|
|
Start(ctx *ExecContext, task *structs.Task) (*StartResponse, error)
|
2015-08-23 23:49:48 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-01-10 21:24:45 +00:00
|
|
|
// Cleanup is called to remove resources which were created for a task
|
2017-01-31 18:51:32 +00:00
|
|
|
// and no longer needed. Cleanup is not called if CreatedResources is
|
|
|
|
// nil.
|
2017-01-10 21:24:45 +00:00
|
|
|
//
|
2017-01-14 00:46:08 +00:00
|
|
|
// If Cleanup returns a recoverable error it may be retried. On retry
|
|
|
|
// it will be passed the same CreatedResources, so all successfully
|
2017-06-09 17:29:41 +00:00
|
|
|
// cleaned up resources should be removed or handled idempotently.
|
2017-01-14 00:46:08 +00:00
|
|
|
Cleanup(*ExecContext, *CreatedResources) error
|
2017-01-10 21:24:45 +00:00
|
|
|
|
2016-04-08 20:19:43 +00:00
|
|
|
// Drivers must validate their configuration
|
|
|
|
Validate(map[string]interface{}) error
|
2016-10-19 22:06:23 +00:00
|
|
|
|
|
|
|
// Abilities returns the abilities of the driver
|
|
|
|
Abilities() DriverAbilities
|
2016-12-03 01:04:07 +00:00
|
|
|
|
|
|
|
// FSIsolation returns the method of filesystem isolation used
|
|
|
|
FSIsolation() cstructs.FSIsolation
|
2016-10-19 22:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DriverAbilities marks the abilities the driver has.
|
|
|
|
type DriverAbilities struct {
|
|
|
|
// SendSignals marks the driver as being able to send signals
|
|
|
|
SendSignals bool
|
2017-04-13 16:52:16 +00:00
|
|
|
|
|
|
|
// Exec marks the driver as being able to execute arbitrary commands
|
|
|
|
// such as health checks. Used by the ScriptExecutor interface.
|
|
|
|
Exec bool
|
2015-08-23 23:49:48 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:39:36 +00:00
|
|
|
// LogEventFn is a callback which allows Drivers to emit task events.
|
|
|
|
type LogEventFn func(message string, args ...interface{})
|
|
|
|
|
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
|
2018-03-11 17:48:37 +00:00
|
|
|
// each time we do it. Used in conjunction with Factory, above.
|
2015-09-10 01:06:23 +00:00
|
|
|
type DriverContext struct {
|
2018-04-22 22:15:29 +00:00
|
|
|
jobName string
|
|
|
|
taskGroupName string
|
|
|
|
taskName string
|
|
|
|
allocID string
|
|
|
|
config *config.Config
|
|
|
|
logger *log.Logger
|
|
|
|
node *structs.Node
|
2016-11-30 00:39:36 +00:00
|
|
|
|
|
|
|
emitEvent LogEventFn
|
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 {
|
2016-11-30 00:39:36 +00:00
|
|
|
return &DriverContext{}
|
2016-04-09 22:38:42 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2018-04-22 22:15:29 +00:00
|
|
|
func NewDriverContext(jobName, taskGroupName, taskName, allocID string,
|
|
|
|
config *config.Config, node *structs.Node,
|
2017-05-23 00:46:40 +00:00
|
|
|
logger *log.Logger, eventEmitter LogEventFn) *DriverContext {
|
2015-09-10 01:06:23 +00:00
|
|
|
return &DriverContext{
|
2018-04-22 22:15:29 +00:00
|
|
|
jobName: jobName,
|
|
|
|
taskGroupName: taskGroupName,
|
|
|
|
taskName: taskName,
|
|
|
|
allocID: allocID,
|
|
|
|
config: config,
|
|
|
|
node: node,
|
|
|
|
logger: logger,
|
|
|
|
emitEvent: eventEmitter,
|
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)
|
2016-10-07 19:37:52 +00:00
|
|
|
|
|
|
|
// Signal is used to send a signal to the task
|
|
|
|
Signal(s os.Signal) error
|
2017-04-13 16:52:16 +00:00
|
|
|
|
|
|
|
// ScriptExecutor is an interface used to execute commands such as
|
|
|
|
// health check scripts in the a DriverHandle's context.
|
|
|
|
ScriptExecutor
|
2018-07-30 02:27:06 +00:00
|
|
|
|
|
|
|
// Network returns the driver's network or nil if the driver did not
|
|
|
|
// create a network.
|
|
|
|
Network() *cstructs.DriverNetwork
|
2015-08-20 23:50:28 +00:00
|
|
|
}
|
2015-08-23 22:36:06 +00:00
|
|
|
|
2017-04-13 16:52:16 +00:00
|
|
|
// ScriptExecutor is an interface that supports Exec()ing commands in the
|
|
|
|
// driver's context. Split out of DriverHandle to ease testing.
|
2017-04-12 20:26:55 +00:00
|
|
|
type ScriptExecutor interface {
|
|
|
|
Exec(ctx context.Context, cmd string, args []string) ([]byte, int, error)
|
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
// ExecContext is a task's execution context
|
2015-08-23 22:36:06 +00:00
|
|
|
type ExecContext struct {
|
2016-12-03 01:04:07 +00:00
|
|
|
// TaskDir contains information about the task directory structure.
|
|
|
|
TaskDir *allocdir.TaskDir
|
2017-05-23 00:46:40 +00:00
|
|
|
|
|
|
|
// TaskEnv contains the task's environment variables.
|
|
|
|
TaskEnv *env.TaskEnv
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
// StdoutFifo is the path to the named pipe to write stdout to
|
|
|
|
StdoutFifo string
|
|
|
|
|
|
|
|
// StderrFifo is the path to the named pipe to write stderr to
|
|
|
|
StderrFifo string
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecContext is used to create a new execution context
|
2017-05-23 00:46:40 +00:00
|
|
|
func NewExecContext(td *allocdir.TaskDir, te *env.TaskEnv) *ExecContext {
|
2017-01-09 19:21:51 +00:00
|
|
|
return &ExecContext{
|
2017-01-12 19:50:49 +00:00
|
|
|
TaskDir: td,
|
2017-05-23 00:46:40 +00:00
|
|
|
TaskEnv: te,
|
2017-01-09 19:21:51 +00:00
|
|
|
}
|
2015-08-23 22:36:06 +00:00
|
|
|
}
|
2015-09-23 05:33:29 +00:00
|
|
|
|
2015-11-20 05:29:37 +00:00
|
|
|
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
|
|
|
|
}
|