2022-08-13 15:41:01 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-17 07:25:14 +01:00
|
|
|
pingv1 "gitea.com/gitea/proto-go/ping/v1"
|
|
|
|
"gitea.com/gitea/proto-go/ping/v1/pingv1connect"
|
|
|
|
runnerv1 "gitea.com/gitea/proto-go/runner/v1"
|
|
|
|
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
|
2022-08-13 15:41:01 +01:00
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-08-14 03:59:09 +01:00
|
|
|
func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
|
2022-08-13 15:41:01 +01:00
|
|
|
client := &HTTPClient{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Secret: secret,
|
|
|
|
SkipVerify: skipverify,
|
|
|
|
}
|
|
|
|
|
2022-08-14 03:59:09 +01:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
opt.Apply(client)
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:41:01 +01:00
|
|
|
client.Client = &http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http2.Transport{
|
|
|
|
AllowHTTP: true,
|
|
|
|
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
|
|
|
return net.Dial(netw, addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipverify {
|
|
|
|
client.Client = &http.Client{
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
|
2022-08-13 15:41:01 +01:00
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
|
|
|
Client *http.Client
|
|
|
|
Endpoint string
|
|
|
|
Secret string
|
|
|
|
SkipVerify bool
|
2022-08-14 03:59:09 +01:00
|
|
|
|
|
|
|
opts []connect.ClientOption
|
2022-08-13 15:41:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping sends a ping message to the server to test connectivity.
|
|
|
|
func (p *HTTPClient) Ping(ctx context.Context, machine string) error {
|
2022-08-17 07:25:14 +01:00
|
|
|
client := pingv1connect.NewPingServiceClient(
|
2022-08-13 15:41:01 +01:00
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
2022-08-14 03:59:09 +01:00
|
|
|
p.opts...,
|
2022-08-13 15:41:01 +01:00
|
|
|
)
|
2022-08-17 07:25:14 +01:00
|
|
|
req := connect.NewRequest(&pingv1.PingRequest{
|
2022-08-13 15:41:01 +01:00
|
|
|
Data: machine,
|
|
|
|
})
|
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
2022-08-13 15:41:01 +01:00
|
|
|
|
|
|
|
_, err := client.Ping(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|
2022-08-14 06:29:00 +01:00
|
|
|
|
2022-09-01 08:09:22 +01:00
|
|
|
// Register a new runner.
|
2022-08-28 07:05:56 +01:00
|
|
|
func (p *HTTPClient) Register(ctx context.Context, arg *runnerv1.RegisterRequest) (*runnerv1.Runner, error) {
|
2022-08-17 07:25:14 +01:00
|
|
|
client := runnerv1connect.NewRunnerServiceClient(
|
2022-08-14 06:29:00 +01:00
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
2022-08-28 07:05:56 +01:00
|
|
|
req := connect.NewRequest(arg)
|
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
2022-08-14 06:29:00 +01:00
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
res, err := client.Register(ctx, req)
|
2022-08-14 06:29:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
return res.Msg.Runner, err
|
2022-08-14 06:29:00 +01:00
|
|
|
}
|
2022-09-01 08:09:22 +01:00
|
|
|
|
|
|
|
// Request requests the next available build stage for execution.
|
|
|
|
func (p *HTTPClient) Request(ctx context.Context, arg *runnerv1.RequestRequest) (*runnerv1.Stage, error) {
|
|
|
|
client := runnerv1connect.NewRunnerServiceClient(
|
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
|
|
|
req := connect.NewRequest(arg)
|
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
|
|
|
|
|
|
|
res, err := client.Request(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Msg.Stage, err
|
|
|
|
}
|