2018-06-22 00:35:07 +00:00
|
|
|
package taskrunner
|
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
import (
|
2019-02-21 23:37:22 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/taskrunner/state"
|
2018-07-16 21:37:27 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
2018-06-22 00:35:07 +00:00
|
|
|
|
2018-07-12 23:15:33 +00:00
|
|
|
func (tr *TaskRunner) Alloc() *structs.Allocation {
|
|
|
|
tr.allocLock.Lock()
|
|
|
|
defer tr.allocLock.Unlock()
|
|
|
|
return tr.alloc
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:29:07 +00:00
|
|
|
// setAlloc and task on TaskRunner
|
|
|
|
func (tr *TaskRunner) setAlloc(updated *structs.Allocation, task *structs.Task) {
|
2018-08-01 18:03:52 +00:00
|
|
|
tr.allocLock.Lock()
|
2018-11-14 18:29:07 +00:00
|
|
|
defer tr.allocLock.Unlock()
|
|
|
|
|
|
|
|
tr.taskLock.Lock()
|
|
|
|
defer tr.taskLock.Unlock()
|
|
|
|
|
2018-08-01 18:03:52 +00:00
|
|
|
tr.alloc = updated
|
2018-11-14 18:29:07 +00:00
|
|
|
tr.task = task
|
2018-08-01 18:03:52 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 01:03:48 +00:00
|
|
|
// IsLeader returns true if this task is the leader of its task group.
|
|
|
|
func (tr *TaskRunner) IsLeader() bool {
|
|
|
|
return tr.taskLeader
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
func (tr *TaskRunner) Task() *structs.Task {
|
|
|
|
tr.taskLock.RLock()
|
|
|
|
defer tr.taskLock.RUnlock()
|
|
|
|
return tr.task
|
|
|
|
}
|
2018-07-12 23:15:33 +00:00
|
|
|
|
2018-07-19 00:06:44 +00:00
|
|
|
func (tr *TaskRunner) TaskState() *structs.TaskState {
|
|
|
|
tr.stateLock.Lock()
|
|
|
|
defer tr.stateLock.Unlock()
|
|
|
|
return tr.state.Copy()
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:15:33 +00:00
|
|
|
func (tr *TaskRunner) getVaultToken() string {
|
|
|
|
tr.vaultTokenLock.Lock()
|
|
|
|
defer tr.vaultTokenLock.Unlock()
|
|
|
|
return tr.vaultToken
|
|
|
|
}
|
|
|
|
|
2018-08-01 18:03:52 +00:00
|
|
|
// setVaultToken updates the vault token on the task runner as well as in the
|
|
|
|
// task's environment. These two places must be set atomically to avoid a task
|
|
|
|
// seeing a different token on the task runner and in its environment.
|
2018-07-12 23:15:33 +00:00
|
|
|
func (tr *TaskRunner) setVaultToken(token string) {
|
|
|
|
tr.vaultTokenLock.Lock()
|
|
|
|
defer tr.vaultTokenLock.Unlock()
|
2018-08-01 18:03:52 +00:00
|
|
|
|
|
|
|
// Update the Vault token on the runner
|
2018-07-12 23:15:33 +00:00
|
|
|
tr.vaultToken = token
|
2018-08-01 18:03:52 +00:00
|
|
|
|
|
|
|
// Update the task's environment
|
2020-07-17 14:41:45 +00:00
|
|
|
taskNamespace := tr.task.Vault.Namespace
|
|
|
|
|
|
|
|
ns := tr.clientConfig.VaultConfig.Namespace
|
|
|
|
if taskNamespace != "" {
|
|
|
|
ns = taskNamespace
|
|
|
|
}
|
|
|
|
tr.envBuilder.SetVaultToken(token, ns, tr.task.Vault.Env)
|
2018-07-12 23:15:33 +00:00
|
|
|
}
|
2018-07-16 21:37:27 +00:00
|
|
|
|
2018-11-16 00:07:35 +00:00
|
|
|
// getDriverHandle returns a driver handle.
|
2018-10-16 03:06:35 +00:00
|
|
|
func (tr *TaskRunner) getDriverHandle() *DriverHandle {
|
2018-07-16 21:37:27 +00:00
|
|
|
tr.handleLock.Lock()
|
|
|
|
defer tr.handleLock.Unlock()
|
2018-10-04 19:08:20 +00:00
|
|
|
return tr.handle
|
2018-07-16 21:37:27 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 00:07:35 +00:00
|
|
|
// setDriverHandle sets the driver handle and updates the driver network in the
|
|
|
|
// task's environment.
|
2018-10-10 03:01:20 +00:00
|
|
|
func (tr *TaskRunner) setDriverHandle(handle *DriverHandle) {
|
2018-07-16 21:37:27 +00:00
|
|
|
tr.handleLock.Lock()
|
|
|
|
defer tr.handleLock.Unlock()
|
|
|
|
tr.handle = handle
|
2018-11-06 01:39:02 +00:00
|
|
|
|
|
|
|
// Update the environment's driver network
|
|
|
|
tr.envBuilder.SetDriverNetwork(handle.net)
|
2018-07-20 00:40:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *TaskRunner) clearDriverHandle() {
|
|
|
|
tr.handleLock.Lock()
|
|
|
|
defer tr.handleLock.Unlock()
|
2018-10-04 19:08:20 +00:00
|
|
|
if tr.handle != nil {
|
|
|
|
tr.driver.DestroyTask(tr.handle.ID(), true)
|
|
|
|
}
|
2018-07-20 00:40:25 +00:00
|
|
|
tr.handle = nil
|
2018-07-16 21:37:27 +00:00
|
|
|
}
|
2019-01-09 00:42:26 +00:00
|
|
|
|
|
|
|
// setKillErr stores any error that arouse while killing the task
|
|
|
|
func (tr *TaskRunner) setKillErr(err error) {
|
|
|
|
tr.killErrLock.Lock()
|
|
|
|
defer tr.killErrLock.Unlock()
|
|
|
|
tr.killErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getKillErr returns any error that arouse while killing the task
|
|
|
|
func (tr *TaskRunner) getKillErr() error {
|
|
|
|
tr.killErrLock.Lock()
|
|
|
|
defer tr.killErrLock.Unlock()
|
|
|
|
return tr.killErr
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// hookState returns the state for the given hook or nil if no state is
|
|
|
|
// persisted for the hook.
|
|
|
|
func (tr *TaskRunner) hookState(name string) *state.HookState {
|
|
|
|
tr.stateLock.RLock()
|
|
|
|
defer tr.stateLock.RUnlock()
|
|
|
|
|
|
|
|
var s *state.HookState
|
|
|
|
if tr.localState.Hooks != nil {
|
|
|
|
s = tr.localState.Hooks[name].Copy()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|