2018-06-22 00:35:07 +00:00
|
|
|
package taskrunner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunnerv2/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/allocrunnerv2/taskrunner/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// initHooks intializes the tasks hooks.
|
|
|
|
func (tr *TaskRunner) initHooks() {
|
|
|
|
hookLogger := tr.logger.Named("task_hook")
|
|
|
|
|
|
|
|
// Create the task directory hook. This is run first to ensure the
|
|
|
|
// directoy path exists for other hooks.
|
2018-06-27 23:57:31 +00:00
|
|
|
tr.runnerHooks = []interfaces.TaskHook{
|
|
|
|
newTaskDirHook(tr, hookLogger),
|
|
|
|
newArtifactHook(tr, hookLogger),
|
|
|
|
}
|
2018-07-12 23:15:33 +00:00
|
|
|
|
|
|
|
// If Vault is enabled, add the hook
|
|
|
|
if task := tr.Task(); task.Vault != nil {
|
|
|
|
tr.runnerHooks = append(tr.runnerHooks, newVaultHook(&vaultHookConfig{
|
|
|
|
vaultStanza: task.Vault,
|
|
|
|
client: tr.vaultClient,
|
|
|
|
events: tr,
|
|
|
|
lifecycle: tr,
|
|
|
|
updater: tr,
|
|
|
|
logger: hookLogger,
|
|
|
|
alloc: tr.Alloc(),
|
|
|
|
task: tr.taskName,
|
|
|
|
}))
|
|
|
|
}
|
2018-07-13 20:45:57 +00:00
|
|
|
|
|
|
|
// If there are templates is enabled, add the hook
|
|
|
|
if task := tr.Task(); len(task.Templates) != 0 {
|
|
|
|
tr.runnerHooks = append(tr.runnerHooks, newTemplateHook(&templateHookConfig{
|
|
|
|
logger: hookLogger,
|
|
|
|
lifecycle: tr,
|
|
|
|
events: tr,
|
|
|
|
templates: task.Templates,
|
|
|
|
clientConfig: tr.clientConfig,
|
|
|
|
envBuilder: tr.envBuilder,
|
|
|
|
}))
|
|
|
|
}
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// prestart is used to run the runners prestart hooks.
|
|
|
|
func (tr *TaskRunner) prestart() error {
|
2018-06-29 22:39:54 +00:00
|
|
|
//XXX is this necessary? maybe we should have a generic cancelletion
|
|
|
|
// method instead of peeking into the alloc
|
2018-06-22 00:35:07 +00:00
|
|
|
// Determine if the allocation is terminaland we should avoid running
|
2018-07-17 00:19:56 +00:00
|
|
|
// prestart hooks.
|
2018-06-29 00:20:13 +00:00
|
|
|
alloc := tr.Alloc()
|
2018-06-22 00:35:07 +00:00
|
|
|
if alloc.TerminalStatus() {
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("skipping prestart hooks since allocation is terminal")
|
2018-06-22 00:35:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running prestart hooks", "start", start)
|
2018-06-22 00:35:07 +00:00
|
|
|
defer func() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished prestart hooks", "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range tr.runnerHooks {
|
2018-07-17 00:19:56 +00:00
|
|
|
pre, ok := hook.(interfaces.TaskPrestartHook)
|
2018-06-22 00:35:07 +00:00
|
|
|
if !ok {
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("skipping non-prestart hook", "name", hook.Name())
|
2018-06-22 00:35:07 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := pre.Name()
|
|
|
|
// Build the request
|
2018-07-17 00:19:56 +00:00
|
|
|
req := interfaces.TaskPrestartRequest{
|
2018-06-22 00:35:07 +00:00
|
|
|
Task: tr.Task(),
|
|
|
|
TaskDir: tr.taskDir.Dir,
|
2018-06-27 23:57:31 +00:00
|
|
|
TaskEnv: tr.envBuilder.Build(),
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:15:33 +00:00
|
|
|
tr.localStateLock.RLock()
|
2018-07-11 04:22:04 +00:00
|
|
|
origHookState := tr.localState.Hooks[name]
|
2018-07-12 23:15:33 +00:00
|
|
|
tr.localStateLock.RUnlock()
|
2018-07-17 00:19:56 +00:00
|
|
|
if origHookState != nil && origHookState.PrestartDone {
|
|
|
|
tr.logger.Trace("skipping done prestart hook", "name", pre.Name())
|
2018-06-29 22:39:54 +00:00
|
|
|
continue
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 23:15:33 +00:00
|
|
|
req.VaultToken = tr.getVaultToken()
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// Time the prestart hook
|
2018-06-29 23:22:18 +00:00
|
|
|
var start time.Time
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start = time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running prestart hook", "name", name, "start", start)
|
2018-06-29 23:22:18 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// Run the prestart hook
|
|
|
|
var resp interfaces.TaskPrestartResponse
|
|
|
|
if err := pre.Prestart(tr.ctx, &req, &resp); err != nil {
|
|
|
|
return structs.WrapRecoverable(fmt.Sprintf("prestart hook %q failed: %v", name, err), err)
|
2018-06-29 22:39:54 +00:00
|
|
|
}
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
// Store the hook state
|
|
|
|
{
|
2018-07-12 23:15:33 +00:00
|
|
|
tr.localStateLock.Lock()
|
2018-07-11 04:22:04 +00:00
|
|
|
hookState, ok := tr.localState.Hooks[name]
|
2018-06-22 00:35:07 +00:00
|
|
|
if !ok {
|
|
|
|
hookState = &state.HookState{}
|
2018-07-11 04:22:04 +00:00
|
|
|
tr.localState.Hooks[name] = hookState
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.HookData != nil {
|
|
|
|
hookState.Data = resp.HookData
|
2018-07-17 00:19:56 +00:00
|
|
|
hookState.PrestartDone = resp.Done
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
2018-07-12 23:15:33 +00:00
|
|
|
tr.localStateLock.Unlock()
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-11 04:22:04 +00:00
|
|
|
// Persist local state if the hook state has changed
|
|
|
|
if !hookState.Equal(origHookState) {
|
|
|
|
if err := tr.persistLocalState(); err != nil {
|
|
|
|
return err
|
2018-06-29 00:01:05 +00:00
|
|
|
}
|
2018-07-11 04:22:04 +00:00
|
|
|
}
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store the environment variables returned by the hook
|
|
|
|
if len(resp.Env) != 0 {
|
|
|
|
tr.envBuilder.SetGenericEnv(resp.Env)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished prestart hooks", "name", name, "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// poststart is used to run the runners poststart hooks.
|
|
|
|
func (tr *TaskRunner) poststart() error {
|
2018-06-22 00:35:07 +00:00
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running poststart hooks", "start", start)
|
2018-06-22 00:35:07 +00:00
|
|
|
defer func() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished poststart hooks", "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range tr.runnerHooks {
|
2018-07-17 00:19:56 +00:00
|
|
|
post, ok := hook.(interfaces.TaskPoststartHook)
|
2018-06-22 00:35:07 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := post.Name()
|
|
|
|
var start time.Time
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start = time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running poststart hook", "name", name, "start", start)
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
req := interfaces.TaskPoststartRequest{}
|
|
|
|
var resp interfaces.TaskPoststartResponse
|
2018-06-22 00:35:07 +00:00
|
|
|
// XXX We shouldn't exit on the first one
|
2018-07-17 00:19:56 +00:00
|
|
|
if err := post.Poststart(tr.ctx, &req, &resp); err != nil {
|
|
|
|
return fmt.Errorf("poststart hook %q failed: %v", name, err)
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished poststart hooks", "name", name, "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// stop is used to run the stop hooks.
|
|
|
|
func (tr *TaskRunner) stop() error {
|
2018-06-22 00:35:07 +00:00
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running stop hooks", "start", start)
|
2018-06-22 00:35:07 +00:00
|
|
|
defer func() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished stop hooks", "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range tr.runnerHooks {
|
2018-07-17 00:19:56 +00:00
|
|
|
post, ok := hook.(interfaces.TaskStopHook)
|
2018-06-22 00:35:07 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := post.Name()
|
|
|
|
var start time.Time
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start = time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("running stop hook", "name", name, "start", start)
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
req := interfaces.TaskStopRequest{}
|
|
|
|
var resp interfaces.TaskStopResponse
|
2018-06-22 00:35:07 +00:00
|
|
|
// XXX We shouldn't exit on the first one
|
2018-07-17 00:19:56 +00:00
|
|
|
if err := post.Stop(tr.ctx, &req, &resp); err != nil {
|
|
|
|
return fmt.Errorf("stop hook %q failed: %v", name, err)
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
end := time.Now()
|
2018-07-17 00:19:56 +00:00
|
|
|
tr.logger.Trace("finished stop hooks", "name", name, "end", end, "duration", end.Sub(start))
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:15:33 +00:00
|
|
|
// update is used to run the runners update hooks.
|
|
|
|
func (tr *TaskRunner) updateHooks() {
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start := time.Now()
|
|
|
|
tr.logger.Trace("running update hooks", "start", start)
|
|
|
|
defer func() {
|
|
|
|
end := time.Now()
|
|
|
|
tr.logger.Trace("finished update hooks", "end", end, "duration", end.Sub(start))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range tr.runnerHooks {
|
|
|
|
upd, ok := hook.(interfaces.TaskUpdateHook)
|
|
|
|
if !ok {
|
|
|
|
tr.logger.Trace("skipping non-update hook", "name", hook.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
name := upd.Name()
|
|
|
|
|
|
|
|
// Build the request
|
|
|
|
req := interfaces.TaskUpdateRequest{
|
|
|
|
VaultToken: tr.getVaultToken(),
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
// Time the update hook
|
2018-07-12 23:15:33 +00:00
|
|
|
var start time.Time
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
start = time.Now()
|
|
|
|
tr.logger.Trace("running update hook", "name", name, "start", start)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the update hook
|
|
|
|
var resp interfaces.TaskUpdateResponse
|
|
|
|
if err := upd.Update(tr.ctx, &req, &resp); err != nil {
|
|
|
|
tr.logger.Error("update hook failed", "name", name, "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tr.logger.IsTrace() {
|
|
|
|
end := time.Now()
|
|
|
|
tr.logger.Trace("finished update hooks", "name", name, "end", end, "duration", end.Sub(start))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
/*
|
|
|
|
TR Hooks:
|
|
|
|
|
|
|
|
> @schmichael
|
|
|
|
Task Validate:
|
|
|
|
Require: Client config, task definiton
|
|
|
|
Return: error
|
|
|
|
Implement: Prestart
|
|
|
|
|
|
|
|
> DONE
|
|
|
|
Task Dir Build:
|
|
|
|
Requires: Folder structure, driver isolation, client config
|
|
|
|
Return env, error
|
|
|
|
Implement: Prestart
|
|
|
|
|
|
|
|
> @alex
|
|
|
|
Vault: Task, RPC to talk to server to derive token, Node SecretID
|
|
|
|
Return vault token (Call a setter), error, env
|
|
|
|
Implement: Prestart
|
|
|
|
|
|
|
|
> @alex
|
|
|
|
Consul Template:
|
|
|
|
Require: Task, alloc directory, way to signal/restart task, updates when vault token changes
|
|
|
|
Return env, error
|
|
|
|
Implement: Prestart and Update (for new Vault token) and Destroy
|
|
|
|
|
|
|
|
> @schmichael
|
|
|
|
Consul Service Reg:
|
|
|
|
Require: Task, interpolation/ENV
|
|
|
|
Return: error
|
2018-07-17 00:19:56 +00:00
|
|
|
Implement: Poststart, Update, Kill, Exited
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
> @alex
|
|
|
|
Dispatch Payload:
|
|
|
|
Require: Alloc
|
|
|
|
Return error
|
2018-07-17 00:19:56 +00:00
|
|
|
Implement: Prestart
|
2018-06-22 00:35:07 +00:00
|
|
|
|
|
|
|
> @schmichael
|
|
|
|
Artifacts:
|
|
|
|
Require: Folder structure, task, interpolation/ENV
|
|
|
|
Return: error
|
2018-07-17 00:19:56 +00:00
|
|
|
Implement: Prestart
|
2018-06-22 00:35:07 +00:00
|
|
|
*/
|