2023-03-19 21:42:21 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-02-24 14:53:44 +00:00
|
|
|
"strings"
|
2019-09-14 12:21:16 +00:00
|
|
|
"time"
|
2017-06-28 17:21:22 +00:00
|
|
|
|
2023-08-18 13:13:13 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-06-28 17:21:22 +00:00
|
|
|
"google.golang.org/grpc"
|
2017-06-29 21:27:06 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-09-24 14:29:26 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2021-11-26 02:34:48 +00:00
|
|
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2023-01-28 13:13:04 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
2017-06-28 17:21:22 +00:00
|
|
|
)
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
// set grpc version on compile time to compare against server version response
|
|
|
|
const ClientGrpcVersion int32 = proto.Version
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
type client struct {
|
2021-09-29 00:10:09 +00:00
|
|
|
client proto.WoodpeckerClient
|
2017-06-28 17:21:22 +00:00
|
|
|
conn *grpc.ClientConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGrpcClient returns a new grpc Client.
|
2023-01-28 13:13:04 +00:00
|
|
|
func NewGrpcClient(conn *grpc.ClientConn) rpc.Peer {
|
2017-06-28 17:21:22 +00:00
|
|
|
client := new(client)
|
2021-09-29 00:10:09 +00:00
|
|
|
client.client = proto.NewWoodpeckerClient(conn)
|
2017-06-28 17:21:22 +00:00
|
|
|
client.conn = conn
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2023-08-18 13:13:13 +00:00
|
|
|
func (c *client) newBackOff() backoff.BackOff {
|
|
|
|
b := backoff.NewExponentialBackOff()
|
|
|
|
b.MaxInterval = 10 * time.Second
|
|
|
|
b.InitialInterval = 10 * time.Millisecond
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
// Version returns the server- & grpc-version
|
|
|
|
func (c *client) Version(ctx context.Context) (*rpc.Version, error) {
|
|
|
|
res, err := c.client.Version(ctx, &proto.Empty{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &rpc.Version{
|
|
|
|
GrpcVersion: res.GrpcVersion,
|
|
|
|
ServerVersion: res.ServerVersion,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Next returns the next workflow in the queue.
|
|
|
|
func (c *client) Next(ctx context.Context, f rpc.Filter) (*rpc.Workflow, error) {
|
2023-03-19 21:42:21 +00:00
|
|
|
var res *proto.NextResponse
|
2017-06-28 17:21:22 +00:00
|
|
|
var err error
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.NextRequest)
|
|
|
|
req.Filter = new(proto.Filter)
|
|
|
|
req.Filter.Labels = f.Labels
|
|
|
|
for {
|
|
|
|
res, err = c.client.Next(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
2023-03-18 19:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove after adding continuous data exchange by something like #536
|
|
|
|
if strings.Contains(err.Error(), "\"too_many_pings\"") {
|
|
|
|
// https://github.com/woodpecker-ci/woodpecker/issues/717#issuecomment-1049365104
|
|
|
|
log.Trace().Err(err).Msg("grpc: to many keepalive pings without sending data")
|
2018-01-10 21:31:07 +00:00
|
|
|
} else {
|
2023-03-18 19:35:27 +00:00
|
|
|
log.Err(err).Msgf("grpc error: done(): code: %v: %s", status.Code(err), err)
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return nil, err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
2017-07-20 16:21:15 +00:00
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 16:00:52 +00:00
|
|
|
if res.GetWorkflow() == nil {
|
2017-06-28 17:21:22 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
w := new(rpc.Workflow)
|
2023-08-28 16:00:52 +00:00
|
|
|
w.ID = res.GetWorkflow().GetId()
|
|
|
|
w.Timeout = res.GetWorkflow().GetTimeout()
|
2023-08-21 16:30:19 +00:00
|
|
|
w.Config = new(backend.Config)
|
2023-08-28 16:00:52 +00:00
|
|
|
if err := json.Unmarshal(res.GetWorkflow().GetPayload(), w.Config); err != nil {
|
2023-08-21 16:30:19 +00:00
|
|
|
log.Error().Err(err).Msgf("could not unmarshal workflow config of '%s'", w.ID)
|
2021-11-23 14:36:52 +00:00
|
|
|
}
|
2023-08-21 16:30:19 +00:00
|
|
|
return w, nil
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Wait blocks until the workflow is complete.
|
2017-06-28 17:21:22 +00:00
|
|
|
func (c *client) Wait(ctx context.Context, id string) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.WaitRequest)
|
|
|
|
req.Id = id
|
|
|
|
for {
|
|
|
|
_, err = c.client.Wait(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: wait(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Init signals the workflow is initialized.
|
2023-01-28 13:13:04 +00:00
|
|
|
func (c *client) Init(ctx context.Context, id string, state rpc.State) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.InitRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
2022-10-28 15:38:53 +00:00
|
|
|
req.State.Name = state.Step
|
2017-06-28 17:21:22 +00:00
|
|
|
for {
|
|
|
|
_, err = c.client.Init(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: init(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Done signals the work is complete.
|
2023-01-28 13:13:04 +00:00
|
|
|
func (c *client) Done(ctx context.Context, id string, state rpc.State) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.DoneRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
2022-10-28 15:38:53 +00:00
|
|
|
req.State.Name = state.Step
|
2017-06-28 17:21:22 +00:00
|
|
|
for {
|
|
|
|
_, err = c.client.Done(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: done(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Extend extends the workflow deadline
|
2017-06-28 17:21:22 +00:00
|
|
|
func (c *client) Extend(ctx context.Context, id string) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.ExtendRequest)
|
|
|
|
req.Id = id
|
|
|
|
for {
|
|
|
|
_, err = c.client.Extend(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: extend(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Update updates the workflow state.
|
2023-01-28 13:13:04 +00:00
|
|
|
func (c *client) Update(ctx context.Context, id string, state rpc.State) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.UpdateRequest)
|
|
|
|
req.Id = id
|
|
|
|
req.State = new(proto.State)
|
|
|
|
req.State.Error = state.Error
|
|
|
|
req.State.ExitCode = int32(state.ExitCode)
|
|
|
|
req.State.Exited = state.Exited
|
|
|
|
req.State.Finished = state.Finished
|
|
|
|
req.State.Started = state.Started
|
2022-10-28 15:38:53 +00:00
|
|
|
req.State.Name = state.Step
|
2017-06-28 17:21:22 +00:00
|
|
|
for {
|
|
|
|
_, err = c.client.Update(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: update(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-21 16:30:19 +00:00
|
|
|
// Log writes the workflow log entry.
|
2023-06-06 07:52:08 +00:00
|
|
|
func (c *client) Log(ctx context.Context, logEntry *rpc.LogEntry) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2017-06-28 17:21:22 +00:00
|
|
|
req := new(proto.LogRequest)
|
2023-06-06 07:52:08 +00:00
|
|
|
req.LogEntry = new(proto.LogEntry)
|
|
|
|
req.LogEntry.StepUuid = logEntry.StepUUID
|
|
|
|
req.LogEntry.Data = logEntry.Data
|
|
|
|
req.LogEntry.Line = int32(logEntry.Line)
|
|
|
|
req.LogEntry.Time = logEntry.Time
|
|
|
|
req.LogEntry.Type = int32(logEntry.Type)
|
2017-06-28 17:21:22 +00:00
|
|
|
for {
|
|
|
|
_, err = c.client.Log(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2023-03-18 19:35:27 +00:00
|
|
|
|
|
|
|
log.Err(err).Msgf("grpc error: log(): code: %v: %s", status.Code(err), err)
|
|
|
|
|
2021-09-24 14:29:26 +00:00
|
|
|
switch status.Code(err) {
|
2017-09-12 16:03:32 +00:00
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
2017-06-29 21:27:06 +00:00
|
|
|
return err
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-01-28 13:13:04 +00:00
|
|
|
|
|
|
|
func (c *client) RegisterAgent(ctx context.Context, platform, backend, version string, capacity int) (int64, error) {
|
|
|
|
req := new(proto.RegisterAgentRequest)
|
|
|
|
req.Platform = platform
|
|
|
|
req.Backend = backend
|
|
|
|
req.Version = version
|
|
|
|
req.Capacity = int32(capacity)
|
|
|
|
|
|
|
|
res, err := c.client.RegisterAgent(ctx, req)
|
|
|
|
return res.GetAgentId(), err
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:53:47 +00:00
|
|
|
func (c *client) UnregisterAgent(ctx context.Context) error {
|
|
|
|
_, err := c.client.UnregisterAgent(ctx, &proto.Empty{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
func (c *client) ReportHealth(ctx context.Context) (err error) {
|
2023-08-18 13:13:13 +00:00
|
|
|
retry := c.newBackOff()
|
2023-01-28 13:13:04 +00:00
|
|
|
req := new(proto.ReportHealthRequest)
|
|
|
|
req.Status = "I am alive!"
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err = c.client.ReportHealth(ctx, req)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
switch status.Code(err) {
|
|
|
|
case
|
|
|
|
codes.Aborted,
|
|
|
|
codes.DataLoss,
|
|
|
|
codes.DeadlineExceeded,
|
|
|
|
codes.Internal,
|
|
|
|
codes.Unavailable:
|
|
|
|
// non-fatal errors
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
2023-08-18 13:13:13 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(retry.NextBackOff()):
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2023-01-28 13:13:04 +00:00
|
|
|
}
|
|
|
|
}
|