This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
deploymentError.go
113 lines (100 loc) · 3.78 KB
/
deploymentError.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
package armhelpers
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/Azure/dcos-engine/pkg/api"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
"github.com/sirupsen/logrus"
)
// DeploymentError contains the root deployment error along with deployment operation errors
type DeploymentError struct {
DeploymentName string
ResourceGroup string
TopError error
StatusCode int
Response []byte
ProvisioningState string
OperationsLists []resources.DeploymentOperationsListResult
}
// Error implements error interface
func (e *DeploymentError) Error() string {
var str string
if e.TopError != nil {
str = e.TopError.Error()
}
var ops []string
for _, operationsList := range e.OperationsLists {
if operationsList.Value == nil {
continue
}
for _, operation := range *operationsList.Value {
if operation.Properties != nil && *operation.Properties.ProvisioningState == string(api.Failed) && operation.Properties.StatusMessage != nil {
if b, err := json.MarshalIndent(operation.Properties.StatusMessage, "", " "); err == nil {
ops = append(ops, string(b))
}
}
}
}
return fmt.Sprintf("DeploymentName[%s] ResourceGroup[%s] TopError[%s] StatusCode[%d] Response[%s] ProvisioningState[%s] Operations[%s]",
e.DeploymentName, e.ResourceGroup, str, e.StatusCode, e.Response, e.ProvisioningState, strings.Join(ops, " | "))
}
// DeploymentValidationError contains validation error
type DeploymentValidationError struct {
Err error
}
// Error implements error interface
func (e *DeploymentValidationError) Error() string {
return e.Err.Error()
}
// DeployTemplateSync deploys the template and returns ArmError
func DeployTemplateSync(az ACSEngineClient, logger *logrus.Entry, resourceGroupName, deploymentName string, template map[string]interface{}, parameters map[string]interface{}) error {
deploymentExtended, err := az.DeployTemplate(resourceGroupName, deploymentName, template, parameters, nil)
if err == nil {
return nil
}
logger.Infof("Getting detailed deployment errors for %s", deploymentName)
deploymentErr := &DeploymentError{}
deploymentErr.DeploymentName = deploymentName
deploymentErr.ResourceGroup = resourceGroupName
deploymentErr.TopError = err
if deploymentExtended == nil {
logger.Warn("DeploymentExtended is nil")
return deploymentErr
}
// try to extract error from ARM Response
if deploymentExtended.Response.Response != nil && deploymentExtended.Body != nil {
buf := new(bytes.Buffer)
buf.ReadFrom(deploymentExtended.Body)
logger.Infof("StatusCode: %d, Error: %s", deploymentExtended.Response.StatusCode, buf.String())
deploymentErr.Response = buf.Bytes()
deploymentErr.StatusCode = deploymentExtended.Response.StatusCode
} else {
logger.Errorf("Got error from Azure SDK without response from ARM")
// This is the failed sdk validation before calling ARM path
return deploymentErr
}
if deploymentExtended.Properties == nil || deploymentExtended.Properties.ProvisioningState == nil {
logger.Warn("No resources.DeploymentExtended.Properties")
return deploymentErr
}
properties := deploymentExtended.Properties
deploymentErr.ProvisioningState = *properties.ProvisioningState
var top int32 = 1
res, err := az.ListDeploymentOperations(resourceGroupName, deploymentName, &top)
if err != nil {
logger.Errorf("unable to list deployment operations %s. error: %v", deploymentName, err)
return deploymentErr
}
deploymentErr.OperationsLists = append(deploymentErr.OperationsLists, res)
for res.NextLink != nil {
res, err = az.ListDeploymentOperationsNextResults(res)
if err != nil {
logger.Warningf("unable to list next deployment operations %s. error: %v", deploymentName, err)
break
}
deploymentErr.OperationsLists = append(deploymentErr.OperationsLists, res)
}
return deploymentErr
}