forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_db.go
More file actions
325 lines (287 loc) · 15.5 KB
/
config_db.go
File metadata and controls
325 lines (287 loc) · 15.5 KB
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
package evergreen
import (
"context"
"reflect"
"github.com/mongodb/anser/bsonutil"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
ConfigCollection = "admin"
ConfigDocID = "global"
)
//nolint:unused
var (
idKey = bsonutil.MustHaveTag(Settings{}, "Id")
bannerKey = bsonutil.MustHaveTag(Settings{}, "Banner")
bannerThemeKey = bsonutil.MustHaveTag(Settings{}, "BannerTheme")
serviceFlagsKey = bsonutil.MustHaveTag(Settings{}, "ServiceFlags")
configDirKey = bsonutil.MustHaveTag(Settings{}, "ConfigDir")
awsInstanceRoleKey = bsonutil.MustHaveTag(Settings{}, "AWSInstanceRole")
hostJasperKey = bsonutil.MustHaveTag(Settings{}, "HostJasper")
domainNameKey = bsonutil.MustHaveTag(Settings{}, "DomainName")
jiraKey = bsonutil.MustHaveTag(Settings{}, "Jira")
splunkKey = bsonutil.MustHaveTag(Settings{}, "Splunk")
slackKey = bsonutil.MustHaveTag(Settings{}, "Slack")
providersKey = bsonutil.MustHaveTag(Settings{}, "Providers")
kanopySSHKeyPathKey = bsonutil.MustHaveTag(Settings{}, "KanopySSHKeyPath")
authConfigKey = bsonutil.MustHaveTag(Settings{}, "AuthConfig")
repoTrackerConfigKey = bsonutil.MustHaveTag(Settings{}, "RepoTracker")
apiKey = bsonutil.MustHaveTag(Settings{}, "Api")
uiKey = bsonutil.MustHaveTag(Settings{}, "Ui")
hostInitConfigKey = bsonutil.MustHaveTag(Settings{}, "HostInit")
notifyKey = bsonutil.MustHaveTag(Settings{}, "Notify")
schedulerConfigKey = bsonutil.MustHaveTag(Settings{}, "Scheduler")
amboyKey = bsonutil.MustHaveTag(Settings{}, "Amboy")
expansionsKey = bsonutil.MustHaveTag(Settings{}, "Expansions")
expansionsNewKey = bsonutil.MustHaveTag(Settings{}, "ExpansionsNew")
pluginsKey = bsonutil.MustHaveTag(Settings{}, "Plugins")
pluginsNewKey = bsonutil.MustHaveTag(Settings{}, "PluginsNew")
loggerConfigKey = bsonutil.MustHaveTag(Settings{}, "LoggerConfig")
logPathKey = bsonutil.MustHaveTag(Settings{}, "LogPath")
oldestAllowedCLIVersionKey = bsonutil.MustHaveTag(Settings{}, "OldestAllowedCLIVersion")
pprofPortKey = bsonutil.MustHaveTag(Settings{}, "PprofPort")
perfMonitoringURLKey = bsonutil.MustHaveTag(Settings{}, "PerfMonitoringURL")
perfMonitoringKanopyURLKey = bsonutil.MustHaveTag(Settings{}, "PerfMonitoringKanopyURL")
githubPRCreatorOrgKey = bsonutil.MustHaveTag(Settings{}, "GithubPRCreatorOrg")
githubOrgsKey = bsonutil.MustHaveTag(Settings{}, "GithubOrgs")
githubWebhookSecretKey = bsonutil.MustHaveTag(Settings{}, "GithubWebhookSecret")
disabledGQLQueriesKey = bsonutil.MustHaveTag(Settings{}, "DisabledGQLQueries")
spawnhostKey = bsonutil.MustHaveTag(Settings{}, "Spawnhost")
shutdownWaitKey = bsonutil.MustHaveTag(Settings{}, "ShutdownWaitSeconds")
sshKey = bsonutil.MustHaveTag(Settings{}, "SSH")
// degraded mode flags
taskDispatchKey = bsonutil.MustHaveTag(ServiceFlags{}, "TaskDispatchDisabled")
hostInitKey = bsonutil.MustHaveTag(ServiceFlags{}, "HostInitDisabled")
podInitDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "PodInitDisabled")
largeParserProjectsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "LargeParserProjectsDisabled")
monitorKey = bsonutil.MustHaveTag(ServiceFlags{}, "MonitorDisabled")
alertsKey = bsonutil.MustHaveTag(ServiceFlags{}, "AlertsDisabled")
agentStartKey = bsonutil.MustHaveTag(ServiceFlags{}, "AgentStartDisabled")
repotrackerKey = bsonutil.MustHaveTag(ServiceFlags{}, "RepotrackerDisabled")
schedulerKey = bsonutil.MustHaveTag(ServiceFlags{}, "SchedulerDisabled")
checkBlockedTasksKey = bsonutil.MustHaveTag(ServiceFlags{}, "CheckBlockedTasksDisabled")
githubPRTestingDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "GithubPRTestingDisabled")
cliUpdatesDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "CLIUpdatesDisabled")
backgroundStatsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "BackgroundStatsDisabled")
eventProcessingDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "EventProcessingDisabled")
jiraNotificationsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "JIRANotificationsDisabled")
slackNotificationsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "SlackNotificationsDisabled")
emailNotificationsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "EmailNotificationsDisabled")
webhookNotificationsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "WebhookNotificationsDisabled")
githubStatusAPIDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "GithubStatusAPIDisabled")
taskLoggingDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "TaskLoggingDisabled")
cacheStatsJobDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "CacheStatsJobDisabled")
cacheStatsEndpointDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "CacheStatsEndpointDisabled")
taskReliabilityDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "TaskReliabilityDisabled")
hostAllocatorDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "HostAllocatorDisabled")
podAllocatorDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "PodAllocatorDisabled")
backgroundReauthDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "BackgroundReauthDisabled")
cloudCleanupDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "CloudCleanupDisabled")
unrecognizedPodCleanupDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "UnrecognizedPodCleanupDisabled")
sleepScheduleDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "SleepScheduleDisabled")
staticAPIKeysDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "StaticAPIKeysDisabled")
JWTTokenForCLIDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "JWTTokenForCLIDisabled")
systemFailedTaskRestartDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "SystemFailedTaskRestartDisabled")
cpuDegradedModeDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "CPUDegradedModeDisabled")
elasticIPsDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "ElasticIPsDisabled")
releaseModeDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "ReleaseModeDisabled")
legacyUIAdminPageDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "LegacyUIAdminPageDisabled")
debugSpawnHostDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "DebugSpawnHostDisabled")
s3LifecycleSyncDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "S3LifecycleSyncDisabled")
useMergeQueuePathFilteringDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "UseMergeQueuePathFilteringDisabled")
psLoggingDisabledKey = bsonutil.MustHaveTag(ServiceFlags{}, "PSLoggingDisabled")
// ContainerPoolsConfig keys
poolsKey = bsonutil.MustHaveTag(ContainerPoolsConfig{}, "Pools")
// ContainerPool keys
ContainerPoolIdKey = bsonutil.MustHaveTag(ContainerPool{}, "Id")
hostInitHostThrottleKey = bsonutil.MustHaveTag(HostInitConfig{}, "HostThrottle")
hostInitProvisioningThrottleKey = bsonutil.MustHaveTag(HostInitConfig{}, "ProvisioningThrottle")
hostInitCloudStatusBatchSizeKey = bsonutil.MustHaveTag(HostInitConfig{}, "CloudStatusBatchSize")
hostInitMaxTotalDynamicHostsKey = bsonutil.MustHaveTag(HostInitConfig{}, "MaxTotalDynamicHosts")
// Spawnhost keys
unexpirableHostsPerUserKey = bsonutil.MustHaveTag(SpawnHostConfig{}, "UnexpirableHostsPerUser")
unexpirableVolumesPerUserKey = bsonutil.MustHaveTag(SpawnHostConfig{}, "UnexpirableVolumesPerUser")
spawnhostsPerUserKey = bsonutil.MustHaveTag(SpawnHostConfig{}, "SpawnHostsPerUser")
tracerEnabledKey = bsonutil.MustHaveTag(TracerConfig{}, "Enabled")
tracerCollectorEndpointKey = bsonutil.MustHaveTag(TracerConfig{}, "CollectorEndpoint")
tracerCollectorInternalEndpointKey = bsonutil.MustHaveTag(TracerConfig{}, "CollectorInternalEndpoint")
tracerCollectorAPIKeyKey = bsonutil.MustHaveTag(TracerConfig{}, "CollectorAPIKey")
// GithubCheckRun keys
checkRunLimitKey = bsonutil.MustHaveTag(GitHubCheckRunConfig{}, "CheckRunLimit")
// SingleTaskDistro Keys
ProjectTasksPairsKey = bsonutil.MustHaveTag(SingleTaskDistroConfig{}, "ProjectTasksPairs")
// GraphiteConfig keys
graphiteCIOptimizationTokenKey = bsonutil.MustHaveTag(GraphiteConfig{}, "CIOptimizationToken")
graphiteServerURLKey = bsonutil.MustHaveTag(GraphiteConfig{}, "ServerURL")
// DebugSpawnHostsConfig keys
setupScriptKey = bsonutil.MustHaveTag(DebugSpawnHostsConfig{}, "SetupScript")
)
func byId(id string) bson.M {
return bson.M{idKey: id}
}
func byIDs(ids []string) bson.M {
return bson.M{idKey: bson.M{"$in": ids}}
}
// getSectionsBSON returns the config documents from the database as a slice of [bson.Raw].
// If no shared database exists all configuration is fetched from the [DB] database. If a
// [SharedDB] database exists, configuration is fetched from the [SharedDB] database, save for the
// overrides config document which is fetched from the [DB] database. If [SharedDB] database exists and
// [includeOverrides], the overrides in the [OverridesConfig] section are applied to the rest of the
// configuration.
func getSectionsBSON(ctx context.Context, ids []string, includeOverrides bool) ([]bson.Raw, error) {
db := GetEnvironment().DB()
if sharedDB := GetEnvironment().SharedDB(); sharedDB != nil {
db = sharedDB
}
cur, err := db.Collection(ConfigCollection).Find(ctx, byIDs(ids))
if err != nil {
return nil, errors.Wrap(err, "finding local configuration sections")
}
docs := make([]bson.Raw, 0, len(ids))
if err := cur.All(ctx, &docs); err != nil {
return nil, errors.Wrap(err, "iterating cursor for local configuration sections")
}
if GetEnvironment().SharedDB() != nil {
docs, err = overrideConfig(ctx, docs, includeOverrides)
if err != nil {
return nil, errors.Wrap(err, "overriding config")
}
}
return docs, nil
}
// overrideConfig replaces the content of the [OverridesConfig] section with the contents
// of the overrides document from the [DB] database. If [applyOverrides] is true overrides
// from the [OverridesConfig] are applied to all the supplied sections.
func overrideConfig(ctx context.Context, sections []bson.Raw, applyOverrides bool) ([]bson.Raw, error) {
res := GetEnvironment().DB().Collection(ConfigCollection).FindOne(ctx, byId(overridesSectionID))
if err := res.Err(); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return sections, nil
}
return nil, errors.Wrap(err, "getting overrides document")
}
rawOverrides, err := res.Raw()
if err != nil {
return nil, errors.Wrap(err, "getting raw overrides config")
}
sections, err = replaceOverridesConfigSection(sections, rawOverrides)
if err != nil {
return nil, errors.Wrap(err, "overrideing overrides config")
}
if applyOverrides {
var overrides OverridesConfig
if err := bson.Unmarshal(rawOverrides, &overrides); err != nil {
return nil, errors.Wrap(err, "unmarshalling overrides config")
}
sections, err = overrides.overrideRawConfiguration(sections)
if err != nil {
return nil, errors.Wrap(err, "applying configuration overrides")
}
}
return sections, nil
}
// replaceOverridesConfigSection replaces the [OverridesConfig] document in [sections] with
// the provided [overrides].
func replaceOverridesConfigSection(sections []bson.Raw, overrides bson.Raw) ([]bson.Raw, error) {
res := make([]bson.Raw, 0, len(sections))
for _, section := range sections {
idVal, err := section.LookupErr("_id")
if err != nil {
return nil, errors.Wrap(err, "getting document id")
}
id, ok := idVal.StringValueOK()
if !ok {
return nil, errors.New("config document id isn't a string")
}
if id == overridesSectionID {
res = append(res, overrides)
} else {
res = append(res, section)
}
}
return res, nil
}
// getConfigSection fetches a section from the database and deserializes it into the provided
// section. If there's a [SharedDB] database the configuration will come from there and
// overrides from the [OverridesConfig] section in the [DB] database will be applied.
// If a document is missing the value of its section is reset to its zero value.
func getConfigSection(ctx context.Context, section ConfigSection) error {
db := GetEnvironment().DB()
if sharedDB := GetEnvironment().SharedDB(); sharedDB != nil {
db = sharedDB
}
res := db.Collection(ConfigCollection).FindOne(ctx, byId(section.SectionId()))
if err := res.Err(); err != nil {
if err != mongo.ErrNoDocuments {
return errors.Wrapf(err, "getting config section '%s'", section.SectionId())
}
// Reset the section to its zero value.
reflect.ValueOf(section).Elem().Set(reflect.New(reflect.ValueOf(section).Elem().Type()).Elem())
return nil
}
raw, err := res.Raw()
if err != nil {
return errors.Wrap(err, "getting raw result")
}
if GetEnvironment().SharedDB() != nil {
overridden, err := overrideConfig(ctx, []bson.Raw{raw}, true)
if err != nil {
return errors.Wrap(err, "overriding configuration section")
}
if len(overridden) != 1 {
return errors.Errorf("overridden config had unexpected length %d", len(overridden))
}
raw = overridden[0]
}
if err := bson.Unmarshal(raw, section); err != nil {
return errors.Wrapf(err, "unmarshalling config section '%s'", section.SectionId())
}
return nil
}
// setConfigSection applies [update] to the specified configuration section. If there's a shared
// database the update is applied there.
func setConfigSection(ctx context.Context, sectionID string, update bson.M) error {
db := GetEnvironment().SharedDB()
if db == nil || sectionID == overridesSectionID {
db = GetEnvironment().DB()
}
_, err := db.Collection(ConfigCollection).UpdateOne(
ctx,
byId(sectionID),
update,
options.Update().SetUpsert(true),
)
return errors.Wrapf(err, "updating config section '%s'", sectionID)
}
// SetBanner sets the text of the Evergreen site-wide banner. Setting a blank
// string here means that there is no banner
func SetBanner(ctx context.Context, bannerText string) error {
coll := GetEnvironment().DB().Collection(ConfigCollection)
_, err := coll.UpdateOne(ctx, byId(ConfigDocID), bson.M{
"$set": bson.M{bannerKey: bannerText},
}, options.Update().SetUpsert(true))
return errors.WithStack(err)
}
// SetBannerTheme sets the text of the Evergreen site-wide banner. Setting a blank
// string here means that there is no banner
func SetBannerTheme(ctx context.Context, theme BannerTheme) error {
coll := GetEnvironment().DB().Collection(ConfigCollection)
_, err := coll.UpdateOne(ctx, byId(ConfigDocID), bson.M{
"$set": bson.M{bannerThemeKey: theme},
}, options.Update().SetUpsert(true))
return errors.WithStack(err)
}
func GetServiceFlags(ctx context.Context) (*ServiceFlags, error) {
flags := &ServiceFlags{}
if err := flags.Get(ctx); err != nil {
return nil, errors.Wrapf(err, "getting section '%s'", flags.SectionId())
}
return flags, nil
}
// SetServiceFlags sets whether each of the runner/API server processes is enabled.
func SetServiceFlags(ctx context.Context, flags ServiceFlags) error {
return flags.Set(ctx)
}