-
Notifications
You must be signed in to change notification settings - Fork 18
/
executorservice.go
67 lines (57 loc) · 1.52 KB
/
executorservice.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
package executorservice
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/certifi/gocertifi"
"github.com/cirruslabs/cirrus-ci-agent/api"
grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"time"
)
var (
ErrRPC = errors.New("RPC error")
)
type ExecutorService struct{}
const (
DefaultRPCEndpoint = "grpc.cirrus-ci.com:443"
defaultTimeout = time.Second * 15
defaultRetries = 3
)
func New() *ExecutorService {
return &ExecutorService{}
}
func (p *ExecutorService) SupportedInstances() (*api.AdditionalInstancesInfo, error) {
// Create a context to enforce the defaultTimeout
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
// Setup Cirrus CI RPC connection
certPool, _ := gocertifi.CACerts()
tlsCredentials := credentials.NewTLS(&tls.Config{
MinVersion: tls.VersionTLS13,
RootCAs: certPool,
})
conn, err := grpc.DialContext(
ctx,
DefaultRPCEndpoint,
grpc.WithBlock(),
grpc.WithTransportCredentials(tlsCredentials),
grpc.WithUnaryInterceptor(
grpcretry.UnaryClientInterceptor(
grpcretry.WithMax(defaultRetries),
),
),
)
if err != nil {
return nil, fmt.Errorf("%w: failed to dial: %v", ErrRPC, err)
}
defer conn.Close()
client := api.NewCirrusRemoteExecutorServiceClient(conn)
response, err := client.Capabilities(ctx, &api.CapabilitiesRequest{})
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrRPC, err)
}
return response.SupportedInstances, nil
}