forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_api.go
More file actions
131 lines (113 loc) · 3.86 KB
/
Copy pathconfig_api.go
File metadata and controls
131 lines (113 loc) · 3.86 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
package evergreen
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/evergreen-ci/utility"
"github.com/mongodb/anser/bsonutil"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
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
OldestAllowedCLIVersion string
// These settings are to support a seemless migration for
// the switch to OAuth. They can be removed in DEVPROD-17405.
// The corresponding fields inside the APIClientConfig struct
// should also be removed then.
OAuthIssuer string
OAuthClientID string
OAuthConnectorID 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"`
URL string `bson:"url" json:"url" yaml:"url"`
CorpURL string `bson:"corp_url" json:"corp_url" yaml:"corp_url"`
}
func (c *APIConfig) SectionId() string { return "api" }
func (c *APIConfig) Get(ctx context.Context) error {
return getConfigSection(ctx, c)
}
var (
httpListenAddrKey = bsonutil.MustHaveTag(APIConfig{}, "HttpListenAddr")
urlKey = bsonutil.MustHaveTag(APIConfig{}, "URL")
corpURLKey = bsonutil.MustHaveTag(APIConfig{}, "CorpURL")
)
func (c *APIConfig) Set(ctx context.Context) error {
return errors.Wrapf(setConfigSection(ctx, c.SectionId(), bson.M{
"$set": bson.M{
httpListenAddrKey: c.HttpListenAddr,
urlKey: c.URL,
corpURLKey: c.CorpURL,
}}), "updating config section '%s'", c.SectionId(),
)
}
func (c *APIConfig) ValidateAndDefault() error {
if c.URL == "" {
return errors.New("URL must not be empty")
}
return nil
}