forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_api.go
126 lines (106 loc) · 3.74 KB
/
config_api.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
package evergreen
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/evergreen-ci/utility"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type ClientBinary struct {
Arch string `yaml:"arch" json:"arch"`
OS string `yaml:"os" json:"os"`
URL string `yaml:"url" json:"url"`
DisplayName string `yaml:"display_name" json:"display_name"`
}
type ClientConfig struct {
ClientBinaries []ClientBinary
LatestRevision string
S3URLPrefix string
}
func (c *ClientConfig) populateClientBinaries(ctx context.Context, s3URLPrefix string) {
client := utility.GetHTTPClient()
defer utility.PutHTTPClient(client)
// We assume that all valid OS/arch combinations listed in Evergreen
// constants are supported platforms.
for platform, displayName := range ValidArchDisplayNames {
osAndArch := strings.Split(platform, "_")
if len(osAndArch) != 2 {
continue
}
binary := "evergreen"
if strings.Contains(platform, "windows") {
binary += ".exe"
}
clientBinary := ClientBinary{
// The items in S3 are expected to be of the form <s3_prefix>/<os>_<arch>/evergreen{.exe}
URL: fmt.Sprintf("%s/%s/%s", s3URLPrefix, platform, binary),
OS: osAndArch[0],
Arch: osAndArch[1],
DisplayName: displayName,
}
checkFailedMsg := message.Fields{
"message": "could not check for existence of Evergreen client binary for this OS/arch, skipping it and continuing app startup",
"os": clientBinary.OS,
"arch": clientBinary.Arch,
"url": clientBinary.URL,
}
// Check that the client exists and is accessible.
req, err := http.NewRequestWithContext(ctx, http.MethodHead, clientBinary.URL, nil)
if err != nil {
grip.Notice(message.WrapError(err, checkFailedMsg))
continue
}
resp, err := client.Do(req)
if err != nil {
grip.Notice(message.WrapError(err, checkFailedMsg))
continue
}
_ = resp.Body.Close()
if resp.StatusCode >= 400 {
checkFailedMsg["status_code"] = resp.StatusCode
grip.Notice(checkFailedMsg)
continue
}
c.ClientBinaries = append(c.ClientBinaries, clientBinary)
}
grip.AlertWhen(len(c.ClientBinaries) == 0, message.Fields{
"message": "could not find any valid Evergreen client binaries during app startup, the API server will not be able to distribute Evergreen clients",
"s3_url_prefix": s3URLPrefix,
})
}
// APIConfig holds relevant log and listener settings for the API server.
type APIConfig struct {
HttpListenAddr string `bson:"http_listen_addr" json:"http_listen_addr" yaml:"httplistenaddr"`
GithubWebhookSecret string `bson:"github_webhook_secret" json:"github_webhook_secret" yaml:"github_webhook_secret"`
}
func (c *APIConfig) SectionId() string { return "api" }
func (c *APIConfig) Get(ctx context.Context) error {
res := GetEnvironment().DB().Collection(ConfigCollection).FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = APIConfig{}
return nil
}
return errors.Wrapf(err, "getting config section '%s'", c.SectionId())
}
if err := res.Decode(&c); err != nil {
return errors.Wrapf(err, "decoding config section '%s'", c.SectionId())
}
return nil
}
func (c *APIConfig) Set(ctx context.Context) error {
_, err := GetEnvironment().DB().Collection(ConfigCollection).UpdateOne(ctx, byId(c.SectionId()), bson.M{
"$set": bson.M{
"http_listen_addr": c.HttpListenAddr,
"github_webhook_secret": c.GithubWebhookSecret,
},
}, options.Update().SetUpsert(true))
return errors.Wrapf(err, "updating config section '%s'", c.SectionId())
}
func (c *APIConfig) ValidateAndDefault() error { return nil }