2018-10-04 19:08:20 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
|
|
)
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
// NewDriverHandle returns a handle for task operations on a specific task
|
2019-01-04 23:01:35 +00:00
|
|
|
func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *drivers.DriverNetwork) *DriverHandle {
|
2018-10-10 03:01:20 +00:00
|
|
|
return &DriverHandle{
|
2018-10-04 19:08:20 +00:00
|
|
|
driver: driver,
|
|
|
|
net: net,
|
|
|
|
taskID: taskID,
|
|
|
|
task: task,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
// DriverHandle encapsulates a driver plugin client and task identifier and exposes
|
|
|
|
// an api to perform driver operations on the task
|
|
|
|
type DriverHandle struct {
|
2018-10-04 19:08:20 +00:00
|
|
|
driver drivers.DriverPlugin
|
2019-01-04 23:01:35 +00:00
|
|
|
net *drivers.DriverNetwork
|
2018-10-04 19:08:20 +00:00
|
|
|
task *structs.Task
|
|
|
|
taskID string
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) ID() string {
|
2018-10-04 19:08:20 +00:00
|
|
|
return h.taskID
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) WaitCh(ctx context.Context) (<-chan *drivers.ExitResult, error) {
|
2018-10-04 19:08:20 +00:00
|
|
|
return h.driver.WaitTask(ctx, h.taskID)
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) Update(task *structs.Task) error {
|
2018-10-04 19:08:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) Kill() error {
|
2018-10-04 19:08:20 +00:00
|
|
|
return h.driver.StopTask(h.taskID, h.task.KillTimeout, h.task.KillSignal)
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
func (h *DriverHandle) Stats(ctx context.Context, interval time.Duration) (<-chan *cstructs.TaskResourceUsage, error) {
|
|
|
|
return h.driver.TaskStats(ctx, h.taskID, interval)
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) Signal(s string) error {
|
2018-10-04 19:08:20 +00:00
|
|
|
return h.driver.SignalTask(h.taskID, s)
|
|
|
|
}
|
|
|
|
|
2018-10-10 03:01:20 +00:00
|
|
|
func (h *DriverHandle) Exec(timeout time.Duration, cmd string, args []string) ([]byte, int, error) {
|
2018-10-04 19:08:20 +00:00
|
|
|
command := append([]string{cmd}, args...)
|
|
|
|
res, err := h.driver.ExecTask(h.taskID, command, timeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return res.Stdout, res.ExitResult.ExitCode, res.ExitResult.Err
|
|
|
|
}
|
|
|
|
|
2019-01-04 23:01:35 +00:00
|
|
|
func (h *DriverHandle) Network() *drivers.DriverNetwork {
|
2018-10-04 19:08:20 +00:00
|
|
|
return h.net
|
|
|
|
}
|