2022-08-13 09:41:01 -05:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-08-17 01:25:14 -05: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 09:41:01 -05:00
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-08-13 21:59:09 -05:00
|
|
|
func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
|
2022-08-13 09:41:01 -05:00
|
|
|
client := &HTTPClient{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Secret: secret,
|
|
|
|
SkipVerify: skipverify,
|
|
|
|
}
|
|
|
|
|
2022-08-13 21:59:09 -05:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
opt.Apply(client)
|
|
|
|
}
|
|
|
|
|
2022-08-13 09:41:01 -05:00
|
|
|
client.Client = &http.Client{
|
2022-09-03 02:59:44 -05:00
|
|
|
Timeout: 1 * time.Minute,
|
2022-08-13 09:41:01 -05:00
|
|
|
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 01:05:56 -05:00
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
|
2022-08-13 09:41:01 -05:00
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
|
|
|
Client *http.Client
|
|
|
|
Endpoint string
|
|
|
|
Secret string
|
|
|
|
SkipVerify bool
|
2022-08-13 21:59:09 -05:00
|
|
|
|
|
|
|
opts []connect.ClientOption
|
2022-08-13 09:41:01 -05: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 01:25:14 -05:00
|
|
|
client := pingv1connect.NewPingServiceClient(
|
2022-08-13 09:41:01 -05:00
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
2022-08-13 21:59:09 -05:00
|
|
|
p.opts...,
|
2022-08-13 09:41:01 -05:00
|
|
|
)
|
2022-08-17 01:25:14 -05:00
|
|
|
req := connect.NewRequest(&pingv1.PingRequest{
|
2022-08-13 09:41:01 -05:00
|
|
|
Data: machine,
|
|
|
|
})
|
|
|
|
|
2022-08-28 01:05:56 -05:00
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
2022-08-13 09:41:01 -05:00
|
|
|
|
|
|
|
_, err := client.Ping(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|
2022-08-14 00:29:00 -05:00
|
|
|
|
2022-09-01 02:09:22 -05:00
|
|
|
// Register a new runner.
|
2022-08-28 01:05:56 -05:00
|
|
|
func (p *HTTPClient) Register(ctx context.Context, arg *runnerv1.RegisterRequest) (*runnerv1.Runner, error) {
|
2022-08-17 01:25:14 -05:00
|
|
|
client := runnerv1connect.NewRunnerServiceClient(
|
2022-08-14 00:29:00 -05:00
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
2022-08-28 01:05:56 -05:00
|
|
|
req := connect.NewRequest(arg)
|
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
2022-08-14 00:29:00 -05:00
|
|
|
|
2022-08-28 01:05:56 -05:00
|
|
|
res, err := client.Register(ctx, req)
|
2022-08-14 00:29:00 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-28 01:05:56 -05:00
|
|
|
return res.Msg.Runner, err
|
2022-08-14 00:29:00 -05:00
|
|
|
}
|
2022-09-01 02:09:22 -05: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
|
|
|
|
}
|
2022-09-01 09:00:41 -05:00
|
|
|
|
|
|
|
// Update updates the build stage.
|
|
|
|
func (p *HTTPClient) Update(ctx context.Context, arg *runnerv1.UpdateRequest) error {
|
|
|
|
client := runnerv1connect.NewRunnerServiceClient(
|
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
|
|
|
req := connect.NewRequest(arg)
|
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
|
|
|
|
|
|
|
_, err := client.Update(ctx, req)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStep updates the build step.
|
|
|
|
func (p *HTTPClient) UpdateStep(ctx context.Context, arg *runnerv1.UpdateStepRequest) error {
|
|
|
|
client := runnerv1connect.NewRunnerServiceClient(
|
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
|
|
|
req := connect.NewRequest(arg)
|
|
|
|
req.Header().Set("X-Runner-Token", p.Secret)
|
|
|
|
|
|
|
|
_, err := client.UpdateStep(ctx, req)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|