forked from pulumi/pulumi
/
display.go
347 lines (285 loc) · 10.5 KB
/
display.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package display
import (
"bytes"
"fmt"
"io"
"math"
"os"
"sort"
"time"
"github.com/dustin/go-humanize/english"
"github.com/pulumi/pulumi/pkg/apitype"
"github.com/pulumi/pulumi/pkg/diag"
"github.com/pulumi/pulumi/pkg/diag/colors"
"github.com/pulumi/pulumi/pkg/engine"
"github.com/pulumi/pulumi/pkg/resource"
"github.com/pulumi/pulumi/pkg/resource/deploy"
"github.com/pulumi/pulumi/pkg/tokens"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/util/contract"
)
// ShowEvents reads events from the `events` channel until it is closed, displaying each event as
// it comes in. Once all events have been read from the channel and displayed, it closes the `done`
// channel so the caller can await all the events being written.
func ShowEvents(
op string, action apitype.UpdateKind, stack tokens.QName, proj tokens.PackageName,
events <-chan engine.Event, done chan<- bool, opts Options, isPreview bool) {
if opts.DiffDisplay {
ShowDiffEvents(op, action, events, done, opts)
} else {
ShowProgressEvents(op, action, stack, proj, events, done, opts, isPreview)
}
}
type nopSpinner struct {
}
func (s *nopSpinner) Tick() {
}
func (s *nopSpinner) Reset() {
}
// ShowDiffEvents displays the engine events with the diff view.
func ShowDiffEvents(op string, action apitype.UpdateKind,
events <-chan engine.Event, done chan<- bool, opts Options) {
prefix := fmt.Sprintf("%s%s...", cmdutil.EmojiOr("✨ ", "@ "), op)
var spinner cmdutil.Spinner
var ticker *time.Ticker
if opts.IsInteractive {
spinner, ticker = cmdutil.NewSpinnerAndTicker(prefix, nil, 8 /*timesPerSecond*/)
} else {
spinner = &nopSpinner{}
ticker = time.NewTicker(math.MaxInt64)
}
defer func() {
spinner.Reset()
ticker.Stop()
close(done)
}()
seen := make(map[resource.URN]engine.StepEventMetadata)
for {
select {
case <-ticker.C:
spinner.Tick()
case event := <-events:
spinner.Reset()
out := os.Stdout
if event.Type == engine.DiagEvent {
payload := event.Payload.(engine.DiagEventPayload)
if payload.Severity == diag.Error || payload.Severity == diag.Warning {
out = os.Stderr
}
}
msg := RenderDiffEvent(action, event, seen, opts)
if msg != "" && out != nil {
fprintIgnoreError(out, msg)
}
if event.Type == engine.CancelEvent {
return
}
}
}
}
func RenderDiffEvent(action apitype.UpdateKind, event engine.Event,
seen map[resource.URN]engine.StepEventMetadata, opts Options) string {
switch event.Type {
case engine.CancelEvent:
return ""
// Currently, prelude, summar, and stdout events are printed the same for both the diff and
// progress displays.
case engine.PreludeEvent:
return renderPreludeEvent(event.Payload.(engine.PreludeEventPayload), opts)
case engine.SummaryEvent:
return renderSummaryEvent(action, event.Payload.(engine.SummaryEventPayload), opts)
case engine.StdoutColorEvent:
return renderStdoutColorEvent(event.Payload.(engine.StdoutEventPayload), opts)
// Resource operations have very specific displays for either diff or progress displays.
// These functions should not be directly used by the progress display without validating
// that the display is appropriate for both.
case engine.ResourceOperationFailed:
return renderDiffResourceOperationFailedEvent(event.Payload.(engine.ResourceOperationFailedPayload), opts)
case engine.ResourceOutputsEvent:
return renderDiffResourceOutputsEvent(event.Payload.(engine.ResourceOutputsEventPayload), seen, opts)
case engine.ResourcePreEvent:
return renderDiffResourcePreEvent(event.Payload.(engine.ResourcePreEventPayload), seen, opts)
case engine.DiagEvent:
return renderDiffDiagEvent(event.Payload.(engine.DiagEventPayload), opts)
default:
contract.Failf("unknown event type '%s'", event.Type)
return ""
}
}
func renderDiffDiagEvent(payload engine.DiagEventPayload, opts Options) string {
if payload.Severity == diag.Debug && !opts.Debug {
return ""
}
return opts.Color.Colorize(payload.Prefix + payload.Message)
}
func renderStdoutColorEvent(payload engine.StdoutEventPayload, opts Options) string {
return opts.Color.Colorize(payload.Message)
}
func renderSummaryEvent(action apitype.UpdateKind, event engine.SummaryEventPayload, opts Options) string {
changes := event.ResourceChanges
out := &bytes.Buffer{}
fprintIgnoreError(out, opts.Color.Colorize(
fmt.Sprintf("%sResources:%s\n", colors.SpecHeadline, colors.Reset)))
var planTo string
if event.IsPreview {
planTo = "to "
}
var changeCount = 0
var sameCount = changes[deploy.OpSame]
// Now summarize all of the changes; we print sames a little differently.
for _, op := range deploy.StepOps {
if op != deploy.OpSame {
if c := changes[op]; c > 0 {
opDescription := string(op)
if !event.IsPreview {
opDescription = op.PastTense()
}
changeCount++
fprintIgnoreError(out, opts.Color.Colorize(
fmt.Sprintf(" %s%d %s%s%s\n", op.Prefix(), c, planTo, opDescription, colors.Reset)))
}
}
}
summaryPieces := []string{}
if changeCount >= 2 {
// Only if we made multiple types of changes do we need to print out the total number of
// changes. i.e. we don't need "10 changes" and "+ 10 to create". We can just say "+ 10 to create"
summaryPieces = append(summaryPieces, fmt.Sprintf("%s%d %s%s",
colors.Bold, changeCount, english.PluralWord(changeCount, "change", ""), colors.Reset))
}
if sameCount != 0 {
summaryPieces = append(summaryPieces, fmt.Sprintf("%d unchanged", sameCount))
}
if len(summaryPieces) > 0 {
fprintfIgnoreError(out, " ")
for i, piece := range summaryPieces {
if i > 0 {
fprintfIgnoreError(out, ". ")
}
out.WriteString(opts.Color.Colorize(piece))
}
fprintfIgnoreError(out, "\n")
}
// For actual deploys, we print some additional summary information
if !event.IsPreview {
// Round up to the nearest second. It's not useful to spit out time with 9 digits of
// precision.
roundedSeconds := int64(math.Ceil(event.Duration.Seconds()))
roundedDuration := time.Duration(roundedSeconds) * time.Second
fprintIgnoreError(out, opts.Color.Colorize(fmt.Sprintf("\n%sDuration:%s %s\n",
colors.SpecHeadline, colors.Reset, roundedDuration)))
}
return out.String()
}
func renderPreludeEvent(event engine.PreludeEventPayload, opts Options) string {
// Only if we have been instructed to show configuration values will we print anything during the prelude.
if !opts.ShowConfig {
return ""
}
out := &bytes.Buffer{}
fprintIgnoreError(out, opts.Color.Colorize(
fmt.Sprintf("%sConfiguration:%s\n", colors.SpecUnimportant, colors.Reset)))
var keys []string
for key := range event.Config {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fprintfIgnoreError(out, " %v: %v\n", key, event.Config[key])
}
return out.String()
}
func renderDiffResourceOperationFailedEvent(
payload engine.ResourceOperationFailedPayload, opts Options) string {
// It's not actually useful or interesting to print out any details about
// the resource state here, because we always assume that the resource state
// is unknown if an error occurs.
//
// In the future, once we get more fine-grained error messages from providers,
// we can provide useful diagnostics here.
return ""
}
func renderDiffResourcePreEvent(
payload engine.ResourcePreEventPayload,
seen map[resource.URN]engine.StepEventMetadata,
opts Options) string {
seen[payload.Metadata.URN] = payload.Metadata
if payload.Metadata.Op == deploy.OpRefresh {
return ""
}
out := &bytes.Buffer{}
if shouldShow(payload.Metadata, opts) || isRootStack(payload.Metadata) {
indent := engine.GetIndent(payload.Metadata, seen)
summary := engine.GetResourcePropertiesSummary(payload.Metadata, indent)
details := engine.GetResourcePropertiesDetails(
payload.Metadata, indent, payload.Planning, opts.SummaryDiff, payload.Debug)
fprintIgnoreError(out, opts.Color.Colorize(summary))
fprintIgnoreError(out, opts.Color.Colorize(details))
fprintIgnoreError(out, opts.Color.Colorize(colors.Reset))
}
return out.String()
}
func renderDiffResourceOutputsEvent(
payload engine.ResourceOutputsEventPayload,
seen map[resource.URN]engine.StepEventMetadata,
opts Options) string {
out := &bytes.Buffer{}
if shouldShow(payload.Metadata, opts) || isRootStack(payload.Metadata) {
indent := engine.GetIndent(payload.Metadata, seen)
refresh := false // are these outputs from a refresh?
if m, has := seen[payload.Metadata.URN]; has && m.Op == deploy.OpRefresh {
refresh = true
summary := engine.GetResourcePropertiesSummary(payload.Metadata, indent)
fprintIgnoreError(out, opts.Color.Colorize(summary))
}
if !opts.SuppressOutputs {
if text := engine.GetResourceOutputsPropertiesString(
payload.Metadata, indent+1, payload.Planning, payload.Debug, refresh); text != "" {
fprintfIgnoreError(out, "%v%v--outputs:--%v\n",
payload.Metadata.Op.Color(), engine.GetIndentationString(indent+1), colors.Reset)
fprintIgnoreError(out, opts.Color.Colorize(text))
}
}
}
return out.String()
}
// isRootStack returns true if the step pertains to the rootmost stack component.
func isRootStack(step engine.StepEventMetadata) bool {
return isRootURN(step.URN)
}
func isRootURN(urn resource.URN) bool {
return urn != "" && urn.Type() == resource.RootStackType
}
// shouldShow returns true if a step should show in the output.
func shouldShow(step engine.StepEventMetadata, opts Options) bool {
// For certain operations, whether they are tracked is controlled by flags (to cut down on superfluous output).
if step.Op == deploy.OpSame {
// If the op is the same, it is possible that the resource's metadata changed. In that case, still show it.
if step.Old.Protect != step.New.Protect {
return true
}
return opts.ShowSameResources
}
return true
}
func fprintfIgnoreError(w io.Writer, format string, a ...interface{}) {
_, err := fmt.Fprintf(w, format, a...)
contract.IgnoreError(err)
}
func fprintIgnoreError(w io.Writer, a ...interface{}) {
_, err := fmt.Fprint(w, a...)
contract.IgnoreError(err)
}