2018-06-22 00:35:07 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-06-22 00:35:07 +00:00
|
|
|
)
|
|
|
|
|
2018-07-11 04:22:04 +00:00
|
|
|
// LocalState is Task state which is persisted for use when restarting Nomad
|
|
|
|
// agents.
|
|
|
|
type LocalState struct {
|
2018-06-22 00:35:07 +00:00
|
|
|
Hooks map[string]*HookState
|
|
|
|
|
2018-07-11 04:22:04 +00:00
|
|
|
// DriverNetwork is the network information returned by the task
|
|
|
|
// driver's Start method
|
2019-01-04 23:01:35 +00:00
|
|
|
DriverNetwork *drivers.DriverNetwork
|
2018-10-04 19:08:20 +00:00
|
|
|
|
|
|
|
// TaskHandle is the handle used to reattach to the task during recovery
|
|
|
|
TaskHandle *drivers.TaskHandle
|
2018-07-11 04:22:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLocalState() *LocalState {
|
|
|
|
return &LocalState{
|
|
|
|
Hooks: make(map[string]*HookState),
|
|
|
|
}
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 22:17:36 +00:00
|
|
|
// Canonicalize ensures LocalState is in a consistent state by initializing
|
|
|
|
// Hooks and ensuring no HookState's are nil. Useful for cleaning unmarshalled
|
|
|
|
// state which may be in an unknown state.
|
|
|
|
func (s *LocalState) Canonicalize() {
|
|
|
|
if s.Hooks == nil {
|
|
|
|
// Hooks is nil, create it
|
|
|
|
s.Hooks = make(map[string]*HookState)
|
|
|
|
} else {
|
|
|
|
for k, v := range s.Hooks {
|
|
|
|
// Remove invalid nil entries from Hooks map
|
|
|
|
if v == nil {
|
|
|
|
delete(s.Hooks, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Copy LocalState. Returns nil if nil.
|
2018-07-11 04:22:04 +00:00
|
|
|
func (s *LocalState) Copy() *LocalState {
|
2019-02-21 23:37:22 +00:00
|
|
|
if s == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
// Create a copy
|
2018-07-11 04:22:04 +00:00
|
|
|
c := &LocalState{
|
|
|
|
Hooks: make(map[string]*HookState, len(s.Hooks)),
|
2018-10-11 00:08:57 +00:00
|
|
|
DriverNetwork: s.DriverNetwork.Copy(),
|
|
|
|
TaskHandle: s.TaskHandle.Copy(),
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Copy the hook state
|
2018-06-22 00:35:07 +00:00
|
|
|
for h, state := range s.Hooks {
|
|
|
|
c.Hooks[h] = state.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type HookState struct {
|
2018-07-17 00:19:56 +00:00
|
|
|
// Prestart is true if the hook has run Prestart successfully and does
|
2018-07-11 04:22:04 +00:00
|
|
|
// not need to run again
|
2018-07-17 00:19:56 +00:00
|
|
|
PrestartDone bool
|
2018-11-27 19:53:47 +00:00
|
|
|
|
|
|
|
// Data allows hooks to persist arbitrary state.
|
|
|
|
Data map[string]string
|
|
|
|
|
|
|
|
// Environment variables set by the hook that will continue to be set
|
|
|
|
// even if PrestartDone=true.
|
|
|
|
Env map[string]string
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:22 +00:00
|
|
|
// Copy HookState. Returns nil if its nil.
|
2018-06-22 00:35:07 +00:00
|
|
|
func (h *HookState) Copy() *HookState {
|
2019-02-21 23:37:22 +00:00
|
|
|
if h == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:35:07 +00:00
|
|
|
c := new(HookState)
|
|
|
|
*c = *h
|
2019-03-01 23:02:53 +00:00
|
|
|
c.Data = helper.CopyMapStringString(h.Data)
|
|
|
|
c.Env = helper.CopyMapStringString(h.Env)
|
2018-06-22 00:35:07 +00:00
|
|
|
return c
|
|
|
|
}
|
2018-07-11 04:22:04 +00:00
|
|
|
|
|
|
|
func (h *HookState) Equal(o *HookState) bool {
|
|
|
|
if h == nil || o == nil {
|
|
|
|
return h == o
|
|
|
|
}
|
|
|
|
|
2018-07-17 00:19:56 +00:00
|
|
|
if h.PrestartDone != o.PrestartDone {
|
2018-07-11 04:22:04 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-07 01:25:23 +00:00
|
|
|
if !helper.CompareMapStringString(h.Data, o.Data) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return helper.CompareMapStringString(h.Env, o.Env)
|
2018-07-11 04:22:04 +00:00
|
|
|
}
|