2018-06-22 00:35:07 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2018-07-11 04:22:04 +00:00
|
|
|
"github.com/hashicorp/nomad/client/structs"
|
2018-06-22 00:35:07 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
DriverNetwork *structs.DriverNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLocalState() *LocalState {
|
|
|
|
return &LocalState{
|
|
|
|
Hooks: make(map[string]*HookState),
|
|
|
|
}
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy should be called with the lock held
|
2018-07-11 04:22:04 +00:00
|
|
|
func (s *LocalState) Copy() *LocalState {
|
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)),
|
|
|
|
DriverNetwork: s.DriverNetwork,
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the hooks
|
|
|
|
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-07-11 04:22:04 +00:00
|
|
|
Data map[string]string
|
2018-06-22 00:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HookState) Copy() *HookState {
|
|
|
|
c := new(HookState)
|
|
|
|
*c = *h
|
|
|
|
c.Data = helper.CopyMapStringString(c.Data)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return helper.CompareMapStringString(h.Data, o.Data)
|
|
|
|
}
|