-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup.go
131 lines (118 loc) · 3.98 KB
/
setup.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
package poplar
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/evergreen-ci/aviation/services"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc"
yaml "gopkg.in/yaml.v2"
)
const (
ProjectEnv = "project"
VersionEnv = "version_id"
OrderEnv = "revision_order_id"
VariantEnv = "build_variant"
TaskNameEnv = "task_name"
ExecutionEnv = "execution"
MainlineEnv = "is_patch"
APIKeyEnv = "POPLAR_BUCKET_API_KEY"
APISecretEnv = "POPLAR_BUCKET_API_SECRET"
APITokenEnv = "POPLAR_BUCKET_API_TOKEN"
BucketNameEnv = "BUCKET_NAME"
BucketPrefixEnv = "BUCKET_PREFIX"
BucketRegionEnv = "BUCKET_REGION"
)
// ReportType describes the marshalled report type.
type ReportType string
const (
ReportTypeJSON ReportType = "JSON"
ReportTypeBSON ReportType = "BSON"
ReportTypeYAML ReportType = "YAML"
ReportTypeEnv ReportType = "ENV"
)
// ReportSetup sets up a Report struct with the given ReportType and filename.
// Note that not all ReportTypes require a filename (such as ReportTypeEnv), if
// this is the case, pass in an empty string for the filename.
func ReportSetup(reportType ReportType, filename string) (*Report, error) {
switch reportType {
case ReportTypeJSON:
return reportSetupUnmarshal(reportType, filename, json.Unmarshal)
case ReportTypeBSON:
return reportSetupUnmarshal(reportType, filename, bson.Unmarshal)
case ReportTypeYAML:
return reportSetupUnmarshal(reportType, filename, yaml.Unmarshal)
case ReportTypeEnv:
return reportSetupEnv()
default:
return nil, errors.Errorf("invalid report type %s", reportType)
}
}
func reportSetupUnmarshal(reportType ReportType, filename string, unmarshal func([]byte, interface{}) error) (*Report, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, errors.Wrapf(err, "opening file '%s'", filename)
}
report := &Report{}
if err := unmarshal(data, report); err != nil {
return nil, errors.Wrapf(err, "unmarshalling report format '%s' from file '%s'", reportType, filename)
}
return report, nil
}
func reportSetupEnv() (*Report, error) {
var order int
var execution int
var mainline bool
var err error
if os.Getenv(MainlineEnv) != "" {
mainline, err = strconv.ParseBool(os.Getenv(MainlineEnv))
if err != nil {
return nil, errors.Wrapf(err, "env var '%s' should be a bool", MainlineEnv)
}
}
if mainline && os.Getenv(OrderEnv) != "" {
order, err = strconv.Atoi(os.Getenv(OrderEnv))
if err != nil {
return nil, errors.Wrapf(err, "env var '%s' should be an int", OrderEnv)
}
}
if os.Getenv(ExecutionEnv) != "" {
execution, err = strconv.Atoi(os.Getenv(ExecutionEnv))
if err != nil {
return nil, errors.Wrapf(err, "env var '%s' should be an int", ExecutionEnv)
}
}
return &Report{
Project: os.Getenv(ProjectEnv),
Version: os.Getenv(VersionEnv),
Order: order,
Variant: os.Getenv(VariantEnv),
TaskName: os.Getenv(TaskNameEnv),
Execution: execution,
Mainline: mainline,
BucketConf: BucketConfiguration{
APIKey: os.Getenv(APIKeyEnv),
APISecret: os.Getenv(APISecretEnv),
APIToken: os.Getenv(APITokenEnv),
Name: os.Getenv(BucketNameEnv),
Prefix: os.Getenv(BucketPrefixEnv),
Region: os.Getenv(BucketRegionEnv),
},
Tests: []Test{},
}, nil
}
// DialCedarOptions describes the options for the DialCedar function. The base
// address defaults to `cedar.mongodb.com` and the RPC port to 7070. If a base
// address is provided the RPC port must also be provided. Username and
// password must always be provided. This aliases the same type in aviation.
type DialCedarOptions services.DialCedarOptions
// DialCedar is a convenience function for creating a RPC client connection
// with cedar via gRPC. This wraps the same function in aviation.
func DialCedar(ctx context.Context, client *http.Client, opts DialCedarOptions) (*grpc.ClientConn, error) {
serviceOpts := services.DialCedarOptions(opts)
return services.DialCedar(ctx, client, &serviceOpts)
}