/
scaleset.go
157 lines (141 loc) · 4.78 KB
/
scaleset.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
package diagnostics
import (
"context"
"crypto/rsa"
"fmt"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/openshift/openshift-azure/pkg/api"
"github.com/openshift/openshift-azure/pkg/util/azureclient"
"github.com/openshift/openshift-azure/pkg/util/azureclient/compute"
"github.com/openshift/openshift-azure/pkg/util/azureclient/insights"
)
type scalesetDebugger struct {
resourceGroup string
subscriptionID string
testConfig api.TestConfig
log *logrus.Entry
ssc compute.VirtualMachineScaleSetsClient
}
// NewScalesetDebugger create a new scaleset debugger
func NewScalesetDebugger(log *logrus.Entry, subscriptionID, resourceGroup string, testConfig api.TestConfig, ssc compute.VirtualMachineScaleSetsClient) *scalesetDebugger {
return &scalesetDebugger{
log: log,
resourceGroup: resourceGroup,
subscriptionID: subscriptionID,
testConfig: testConfig,
ssc: ssc,
}
}
func (ssd *scalesetDebugger) GatherHostLogs(ctx context.Context, ssName string, vmCount int64, sshkey *rsa.PrivateKey) {
s, err := NewSSHer(ctx, ssd.log, ssd.subscriptionID, ssd.resourceGroup, sshkey)
if err != nil {
ssd.log.Warnf("NewSSHer err %v", err)
return
}
for i := int64(0); i < vmCount; i++ {
hostname := fmt.Sprintf("%s-%06s", ssName[3:], strconv.FormatInt(i, 36))
cli, err := s.Dial(ctx, hostname)
if err != nil {
ssd.log.Warnf("Dial failed: %v", err)
continue
}
err = s.RunRemoteCommandAndSaveToFile(cli, "sudo journalctl", ssd.testConfig.ArtifactDir+"/"+hostname+"-early-journal")
if err != nil {
ssd.log.Warnf("RunRemoteCommandAndSaveToFile failed: %v", err)
continue
}
err = s.RunRemoteCommandAndSaveToFile(cli, "sudo cat /var/lib/waagent/custom-script/download/1/stdout", ssd.testConfig.ArtifactDir+"/"+hostname+"-waagent-stdout")
if err != nil {
ssd.log.Warnf("RunRemoteCommandAndSaveToFile failed: %v", err)
continue
}
err = s.RunRemoteCommandAndSaveToFile(cli, "sudo cat /var/lib/waagent/custom-script/download/1/stderr", ssd.testConfig.ArtifactDir+"/"+hostname+"-waagent-stderr")
if err != nil {
ssd.log.Warnf("RunRemoteCommandAndSaveToFile failed: %v", err)
continue
}
}
}
func (ssd *scalesetDebugger) GatherActivityLogs(ctx context.Context) {
authorizer, err := azureclient.GetAuthorizerFromContext(ctx, api.ContextKeyClientAuthorizer)
if err != nil {
ssd.log.Warnf("authorizer err %v", err)
return
}
startTime := time.Now().Add(time.Duration(-1) * time.Hour)
alc := insights.NewActivityLogsClient(ctx, ssd.log, ssd.subscriptionID, authorizer)
logs, err := alc.List(ctx, fmt.Sprintf("eventTimestamp ge '%s' and resourceGroupName eq '%s'", startTime.Format(time.RFC3339), ssd.resourceGroup), "eventName,id,operationName,status")
if err != nil {
ssd.log.Warnf("alc.List err %v", err)
return
}
logFile, err := os.OpenFile(path.Join(ssd.testConfig.ArtifactDir, "activity.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
ssd.log.Warnf("os.OpenFile err %v", err)
return
}
defer logFile.Close()
for logs.NotDone() {
for _, log := range logs.Values() {
lb, err := log.MarshalJSON()
if err == nil {
fmt.Fprintf(logFile, "%v\n", string(lb))
}
}
err = logs.Next()
if err != nil {
break
}
}
}
func (ssd *scalesetDebugger) GatherStatuses(ctx context.Context, ssName string) {
ss, err := ssd.ssc.Get(ctx, ssd.resourceGroup, ssName)
if err != nil {
ssd.log.Warnf("ssc.Get err %v", err)
return
}
if ssd.subscriptionID == "" {
ssd.subscriptionID = strings.Split(*ss.ID, "/")[2]
}
ssd.log.Debugf("%s status:%s, provisioning state:%s", *ss.Name, ss.Status, *ss.VirtualMachineScaleSetProperties.ProvisioningState)
instView, err := ssd.ssc.GetInstanceView(ctx, ssd.resourceGroup, *ss.Name)
if err != nil {
ssd.log.Warnf("GetInstanceView err %v", err)
return
}
logFile, err := os.OpenFile(path.Join(ssd.testConfig.ArtifactDir, ssName+"-statuses.log"), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
ssd.log.Warnf("os.OpenFile err %v", err)
return
}
defer logFile.Close()
if instView.Statuses != nil {
for _, status := range *instView.Statuses {
var ds, msg string
if status.DisplayStatus != nil {
ds = *status.DisplayStatus
}
if status.Message != nil {
msg = *status.Message
}
fmt.Fprintf(logFile, "Statuses: displayStatus:%s, msg:%s\n", ds, msg)
}
}
if instView.VirtualMachine != nil {
for _, status := range *instView.VirtualMachine.StatusesSummary {
fmt.Fprintf(logFile, "VirtualMachine: code:%s count:%d\n", *status.Code, *status.Count)
}
}
if instView.Extensions != nil {
for _, extView := range *instView.Extensions {
for _, status := range *extView.StatusesSummary {
fmt.Fprintf(logFile, "Extensions: %s, status: %s %d\n", *extView.Name, *status.Code, *status.Count)
}
}
}
}