-
Notifications
You must be signed in to change notification settings - Fork 124
/
admin.go
205 lines (178 loc) · 6.58 KB
/
admin.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
package data
import (
"context"
"reflect"
"time"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/model/event"
"github.com/evergreen-ci/evergreen/model/user"
restModel "github.com/evergreen-ci/evergreen/rest/model"
"github.com/evergreen-ci/evergreen/units"
"github.com/mongodb/amboy"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
func GetBanner(ctx context.Context) (string, string, error) {
settings, err := evergreen.GetConfig(ctx)
if err != nil {
return "", "", errors.Wrap(err, "retrieving admin settings from DB")
}
return settings.Banner, string(settings.BannerTheme), nil
}
// SetEvergreenSettings sets the admin settings document in the DB and event logs it
func SetEvergreenSettings(ctx context.Context, changes *restModel.APIAdminSettings,
oldSettings *evergreen.Settings, u *user.DBUser, persist bool) (*evergreen.Settings, error) {
settingsAPI := restModel.NewConfigModel()
err := settingsAPI.BuildFromService(oldSettings)
if err != nil {
return nil, errors.Wrap(err, "converting existing settings to API model")
}
changesReflect := reflect.ValueOf(*changes)
settingsReflect := reflect.ValueOf(settingsAPI)
//iterate over each field in the changes struct and apply any changes to the existing settings
for i := 0; i < changesReflect.NumField(); i++ {
// get the property name and find its value within the settings struct
propName := changesReflect.Type().Field(i).Name
changedVal := changesReflect.FieldByName(propName)
if changedVal.IsNil() {
continue
}
settingsReflect.Elem().FieldByName(propName).Set(changedVal)
}
i, err := settingsAPI.ToService()
if err != nil {
return nil, errors.Wrap(err, "converting settings to service model")
}
newSettings := i.(evergreen.Settings)
if persist {
// We have to call Validate before we attempt to persist it because the
// evergreen.Settings internally calls ValidateAndDefault to set the
// default values.
if err = newSettings.Validate(); err != nil {
return nil, errors.Wrap(err, "new admin settings are invalid")
}
err = evergreen.UpdateConfig(ctx, &newSettings)
if err != nil {
return nil, errors.Wrap(err, "saving new admin settings")
}
newSettings.Id = evergreen.ConfigDocID
return &newSettings, LogConfigChanges(&newSettings, oldSettings, u)
}
return &newSettings, nil
}
func LogConfigChanges(newSettings *evergreen.Settings, oldSettings *evergreen.Settings, u *user.DBUser) error {
catcher := grip.NewSimpleCatcher()
// log the other config sub-documents
valConfig := reflect.ValueOf(*newSettings)
var oldStruct reflect.Value
if oldSettings != nil {
oldStruct = reflect.ValueOf(*oldSettings)
}
//iterate over each field in the config struct
for i := 0; i < valConfig.NumField(); i++ {
// retrieve the 'id' struct tag
sectionId := valConfig.Type().Field(i).Tag.Get("id")
if sectionId == "" { // no 'id' tag means this is a simple field that we can skip
continue
}
// get the property name and find its value within the settings struct
propName := valConfig.Type().Field(i).Name
propVal := valConfig.FieldByName(propName)
// create a reflective copy of the struct
valPointer := reflect.Indirect(reflect.New(propVal.Type()))
valPointer.Set(propVal)
// convert the pointer to that struct to an empty interface
propInterface := valPointer.Addr().Interface()
// type assert to the ConfigSection interface
section, ok := propInterface.(evergreen.ConfigSection)
if !ok {
catcher.Errorf("converting config section '%s'", propName)
continue
}
// save changes in the event log
if oldSettings != nil {
field := oldStruct.FieldByName(propName)
oldPointer := reflect.Indirect(reflect.New(field.Type()))
oldPointer.Set(field)
oldInterface := oldPointer.Addr().Interface()
catcher.Add(event.LogAdminEvent(section.SectionId(), oldInterface.(evergreen.ConfigSection), section, u.Username()))
}
}
// log the root config document
catcher.Wrap(event.LogAdminEvent(newSettings.SectionId(), oldSettings, newSettings, u.Username()), "logging admin event for modifying admin settings")
return catcher.Resolve()
}
// SetBannerTheme sets the banner theme in the DB and event logs it
func SetBannerTheme(ctx context.Context, themeString string, u *user.DBUser) error {
valid, theme := evergreen.IsValidBannerTheme(themeString)
if !valid {
return errors.Errorf("invalid banner theme '%s'", themeString)
}
return evergreen.SetBannerTheme(ctx, theme)
}
// RestartFailedTasks attempts to restart failed tasks that started between 2 times
func RestartFailedTasks(ctx context.Context, queue amboy.Queue, opts model.RestartOptions) (*restModel.RestartResponse, error) {
var results model.RestartResults
var err error
if opts.DryRun {
results, err = model.RestartFailedTasks(ctx, opts)
if err != nil {
return nil, err
}
} else {
if err = queue.Put(context.TODO(), units.NewTasksRestartJob(opts)); err != nil {
return nil, errors.Wrap(err, "enqueueing job for task restart")
}
}
return &restModel.RestartResponse{
ItemsRestarted: results.ItemsRestarted,
ItemsErrored: results.ItemsErrored,
}, nil
}
// RestartFailedCommitQueueVersions takes in a time range
func RestartFailedCommitQueueVersions(opts model.RestartOptions) (*restModel.RestartResponse, error) {
totalRestarted := []string{}
totalNotRestarted := []string{}
projectIds, err := model.FindProjectRefIdsWithCommitQueueEnabled()
if err != nil {
return nil, errors.Wrap(err, "finding project refs with commit queue enabled")
}
for _, id := range projectIds {
restarted, notRestarted, err := model.RetryCommitQueueItems(id, opts)
if err != nil {
grip.Error(message.WrapError(err, message.Fields{
"project": id,
"start_time": opts.StartTime,
"end_time": opts.EndTime,
"message": "unable to restart failed commit queue versions for project",
}))
continue
}
totalRestarted = append(totalRestarted, restarted...)
totalNotRestarted = append(totalNotRestarted, notRestarted...)
}
return &restModel.RestartResponse{
ItemsRestarted: totalRestarted,
ItemsErrored: totalNotRestarted,
}, nil
}
func GetAdminEventLog(before time.Time, n int) ([]restModel.APIAdminEvent, error) {
events, err := event.FindAdmin(event.AdminEventsBefore(before, n))
if err != nil {
return nil, err
}
out := []restModel.APIAdminEvent{}
catcher := grip.NewBasicCatcher()
for _, evt := range events {
apiEvent := restModel.APIAdminEvent{}
err = apiEvent.BuildFromService(evt)
if err != nil {
catcher.Add(err)
continue
}
out = append(out, apiEvent)
}
return out, catcher.Resolve()
}