/
model.go
422 lines (373 loc) · 12.1 KB
/
model.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package operations
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/evergreen-ci/evergreen/rest/client"
"github.com/kardianos/osext"
"github.com/mitchellh/go-homedir"
"github.com/mongodb/grip"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
const localConfigPath = ".evergreen.local.yml"
type ClientProjectConf struct {
Name string `json:"name" yaml:"name,omitempty"`
Default bool `json:"default" yaml:"default,omitempty"`
Alias string `json:"alias" yaml:"alias,omitempty"`
Variants []string `json:"variants" yaml:"variants,omitempty"`
Tasks []string `json:"tasks" yaml:"tasks,omitempty"`
Parameters map[string]string `json:"parameters" yaml:"parameters,omitempty"`
ModulePaths map[string]string `json:"module_paths" yaml:"module_paths,omitempty"`
TriggerAliases []string `json:"trigger_aliases" yaml:"trigger_aliases"`
LocalAliases []model.ProjectAlias `json:"local_aliases,omitempty" yaml:"local_aliases,omitempty"`
}
func findConfigFilePath(fn string) (string, error) {
currentBinPath, _ := osext.Executable()
userHome, err := homedir.Dir()
if err != nil {
// workaround for cygwin if we're on windows but couldn't get a homedir
if runtime.GOOS == "windows" && len(os.Getenv("HOME")) > 0 {
userHome = os.Getenv("HOME")
}
}
if fn != "" {
if isValidPath(fn) {
return fn, nil
}
absfn, _ := filepath.Abs(fn)
if isValidPath(absfn) {
return absfn, nil
}
}
defaultFiles := []string{
filepath.Join(userHome, evergreen.DefaultEvergreenConfig),
filepath.Join(filepath.Dir(currentBinPath), evergreen.DefaultEvergreenConfig),
}
for _, path := range defaultFiles {
if isValidPath(path) {
grip.WarningWhen(fn != "", "Couldn't find configuration file, falling back on default.")
return path, nil
}
}
return "", errors.New("could not find client configuration file on the local system")
}
func isValidPath(path string) bool {
stat, err := os.Stat(path)
if os.IsNotExist(err) || stat.IsDir() {
return false
}
return true
}
// Client represents the data stored in the user's config file, by default
// located at ~/.evergreen.yml
// If you change the JSON tags, you must also change an anonymous struct in hostinit/setup.go
type ClientSettings struct {
APIServerHost string `json:"api_server_host" yaml:"api_server_host,omitempty"`
UIServerHost string `json:"ui_server_host" yaml:"ui_server_host,omitempty"`
APIKey string `json:"api_key" yaml:"api_key,omitempty"`
User string `json:"user" yaml:"user,omitempty"`
UncommittedChanges bool `json:"patch_uncommitted_changes" yaml:"patch_uncommitted_changes,omitempty"`
AutoUpgradeCLI bool `json:"auto_upgrade_cli" yaml:"auto_upgrade_cli,omitempty"`
PreserveCommits bool `json:"preserve_commits" yaml:"preserve_commits,omitempty"`
Projects []ClientProjectConf `json:"projects" yaml:"projects,omitempty"`
LoadedFrom string `json:"-" yaml:"-"`
DisableAutoDefaulting bool `json:"disable_auto_defaulting" yaml:"disable_auto_defaulting"`
ProjectsForDirectory map[string]string `json:"projects_for_directory,omitempty" yaml:"projects_for_directory,omitempty"`
}
func NewClientSettings(fn string) (*ClientSettings, error) {
path, err := findConfigFilePath(fn)
if err != nil {
return nil, errors.Wrapf(err, "finding config file '%s'", fn)
}
data, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrapf(err, "reading configuration from file '%s'", path)
}
conf := &ClientSettings{}
if err = yaml.Unmarshal(data, conf); err != nil {
return nil, errors.Wrapf(err, "reading YAML data from configuration file '%s'", path)
}
conf.LoadedFrom = path
localData, err := os.ReadFile(localConfigPath)
if os.IsNotExist(err) {
return conf, nil
} else if err != nil {
return nil, errors.Wrapf(err, "reading local configuration from file '%s'", localConfigPath)
}
// Unmarshalling into the same struct will only override fields which are set
// in the new YAML
if err = yaml.Unmarshal(localData, conf); err != nil {
return nil, errors.Wrapf(err, "unmarshalling YAML data from local configuration file '%s'", localConfigPath)
}
return conf, nil
}
func (s *ClientSettings) Write(fn string) error {
if fn == "" {
if s.LoadedFrom != "" {
fn = s.LoadedFrom
}
}
if fn == "" {
return errors.New("no output location specified")
}
yamlData, err := yaml.Marshal(s)
if err != nil {
return errors.Wrap(err, "marshalling data to write")
}
return errors.Wrapf(os.WriteFile(fn, yamlData, 0644), "writing file '%s'", fn)
}
// setupRestCommunicator returns the rest communicator and prints any available info messages if set.
// Callers are responsible for calling (Communicator).Close() when finished with the client.
// We want to avoid printing messages if output is requested in a specific format or silenced.
func (s *ClientSettings) setupRestCommunicator(ctx context.Context, printMessages bool) (client.Communicator, error) {
c, err := client.NewCommunicator(s.APIServerHost)
if err != nil {
return nil, errors.Wrap(err, "getting REST communicator")
}
c.SetAPIUser(s.User)
c.SetAPIKey(s.APIKey)
if printMessages {
printUserMessages(ctx, c, !s.AutoUpgradeCLI)
}
return c, nil
}
// printUserMessages prints any available info messages.
func printUserMessages(ctx context.Context, c client.Communicator, checkForUpdate bool) {
banner, err := c.GetBannerMessage(ctx)
if err != nil {
grip.Debug(err)
} else if len(banner) > 0 {
grip.Noticef("Banner: %s", banner)
}
if checkForUpdate {
update, err := checkUpdate(c, true, false)
if err != nil {
grip.Debug(err)
}
if update.needsUpdate {
if runtime.GOOS == "windows" {
fmt.Fprintf(os.Stderr, "A new version is available. Run '%s get-update' to fetch it.\n", os.Args[0])
} else {
fmt.Fprintf(os.Stderr, "A new version is available. Run '%s get-update --install' to download and install it.\n", os.Args[0])
}
}
}
}
func (s *ClientSettings) getLegacyClients() (*legacyClient, *legacyClient, error) {
// create client for the REST APIs
apiURL, err := url.Parse(s.APIServerHost)
if err != nil {
return nil, nil, errors.Wrap(err, "parsing API server URL from settings file")
}
ac := &legacyClient{
APIRoot: s.APIServerHost,
APIRootV2: s.APIServerHost + "/rest/v2",
User: s.User,
APIKey: s.APIKey,
UIRoot: s.UIServerHost,
}
rc := &legacyClient{
APIRoot: apiURL.Scheme + "://" + apiURL.Host + "/rest/v1",
APIRootV2: apiURL.Scheme + "://" + apiURL.Host + "/rest/v2",
User: s.User,
APIKey: s.APIKey,
UIRoot: s.UIServerHost,
}
return ac, rc, nil
}
func (s *ClientSettings) getModule(patchId, moduleName string) (*model.Module, error) {
_, rc, err := s.getLegacyClients()
if err != nil {
return nil, errors.Wrap(err, "setting up legacy Evergreen client")
}
proj, err := rc.GetPatchedConfig(patchId)
if err != nil {
return nil, err
}
const helpText = "Note: In order to set a module, you need to be in the directory for the module project, not the directory for the project that the module is being applied onto."
if len(proj.Modules) == 0 {
return nil, errors.Errorf("Project has no configured modules. Specify different project or "+
"see the evergreen configuration file for module configuration.\n %s", helpText)
}
module, err := model.GetModuleByName(proj.Modules, moduleName)
if err != nil {
moduleNames := []string{}
for _, m := range proj.Modules {
moduleNames = append(moduleNames, m.Name)
}
return nil, errors.Errorf("Could not find module named '%s' for project; specify different project or select correct module from:\n\t%s\n%s",
moduleName, strings.Join(moduleNames, "\n\t"), helpText)
}
return module, nil
}
func (s *ClientSettings) FindDefaultProject(cwd string, useRoot bool) string {
if project, exists := s.ProjectsForDirectory[cwd]; exists {
return project
}
if useRoot {
for _, p := range s.Projects {
if p.Default {
return p.Name
}
}
}
return ""
}
func (s *ClientSettings) getModulePath(project, moduleName string) string {
var modulePath string
for _, p := range s.Projects {
if p.Name == project && p.ModulePaths[moduleName] != "" {
modulePath = p.ModulePaths[moduleName]
break
}
}
return modulePath
}
func (s *ClientSettings) setModulePath(project, moduleName, modulePath string) {
if s.DisableAutoDefaulting {
return
}
grip.Infof("Project module '%s' will be set to use path '%s'. "+
"To disable automatic defaulting, set 'disable_auto_defaulting' to true.", moduleName, modulePath)
for i, p := range s.Projects {
if p.Name == project {
s.Projects[i].ModulePaths[moduleName] = modulePath
return
}
}
s.Projects = append(s.Projects, ClientProjectConf{
Name: project,
ModulePaths: map[string]string{moduleName: modulePath},
})
}
func (s *ClientSettings) FindDefaultVariants(project string) []string {
for _, p := range s.Projects {
if p.Name == project {
return p.Variants
}
}
return nil
}
func (s *ClientSettings) SetDefaultVariants(project string, variants ...string) {
for i, p := range s.Projects {
if p.Name == project {
s.Projects[i].Variants = variants
return
}
}
s.Projects = append(s.Projects, ClientProjectConf{
Name: project,
Default: true,
Alias: "",
Variants: variants,
Tasks: nil,
})
}
func (s *ClientSettings) FindDefaultTriggerAliases(project string) []string {
for _, p := range s.Projects {
if p.Name == project {
return p.TriggerAliases
}
}
return nil
}
func (s *ClientSettings) SetDefaultTriggerAliases(project string, triggerAliases []string) {
for i, p := range s.Projects {
if p.Name == project {
s.Projects[i].TriggerAliases = triggerAliases
return
}
}
s.Projects = append(s.Projects, ClientProjectConf{
Name: project,
Default: true,
TriggerAliases: triggerAliases,
})
}
func (s *ClientSettings) FindDefaultParameters(project string) []patch.Parameter {
for _, p := range s.Projects {
if p.Name == project {
return parametersFromMap(p.Parameters)
}
}
return nil
}
func parametersFromMap(params map[string]string) []patch.Parameter {
res := []patch.Parameter{}
for key, val := range params {
res = append(res, patch.Parameter{Key: key, Value: val})
}
return res
}
func (s *ClientSettings) FindDefaultTasks(project string) []string {
for _, p := range s.Projects {
if p.Name == project {
return p.Tasks
}
}
return nil
}
func (s *ClientSettings) SetDefaultTasks(project string, tasks ...string) {
for i, p := range s.Projects {
if p.Name == project {
s.Projects[i].Tasks = tasks
return
}
}
s.Projects = append(s.Projects, ClientProjectConf{
Name: project,
Default: true,
Alias: "",
Variants: nil,
Tasks: tasks,
})
}
func (s *ClientSettings) FindDefaultAlias(project string) string {
for _, p := range s.Projects {
if p.Name == project {
return p.Alias
}
}
return ""
}
func (s *ClientSettings) SetDefaultAlias(project string, alias string) {
for i, p := range s.Projects {
if p.Name == project {
s.Projects[i].Alias = alias
return
}
}
s.Projects = append(s.Projects, ClientProjectConf{
Name: project,
Default: true,
Alias: alias,
Variants: nil,
Tasks: nil,
})
}
func (s *ClientSettings) SetDefaultProject(cwd, project string) {
if s.DisableAutoDefaulting {
return
}
_, found := s.ProjectsForDirectory[cwd]
if found {
return
}
if s.ProjectsForDirectory == nil {
s.ProjectsForDirectory = map[string]string{}
}
s.ProjectsForDirectory[cwd] = project
grip.Infof("Project '%s' will be set as the one to use for directory '%s'. To disable automatic defaulting, set 'disable_auto_defaulting' to true.", project, cwd)
}
func (s *ClientSettings) SetAutoUpgradeCLI() {
s.AutoUpgradeCLI = true
grip.Info("Evergreen CLI will be automatically updated and installed before each command if a more recent version is detected.")
}