2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-05-25 15:18:42 +02:00
|
|
|
"time"
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/state"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2017-05-25 15:18:42 +02:00
|
|
|
// interval between forced PersistState calls by StateHook
|
|
|
|
const persistStateHookInterval = 10 * time.Second
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
// StateHook is a hook that continuously updates the state by calling
|
|
|
|
// WriteState on a state.State.
|
|
|
|
type StateHook struct {
|
|
|
|
terraform.NilHook
|
|
|
|
sync.Mutex
|
|
|
|
|
2017-05-25 15:18:42 +02:00
|
|
|
// lastPersist is the time of the last call to PersistState, for periodic
|
|
|
|
// updates to remote state. PostStateUpdate will force a call PersistState
|
|
|
|
// if it has been more that persistStateHookInterval since the last call to
|
|
|
|
// PersistState.
|
|
|
|
lastPersist time.Time
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
State state.State
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *StateHook) PostStateUpdate(
|
|
|
|
s *terraform.State) (terraform.HookAction, error) {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.State != nil {
|
|
|
|
// Write the new state
|
|
|
|
if err := h.State.WriteState(s); err != nil {
|
|
|
|
return terraform.HookActionHalt, err
|
|
|
|
}
|
2017-05-25 15:18:42 +02:00
|
|
|
|
|
|
|
// periodically persist the state
|
|
|
|
if time.Since(h.lastPersist) > persistStateHookInterval {
|
|
|
|
if err := h.persistState(); err != nil {
|
|
|
|
return terraform.HookActionHalt, err
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Continue forth
|
|
|
|
return terraform.HookActionContinue, nil
|
|
|
|
}
|
2017-05-25 15:18:42 +02:00
|
|
|
|
|
|
|
func (h *StateHook) persistState() error {
|
|
|
|
if h.State != nil {
|
|
|
|
err := h.State.PersistState()
|
|
|
|
h.lastPersist = time.Now()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|