-
Notifications
You must be signed in to change notification settings - Fork 9
/
logging.go
155 lines (132 loc) · 4.05 KB
/
logging.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
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 pulumi
import (
"fmt"
"io"
"strings"
"time"
"github.com/pulumi/pulumi/sdk/v3/go/auto/debug"
"github.com/pulumi/pulumi/sdk/v3/go/auto/events"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optdestroy"
"github.com/pulumi/pulumi/sdk/v3/go/auto/optup"
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/nitrictech/cli/pkg/output"
)
func updateLoggingOpts(log output.Progress) []optup.Option {
upChannel := make(chan events.EngineEvent)
opts := []optup.Option{
optup.EventStreams(upChannel),
}
go collectEvents(log, upChannel, "Deploying.. ")
if output.VerboseLevel >= 2 {
piper, pipew := io.Pipe()
defer pipew.Close()
go output.StdoutToPtermDebug(piper, log, "Deploying.. ")
opts = append(opts, optup.ProgressStreams(pipew))
}
if output.VerboseLevel > 2 {
var loglevel uint = uint(output.VerboseLevel)
opts = append(opts, optup.DebugLogging(debug.LoggingOptions{
LogLevel: &loglevel,
LogToStdErr: true,
FlowToPlugins: true,
}))
}
return opts
}
func destroyLoggingOpts(log output.Progress) []optdestroy.Option {
upChannel := make(chan events.EngineEvent)
opts := []optdestroy.Option{
optdestroy.EventStreams(upChannel),
}
go collectEvents(log, upChannel, "Deleting.. ")
if output.VerboseLevel >= 2 {
piper, pipew := io.Pipe()
defer pipew.Close()
go output.StdoutToPtermDebug(piper, log, "Deleting.. ")
opts = append(opts, optdestroy.ProgressStreams(pipew))
}
if output.VerboseLevel > 2 {
var loglevel uint = uint(output.VerboseLevel)
opts = append(opts, optdestroy.DebugLogging(debug.LoggingOptions{
LogLevel: &loglevel,
LogToStdErr: true,
FlowToPlugins: true,
}))
}
return opts
}
func stepEventToString(eType string, evt *apitype.StepEventMetadata) string {
urnSplit := strings.Split(evt.URN, "::")
name := urnSplit[len(urnSplit)-1]
typeSplit := strings.Split(evt.Type, ":")
rType := typeSplit[len(typeSplit)-1]
return fmt.Sprintf("%s/%s", rType, name)
}
func collectEvents(log output.Progress, eventChannel <-chan events.EngineEvent, prefix string) {
busyList := map[string]time.Time{}
getBusyList := func() string {
ls := []string{}
for res := range busyList {
if len(ls) == 5 && len(busyList) > 5 {
ls = append(ls, "...")
break
}
ls = append(ls, res)
}
return prefix + strings.Join(ls, ",")
}
for {
var event events.EngineEvent
var ok bool
event, ok = <-eventChannel
if !ok {
return
}
if event.ResourcePreEvent != nil && event.ResourcePreEvent.Metadata.Op != apitype.OpSame {
lastCreating := stepEventToString("ResourcePreEvent", &event.ResourcePreEvent.Metadata)
busyList[lastCreating] = time.Now()
log.Busyf("%s", getBusyList())
}
if event.ResOutputsEvent != nil {
lc := stepEventToString("ResOutputsEvent", &event.ResOutputsEvent.Metadata)
if event.ResOutputsEvent.Metadata.Op == apitype.OpSame {
log.Debugf("%s\n", lc)
} else {
if st, ok := busyList[lc]; ok {
// if possible print out how long it took
d := time.Since(st).Round(time.Second)
log.Successf("%s (%s)\n", lc, d.String())
} else {
log.Successf("%s %t\n", lc, busyList[lc])
}
}
delete(busyList, lc)
if len(busyList) > 0 {
log.Busyf("%s", getBusyList())
}
}
if event.ResOpFailedEvent != nil {
lc := stepEventToString("ResOpFailedEvent", &event.ResOpFailedEvent.Metadata)
log.Failf("%s\n", lc)
delete(busyList, lc)
if len(busyList) > 0 {
log.Busyf("%s", getBusyList())
}
}
}
}