forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversions.go
256 lines (220 loc) · 8.92 KB
/
versions.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package upgradetest
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/elastic/elastic-agent/pkg/testing/define"
"github.com/elastic/elastic-agent/pkg/version"
)
var (
// Version_8_2_0 is the first version to include --non-interactive flag during install
Version_8_2_0 = version.NewParsedSemVer(8, 2, 0, "", "")
// Version_8_6_0 is the first version to use agent v2 protocol
Version_8_6_0 = version.NewParsedSemVer(8, 6, 0, "", "")
// Version_8_7_0 is the minimum version for passing --skip-verify when upgrading
Version_8_7_0 = version.NewParsedSemVer(8, 7, 0, "", "")
// Version_8_9_0_SNAPSHOT is the minimum version for upgrade to specific snapshot + minimum version
// for setting shorter watch period after upgrade
Version_8_9_0_SNAPSHOT = version.NewParsedSemVer(8, 9, 0, "SNAPSHOT", "")
// Version_8_10_0_SNAPSHOT is the minimum version for upgrade with remote pgp and skipping
// default pgp verification
Version_8_10_0_SNAPSHOT = version.NewParsedSemVer(8, 10, 0, "SNAPSHOT", "")
// Version_8_11_0_SNAPSHOT is the minimum version for uninstall command to kill the watcher upon uninstall
Version_8_11_0_SNAPSHOT = version.NewParsedSemVer(8, 11, 0, "SNAPSHOT", "")
// Version_8_13_0_SNAPSHOT is the minimum version for testing upgrading agent with the same hash
Version_8_13_0_SNAPSHOT = version.NewParsedSemVer(8, 13, 0, "SNAPSHOT", "")
// Version_8_14_0_SNAPSHOT is the minimum version for proper unprivileged execution on all platforms
Version_8_14_0_SNAPSHOT = version.NewParsedSemVer(8, 14, 0, "SNAPSHOT", "")
// ErrNoSnapshot is returned when a requested snapshot is not on the version list.
ErrNoSnapshot = errors.New("failed to find a snapshot on the version list")
// ErrNoPreviousMinor is returned when a requested previous minor is not on the version list.
ErrNoPreviousMinor = errors.New("failed to find a previous minor on the version list")
)
type VersionsFetcher interface {
FetchAgentVersions(ctx context.Context) (version.SortableParsedVersions, error)
}
type SnapshotFetcher interface {
FindLatestSnapshots(ctx context.Context, branches []string) (version.SortableParsedVersions, error)
}
// VersionRequirements is to set requirements for upgradable versions while fetching them.
//
// Keep in mind that requirements can overlap. For example 2 previous minors might already
// cover 2 current majors, so the results would be combined.
//
// `SnapshotBranches` is a list of active release branches used for finding latest snapshots on them.
// A branch might have no snapshot, in this case it's getting silently skipped.
type VersionRequirements struct {
UpgradeToVersion string
CurrentMajors int
PreviousMajors int
PreviousMinors int
SnapshotBranches []string
}
const AgentVersionsFilename = ".agent-versions.json"
type AgentVersions struct {
// TestVersions contains semver-compliant versions of the agent to run integration tests against.
TestVersions []string `json:"testVersions"`
}
var (
agentVersions *AgentVersions
)
func init() {
v, err := getAgentVersions()
if err != nil {
panic(err)
}
agentVersions = v
}
func getAgentVersions() (*AgentVersions, error) {
var (
filePath string
dir string
)
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get the current directory: %w", err)
}
dir = wd
for {
pathToCheck := filepath.Join(dir, AgentVersionsFilename)
fi, err := os.Stat(pathToCheck)
if (err == nil || os.IsExist(err)) && !fi.IsDir() {
filePath = pathToCheck
break
}
if strings.HasSuffix(dir, string(filepath.Separator)) {
return nil, fmt.Errorf("failed to find %s using working directory %s", AgentVersionsFilename, wd)
}
dir = filepath.Dir(dir)
}
f, err := os.OpenFile(filePath, os.O_RDONLY, 0)
if err != nil {
return nil, fmt.Errorf("failed to open %s: %w", filePath, err)
}
defer f.Close()
d := json.NewDecoder(f)
var versionFile AgentVersions
err = d.Decode(&versionFile)
if err != nil {
return nil, fmt.Errorf("failed to decode JSON in %s: %w", filePath, err)
}
return &versionFile, nil
}
// FetchUpgradableVersions returns the versions list from the agent version file.
func GetUpgradableVersions() ([]*version.ParsedSemVer, error) {
parsedVersions := make([]*version.ParsedSemVer, 0, len(agentVersions.TestVersions))
for _, v := range agentVersions.TestVersions {
parsed, err := version.ParseVersion(v)
if err != nil {
return nil, fmt.Errorf("failed to parse version %q from %s: %w", v, AgentVersionsFilename, err)
}
parsedVersions = append(parsedVersions, parsed)
}
return parsedVersions, nil
}
// FetchUpgradableVersions returns a list of versions that meet the specified requirements.
//
// Every version on the resulting list will meet the given requirements (by OR condition).
// However, it's not guaranteed that the list contains the amount of versions per requirement.
// For example, if only 2 previous minor versions exist but 5 requested, the list will have only 2.
func FetchUpgradableVersions(ctx context.Context, vf VersionsFetcher, sf SnapshotFetcher, reqs VersionRequirements) ([]string, error) {
releaseVersions, err := vf.FetchAgentVersions(ctx)
if err != nil {
return nil, fmt.Errorf("error retrieving release versions: %w", err)
}
if len(releaseVersions) == 0 {
return nil, errors.New("retrieved release versions list is empty")
}
snapshotVersions, err := sf.FindLatestSnapshots(ctx, reqs.SnapshotBranches)
if err != nil {
return nil, fmt.Errorf("error retrieving snapshot versions: %w", err)
}
if len(snapshotVersions) == 0 {
return nil, errors.New("retrieved snapshot versions list is empty")
}
allVersions := append(releaseVersions, snapshotVersions...)
// now sort the complete list
sort.Sort(sort.Reverse(allVersions))
return findRequiredVersions(allVersions, reqs)
}
// findRequiredVersions filters the version list according to the set requirements.
func findRequiredVersions(sortedParsedVersions []*version.ParsedSemVer, reqs VersionRequirements) ([]string, error) {
parsedUpgradeToVersion, err := version.ParseVersion(reqs.UpgradeToVersion)
if err != nil {
return nil, fmt.Errorf("upgradeToVersion %q is not a valid version string: %w", reqs.UpgradeToVersion, err)
}
upgradableVersions := make([]string, 0, reqs.CurrentMajors+reqs.PreviousMajors+reqs.PreviousMinors+len(reqs.SnapshotBranches))
currentMajor := parsedUpgradeToVersion.Major()
currentMinor := parsedUpgradeToVersion.Minor()
currentMajorsToFind := reqs.CurrentMajors
previousMajorsToFind := reqs.PreviousMajors
previousMinorsToFind := reqs.PreviousMinors
recentSnapshotsToFind := len(reqs.SnapshotBranches)
for _, version := range sortedParsedVersions {
switch {
// we skip version above the target
case !version.Less(*parsedUpgradeToVersion):
continue
case recentSnapshotsToFind > 0 && version.IsSnapshot():
upgradableVersions = append(upgradableVersions, version.String())
recentSnapshotsToFind--
// for the rest of the checks we capture only released versions
case version.Prerelease() != "" || version.BuildMetadata() != "":
continue
// previous minors
case previousMinorsToFind > 0 && version.Major() == currentMajor && version.Minor() < currentMinor:
upgradableVersions = append(upgradableVersions, version.String())
currentMinor = version.Minor() // so, we pick a lower minor next time
previousMinorsToFind--
currentMajorsToFind-- // counts as the current major as well
// current majors
case currentMajorsToFind > 0 && version.Major() == currentMajor:
upgradableVersions = append(upgradableVersions, version.String())
currentMajorsToFind--
// previous majors
case previousMajorsToFind > 0 && version.Major() < currentMajor:
upgradableVersions = append(upgradableVersions, version.String())
currentMajor = version.Major()
previousMajorsToFind--
// since the list is sorted we can stop here
default:
break
}
}
return upgradableVersions, nil
}
// PreviousMinor returns the previous minor version available for upgrade.
func PreviousMinor() (*version.ParsedSemVer, error) {
versions, err := GetUpgradableVersions()
if err != nil {
return nil, fmt.Errorf("failed to get upgradable versions: %w", err)
}
current, err := version.ParseVersion(define.Version())
if err != nil {
return nil, fmt.Errorf("failed to parse the current version %s: %w", define.Version(), err)
}
for _, v := range versions {
if v.Prerelease() != "" || v.BuildMetadata() != "" {
continue
}
if v.Major() == current.Major() && v.Minor() < current.Minor() {
return v, nil
}
}
return nil, ErrNoPreviousMinor
}
// EnsureSnapshot ensures that the version string is a snapshot version.
func EnsureSnapshot(version string) string {
if !strings.HasSuffix(version, "-SNAPSHOT") {
version += "-SNAPSHOT"
}
return version
}