/
hook_json.go
168 lines (140 loc) · 4.97 KB
/
hook_json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package views
import (
"bufio"
"strings"
"sync"
"time"
"unicode"
"github.com/zclconf/go-cty/cty"
"github.com/opentofu/opentofu/internal/addrs"
"github.com/opentofu/opentofu/internal/command/format"
"github.com/opentofu/opentofu/internal/command/views/json"
"github.com/opentofu/opentofu/internal/plans"
"github.com/opentofu/opentofu/internal/states"
"github.com/opentofu/opentofu/internal/tofu"
)
// How long to wait between sending heartbeat/progress messages
const heartbeatInterval = 10 * time.Second
func newJSONHook(view *JSONView) *jsonHook {
return &jsonHook{
view: view,
applying: make(map[string]applyProgress),
timeNow: time.Now,
timeAfter: time.After,
}
}
type jsonHook struct {
tofu.NilHook
view *JSONView
// Concurrent map of resource addresses to allow the sequence of pre-apply,
// progress, and post-apply messages to share data about the resource
applying map[string]applyProgress
applyingLock sync.Mutex
// Mockable functions for testing the progress timer goroutine
timeNow func() time.Time
timeAfter func(time.Duration) <-chan time.Time
}
var _ tofu.Hook = (*jsonHook)(nil)
type applyProgress struct {
addr addrs.AbsResourceInstance
action plans.Action
start time.Time
// done is used for post-apply to stop the progress goroutine
done chan struct{}
// heartbeatDone is used to allow tests to safely wait for the progress
// goroutine to finish
heartbeatDone chan struct{}
}
func (h *jsonHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (tofu.HookAction, error) {
if action != plans.NoOp {
idKey, idValue := format.ObjectValueIDOrName(priorState)
h.view.Hook(json.NewApplyStart(addr, action, idKey, idValue))
}
progress := applyProgress{
addr: addr,
action: action,
start: h.timeNow().Round(time.Second),
done: make(chan struct{}),
heartbeatDone: make(chan struct{}),
}
h.applyingLock.Lock()
h.applying[addr.String()] = progress
h.applyingLock.Unlock()
if action != plans.NoOp {
go h.applyingHeartbeat(progress)
}
return tofu.HookActionContinue, nil
}
func (h *jsonHook) applyingHeartbeat(progress applyProgress) {
defer close(progress.heartbeatDone)
for {
select {
case <-progress.done:
return
case <-h.timeAfter(heartbeatInterval):
}
elapsed := h.timeNow().Round(time.Second).Sub(progress.start)
h.view.Hook(json.NewApplyProgress(progress.addr, progress.action, elapsed))
}
}
func (h *jsonHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (tofu.HookAction, error) {
key := addr.String()
h.applyingLock.Lock()
progress := h.applying[key]
if progress.done != nil {
close(progress.done)
}
delete(h.applying, key)
h.applyingLock.Unlock()
if progress.action == plans.NoOp {
return tofu.HookActionContinue, nil
}
elapsed := h.timeNow().Round(time.Second).Sub(progress.start)
if err != nil {
// Errors are collected and displayed post-apply, so no need to
// re-render them here. Instead just signal that this resource failed
// to apply.
h.view.Hook(json.NewApplyErrored(addr, progress.action, elapsed))
} else {
idKey, idValue := format.ObjectValueID(newState)
h.view.Hook(json.NewApplyComplete(addr, progress.action, idKey, idValue, elapsed))
}
return tofu.HookActionContinue, nil
}
func (h *jsonHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (tofu.HookAction, error) {
h.view.Hook(json.NewProvisionStart(addr, typeName))
return tofu.HookActionContinue, nil
}
func (h *jsonHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (tofu.HookAction, error) {
if err != nil {
// Errors are collected and displayed post-apply, so no need to
// re-render them here. Instead just signal that this provisioner step
// failed.
h.view.Hook(json.NewProvisionErrored(addr, typeName))
} else {
h.view.Hook(json.NewProvisionComplete(addr, typeName))
}
return tofu.HookActionContinue, nil
}
func (h *jsonHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, msg string) {
s := bufio.NewScanner(strings.NewReader(msg))
s.Split(scanLines)
for s.Scan() {
line := strings.TrimRightFunc(s.Text(), unicode.IsSpace)
if line != "" {
h.view.Hook(json.NewProvisionProgress(addr, typeName, line))
}
}
}
func (h *jsonHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (tofu.HookAction, error) {
idKey, idValue := format.ObjectValueID(priorState)
h.view.Hook(json.NewRefreshStart(addr, idKey, idValue))
return tofu.HookActionContinue, nil
}
func (h *jsonHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (tofu.HookAction, error) {
idKey, idValue := format.ObjectValueID(newState)
h.view.Hook(json.NewRefreshComplete(addr, idKey, idValue))
return tofu.HookActionContinue, nil
}