2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2021-09-23 14:58:12 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
|
|
|
//
|
|
|
|
// 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 agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-23 14:35:24 +00:00
|
|
|
"errors"
|
2023-07-20 18:39:20 +00:00
|
|
|
"fmt"
|
2021-09-23 14:58:12 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/tevino/abool"
|
2021-09-23 14:58:12 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline"
|
2021-11-26 02:34:48 +00:00
|
|
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
2022-05-11 11:40:44 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2021-09-23 14:58:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Runner struct {
|
|
|
|
client rpc.Peer
|
|
|
|
filter rpc.Filter
|
|
|
|
hostname string
|
|
|
|
counter *State
|
|
|
|
engine *backend.Engine
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRunner(workEngine rpc.Peer, f rpc.Filter, h string, state *State, backend *backend.Engine) Runner {
|
|
|
|
return Runner{
|
|
|
|
client: workEngine,
|
|
|
|
filter: f,
|
|
|
|
hostname: h,
|
|
|
|
counter: state,
|
|
|
|
engine: backend,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
func (r *Runner) Run(runnerCtx context.Context) error {
|
2021-11-13 19:18:06 +00:00
|
|
|
log.Debug().Msg("request next execution")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
meta, _ := metadata.FromOutgoingContext(runnerCtx)
|
2021-09-23 14:58:12 +00:00
|
|
|
ctxmeta := metadata.NewOutgoingContext(context.Background(), meta)
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
// get the next workflow from the queue
|
2022-11-23 14:35:24 +00:00
|
|
|
work, err := r.client.Next(runnerCtx, r.filter)
|
2021-09-23 14:58:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if work == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Hour
|
|
|
|
if minutes := work.Timeout; minutes != 0 {
|
|
|
|
timeout = time.Duration(minutes) * time.Minute
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
repoName := extractRepositoryName(work.Config) // hack
|
|
|
|
pipelineNumber := extractPipelineNumber(work.Config) // hack
|
2022-06-15 16:11:20 +00:00
|
|
|
|
2021-09-23 14:58:12 +00:00
|
|
|
r.counter.Add(
|
|
|
|
work.ID,
|
|
|
|
timeout,
|
2022-06-15 16:11:20 +00:00
|
|
|
repoName,
|
2022-10-18 01:24:12 +00:00
|
|
|
pipelineNumber,
|
2021-09-23 14:58:12 +00:00
|
|
|
)
|
|
|
|
defer r.counter.Done(work.ID)
|
|
|
|
|
|
|
|
logger := log.With().
|
2022-06-15 16:11:20 +00:00
|
|
|
Str("repo", repoName).
|
2022-10-18 01:24:12 +00:00
|
|
|
Str("pipeline", pipelineNumber).
|
2021-09-23 14:58:12 +00:00
|
|
|
Str("id", work.ID).
|
|
|
|
Logger()
|
|
|
|
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("received execution")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
workflowCtx, cancel := context.WithTimeout(ctxmeta, timeout)
|
2021-09-23 14:58:12 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-05-11 11:40:44 +00:00
|
|
|
// Add sigterm support for internal context.
|
|
|
|
// Required when the pipeline is terminated by external signals
|
|
|
|
// like kubernetes.
|
2022-11-23 14:35:24 +00:00
|
|
|
workflowCtx = utils.WithContextSigtermCallback(workflowCtx, func() {
|
2022-05-11 11:40:44 +00:00
|
|
|
logger.Error().Msg("Received sigterm termination signal")
|
|
|
|
})
|
|
|
|
|
2021-11-24 01:01:12 +00:00
|
|
|
canceled := abool.New()
|
2021-09-23 14:58:12 +00:00
|
|
|
go func() {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("listen for cancel signal")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
if werr := r.client.Wait(workflowCtx, work.ID); werr != nil {
|
2021-11-24 01:01:12 +00:00
|
|
|
canceled.SetTo(true)
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Warn().Err(werr).Msg("cancel signal received")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
} else {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("stop listening for cancel signal")
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
2022-11-23 14:35:24 +00:00
|
|
|
case <-workflowCtx.Done():
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("pipeline done")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
case <-time.After(time.Minute):
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("pipeline lease renewed")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
if err := r.client.Extend(workflowCtx, work.ID); err != nil {
|
2021-11-23 14:36:52 +00:00
|
|
|
log.Error().Err(err).Msg("extending pipeline deadline failed")
|
|
|
|
}
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
state := rpc.State{}
|
|
|
|
state.Started = time.Now().Unix()
|
|
|
|
|
|
|
|
err = r.client.Init(ctxmeta, work.ID, state)
|
|
|
|
if err != nil {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Error().Err(err).Msg("pipeline initialization failed")
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var uploads sync.WaitGroup
|
|
|
|
err = pipeline.New(work.Config,
|
2022-11-23 14:35:24 +00:00
|
|
|
pipeline.WithContext(workflowCtx),
|
2023-07-20 18:39:20 +00:00
|
|
|
pipeline.WithTaskUUID(fmt.Sprint(work.ID)),
|
|
|
|
pipeline.WithLogger(r.createLogger(logger, &uploads, work)),
|
2022-11-23 14:35:24 +00:00
|
|
|
pipeline.WithTracer(r.createTracer(ctxmeta, logger, work)),
|
2021-09-23 14:58:12 +00:00
|
|
|
pipeline.WithEngine(*r.engine),
|
2022-06-15 16:11:20 +00:00
|
|
|
pipeline.WithDescription(map[string]string{
|
2022-10-18 01:24:12 +00:00
|
|
|
"ID": work.ID,
|
|
|
|
"Repo": repoName,
|
|
|
|
"Pipeline": pipelineNumber,
|
2022-06-15 16:11:20 +00:00
|
|
|
}),
|
2022-11-23 14:35:24 +00:00
|
|
|
).Run(runnerCtx)
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
state.Finished = time.Now().Unix()
|
|
|
|
state.Exited = true
|
2022-11-23 14:35:24 +00:00
|
|
|
|
|
|
|
if canceled.IsSet() {
|
|
|
|
state.Error = ""
|
|
|
|
state.ExitCode = 137
|
|
|
|
} else if err != nil {
|
|
|
|
pExitError := &pipeline.ExitError{}
|
|
|
|
if errors.As(err, &pExitError) {
|
|
|
|
state.ExitCode = pExitError.Code
|
|
|
|
} else if errors.Is(err, pipeline.ErrCancel) {
|
|
|
|
state.Error = ""
|
|
|
|
state.ExitCode = 137
|
|
|
|
canceled.SetTo(true)
|
|
|
|
} else {
|
2021-09-23 14:58:12 +00:00
|
|
|
state.ExitCode = 1
|
|
|
|
state.Error = err.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug().
|
|
|
|
Str("error", state.Error).
|
|
|
|
Int("exit_code", state.ExitCode).
|
2022-11-23 14:35:24 +00:00
|
|
|
Bool("canceled", canceled.IsSet()).
|
2021-09-23 14:58:12 +00:00
|
|
|
Msg("pipeline complete")
|
|
|
|
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("uploading logs")
|
2021-09-23 14:58:12 +00:00
|
|
|
uploads.Wait()
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("uploading logs complete")
|
2021-09-23 14:58:12 +00:00
|
|
|
|
|
|
|
logger.Debug().
|
|
|
|
Str("error", state.Error).
|
|
|
|
Int("exit_code", state.ExitCode).
|
|
|
|
Msg("updating pipeline status")
|
|
|
|
|
2022-11-23 14:35:24 +00:00
|
|
|
if err := r.client.Done(ctxmeta, work.ID, state); err != nil {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Error().Err(err).Msg("updating pipeline status failed")
|
2021-09-23 14:58:12 +00:00
|
|
|
} else {
|
2021-11-14 21:33:45 +00:00
|
|
|
logger.Debug().Msg("updating pipeline status complete")
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract repository name from the configuration
|
|
|
|
func extractRepositoryName(config *backend.Config) string {
|
2021-11-25 19:43:31 +00:00
|
|
|
return config.Stages[0].Steps[0].Environment["CI_REPO"]
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
// extract pipeline number from the configuration
|
|
|
|
func extractPipelineNumber(config *backend.Config) string {
|
|
|
|
return config.Stages[0].Steps[0].Environment["CI_PIPELINE_NUMBER"]
|
2021-09-23 14:58:12 +00:00
|
|
|
}
|