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