2022-02-26 13:37:10 +00:00
|
|
|
// Copyright 2022 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.
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2024-06-04 06:30:54 +00:00
|
|
|
prometheus_auto "github.com/prometheus/client_golang/prometheus/promauto"
|
2022-02-26 13:37:10 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc/proto"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/logging"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/pubsub"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/queue"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/version"
|
2022-02-26 13:37:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WoodpeckerServer is a grpc server implementation.
|
|
|
|
type WoodpeckerServer struct {
|
|
|
|
proto.UnimplementedWoodpeckerServer
|
|
|
|
peer RPC
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:04:55 +00:00
|
|
|
func NewWoodpeckerServer(queue queue.Queue, logger logging.Log, pubsub *pubsub.Publisher, store store.Store) proto.WoodpeckerServer {
|
2024-06-04 06:30:54 +00:00
|
|
|
pipelineTime := prometheus_auto.NewGaugeVec(prometheus.GaugeOpts{
|
2022-02-26 13:37:10 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-18 01:24:12 +00:00
|
|
|
Name: "pipeline_time",
|
|
|
|
Help: "Pipeline time.",
|
2022-02-26 13:37:10 +00:00
|
|
|
}, []string{"repo", "branch", "status", "pipeline"})
|
2024-06-04 06:30:54 +00:00
|
|
|
pipelineCount := prometheus_auto.NewCounterVec(prometheus.CounterOpts{
|
2022-02-26 13:37:10 +00:00
|
|
|
Namespace: "woodpecker",
|
2022-10-18 01:24:12 +00:00
|
|
|
Name: "pipeline_count",
|
|
|
|
Help: "Pipeline count.",
|
2022-02-26 13:37:10 +00:00
|
|
|
}, []string{"repo", "branch", "status", "pipeline"})
|
|
|
|
peer := RPC{
|
2022-10-18 01:24:12 +00:00
|
|
|
store: store,
|
|
|
|
queue: queue,
|
|
|
|
pubsub: pubsub,
|
|
|
|
logger: logger,
|
|
|
|
pipelineTime: pipelineTime,
|
|
|
|
pipelineCount: pipelineCount,
|
2022-02-26 13:37:10 +00:00
|
|
|
}
|
|
|
|
return &WoodpeckerServer{peer: peer}
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
func (s *WoodpeckerServer) Version(_ context.Context, _ *proto.Empty) (*proto.VersionResponse, error) {
|
|
|
|
return &proto.VersionResponse{
|
|
|
|
GrpcVersion: proto.Version,
|
|
|
|
ServerVersion: version.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Next(c context.Context, req *proto.NextRequest) (*proto.NextResponse, error) {
|
2022-02-26 13:37:10 +00:00
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: req.GetFilter().GetLabels(),
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
res := new(proto.NextResponse)
|
2022-02-26 13:37:10 +00:00
|
|
|
pipeline, err := s.peer.Next(c, filter)
|
2024-01-09 20:35:37 +00:00
|
|
|
if err != nil || pipeline == nil {
|
2022-02-26 13:37:10 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2023-08-28 16:00:52 +00:00
|
|
|
res.Workflow = new(proto.Workflow)
|
|
|
|
res.Workflow.Id = pipeline.ID
|
|
|
|
res.Workflow.Timeout = pipeline.Timeout
|
2024-01-09 20:35:37 +00:00
|
|
|
res.Workflow.Payload, err = json.Marshal(pipeline.Config)
|
2022-02-26 13:37:10 +00:00
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Init(c context.Context, req *proto.InitRequest) (*proto.Empty, error) {
|
2024-07-01 09:20:55 +00:00
|
|
|
state := rpc.WorkflowState{
|
2022-02-26 13:37:10 +00:00
|
|
|
Started: req.GetState().GetStarted(),
|
2024-07-01 09:20:55 +00:00
|
|
|
Finished: req.GetState().GetFinished(),
|
|
|
|
Error: req.GetState().GetError(),
|
2022-02-26 13:37:10 +00:00
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.Init(c, req.GetId(), state)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Update(c context.Context, req *proto.UpdateRequest) (*proto.Empty, error) {
|
2024-07-01 09:20:55 +00:00
|
|
|
state := rpc.StepState{
|
2024-01-09 14:39:09 +00:00
|
|
|
StepUUID: req.GetState().GetStepUuid(),
|
2024-07-01 09:20:55 +00:00
|
|
|
Started: req.GetState().GetStarted(),
|
|
|
|
Finished: req.GetState().GetFinished(),
|
2022-02-26 13:37:10 +00:00
|
|
|
Exited: req.GetState().GetExited(),
|
2024-07-01 09:20:55 +00:00
|
|
|
Error: req.GetState().GetError(),
|
|
|
|
ExitCode: int(req.GetState().GetExitCode()),
|
2022-02-26 13:37:10 +00:00
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.Update(c, req.GetId(), state)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Done(c context.Context, req *proto.DoneRequest) (*proto.Empty, error) {
|
2024-07-01 09:20:55 +00:00
|
|
|
state := rpc.WorkflowState{
|
2022-02-26 13:37:10 +00:00
|
|
|
Started: req.GetState().GetStarted(),
|
2024-07-01 09:20:55 +00:00
|
|
|
Finished: req.GetState().GetFinished(),
|
|
|
|
Error: req.GetState().GetError(),
|
2022-02-26 13:37:10 +00:00
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.Done(c, req.GetId(), state)
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Wait(c context.Context, req *proto.WaitRequest) (*proto.Empty, error) {
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.Wait(c, req.GetId())
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Extend(c context.Context, req *proto.ExtendRequest) (*proto.Empty, error) {
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.Extend(c, req.GetId())
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WoodpeckerServer) Log(c context.Context, req *proto.LogRequest) (*proto.Empty, error) {
|
2023-06-06 07:52:08 +00:00
|
|
|
logEntry := &rpc.LogEntry{
|
|
|
|
Data: req.GetLogEntry().GetData(),
|
|
|
|
Line: int(req.GetLogEntry().GetLine()),
|
|
|
|
Time: req.GetLogEntry().GetTime(),
|
|
|
|
StepUUID: req.GetLogEntry().GetStepUuid(),
|
|
|
|
Type: int(req.GetLogEntry().GetType()),
|
2022-02-26 13:37:10 +00:00
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
2023-06-06 07:52:08 +00:00
|
|
|
err := s.peer.Log(c, logEntry)
|
2022-02-26 13:37:10 +00:00
|
|
|
return res, err
|
|
|
|
}
|
2023-01-28 13:13:04 +00:00
|
|
|
|
|
|
|
func (s *WoodpeckerServer) RegisterAgent(c context.Context, req *proto.RegisterAgentRequest) (*proto.RegisterAgentResponse, error) {
|
|
|
|
res := new(proto.RegisterAgentResponse)
|
|
|
|
agentID, err := s.peer.RegisterAgent(c, req.GetPlatform(), req.GetBackend(), req.GetVersion(), req.GetCapacity())
|
|
|
|
res.AgentId = agentID
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:53:47 +00:00
|
|
|
func (s *WoodpeckerServer) UnregisterAgent(ctx context.Context, _ *proto.Empty) (*proto.Empty, error) {
|
|
|
|
err := s.peer.UnregisterAgent(ctx)
|
|
|
|
return new(proto.Empty), err
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
func (s *WoodpeckerServer) ReportHealth(c context.Context, req *proto.ReportHealthRequest) (*proto.Empty, error) {
|
|
|
|
res := new(proto.Empty)
|
|
|
|
err := s.peer.ReportHealth(c, req.GetStatus())
|
|
|
|
return res, err
|
|
|
|
}
|