-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathexecution_monitor.go
More file actions
174 lines (156 loc) · 6.01 KB
/
execution_monitor.go
File metadata and controls
174 lines (156 loc) · 6.01 KB
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
package commands
import (
"strings"
"time"
"net/url"
"code.cloudfoundry.org/cli/v8/cf/terminal"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/ui"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/util"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/baseclient"
"github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/models"
mtaclient "github.com/cloudfoundry-incubator/multiapps-cli-plugin/clients/mtaclient"
)
// ExecutionMonitor monitors execution of a process
type ExecutionMonitor struct {
mtaClient mtaclient.MtaClientOperations
reportedMessages map[int64]bool
commandName string
monitoringLocation string
operationID string
embed string
retries uint
}
func NewExecutionMonitorFromLocationHeader(commandName, location string, retries uint, reportedOperationMessages []*models.Message, mtaClient mtaclient.MtaClientOperations) *ExecutionMonitor {
operationID, embed := getMonitoringInformation(location)
return &ExecutionMonitor{
mtaClient: mtaClient,
reportedMessages: getAlreadyReportedOperationMessages(reportedOperationMessages),
commandName: commandName,
operationID: operationID,
embed: embed,
retries: retries,
}
}
func getMonitoringInformation(monitoringLocation string) (string, string) {
parsedURL, _ := url.Parse(monitoringLocation)
path := parsedURL.Path
parsedQuery, _ := url.ParseQuery(parsedURL.RawQuery)
return strings.Split(path, "operations/")[1], parsedQuery["embed"][0]
}
// NewExecutionMonitor creates a new execution monitor
func NewExecutionMonitor(commandName, operationID, embed string, retries uint, reportedOperationMessages []*models.Message, mtaClient mtaclient.MtaClientOperations) *ExecutionMonitor {
return &ExecutionMonitor{
mtaClient: mtaClient,
reportedMessages: getAlreadyReportedOperationMessages(reportedOperationMessages),
commandName: commandName,
operationID: operationID,
embed: embed,
retries: retries,
}
}
func getAlreadyReportedOperationMessages(reportedOperationMessages []*models.Message) map[int64]bool {
result := make(map[int64]bool)
for _, message := range reportedOperationMessages {
result[message.ID] = true
}
return result
}
func (m *ExecutionMonitor) Monitor() ExecutionStatus {
totalRetries := m.retries
for {
operation, err := m.mtaClient.GetMtaOperation(m.operationID, m.embed)
if err != nil {
ui.Failed("Could not get ongoing operation: %s", baseclient.NewClientError(err))
return Failure
}
m.reportOperationMessages(operation)
switch operation.State {
case models.StateRUNNING:
time.Sleep(3 * time.Second)
case models.StateFINISHED:
ui.Say("Process finished.")
m.reportCommandForDownloadOfProcessLogs(m.operationID)
return Success
case models.StateABORTED:
ui.Say("Process was aborted.")
m.reportCommandForDownloadOfProcessLogs(m.operationID)
return Failure
case models.StateERROR:
if canRetry(m.retries, operation) {
ui.Say("Proceeding with automatic retry... (%d of %d attempts left)", m.retries, totalRetries)
executeRetryAction(m)
continue
}
messageInError := findErrorMessage(operation.Messages)
if messageInError == nil {
ui.Failed("There is no error message for operation with ID %s", m.operationID)
return Failure
}
ui.Say("Process failed.")
m.reportAvaiableActions(m.operationID)
m.reportCommandForDownloadOfProcessLogs(m.operationID)
return Failure
case models.StateACTIONREQUIRED:
intermediatePhase, flag := getIntermediatePhaseAndFlag(m.commandName)
ui.Say("Process has entered %s phase. After testing your new deployment you can resume or abort the process.", intermediatePhase)
m.reportAvaiableActions(m.operationID)
ui.Say("Hint: Use the %q option of the %s command to skip this phase.", flag, m.commandName)
return Success
default:
ui.Failed("Process is in illegal state %s.", terminal.EntityNameColor(string(operation.State)))
return Failure
}
}
}
func getIntermediatePhaseAndFlag(commandName string) (string, string) {
//for backwards compatibility until the bg-deploy deprecation period expires
if commandName == "bg-deploy" {
return "validation", "--no-confirm"
}
return "testing", "--skip-testing-phase"
}
func canRetry(retries uint, operation *models.Operation) bool {
return retries > 0 && operation.ErrorType != models.ErrorTypeCONTENT
}
func executeRetryAction(executionMonitor *ExecutionMonitor) {
retryAction := newAction("retry", VerbosityLevelSILENT)
retryAction.Execute(executionMonitor.operationID, executionMonitor.mtaClient)
executionMonitor.retries--
}
func findErrorMessage(messages models.OperationMessages) *models.Message {
for _, message := range messages {
if message.Type == models.MessageTypeERROR {
return message
}
}
return nil
}
func (m *ExecutionMonitor) reportOperationMessages(operation *models.Operation) {
for _, message := range operation.Messages {
if m.reportedMessages[message.ID] {
continue
}
m.reportedMessages[message.ID] = true
ui.Say("%s", message.Text)
}
}
func (m *ExecutionMonitor) reportAvaiableActions(operationID string) {
actions, _ := m.mtaClient.GetOperationActions(operationID)
for _, action := range actions {
m.reportAvailableAction(action, operationID)
}
}
func (m *ExecutionMonitor) reportCommandForDownloadOfProcessLogs(operationID string) {
downloadProcessLogsCommand := DownloadMtaOperationLogsCommand{}
commandBuilder := util.NewCfCommandStringBuilder()
commandBuilder.SetName(downloadProcessLogsCommand.GetPluginCommand().Alias)
commandBuilder.AddOption(operationIDOpt, operationID)
ui.Say("Use \"%s\" to download the logs of the process.", commandBuilder.Build())
}
func (m *ExecutionMonitor) reportAvailableAction(action, operationID string) {
commandBuilder := util.NewCfCommandStringBuilder()
commandBuilder.SetName(m.commandName)
commandBuilder.AddOption(operationIDOpt, operationID)
commandBuilder.AddOption(actionOpt, action)
ui.Say("Use \"%s\" to %s the process.", commandBuilder.Build(), action)
}