2018-06-22 00:35:07 +00:00
|
|
|
package interfaces
|
|
|
|
|
|
|
|
import (
|
2018-07-11 22:42:35 +00:00
|
|
|
"context"
|
2018-12-11 20:27:50 +00:00
|
|
|
"time"
|
2018-07-11 22:42:35 +00:00
|
|
|
|
2018-10-10 19:31:59 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-10-06 01:42:15 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
|
2018-07-20 00:40:25 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-11-30 11:18:39 +00:00
|
|
|
"github.com/hashicorp/nomad/client/taskenv"
|
2018-06-22 00:35:07 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-11-16 23:29:59 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-06-22 00:35:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
Restart
|
|
|
|
+--------------------------------------------------------+
|
|
|
|
| |
|
|
|
|
| *Update |
|
|
|
|
| +-------+ |
|
|
|
|
| | | |
|
|
|
|
| | | |
|
|
|
|
| +---v-------+----+ |
|
|
|
|
+----v----+ | Running | +----+-----+ +--------------+
|
|
|
|
| | *Prestart |----------------| *Exited | | *Stop | |
|
|
|
|
| Pending +-------------> *Poststart run +---^-----------> Exited +-----------> Terminal |
|
|
|
|
| | | upon entering | | | | NoRestart | |
|
|
|
|
+---------+ | running | | +----------+ +--------------+
|
|
|
|
| | |
|
|
|
|
+--------+-------+ |
|
|
|
|
| |
|
|
|
|
+-----------+
|
|
|
|
*Kill
|
|
|
|
(forces terminal)
|
|
|
|
|
|
|
|
Link: http://stable.ascii-flow.appspot.com/#Draw4489375405966393064/1824429135
|
2018-06-22 00:35:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// TaskHook is a lifecycle hook into the life cycle of a task runner.
|
|
|
|
type TaskHook interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPrestartRequest struct {
|
2019-02-21 23:37:22 +00:00
|
|
|
// PreviousState is previously set data by the hook. It must be copied
|
|
|
|
// to State below to be maintained across restarts.
|
|
|
|
PreviousState map[string]string
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
// Task is the task to run
|
|
|
|
Task *structs.Task
|
|
|
|
|
2018-11-16 23:29:59 +00:00
|
|
|
// TaskResources is the resources assigned to the task
|
|
|
|
TaskResources *structs.AllocatedTaskResources
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// Vault token may optionally be set if a Vault token is available
|
|
|
|
VaultToken string
|
|
|
|
|
2018-10-10 19:31:59 +00:00
|
|
|
// TaskDir contains the task's directory tree on the host
|
|
|
|
TaskDir *allocdir.TaskDir
|
2018-06-27 23:57:31 +00:00
|
|
|
|
|
|
|
// TaskEnv is the task's environment
|
2018-11-30 11:18:39 +00:00
|
|
|
TaskEnv *taskenv.TaskEnv
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPrestartResponse struct {
|
2018-06-22 00:35:07 +00:00
|
|
|
// Env is the environment variables to set for the task
|
|
|
|
Env map[string]string
|
|
|
|
|
2018-11-16 23:29:59 +00:00
|
|
|
// Mounts is the set of host volumes to mount into the task
|
|
|
|
Mounts []*drivers.MountConfig
|
|
|
|
|
|
|
|
// Devices are the set of devices to mount into the task
|
|
|
|
Devices []*drivers.DeviceConfig
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// State allows the hook to emit data to be passed in the next time it is
|
|
|
|
// run. Hooks must copy relevant PreviousState to State to maintain it
|
|
|
|
// across restarts.
|
|
|
|
State map[string]string
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Done lets the hook indicate that it completed successfully and
|
|
|
|
// should not be run again.
|
2018-07-11 04:22:04 +00:00
|
|
|
Done bool
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPrestartHook interface {
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskHook
|
2018-07-17 00:40:04 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Prestart is called before the task is started including after every
|
2019-03-05 23:12:02 +00:00
|
|
|
// restart. Prestart is not called if the allocation is terminal.
|
2019-03-12 22:03:54 +00:00
|
|
|
//
|
|
|
|
// The context is cancelled if the task is killed.
|
2018-07-17 00:19:56 +00:00
|
|
|
Prestart(context.Context, *TaskPrestartRequest, *TaskPrestartResponse) error
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 00:08:26 +00:00
|
|
|
// DriverStats is the interface implemented by DriverHandles to return task stats.
|
|
|
|
type DriverStats interface {
|
2018-12-11 20:27:50 +00:00
|
|
|
Stats(context.Context, time.Duration) (<-chan *cstructs.TaskResourceUsage, error)
|
2018-09-15 00:08:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPoststartRequest struct {
|
2018-07-20 00:40:25 +00:00
|
|
|
// Exec hook (may be nil)
|
2018-10-04 19:08:20 +00:00
|
|
|
DriverExec interfaces.ScriptExecutor
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Network info (may be nil)
|
2019-01-04 23:01:35 +00:00
|
|
|
DriverNetwork *drivers.DriverNetwork
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// TaskEnv is the task's environment
|
2018-11-30 11:18:39 +00:00
|
|
|
TaskEnv *taskenv.TaskEnv
|
2018-09-15 00:08:26 +00:00
|
|
|
|
|
|
|
// Stats collector
|
|
|
|
DriverStats DriverStats
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPoststartResponse struct{}
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskPoststartHook interface {
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskHook
|
2018-07-17 00:40:04 +00:00
|
|
|
|
2019-03-05 23:12:02 +00:00
|
|
|
// Poststart is called after the task has started. Poststart is not
|
|
|
|
// called if the allocation is terminal.
|
2019-03-12 22:03:54 +00:00
|
|
|
//
|
|
|
|
// The context is cancelled if the task is killed.
|
2018-07-17 00:19:56 +00:00
|
|
|
Poststart(context.Context, *TaskPoststartRequest, *TaskPoststartResponse) error
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-01-22 15:54:02 +00:00
|
|
|
type TaskPreKillRequest struct{}
|
|
|
|
type TaskPreKillResponse struct{}
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2019-01-17 21:27:15 +00:00
|
|
|
type TaskPreKillHook interface {
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskHook
|
2018-07-17 00:40:04 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// PreKilling is called right before a task is going to be killed or
|
2019-03-05 23:12:02 +00:00
|
|
|
// restarted. They are called concurrently with TaskRunner.Run and may
|
|
|
|
// be called without Prestart being called.
|
2019-01-22 15:54:02 +00:00
|
|
|
PreKilling(context.Context, *TaskPreKillRequest, *TaskPreKillResponse) error
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskExitedRequest struct{}
|
|
|
|
type TaskExitedResponse struct{}
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskExitedHook interface {
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskHook
|
2018-07-17 00:40:04 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Exited is called after a task exits and may or may not be restarted.
|
2019-03-05 23:12:02 +00:00
|
|
|
// Prestart may or may not have been called.
|
2019-03-12 22:03:54 +00:00
|
|
|
//
|
|
|
|
// The context is cancelled if the task is killed.
|
2018-07-17 00:19:56 +00:00
|
|
|
Exited(context.Context, *TaskExitedRequest, *TaskExitedResponse) error
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TaskUpdateRequest struct {
|
2018-07-12 23:15:33 +00:00
|
|
|
VaultToken string
|
2018-07-20 00:40:25 +00:00
|
|
|
|
|
|
|
// Alloc is the current version of the allocation (may have been
|
|
|
|
// updated since the hook was created)
|
|
|
|
Alloc *structs.Allocation
|
|
|
|
|
|
|
|
// TaskEnv is the task's environment
|
2018-11-30 11:18:39 +00:00
|
|
|
TaskEnv *taskenv.TaskEnv
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
type TaskUpdateResponse struct{}
|
|
|
|
|
|
|
|
type TaskUpdateHook interface {
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskHook
|
2019-03-12 22:03:54 +00:00
|
|
|
|
|
|
|
// Update is called when the servers have updated the Allocation for
|
|
|
|
// this task. Updates are concurrent with all other task hooks and
|
|
|
|
// therefore hooks that implement this interface must be completely
|
|
|
|
// safe for concurrent access.
|
|
|
|
//
|
|
|
|
// The context is cancelled if the task is killed.
|
2018-07-12 23:15:33 +00:00
|
|
|
Update(context.Context, *TaskUpdateRequest, *TaskUpdateResponse) error
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
2018-07-17 00:19:56 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
type TaskStopRequest struct {
|
|
|
|
// ExistingState is previously set hook data and should only be
|
|
|
|
// read. Stop hooks cannot alter state.
|
|
|
|
ExistingState map[string]string
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
type TaskStopResponse struct{}
|
|
|
|
|
|
|
|
type TaskStopHook interface {
|
|
|
|
TaskHook
|
2018-07-17 00:40:04 +00:00
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Stop is called after the task has exited and will not be started
|
|
|
|
// again. It is the only hook guaranteed to be executed whenever
|
|
|
|
// TaskRunner.Run is called (and not gracefully shutting down).
|
|
|
|
// Therefore it may be called even when prestart and the other hooks
|
|
|
|
// have not.
|
|
|
|
//
|
2019-03-12 22:03:54 +00:00
|
|
|
// Stop hooks must be idempotent. The context is cancelled if the task
|
|
|
|
// is killed.
|
2018-07-17 00:19:56 +00:00
|
|
|
Stop(context.Context, *TaskStopRequest, *TaskStopResponse) error
|
|
|
|
}
|