2023-03-19 21:42:21 +00:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
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.
|
|
|
|
|
2017-06-29 22:51:22 +00:00
|
|
|
package main
|
2016-04-20 01:37:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-16 10:14:02 +00:00
|
|
|
"context"
|
2020-05-16 18:56:24 +00:00
|
|
|
"crypto/tls"
|
2023-03-19 21:42:21 +00:00
|
|
|
"errors"
|
2023-04-28 18:41:05 +00:00
|
|
|
"fmt"
|
2017-09-12 18:25:55 +00:00
|
|
|
"net/http"
|
2017-07-19 21:46:03 +00:00
|
|
|
"os"
|
2022-05-30 23:12:18 +00:00
|
|
|
"strings"
|
2016-04-20 01:37:53 +00:00
|
|
|
"sync"
|
2023-01-28 13:13:04 +00:00
|
|
|
"time"
|
2016-04-20 01:37:53 +00:00
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-09-14 05:34:36 +00:00
|
|
|
"github.com/tevino/abool/v2"
|
2021-10-27 19:03:14 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2017-06-28 17:21:22 +00:00
|
|
|
"google.golang.org/grpc"
|
2023-05-03 11:31:29 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-10-12 07:25:13 +00:00
|
|
|
grpccredentials "google.golang.org/grpc/credentials"
|
2022-01-29 15:04:50 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2018-01-08 15:28:38 +00:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2017-07-19 21:46:03 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-05-03 11:31:29 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2017-06-28 17:21:22 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/agent"
|
|
|
|
agentRpc "go.woodpecker-ci.org/woodpecker/v2/agent/rpc"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
2023-12-20 13:26:57 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/addon"
|
|
|
|
addonTypes "go.woodpecker-ci.org/woodpecker/v2/shared/addon/types"
|
2023-12-31 22:29:56 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/version"
|
2016-04-20 01:37:53 +00:00
|
|
|
)
|
|
|
|
|
2023-05-03 11:31:29 +00:00
|
|
|
func run(c *cli.Context) error {
|
2023-07-12 16:51:40 +00:00
|
|
|
agentConfigPath := c.String("agent-config")
|
2017-07-19 21:46:03 +00:00
|
|
|
hostname := c.String("hostname")
|
|
|
|
if len(hostname) == 0 {
|
|
|
|
hostname, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
counter.Polling = c.Int("max-workflows")
|
2017-09-12 20:40:24 +00:00
|
|
|
counter.Running = 0
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
if c.Bool("healthcheck") {
|
|
|
|
go func() {
|
Change healtcheck port into address format, redo #1197 (#1423)
As discussed in the comments in PR #1197. Also add documenation
accordingly.
One thing I'm not sure about is the simple check in health.go if the
address is usable in the GET request or not. From reading
https://pkg.go.dev/net#Dial it seems that the only non-standard address
format that would work in the `net` package but not in a GET url would
likely only be `:port`, as the others listed here are actually also
valid urls:
`For TCP, UDP and IP networks, if the host is empty or a literal
unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for TCP
and UDP, "", "0.0.0.0" or "::" for IP, the local system is assumed.`
One additional thing I noticed is that while `WOODPECKER_SERVER_ADDR`
and `WOODPECKER_SERVER_ADDR` use the default value format of `:PORT`,
`WOODPECKER_SERVER` actually uses `localhost:9000`. I guess it makes a
bit of sense, considering the server might not be local to the agent,
but it looks a bit inconsistent this way. I don't think it would hurt to
make the `WOODPECKER_HEALTHCHECK_ADDR` in this format too, but then it's
different from the server flags again... :-)
2022-11-19 11:06:51 +00:00
|
|
|
if err := http.ListenAndServe(c.String("healthcheck-addr"), nil); err != nil {
|
2024-01-10 19:57:12 +00:00
|
|
|
log.Error().Err(err).Msgf("cannot listen on address %s", c.String("healthcheck-addr"))
|
2021-10-27 19:03:14 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-09-12 18:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 15:04:50 +00:00
|
|
|
var transport grpc.DialOption
|
2022-01-31 14:38:00 +00:00
|
|
|
if c.Bool("grpc-secure") {
|
2023-08-01 07:06:27 +00:00
|
|
|
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("grpc-skip-insecure")}))
|
2022-01-29 15:04:50 +00:00
|
|
|
} else {
|
|
|
|
transport = grpc.WithTransportCredentials(insecure.NewCredentials())
|
2020-05-16 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
authConn, err := grpc.Dial(
|
2017-06-28 17:21:22 +00:00
|
|
|
c.String("server"),
|
2020-05-16 18:56:24 +00:00
|
|
|
transport,
|
2023-01-28 13:13:04 +00:00
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: c.Duration("grpc-keepalive-time"),
|
|
|
|
Timeout: c.Duration("grpc-keepalive-timeout"),
|
2017-06-29 23:35:38 +00:00
|
|
|
}),
|
2023-01-28 13:13:04 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer authConn.Close()
|
|
|
|
|
2023-07-12 16:51:40 +00:00
|
|
|
agentConfig := readAgentConfig(agentConfigPath)
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
agentToken := c.String("grpc-token")
|
2023-07-12 16:51:40 +00:00
|
|
|
authClient := agentRpc.NewAuthGrpcClient(authConn, agentToken, agentConfig.AgentID)
|
2023-01-28 13:13:04 +00:00
|
|
|
authInterceptor, err := agentRpc.NewAuthInterceptor(authClient, 30*time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := grpc.Dial(
|
|
|
|
c.String("server"),
|
|
|
|
transport,
|
2018-01-08 18:47:08 +00:00
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
2022-01-31 14:38:00 +00:00
|
|
|
Time: c.Duration("grpc-keepalive-time"),
|
|
|
|
Timeout: c.Duration("grpc-keepalive-timeout"),
|
2018-01-08 18:47:08 +00:00
|
|
|
}),
|
2023-01-28 13:13:04 +00:00
|
|
|
grpc.WithUnaryInterceptor(authInterceptor.Unary()),
|
|
|
|
grpc.WithStreamInterceptor(authInterceptor.Stream()),
|
2017-03-16 10:14:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
2017-06-28 17:21:22 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
2023-01-28 13:13:04 +00:00
|
|
|
client := agentRpc.NewGrpcClient(conn)
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
sigterm := abool.New()
|
2017-07-19 21:46:03 +00:00
|
|
|
ctx := metadata.NewOutgoingContext(
|
|
|
|
context.Background(),
|
|
|
|
metadata.Pairs("hostname", hostname),
|
|
|
|
)
|
2023-11-01 23:53:47 +00:00
|
|
|
|
|
|
|
agentConfigPersisted := abool.New()
|
2022-02-28 08:27:31 +00:00
|
|
|
ctx = utils.WithContextSigtermCallback(ctx, func() {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Info().Msg("termination signal is received, shutting down")
|
2017-03-16 10:14:02 +00:00
|
|
|
sigterm.Set()
|
2023-11-01 23:53:47 +00:00
|
|
|
|
|
|
|
// Remove stateless agents from server
|
|
|
|
if agentConfigPersisted.IsNotSet() {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Debug().Msg("unregistering agent from server")
|
2023-11-01 23:53:47 +00:00
|
|
|
err := client.UnregisterAgent(ctx)
|
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Err(err).Msg("failed to unregister agent from server")
|
2023-11-01 23:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-16 10:14:02 +00:00
|
|
|
})
|
2016-09-29 21:45:13 +00:00
|
|
|
|
2023-03-19 21:42:21 +00:00
|
|
|
// check if grpc server version is compatible with agent
|
|
|
|
grpcServerVersion, err := client.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not get grpc server version")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if grpcServerVersion.GrpcVersion != agentRpc.ClientGrpcVersion {
|
|
|
|
err := errors.New("GRPC version mismatch")
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Error().Err(err).Msgf("server version %s does report grpc version %d but we only understand %d",
|
2023-03-19 21:42:21 +00:00
|
|
|
grpcServerVersion.ServerVersion,
|
|
|
|
grpcServerVersion.GrpcVersion,
|
|
|
|
agentRpc.ClientGrpcVersion)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-10-28 15:38:53 +00:00
|
|
|
parallel := c.Int("max-workflows")
|
2017-03-16 10:14:02 +00:00
|
|
|
wg.Add(parallel)
|
2016-09-29 21:45:13 +00:00
|
|
|
|
2023-12-20 13:26:57 +00:00
|
|
|
// new engine
|
|
|
|
backendCtx := context.WithValue(ctx, types.CliContext, c)
|
|
|
|
backendEngine, err := getBackendEngine(backendCtx, c.String("backend-engine"), c.StringSlice("addons"))
|
2022-09-03 18:41:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-20 13:26:57 +00:00
|
|
|
if !backendEngine.IsAvailable(backendCtx) {
|
|
|
|
log.Error().Str("engine", backendEngine.Name()).Msg("selected backend engine is unavailable")
|
|
|
|
return fmt.Errorf("selected backend engine %s is unavailable", backendEngine.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// load engine (e.g. init api client)
|
2023-12-14 18:20:47 +00:00
|
|
|
engInfo, err := backendEngine.Load(backendCtx)
|
2023-11-01 14:38:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("cannot load backend engine")
|
|
|
|
return err
|
|
|
|
}
|
2023-12-14 18:20:47 +00:00
|
|
|
log.Debug().Msgf("loaded %s backend engine", backendEngine.Name())
|
2023-11-01 14:38:37 +00:00
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, backendEngine.Name(), version.String(), parallel)
|
2023-01-28 13:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-01 23:53:47 +00:00
|
|
|
if agentConfigPath != "" {
|
|
|
|
if err := writeAgentConfig(agentConfig, agentConfigPath); err == nil {
|
|
|
|
agentConfigPersisted.Set()
|
|
|
|
}
|
|
|
|
}
|
2023-07-02 15:22:05 +00:00
|
|
|
|
2023-04-03 10:30:52 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"hostname": hostname,
|
2023-11-01 14:38:37 +00:00
|
|
|
"platform": engInfo.Platform,
|
2023-12-14 18:20:47 +00:00
|
|
|
"backend": backendEngine.Name(),
|
2023-04-03 10:30:52 +00:00
|
|
|
"repo": "*", // allow all repos by default
|
|
|
|
}
|
|
|
|
|
2023-04-28 18:41:05 +00:00
|
|
|
if err := stringSliceAddToMap(c.StringSlice("filter"), labels); err != nil {
|
|
|
|
return err
|
2023-04-03 10:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Debug().Msgf("agent registered with ID %d", agentConfig.AgentID)
|
2023-01-28 13:13:04 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if sigterm.IsSet() {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Debug().Msg("terminating health reporting")
|
2023-01-28 13:13:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ReportHealth(ctx)
|
|
|
|
if err != nil {
|
2024-01-11 18:17:07 +00:00
|
|
|
log.Err(err).Msg("failed to report health")
|
2023-01-28 13:13:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(time.Second * 10)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
for i := 0; i < parallel; i++ {
|
2023-07-20 18:39:20 +00:00
|
|
|
i := i
|
2017-03-16 10:14:02 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2023-12-14 18:20:47 +00:00
|
|
|
r := agent.NewRunner(client, filter, hostname, counter, &backendEngine)
|
2023-07-20 18:39:20 +00:00
|
|
|
log.Debug().Msgf("created new runner %d", i)
|
2022-03-08 15:21:43 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
if sigterm.IsSet() {
|
2023-11-01 23:53:47 +00:00
|
|
|
log.Debug().Msgf("terminating runner %d", i)
|
2021-11-26 02:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 15:38:53 +00:00
|
|
|
log.Debug().Msg("polling new steps")
|
2021-09-23 14:58:12 +00:00
|
|
|
if err := r.Run(ctx); err != nil {
|
2017-08-03 19:36:22 +00:00
|
|
|
log.Error().Err(err).Msg("pipeline done with error")
|
2017-03-16 10:14:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-04-20 01:37:53 +00:00
|
|
|
}
|
2016-05-24 01:47:44 +00:00
|
|
|
|
2022-09-03 18:41:23 +00:00
|
|
|
log.Info().Msgf(
|
2024-01-11 18:17:07 +00:00
|
|
|
"starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
|
2023-12-14 18:20:47 +00:00
|
|
|
version.String(), backendEngine.Name(), engInfo.Platform, parallel)
|
2022-09-03 18:41:23 +00:00
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-28 18:41:05 +00:00
|
|
|
|
2023-12-20 13:26:57 +00:00
|
|
|
func getBackendEngine(backendCtx context.Context, backendName string, addons []string) (types.Backend, error) {
|
|
|
|
addonBackend, err := addon.Load[types.Backend](addons, addonTypes.TypeBackend)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("cannot load backend addon")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addonBackend != nil {
|
|
|
|
return addonBackend.Value, nil
|
|
|
|
}
|
|
|
|
|
2024-01-11 21:15:15 +00:00
|
|
|
backend.Init()
|
2023-12-20 13:26:57 +00:00
|
|
|
engine, err := backend.FindBackend(backendCtx, backendName)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("cannot find backend engine '%s'", backendName)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return engine, nil
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:31:29 +00:00
|
|
|
func runWithRetry(context *cli.Context) error {
|
2024-01-10 14:34:44 +00:00
|
|
|
if err := logger.SetupGlobalLogger(context, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 20:35:37 +00:00
|
|
|
initHealth()
|
|
|
|
|
2023-05-03 11:31:29 +00:00
|
|
|
retryCount := context.Int("connect-retry-count")
|
|
|
|
retryDelay := context.Duration("connect-retry-delay")
|
|
|
|
var err error
|
|
|
|
for i := 0; i < retryCount; i++ {
|
|
|
|
if err = run(context); status.Code(err) == codes.Unavailable {
|
|
|
|
log.Warn().Err(err).Msg(fmt.Sprintf("cannot connect to server, retrying in %v", retryDelay))
|
|
|
|
time.Sleep(retryDelay)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-28 18:41:05 +00:00
|
|
|
func stringSliceAddToMap(sl []string, m map[string]string) error {
|
|
|
|
if m == nil {
|
|
|
|
m = make(map[string]string)
|
|
|
|
}
|
|
|
|
for _, v := range sl {
|
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
switch len(parts) {
|
|
|
|
case 2:
|
|
|
|
m[parts[0]] = parts[1]
|
|
|
|
case 1:
|
|
|
|
return fmt.Errorf("key '%s' does not have a value assigned", parts[0])
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("empty string in slice")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|