2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2021-06-28 17:28:18 +00:00
|
|
|
// Copyright 2021 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
2021-06-28 17:28:18 +00:00
|
|
|
//
|
|
|
|
// This file has been modified by Informatyka Boguslawski sp. z o.o. sp.k.
|
2018-02-19 22:24:10 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package grpc
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2017-04-12 17:56:30 +00:00
|
|
|
"fmt"
|
2017-03-05 07:56:08 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2017-07-19 21:46:03 +00:00
|
|
|
|
2021-06-22 10:34:35 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
2021-10-12 07:25:13 +00:00
|
|
|
grpcMetadata "google.golang.org/grpc/metadata"
|
|
|
|
|
|
|
|
"github.com/woodpecker-ci/expr"
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc/proto"
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-23 20:29:09 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/logging"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 20:29:09 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pubsub"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/queue"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
2021-09-23 11:33:59 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2017-03-05 07:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RPC struct {
|
2019-06-28 12:23:52 +00:00
|
|
|
remote remote.Remote
|
|
|
|
queue queue.Queue
|
|
|
|
pubsub pubsub.Publisher
|
|
|
|
logger logging.Log
|
|
|
|
store store.Store
|
|
|
|
host string
|
|
|
|
buildTime *prometheus.GaugeVec
|
|
|
|
buildCount *prometheus.CounterVec
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next implements the rpc.Next function
|
2017-03-12 08:46:59 +00:00
|
|
|
func (s *RPC) Next(c context.Context, filter rpc.Filter) (*rpc.Pipeline, error) {
|
2021-09-22 18:48:01 +00:00
|
|
|
metadata, ok := grpcMetadata.FromIncomingContext(c)
|
2017-07-20 16:21:15 +00:00
|
|
|
if ok {
|
|
|
|
hostname, ok := metadata["hostname"]
|
|
|
|
if ok && len(hostname) != 0 {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Debug().Msgf("agent connected: %s: polling", hostname[0])
|
2017-07-20 16:21:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 23:45:17 +00:00
|
|
|
fn, err := createFilterFunc(filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-12 08:46:59 +00:00
|
|
|
}
|
2019-06-16 13:56:32 +00:00
|
|
|
for {
|
|
|
|
task, err := s.queue.Poll(c, fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if task == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.ShouldRun() {
|
|
|
|
pipeline := new(rpc.Pipeline)
|
|
|
|
err = json.Unmarshal(task.Data, pipeline)
|
|
|
|
return pipeline, err
|
2021-12-01 13:22:06 +00:00
|
|
|
}
|
|
|
|
if err := s.Done(c, task.ID, rpc.State{}); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("mark task '%s' done failed", task.ID)
|
2019-06-16 13:56:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 11:05:16 +00:00
|
|
|
// Wait implements the rpc.Wait function
|
|
|
|
func (s *RPC) Wait(c context.Context, id string) error {
|
|
|
|
return s.queue.Wait(c, id)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extend implements the rpc.Extend function
|
|
|
|
func (s *RPC) Extend(c context.Context, id string) error {
|
|
|
|
return s.queue.Extend(c, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements the rpc.Update function
|
|
|
|
func (s *RPC) Update(c context.Context, id string, state rpc.State) error {
|
2017-04-01 11:17:04 +00:00
|
|
|
procID, err := strconv.ParseInt(id, 10, 64)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-03 09:34:37 +00:00
|
|
|
pproc, err := s.store.ProcLoad(procID)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: rpc.update: cannot find pproc with id %d: %s", procID, err)
|
2017-03-05 07:56:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-03 09:34:37 +00:00
|
|
|
build, err := s.store.GetBuild(pproc.BuildID)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find build with id %d: %s", pproc.BuildID, err)
|
2017-04-03 09:34:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
proc, err := s.store.ProcChild(build, pproc.PID, state.Proc)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find proc with name %s: %s", state.Proc, err)
|
2017-03-05 07:56:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
metadata, ok := grpcMetadata.FromIncomingContext(c)
|
2017-07-20 03:07:29 +00:00
|
|
|
if ok {
|
|
|
|
hostname, ok := metadata["hostname"]
|
|
|
|
if ok && len(hostname) != 0 {
|
|
|
|
proc.Machine = hostname[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:56:08 +00:00
|
|
|
repo, err := s.store.GetRepo(build.RepoID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find repo with id %d: %s", build.RepoID, err)
|
2017-03-05 07:56:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:15:36 +00:00
|
|
|
if _, err = shared.UpdateProcStatus(s.store, *proc, state, build.Started); err != nil {
|
2021-12-08 22:36:23 +00:00
|
|
|
log.Error().Err(err).Msg("rpc.update: cannot update proc")
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
if build.Procs, err = s.store.ProcList(build); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not get proc list from store")
|
|
|
|
}
|
|
|
|
if build.Procs, err = model.Tree(build.Procs); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not build tree from proc list")
|
|
|
|
return err
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
message := pubsub.Message{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"repo": repo.FullName,
|
2021-11-22 11:55:13 +00:00
|
|
|
"private": strconv.FormatBool(repo.IsSCMPrivate),
|
2017-04-01 11:17:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
message.Data, _ = json.Marshal(model.Event{
|
|
|
|
Repo: *repo,
|
|
|
|
Build: *build,
|
|
|
|
})
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := s.pubsub.Publish(c, "topic/events", message); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not publish proc list to")
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2017-04-01 11:17:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Upload implements the rpc.Upload function
|
|
|
|
func (s *RPC) Upload(c context.Context, id string, file *rpc.File) error {
|
|
|
|
procID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-03 09:34:37 +00:00
|
|
|
pproc, err := s.store.ProcLoad(procID)
|
2017-04-01 11:17:04 +00:00
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find parent proc with id %d: %s", procID, err)
|
2017-04-03 09:34:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
build, err := s.store.GetBuild(pproc.BuildID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find build with id %d: %s", pproc.BuildID, err)
|
2017-04-03 09:34:37 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
proc, err := s.store.ProcChild(build, pproc.PID, file.Proc)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find child proc with name %s: %s", file.Proc, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-03 09:34:37 +00:00
|
|
|
if file.Mime == "application/json+logs" {
|
|
|
|
return s.store.LogSave(
|
|
|
|
proc,
|
|
|
|
bytes.NewBuffer(file.Data),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-08-02 20:04:00 +00:00
|
|
|
report := &model.File{
|
2017-04-01 11:17:04 +00:00
|
|
|
BuildID: proc.BuildID,
|
|
|
|
ProcID: proc.ID,
|
2017-07-27 17:06:24 +00:00
|
|
|
PID: proc.PID,
|
2017-04-01 11:17:04 +00:00
|
|
|
Mime: file.Mime,
|
|
|
|
Name: file.Name,
|
|
|
|
Size: file.Size,
|
|
|
|
Time: file.Time,
|
2017-08-02 20:04:00 +00:00
|
|
|
}
|
|
|
|
if d, ok := file.Meta["X-Tests-Passed"]; ok {
|
|
|
|
report.Passed, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
if d, ok := file.Meta["X-Tests-Failed"]; ok {
|
|
|
|
report.Failed, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
if d, ok := file.Meta["X-Tests-Skipped"]; ok {
|
|
|
|
report.Skipped, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := file.Meta["X-Checks-Passed"]; ok {
|
|
|
|
report.Passed, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
if d, ok := file.Meta["X-Checks-Failed"]; ok {
|
|
|
|
report.Failed, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := file.Meta["X-Coverage-Lines"]; ok {
|
|
|
|
report.Passed, _ = strconv.Atoi(d)
|
|
|
|
}
|
|
|
|
if d, ok := file.Meta["X-Coverage-Total"]; ok {
|
|
|
|
if total, _ := strconv.Atoi(d); total != 0 {
|
|
|
|
report.Failed = total - report.Passed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
return server.Config.Storage.Files.FileCreate(
|
2017-08-02 20:04:00 +00:00
|
|
|
report,
|
2017-04-01 11:17:04 +00:00
|
|
|
bytes.NewBuffer(file.Data),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init implements the rpc.Init function
|
|
|
|
func (s *RPC) Init(c context.Context, id string, state rpc.State) error {
|
|
|
|
procID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
proc, err := s.store.ProcLoad(procID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find proc with id %d: %s", procID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-22 18:48:01 +00:00
|
|
|
metadata, ok := grpcMetadata.FromIncomingContext(c)
|
2017-07-19 21:46:03 +00:00
|
|
|
if ok {
|
|
|
|
hostname, ok := metadata["hostname"]
|
|
|
|
if ok && len(hostname) != 0 {
|
|
|
|
proc.Machine = hostname[0]
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
|
|
|
|
build, err := s.store.GetBuild(proc.BuildID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find build with id %d: %s", proc.BuildID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := s.store.GetRepo(build.RepoID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find repo with id %d: %s", build.RepoID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
if build.Status == model.StatusPending {
|
2021-09-22 18:48:01 +00:00
|
|
|
if build, err = shared.UpdateToStatusRunning(s.store, *build, state.Started); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: init: cannot update build_id %d state: %s", build.ID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 11:17:04 +00:00
|
|
|
defer func() {
|
|
|
|
build.Procs, _ = s.store.ProcList(build)
|
|
|
|
message := pubsub.Message{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"repo": repo.FullName,
|
2021-11-22 11:55:13 +00:00
|
|
|
"private": strconv.FormatBool(repo.IsSCMPrivate),
|
2017-04-01 11:17:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
message.Data, _ = json.Marshal(model.Event{
|
|
|
|
Repo: *repo,
|
|
|
|
Build: *build,
|
|
|
|
})
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := s.pubsub.Publish(c, "topic/events", message); err != nil {
|
|
|
|
log.Error().Err(err).Msg("can not publish proc list to")
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
}()
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
_, err = shared.UpdateProcToStatusStarted(s.store, *proc, state)
|
2019-09-15 05:29:45 +00:00
|
|
|
return err
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Done implements the rpc.Done function
|
|
|
|
func (s *RPC) Done(c context.Context, id string, state rpc.State) error {
|
|
|
|
procID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2017-04-01 11:17:04 +00:00
|
|
|
proc, err := s.store.ProcLoad(procID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find proc with id %d: %s", procID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
build, err := s.store.GetBuild(proc.BuildID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find build with id %d: %s", proc.BuildID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
repo, err := s.store.GetRepo(build.RepoID)
|
|
|
|
if err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: cannot find repo with id %d: %s", build.RepoID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
if proc, err = shared.UpdateProcStatusToDone(s.store, *proc, state); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d state: %s", proc.ID, err)
|
2019-09-15 05:29:45 +00:00
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
|
2019-06-16 13:56:32 +00:00
|
|
|
var queueErr error
|
|
|
|
if proc.Failing() {
|
|
|
|
queueErr = s.queue.Error(c, id, fmt.Errorf("Proc finished with exitcode %d, %s", state.ExitCode, state.Error))
|
|
|
|
} else {
|
2019-07-22 10:43:59 +00:00
|
|
|
queueErr = s.queue.Done(c, id, proc.State)
|
2019-06-16 13:56:32 +00:00
|
|
|
}
|
|
|
|
if queueErr != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: done: cannot ack proc_id %d: %s", procID, err)
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
procs, err := s.store.ProcList(build)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
s.completeChildrenIfParentCompleted(procs, proc)
|
|
|
|
|
|
|
|
if !isThereRunningStage(procs) {
|
2021-09-22 18:48:01 +00:00
|
|
|
if build, err = shared.UpdateStatusToDone(s.store, *build, buildStatus(procs), proc.Stopped); err != nil {
|
2021-12-08 22:36:23 +00:00
|
|
|
log.Error().Err(err).Msgf("error: done: cannot update build_id %d final state", build.ID)
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
2019-06-17 08:48:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:02:49 +00:00
|
|
|
s.updateRemoteStatus(c, repo, build, proc)
|
2019-06-16 08:54:31 +00:00
|
|
|
|
|
|
|
if err := s.logger.Close(c, id); err != nil {
|
2021-12-08 22:36:23 +00:00
|
|
|
log.Error().Err(err).Msgf("done: cannot close build_id %d logger", proc.ID)
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
if err := s.notify(c, repo, build, procs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
|
2019-06-28 12:23:52 +00:00
|
|
|
if build.Status == model.StatusSuccess || build.Status == model.StatusFailure {
|
2021-11-22 11:55:13 +00:00
|
|
|
s.buildCount.WithLabelValues(repo.FullName, build.Branch, string(build.Status), "total").Inc()
|
|
|
|
s.buildTime.WithLabelValues(repo.FullName, build.Branch, string(build.Status), "total").Set(float64(build.Finished - build.Started))
|
2019-06-28 12:23:52 +00:00
|
|
|
}
|
|
|
|
if isMultiPipeline(procs) {
|
2021-11-22 11:55:13 +00:00
|
|
|
s.buildTime.WithLabelValues(repo.FullName, build.Branch, string(proc.State), proc.Name).Set(float64(proc.Stopped - proc.Started))
|
2019-06-28 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 08:54:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 08:48:40 +00:00
|
|
|
func isMultiPipeline(procs []*model.Proc) bool {
|
|
|
|
countPPIDZero := 0
|
|
|
|
for _, proc := range procs {
|
|
|
|
if proc.PPID == 0 {
|
|
|
|
countPPIDZero++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return countPPIDZero > 1
|
|
|
|
}
|
|
|
|
|
2019-06-16 08:54:31 +00:00
|
|
|
// Log implements the rpc.Log function
|
|
|
|
func (s *RPC) Log(c context.Context, id string, line *rpc.Line) error {
|
|
|
|
entry := new(logging.Entry)
|
|
|
|
entry.Data, _ = json.Marshal(line)
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := s.logger.Write(c, id, entry); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("rpc server could not write to logger")
|
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RPC) completeChildrenIfParentCompleted(procs []*model.Proc, completedProc *model.Proc) {
|
2017-04-01 11:17:04 +00:00
|
|
|
for _, p := range procs {
|
2019-06-16 08:54:31 +00:00
|
|
|
if p.Running() && p.PPID == completedProc.PID {
|
2021-09-22 18:48:01 +00:00
|
|
|
if _, err := shared.UpdateProcToStatusSkipped(s.store, *p, completedProc.Stopped); err != nil {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Error().Msgf("error: done: cannot update proc_id %d child state: %s", p.ID, err)
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-04-04 09:30:06 +00:00
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
2017-04-04 09:30:06 +00:00
|
|
|
|
2019-06-16 08:54:31 +00:00
|
|
|
func isThereRunningStage(procs []*model.Proc) bool {
|
2017-04-04 09:30:06 +00:00
|
|
|
for _, p := range procs {
|
|
|
|
if p.PPID == 0 {
|
|
|
|
if p.Running() {
|
2019-06-16 08:54:31 +00:00
|
|
|
return true
|
2017-04-04 09:30:06 +00:00
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-22 11:55:13 +00:00
|
|
|
func buildStatus(procs []*model.Proc) model.StatusValue {
|
2019-06-16 08:54:31 +00:00
|
|
|
status := model.StatusSuccess
|
|
|
|
|
|
|
|
for _, p := range procs {
|
|
|
|
if p.PPID == 0 {
|
2017-04-01 11:17:04 +00:00
|
|
|
if p.Failing() {
|
2017-04-04 09:30:06 +00:00
|
|
|
status = p.State
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
}
|
2017-04-12 17:56:30 +00:00
|
|
|
|
2019-06-16 08:54:31 +00:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2021-09-28 10:56:59 +00:00
|
|
|
func (s *RPC) updateRemoteStatus(ctx context.Context, repo *model.Repo, build *model.Build, proc *model.Proc) {
|
2019-06-16 08:54:31 +00:00
|
|
|
user, err := s.store.GetUser(repo.UserID)
|
2021-12-28 16:02:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("can not get user with id '%d'", repo.UserID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if refresher, ok := s.remote.(remote.Refresher); ok {
|
|
|
|
ok, err := refresher.Refresh(ctx, user)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("grpc: refresh oauth token of user '%s' failed", user.Login)
|
|
|
|
} else if ok {
|
|
|
|
if err := s.store.UpdateUser(user); err != nil {
|
|
|
|
log.Error().Err(err).Msg("fail to save user to store after refresh oauth token")
|
2017-04-12 17:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-28 16:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only do status updates for parent procs
|
|
|
|
if proc != nil && proc.IsParent() {
|
|
|
|
err = s.remote.Status(ctx, user, repo, build, proc)
|
2019-06-16 08:54:31 +00:00
|
|
|
if err != nil {
|
2021-12-28 16:02:49 +00:00
|
|
|
log.Error().Err(err).Msgf("error setting commit status for %s/%d", repo.FullName, build.Number)
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2019-06-16 08:54:31 +00:00
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2021-12-08 22:36:23 +00:00
|
|
|
func (s *RPC) notify(c context.Context, repo *model.Repo, build *model.Build, procs []*model.Proc) (err error) {
|
|
|
|
if build.Procs, err = model.Tree(procs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-01 11:17:04 +00:00
|
|
|
message := pubsub.Message{
|
|
|
|
Labels: map[string]string{
|
|
|
|
"repo": repo.FullName,
|
2021-11-22 11:55:13 +00:00
|
|
|
"private": strconv.FormatBool(repo.IsSCMPrivate),
|
2017-04-01 11:17:04 +00:00
|
|
|
},
|
|
|
|
}
|
2017-03-05 07:56:08 +00:00
|
|
|
message.Data, _ = json.Marshal(model.Event{
|
|
|
|
Repo: *repo,
|
|
|
|
Build: *build,
|
|
|
|
})
|
2021-11-23 14:36:52 +00:00
|
|
|
if err := s.pubsub.Publish(c, "topic/events", message); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("grpc could not notify event: '%v'", message)
|
|
|
|
}
|
2021-12-08 22:36:23 +00:00
|
|
|
return nil
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-06-22 19:11:05 +00:00
|
|
|
|
2017-09-08 23:45:17 +00:00
|
|
|
func createFilterFunc(filter rpc.Filter) (queue.Filter, error) {
|
|
|
|
var st *expr.Selector
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if filter.Expr != "" {
|
|
|
|
st, err = expr.ParseString(filter.Expr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(task *queue.Task) bool {
|
|
|
|
if st != nil {
|
|
|
|
match, _ := st.Eval(expr.NewRow(task.Labels))
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range filter.Labels {
|
|
|
|
if task.Labels[k] != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2021-09-29 00:10:09 +00:00
|
|
|
// WoodpeckerServer is a grpc server implementation.
|
|
|
|
type WoodpeckerServer struct {
|
|
|
|
proto.UnimplementedWoodpeckerServer
|
2019-06-28 12:23:52 +00:00
|
|
|
peer RPC
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 00:10:09 +00:00
|
|
|
func NewWoodpeckerServer(remote remote.Remote, queue queue.Queue, logger logging.Log, pubsub pubsub.Publisher, store store.Store, host string) *WoodpeckerServer {
|
2019-06-28 12:23:52 +00:00
|
|
|
buildTime := promauto.NewGaugeVec(prometheus.GaugeOpts{
|
2021-10-02 08:59:34 +00:00
|
|
|
Namespace: "woodpecker",
|
2019-06-28 12:23:52 +00:00
|
|
|
Name: "build_time",
|
|
|
|
Help: "Build time.",
|
|
|
|
}, []string{"repo", "branch", "status", "pipeline"})
|
|
|
|
buildCount := promauto.NewCounterVec(prometheus.CounterOpts{
|
2021-10-02 08:59:34 +00:00
|
|
|
Namespace: "woodpecker",
|
2019-06-28 12:23:52 +00:00
|
|
|
Name: "build_count",
|
|
|
|
Help: "Build count.",
|
|
|
|
}, []string{"repo", "branch", "status", "pipeline"})
|
2017-06-28 17:21:22 +00:00
|
|
|
peer := RPC{
|
2019-06-28 12:23:52 +00:00
|
|
|
remote: remote,
|
|
|
|
store: store,
|
|
|
|
queue: queue,
|
|
|
|
pubsub: pubsub,
|
|
|
|
logger: logger,
|
|
|
|
host: host,
|
|
|
|
buildTime: buildTime,
|
|
|
|
buildCount: buildCount,
|
|
|
|
}
|
2021-09-29 00:10:09 +00:00
|
|
|
return &WoodpeckerServer{peer: peer}
|
2019-06-28 12:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Next(c context.Context, req *proto.NextRequest) (*proto.NextReply, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: req.GetFilter().GetLabels(),
|
2017-10-17 15:47:53 +00:00
|
|
|
Expr: req.GetFilter().GetExpr(),
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := new(proto.NextReply)
|
2019-06-28 12:23:52 +00:00
|
|
|
pipeline, err := s.peer.Next(c, filter)
|
2017-06-28 17:21:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
if pipeline == nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Pipeline = new(proto.Pipeline)
|
|
|
|
res.Pipeline.Id = pipeline.ID
|
|
|
|
res.Pipeline.Timeout = pipeline.Timeout
|
|
|
|
res.Pipeline.Payload, _ = json.Marshal(pipeline.Config)
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Init(c context.Context, req *proto.InitRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Error: req.GetState().GetError(),
|
|
|
|
ExitCode: int(req.GetState().GetExitCode()),
|
|
|
|
Finished: req.GetState().GetFinished(),
|
|
|
|
Started: req.GetState().GetStarted(),
|
|
|
|
Proc: req.GetState().GetName(),
|
|
|
|
Exited: req.GetState().GetExited(),
|
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Init(c, req.GetId(), state)
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Update(c context.Context, req *proto.UpdateRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Error: req.GetState().GetError(),
|
|
|
|
ExitCode: int(req.GetState().GetExitCode()),
|
|
|
|
Finished: req.GetState().GetFinished(),
|
|
|
|
Started: req.GetState().GetStarted(),
|
|
|
|
Proc: req.GetState().GetName(),
|
|
|
|
Exited: req.GetState().GetExited(),
|
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Update(c, req.GetId(), state)
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Upload(c context.Context, req *proto.UploadRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
file := &rpc.File{
|
|
|
|
Data: req.GetFile().GetData(),
|
|
|
|
Mime: req.GetFile().GetMime(),
|
|
|
|
Name: req.GetFile().GetName(),
|
|
|
|
Proc: req.GetFile().GetProc(),
|
|
|
|
Size: int(req.GetFile().GetSize()),
|
|
|
|
Time: req.GetFile().GetTime(),
|
2017-08-02 20:04:00 +00:00
|
|
|
Meta: req.GetFile().GetMeta(),
|
2017-06-28 17:21:22 +00:00
|
|
|
}
|
2017-08-02 20:04:00 +00:00
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Upload(c, req.GetId(), file)
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Done(c context.Context, req *proto.DoneRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
state := rpc.State{
|
|
|
|
Error: req.GetState().GetError(),
|
|
|
|
ExitCode: int(req.GetState().GetExitCode()),
|
|
|
|
Finished: req.GetState().GetFinished(),
|
|
|
|
Started: req.GetState().GetStarted(),
|
|
|
|
Proc: req.GetState().GetName(),
|
|
|
|
Exited: req.GetState().GetExited(),
|
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Done(c, req.GetId(), state)
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Wait(c context.Context, req *proto.WaitRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Wait(c, req.GetId())
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Extend(c context.Context, req *proto.ExtendRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Extend(c, req.GetId())
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (s *WoodpeckerServer) Log(c context.Context, req *proto.LogRequest) (*proto.Empty, error) {
|
2017-06-28 17:21:22 +00:00
|
|
|
line := &rpc.Line{
|
|
|
|
Out: req.GetLine().GetOut(),
|
|
|
|
Pos: int(req.GetLine().GetPos()),
|
|
|
|
Time: req.GetLine().GetTime(),
|
|
|
|
Proc: req.GetLine().GetProc(),
|
|
|
|
}
|
|
|
|
res := new(proto.Empty)
|
2019-06-28 12:23:52 +00:00
|
|
|
err := s.peer.Log(c, req.GetId(), line)
|
2017-06-28 17:21:22 +00:00
|
|
|
return res, err
|
|
|
|
}
|