-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionplan.go
More file actions
318 lines (276 loc) · 14.9 KB
/
actionplan.go
File metadata and controls
318 lines (276 loc) · 14.9 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
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
package actionplan
import (
"encoding/json"
"fmt"
"math"
"sort"
"strings"
"github.com/Azure/VMApplication-Extension/internal/hostgacommunicator"
"github.com/Azure/VMApplication-Extension/internal/packageregistry"
"github.com/Azure/VMApplication-Extension/pkg/utils"
"github.com/Azure/azure-extension-platform/pkg/commandhandler"
"github.com/Azure/azure-extension-platform/pkg/constants"
"github.com/Azure/azure-extension-platform/pkg/extensionerrors"
"github.com/Azure/azure-extension-platform/pkg/extensionevents"
"github.com/Azure/azure-extension-platform/pkg/handlerenv"
"github.com/Azure/azure-extension-platform/pkg/logging"
vmextensionhelper "github.com/Azure/azure-extension-platform/vmextension"
)
type action struct {
// vmAppPackage is not a pointer type on purpose, we don't want it to be mutated
vmAppPackage packageregistry.VMAppPackageCurrent
treatFailureAsDeploymentFailure bool
actionToPerform packageregistry.ActionEnum
}
const Success = "SUCCESS"
const Failed = "FAILED"
// when an update requires an implicit remove and uninstall, the install is dependent on the remove
// this data structure helps us skip the install if the remove fails
type dependentActions []*action
type ActionPlan struct {
environment *handlerenv.HandlerEnvironment
unorderedOperations []dependentActions
// we need to skip the actions that have a higher order number if applications in the lower order numbers fail
// this data structure helps us achieve it
orderedOperations map[int][]dependentActions
// we keep the user provided orders sorted order to look up the orderedOperations map
// remember to sort while initializing
sortedOrder []int
unorderedImplicitUninstalls []*action
hostGaCommunicator hostgacommunicator.IHostGaCommunicator
logger *logging.ExtensionLogger
}
type IResult interface {
ToJsonString() string
}
type StatusErrorMessage string
func (err StatusErrorMessage) ToJsonString() string {
return string(err)
}
type PackageOperationResults []PackageOperationResult
func (packageOperationResults *PackageOperationResults) ToJsonString() (message string) {
jsonBytes, err := json.Marshal(packageOperationResults)
if err != nil {
message = fmt.Sprintf("%v", packageOperationResults)
} else {
message = string(jsonBytes)
}
return
}
type PackageOperationResult struct {
PackageName string `json:"package"`
AppVersion string `json:"version"`
Operation string `json:"operation"`
Result string `json:"result"`
TreatFailureAsDeploymentFailure bool `json:"treatFailureAsDeploymentFailure"`
Timestamp string `json:"timestamp"`
}
func appendExecutionResult(executionResult *PackageOperationResults, act *action, err error) {
if err == nil {
*executionResult = append(*executionResult, PackageOperationResult{PackageName: act.vmAppPackage.ApplicationName, AppVersion: act.vmAppPackage.Version, Operation: act.actionToPerform.ToString(), Result: Success, TreatFailureAsDeploymentFailure: act.treatFailureAsDeploymentFailure})
} else {
*executionResult = append(*executionResult, PackageOperationResult{PackageName: act.vmAppPackage.ApplicationName, AppVersion: act.vmAppPackage.Version, Operation: act.actionToPerform.ToString(), Result: err.Error(), TreatFailureAsDeploymentFailure: act.treatFailureAsDeploymentFailure})
}
}
func appendExecutionResultExplicit(executionResult *PackageOperationResults, act *action, result string) {
*executionResult = append(*executionResult, PackageOperationResult{PackageName: act.vmAppPackage.ApplicationName, AppVersion: act.vmAppPackage.Version, Operation: act.actionToPerform.ToString(), Result: result, TreatFailureAsDeploymentFailure: act.treatFailureAsDeploymentFailure})
}
type failedDeploymentError struct {
appsWithTreatFailureAsDeploymentFailure []string
additionalErrorForContext error
}
func (err *failedDeploymentError) Error() string {
stringBuilder := strings.Builder{}
stringBuilder.WriteString("Install/Update failed for VMApps with 'TreatFailureAsDeploymentFailure' set to true:" + constants.NewLineCharacter)
stringBuilder.WriteString(strings.Join(err.appsWithTreatFailureAsDeploymentFailure, constants.NewLineCharacter))
stringBuilder.WriteString(constants.NewLineCharacter)
if err.additionalErrorForContext != nil {
// TODO: limit the length of all the errors
stringBuilder.WriteString(fmt.Sprintf("Additional errors: %s%s", err.additionalErrorForContext.Error(), constants.NewLineCharacter))
}
return stringBuilder.String()
}
func updateFailDeploymentError(failDeploymentError *failedDeploymentError, act *action, singleExecutionError error) *failedDeploymentError {
if singleExecutionError != nil && act.treatFailureAsDeploymentFailure && (act.actionToPerform == packageregistry.Install || act.actionToPerform == packageregistry.Update) {
if failDeploymentError == nil {
failDeploymentError = &failedDeploymentError{appsWithTreatFailureAsDeploymentFailure: []string{}}
}
failDeploymentError.appsWithTreatFailureAsDeploymentFailure = append(failDeploymentError.appsWithTreatFailureAsDeploymentFailure, act.vmAppPackage.ApplicationName)
}
return failDeploymentError
}
type ExecuteError struct {
failedDeploymentErr *failedDeploymentError
combinedExecuteErrors error
errorWithClarification vmextensionhelper.ErrorWithClarification
}
func (executeError *ExecuteError) GetCombinedExecuteError() error {
return executeError.combinedExecuteErrors
}
func (executeError *ExecuteError) GetErrorWithClarification() vmextensionhelper.ErrorWithClarification {
return executeError.errorWithClarification
}
func (executeError *ExecuteError) SetFailedDeploymentErr(err *failedDeploymentError) {
executeError.failedDeploymentErr = err
}
func (executeError *ExecuteError) SetCombinedExecuteErrors(errs error) {
executeError.combinedExecuteErrors = errs
}
func (exeucuteError *ExecuteError) update(act *action, singleExecutionError error, code ...vmextensionhelper.ErrorWithClarification) {
exeucuteError.failedDeploymentErr = updateFailDeploymentError(exeucuteError.failedDeploymentErr, act, singleExecutionError)
exeucuteError.combinedExecuteErrors = extensionerrors.CombineErrors(exeucuteError.combinedExecuteErrors, singleExecutionError)
if len(code) != 0 {
exeucuteError.errorWithClarification = vmextensionhelper.NewErrorWithClarification(code[0].ErrorCode, code[0].Err)
}
}
func (exeucuteError *ExecuteError) GetErrorIfDeploymentFailed() error {
if exeucuteError.failedDeploymentErr == nil {
return nil
}
exeucuteError.failedDeploymentErr.additionalErrorForContext = exeucuteError.combinedExecuteErrors
return exeucuteError.failedDeploymentErr
}
func New(currentPackageRegistry packageregistry.CurrentPackageRegistry, desiredVMAppCollection packageregistry.VMAppPackageIncomingCollection, environment *handlerenv.HandlerEnvironment, hostGaCommunicator hostgacommunicator.IHostGaCommunicator, logger *logging.ExtensionLogger) *ActionPlan {
actionPlan := &ActionPlan{
environment: environment,
unorderedOperations: make([]dependentActions, 0),
orderedOperations: make(map[int][]dependentActions),
sortedOrder: make([]int, 0),
unorderedImplicitUninstalls: make([]*action, 0),
hostGaCommunicator: hostGaCommunicator,
logger: logger,
}
// get list of previously existing applications that don't exist in the new configuration
packageRegistryIncoming := make(packageregistry.DesiredPackageRegistry)
packageRegistryIncoming.Populate(desiredVMAppCollection)
vmAppCurrentCollection := currentPackageRegistry.GetPackageCollection()
for _, vmAppCurrent := range vmAppCurrentCollection {
_, existsInNewConfiguration := packageRegistryIncoming[vmAppCurrent.ApplicationName]
if !existsInNewConfiguration {
if vmAppCurrent.OngoingOperation == packageregistry.Skipped {
// remove the package without from the registry without calling the remove command
logger.Info("Application %v not in incoming package collection. Cleaning up data for previously skipped installation.", vmAppCurrent.ApplicationName)
deleteAction := &action{*vmAppCurrent, false, packageregistry.Cleanup}
actionPlan.unorderedImplicitUninstalls = append(actionPlan.unorderedImplicitUninstalls, deleteAction)
} else {
logger.Info("Application %v not in incoming package collection. Marking to delete.", vmAppCurrent.ApplicationName)
deleteAction := &action{*vmAppCurrent, false, packageregistry.Remove}
actionPlan.unorderedImplicitUninstalls = append(actionPlan.unorderedImplicitUninstalls, deleteAction)
}
}
}
// second pass for updates and installs
for _, vmAppIncoming := range desiredVMAppCollection {
vmAppCurrent, exists := currentPackageRegistry[vmAppIncoming.ApplicationName]
if exists {
// updates
versionsEqual := utils.AreVersionsEqual(&vmAppCurrent.Version, &vmAppIncoming.Version)
if !versionsEqual {
if len(vmAppIncoming.UpdateCommand) == 0 {
// not the same version and there is no update command
logger.Info("Application %v has version %v, but %v is desired. No update command exists, so removing and adding",
vmAppCurrent.ApplicationName, vmAppCurrent.Version, vmAppIncoming.Version)
deleteAction := &action{*vmAppCurrent, false, packageregistry.RemoveForUpdate} // delete current and install incoming
installAction := &action{*packageregistry.VMAppPackageIncomingToVmAppPackageCurrent(vmAppIncoming), vmAppIncoming.TreatFailureAsDeploymentFailure, packageregistry.Install}
actionPlan.insertOperation(vmAppIncoming.Order, deleteAction, installAction)
} else {
logger.Info("Application %v has version %v, but %v is desired. Will call update.",
vmAppCurrent.ApplicationName, vmAppCurrent.Version, vmAppIncoming.Version)
updateAction := &action{*packageregistry.VMAppPackageIncomingToVmAppPackageCurrent(vmAppIncoming), vmAppIncoming.TreatFailureAsDeploymentFailure, packageregistry.Update}
actionPlan.insertOperation(vmAppIncoming.Order, updateAction)
}
} else if vmAppCurrent.NumRebootsOccurred > 0 {
logger.Info("Application %v with version %v already exists on system, but previous %v operation resulted in a reboot. Retrying operation because reboot behavior is set to 'Retry'",
vmAppCurrent.ApplicationName, vmAppCurrent.Version, vmAppCurrent.OngoingOperation.ToString())
// Pass in vmAppCurrent instead of vmAppIncoming since exact version already exists in registry and contains the number of reboots occurred so far
actionAfterReboot := &action{*vmAppCurrent, vmAppIncoming.TreatFailureAsDeploymentFailure, vmAppCurrent.OngoingOperation}
actionPlan.insertOperation(vmAppIncoming.Order, actionAfterReboot)
}
} else {
// installs
logger.Info("Application %v does not exist on the system. Installing", vmAppIncoming.ApplicationName)
installAction := &action{*packageregistry.VMAppPackageIncomingToVmAppPackageCurrent(vmAppIncoming), vmAppIncoming.TreatFailureAsDeploymentFailure, packageregistry.Install}
actionPlan.insertOperation(vmAppIncoming.Order, installAction)
}
}
sort.Ints(actionPlan.sortedOrder)
return actionPlan
}
func (actionPlan *ActionPlan) insertOperation(order *int, dependentActions1 ...*action) {
if order == nil {
actionPlan.unorderedOperations = append(actionPlan.unorderedOperations, dependentActions1)
} else {
orderedActions, present := actionPlan.orderedOperations[*order]
if present {
actionPlan.orderedOperations[*order] = append(orderedActions, dependentActions1)
} else {
actionPlan.orderedOperations[*order] = []dependentActions{dependentActions1}
actionPlan.sortedOrder = append(actionPlan.sortedOrder, *order)
}
}
}
func (actionPlan *ActionPlan) Execute(registryHandler packageregistry.IPackageRegistry, eem *extensionevents.ExtensionEventManager, commandHandler commandhandler.ICommandHandler) (*ExecuteError, IResult) {
var executeError *ExecuteError = &ExecuteError{failedDeploymentErr: nil, combinedExecuteErrors: nil, errorWithClarification: vmextensionhelper.ErrorWithClarification{}}
// registry will be mutated and written to disk so that we can keep track of all the actions that have happened
registry, err := registryHandler.GetExistingPackages()
if err != nil {
return executeError, StatusErrorMessage(err.Error())
}
executionResult := make(PackageOperationResults, 0)
// handle unordered implicit uninstalls
for _, act := range actionPlan.unorderedImplicitUninstalls {
ewc, newError := actionPlan.executeHelper(registryHandler, commandHandler, registry, act, eem)
appendExecutionResult(&executionResult, act, newError)
executeError.update(act, newError, ewc)
}
// handle ordered operations
var atLeastOneActionFailed = false
var actionFailedAtOrder = math.MaxInt32
for _, order := range actionPlan.sortedOrder {
actionsInTheSameOrder := actionPlan.orderedOperations[order]
for _, depActions := range actionsInTheSameOrder {
for _, act := range depActions {
// if we encountered and error in the past, skip all the operations for a higher order
if atLeastOneActionFailed && order > actionFailedAtOrder {
actionPlan.logger.Warn("Skipping application %v because a previous application failed", act.vmAppPackage.ApplicationName)
appName := act.vmAppPackage.ApplicationName
currentVmApp := act.vmAppPackage
registry[appName] = ¤tVmApp
currentVmApp.OngoingOperation = packageregistry.Skipped
currentVmApp.Result = "skipped, lower order operation failed"
appendExecutionResultExplicit(&executionResult, act, "Skipped, lower order operation failed")
err = registryHandler.WriteToDisk(registry)
if err != nil {
executeError.combinedExecuteErrors = extensionerrors.CombineErrors(executeError.combinedExecuteErrors, err)
return executeError, &executionResult
}
break
}
ewc, newError := actionPlan.executeHelper(registryHandler, commandHandler, registry, act, eem)
executeError.update(act, newError, ewc)
appendExecutionResult(&executionResult, act, newError)
if newError != nil {
actionPlan.logger.Warn("Application %v failed. Skipping the remaining applications", act.vmAppPackage.ApplicationName)
atLeastOneActionFailed = true
actionFailedAtOrder = order
// no need to execute the remaining dependent operations
break
}
}
}
}
// handle remaining unordered operations
for _, depActions := range actionPlan.unorderedOperations {
for _, act := range depActions {
ewc, newError := actionPlan.executeHelper(registryHandler, commandHandler, registry, act, eem)
executeError.update(act, newError, ewc)
appendExecutionResult(&executionResult, act, newError)
if newError != nil {
actionPlan.logger.Warn("Application %v failed. Skipping the remaining applications", act.vmAppPackage.ApplicationName)
break // will skip the remaining dependant actions
}
}
}
eem.SetPrefix("")
return executeError, &executionResult
}