/
checks.go
355 lines (314 loc) · 9.89 KB
/
checks.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package appliance
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"text/template"
"github.com/appgate/sdp-api-client-go/api/v20/openapi"
"github.com/appgate/sdpctl/pkg/util"
"github.com/hashicorp/go-version"
log "github.com/sirupsen/logrus"
)
func PrintDiskSpaceWarningMessage(out io.Writer, stats []openapi.StatsAppliancesListAllOfData, apiVersion int) {
p := util.NewPrinter(out, 4)
diskHeader := "Disk Usage"
if apiVersion >= 18 {
diskHeader += " (used / total)"
}
p.AddHeader("Name", diskHeader)
for _, a := range stats {
diskUsage := fmt.Sprintf("%v%%", a.GetDisk())
if v, ok := a.GetDiskInfoOk(); ok {
diskInfo := *v
used, total := diskInfo.GetUsed(), diskInfo.GetTotal()
percentUsed := (used / total) * 100
diskUsage = fmt.Sprintf("%.2f%% (%s / %s)", percentUsed, PrettyBytes(float64(used)), PrettyBytes(float64(total)))
}
p.AddLine(a.GetName(), diskUsage)
}
fmt.Fprint(out, "\nWARNING: Some appliances have very little space available\n\n")
p.Print()
fmt.Fprintln(out, `
Upgrading requires the upload and decompression of big images.
To avoid problems during the upgrade process it's recommended to
increase the space on those appliances.`)
}
func HasLowDiskSpace(stats []openapi.StatsAppliancesListAllOfData) []openapi.StatsAppliancesListAllOfData {
result := []openapi.StatsAppliancesListAllOfData{}
for _, s := range stats {
if s.GetDisk() >= 75 {
result = append(result, s)
}
}
return result
}
func applianceGroupDescription(appliances []openapi.Appliance) string {
functions := ActiveFunctions(appliances)
var funcs []string
for k, value := range functions {
if _, ok := functions[k]; ok && value {
funcs = append(funcs, k)
}
}
sort.Strings(funcs)
return strings.Join(funcs, ", ")
}
func unique(slice []openapi.Appliance) []openapi.Appliance {
keys := make(map[string]bool)
list := []openapi.Appliance{}
for _, entry := range slice {
if _, value := keys[entry.GetId()]; !value {
keys[entry.GetId()] = true
list = append(list, entry)
}
}
return list
}
func uniqueString(slice []string) []string {
keys := make(map[string]bool)
list := []string{}
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
const autoScalingWarning = `
{{ if .Template }}
There is an auto-scale template configured: {{ .Template.Name }}
{{end}}
Found {{ .Count }} auto-scaled gateway running version < 16:
{{range .Appliances}}
- {{.Name -}}
{{end}}
Make sure that the health check for those auto-scaled gateways is disabled.
Not disabling the health checks in those auto-scaled gateways could cause them to be deleted, breaking all the connections established with them.
`
func ShowAutoscalingWarningMessage(templateAppliance *openapi.Appliance, gateways []openapi.Appliance) (string, error) {
type stub struct {
Template *openapi.Appliance
Appliances []openapi.Appliance
Count int
}
data := stub{
Template: templateAppliance,
Appliances: unique(gateways),
Count: len(gateways),
}
t := template.Must(template.New("").Parse(autoScalingWarning))
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
return "", err
}
return tpl.String(), nil
}
type SkipUpgrade struct {
Reason string
Appliance openapi.Appliance
}
const (
SkipReasonNotPrepared = "appliance is not prepared for upgrade"
SkipReasonOffline = "appliance is offline"
SkipReasonFiltered = "filtered using the '--include' and/or '--exclude' flag"
SkipReasonAlreadyPrepared = "appliance is already prepared for upgrade with a higher or equal version"
SkipReasonUnsupportedUpgradePath = "Upgrading from version 6.0.0 to version 6.2.x is unsupported. Version 6.0.1 or later is required."
)
func (su SkipUpgrade) Error() string {
return fmt.Sprintf("%s: %s", su.Appliance.GetName(), su.Reason)
}
// CheckVersions will check if appliance versions are equal to the version being uploaded on all appliances
// Returns a slice of appliances that are not equal, a slice of appliances that have the same version and an error
func CheckVersions(ctx context.Context, stats openapi.StatsAppliancesList, appliances []openapi.Appliance, v *version.Version) ([]openapi.Appliance, []SkipUpgrade) {
skip := []SkipUpgrade{}
keep := []openapi.Appliance{}
for _, appliance := range appliances {
for _, stat := range stats.GetData() {
if stat.GetId() == appliance.GetId() {
statV, err := ParseVersionString(stat.GetVersion())
if err != nil {
log.Warn("failed to parse version from stats")
skip = append(skip, SkipUpgrade{
Appliance: appliance,
Reason: "failed to parse version from stats",
})
continue
}
res, err := CompareVersionsAndBuildNumber(statV, v)
if err != nil {
log.Warn("failed to compare versions")
skip = append(skip, SkipUpgrade{
Appliance: appliance,
Reason: "failed to compare versions",
})
continue
}
if res < 1 {
skip = append(skip, SkipUpgrade{
Appliance: appliance,
Reason: "appliance version is already greater or equal to prepare version",
})
continue
}
// Check version 6.0.0 -> 6.2.x upgrade, since it will fail
v600, _ := version.NewVersion("6.0.0")
v62, _ := version.NewVersion("6.2.0")
if statV.Equal(v600) && v.GreaterThanOrEqual(v62) {
skip = append(skip, SkipUpgrade{
Appliance: appliance,
Reason: SkipReasonUnsupportedUpgradePath,
})
continue
}
keep = append(keep, appliance)
}
}
}
return keep, skip
}
const (
IsLower = -1
IsEqual = 0
IsGreater = 1
)
// CompareVersionsAndBuildNumber compares two versions and returns the result with an integer representation
// -1 if y is lower than x
// 0 if versions match
// 1 if y is greater than x
func CompareVersionsAndBuildNumber(x, y *version.Version) (int, error) {
if x == nil || y == nil {
return 0, fmt.Errorf("Failed to compare versions, got nil version - x=%v, y=%v", x, y)
}
res := y.Compare(x)
// if res is 0, we also compare build number
// both x and y needs to have a parsable build number for this check to run
if res == IsEqual && len(y.Metadata()) > 0 && len(x.Metadata()) > 0 {
buildX, err := version.NewVersion(x.Metadata())
if err != nil {
return res, err
}
buildY, err := version.NewVersion(y.Metadata())
if err != nil {
return res, err
}
res = buildY.Compare(buildX)
}
return res, nil
}
// unknownStat is the response given by the appliance stats api if the appliance is offline.
const unknownStat = "unknown"
func HasDiffVersions(stats []openapi.StatsAppliancesListAllOfData) (bool, map[string]string) {
res := map[string]string{}
versionList := []string{}
for _, stat := range stats {
statVersionString := stat.GetVersion()
if statVersionString != unknownStat {
v, err := ParseVersionString(statVersionString)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"appliance": stat.GetName(),
"version": statVersionString,
}).Warn("failed to parse version string")
}
versionString := statVersionString
if v != nil {
versionString = v.String()
}
res[stat.GetName()] = versionString
versionList = append(versionList, versionString)
}
}
unique := uniqueString(versionList)
return len(unique) != 1, res
}
const (
MajorVersion = uint8(4)
MinorVersion = uint8(2)
PatchVersion = uint8(1)
)
func getUpgradeVersionType(x, y *version.Version) uint8 {
var patch, minor, major uint8
if x == nil || y == nil {
return 0
}
xSeg := x.Segments()
ySeg := y.Segments()
// Patch
if xSeg[2] < ySeg[2] {
patch = PatchVersion
}
// Minor
if xSeg[1] < ySeg[1] {
minor = MinorVersion
}
// Major
if xSeg[0] < ySeg[0] {
major = MajorVersion
}
return major | minor | patch
}
func IsMajorUpgrade(current, next *version.Version) bool {
return getUpgradeVersionType(current, next)&MajorVersion == MajorVersion
}
func IsMinorUpgrade(current, next *version.Version) bool {
return getUpgradeVersionType(current, next)&MinorVersion == MinorVersion
}
func IsPatchUpgrade(current, next *version.Version) bool {
return getUpgradeVersionType(current, next)&PatchVersion == PatchVersion
}
func controllerCount(appliances []openapi.Appliance) int {
i := 0
for _, a := range appliances {
if v, ok := a.GetControllerOk(); ok && v.GetEnabled() {
i++
}
}
return i
}
func NeedsMultiControllerUpgrade(upgradeStatuses map[string]UpgradeStatusResult, initialStatData []openapi.StatsAppliancesListAllOfData, all, preparing []openapi.Appliance, majorOrMinor bool) (bool, error) {
controllerCount := controllerCount(all)
controllerPrepareCount := 0
alreadySameVersion := 0
unpreparedCurrentVersions := []*version.Version{}
var highestPreparedVersion *version.Version
for _, a := range preparing {
if v, ok := a.GetControllerOk(); ok && v.GetEnabled() {
if ugs := upgradeStatuses[a.GetId()]; ugs.Status == UpgradeStatusReady {
preparedVersion, err := ParseVersionString(ugs.Details)
if err != nil {
return false, err
}
if res, _ := CompareVersionsAndBuildNumber(highestPreparedVersion, preparedVersion); highestPreparedVersion == nil || res >= 1 {
highestPreparedVersion = preparedVersion
}
controllerPrepareCount++
} else {
for _, data := range initialStatData {
if data.GetId() != a.GetId() {
continue
}
currentVersion, err := ParseVersionString(data.GetVersion())
if err != nil {
return false, err
}
unpreparedCurrentVersions = append(unpreparedCurrentVersions, currentVersion)
}
}
}
}
if controllerCount != controllerPrepareCount {
for _, uv := range unpreparedCurrentVersions {
if v, _ := CompareVersionsAndBuildNumber(highestPreparedVersion, uv); v == 0 {
alreadySameVersion++
}
}
}
return (controllerCount != controllerPrepareCount+alreadySameVersion) && majorOrMinor, nil
}
func SpecificVersionChecks(current, future *version.Version) error {
return nil
}