2018-06-22 00:35:07 +00:00
|
|
|
package taskrunner
|
|
|
|
|
2018-07-16 21:37:27 +00:00
|
|
|
import (
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunnerv2/taskrunner/interfaces"
|
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-08-01 18:03:52 +00:00
|
|
|
func (tr *TaskRunner) setAlloc(updated *structs.Allocation) {
|
|
|
|
tr.allocLock.Lock()
|
|
|
|
tr.alloc = updated
|
|
|
|
tr.allocLock.Unlock()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
tr.envBuilder.SetVaultToken(token, tr.task.Vault.Env)
|
2018-07-12 23:15:33 +00:00
|
|
|
}
|
2018-07-16 21:37:27 +00:00
|
|
|
|
2018-09-20 22:44:27 +00:00
|
|
|
// getDriverHandle returns a driver handle and its result proxy. Use the
|
|
|
|
// result proxy instead of the handle's WaitCh.
|
2018-10-04 19:08:20 +00:00
|
|
|
func (tr *TaskRunner) getDriverHandle() interfaces.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-09-20 22:44:27 +00:00
|
|
|
// setDriverHanlde sets the driver handle and creates a new result proxy.
|
2018-10-04 19:08:20 +00:00
|
|
|
func (tr *TaskRunner) setDriverHandle(handle interfaces.DriverHandle) {
|
2018-07-16 21:37:27 +00:00
|
|
|
tr.handleLock.Lock()
|
|
|
|
defer tr.handleLock.Unlock()
|
|
|
|
tr.handle = handle
|
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
|
|
|
}
|