2023-03-19 23:42:21 +02:00
|
|
|
// Copyright 2023 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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-30 00:51:22 +02:00
|
|
|
package main
|
2016-04-20 03:37:53 +02:00
|
|
|
|
|
|
|
import (
|
2017-03-16 12:14:02 +02:00
|
|
|
"context"
|
2020-05-16 20:56:24 +02:00
|
|
|
"crypto/tls"
|
2023-03-19 23:42:21 +02:00
|
|
|
"errors"
|
2023-04-28 20:41:05 +02:00
|
|
|
"fmt"
|
2017-09-12 20:25:55 +02:00
|
|
|
"net/http"
|
2017-07-19 23:46:03 +02:00
|
|
|
"os"
|
2022-01-17 16:19:30 +02:00
|
|
|
"runtime"
|
2022-05-31 01:12:18 +02:00
|
|
|
"strings"
|
2016-04-20 03:37:53 +02:00
|
|
|
"sync"
|
2023-01-28 15:13:04 +02:00
|
|
|
"time"
|
2016-04-20 03:37:53 +02:00
|
|
|
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-09-14 07:34:36 +02:00
|
|
|
"github.com/tevino/abool/v2"
|
2021-10-27 21:03:14 +02:00
|
|
|
"github.com/urfave/cli/v2"
|
2017-06-28 19:21:22 +02:00
|
|
|
"google.golang.org/grpc"
|
2023-05-03 13:31:29 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
2021-10-12 09:25:13 +02:00
|
|
|
grpccredentials "google.golang.org/grpc/credentials"
|
2022-01-29 17:04:50 +02:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2018-01-08 17:28:38 +02:00
|
|
|
"google.golang.org/grpc/keepalive"
|
2017-07-19 23:46:03 +02:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-05-03 13:31:29 +02:00
|
|
|
"google.golang.org/grpc/status"
|
2017-06-28 19:21:22 +02:00
|
|
|
|
2021-09-23 16:58:12 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/agent"
|
2023-01-28 15:13:04 +02:00
|
|
|
agentRpc "github.com/woodpecker-ci/woodpecker/agent/rpc"
|
2023-08-07 20:47:30 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
2021-11-26 04:34:48 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
2022-09-05 06:01:14 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2021-09-24 13:18:34 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
2022-02-28 10:27:31 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2022-09-03 20:41:23 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/version"
|
2016-04-20 03:37:53 +02:00
|
|
|
)
|
|
|
|
|
2023-05-03 13:31:29 +02:00
|
|
|
func run(c *cli.Context) error {
|
2023-08-07 20:47:30 +02:00
|
|
|
common.SetupGlobalLogger(c)
|
|
|
|
|
2023-07-12 18:51:40 +02:00
|
|
|
agentConfigPath := c.String("agent-config")
|
2017-07-19 23:46:03 +02:00
|
|
|
hostname := c.String("hostname")
|
|
|
|
if len(hostname) == 0 {
|
|
|
|
hostname, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
2023-01-28 15:13:04 +02:00
|
|
|
platform := runtime.GOOS + "/" + runtime.GOARCH
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
counter.Polling = c.Int("max-workflows")
|
2017-09-12 22:40:24 +02:00
|
|
|
counter.Running = 0
|
|
|
|
|
2021-10-27 21:03:14 +02: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 13:06:51 +02:00
|
|
|
if err := http.ListenAndServe(c.String("healthcheck-addr"), nil); err != nil {
|
|
|
|
log.Error().Msgf("cannot listen on address %s: %v", c.String("healthcheck-addr"), err)
|
2021-10-27 21:03:14 +02:00
|
|
|
}
|
|
|
|
}()
|
2017-09-12 20:25:55 +02:00
|
|
|
}
|
|
|
|
|
2022-01-29 17:04:50 +02:00
|
|
|
var transport grpc.DialOption
|
2022-01-31 16:38:00 +02:00
|
|
|
if c.Bool("grpc-secure") {
|
2023-08-01 09:06:27 +02:00
|
|
|
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("grpc-skip-insecure")}))
|
2022-01-29 17:04:50 +02:00
|
|
|
} else {
|
|
|
|
transport = grpc.WithTransportCredentials(insecure.NewCredentials())
|
2020-05-16 20:56:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 15:13:04 +02:00
|
|
|
authConn, err := grpc.Dial(
|
2017-06-28 19:21:22 +02:00
|
|
|
c.String("server"),
|
2020-05-16 20:56:24 +02:00
|
|
|
transport,
|
2023-01-28 15:13:04 +02:00
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
|
|
|
Time: c.Duration("grpc-keepalive-time"),
|
|
|
|
Timeout: c.Duration("grpc-keepalive-timeout"),
|
2017-06-30 01:35:38 +02:00
|
|
|
}),
|
2023-01-28 15:13:04 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer authConn.Close()
|
|
|
|
|
2023-07-12 18:51:40 +02:00
|
|
|
agentConfig := readAgentConfig(agentConfigPath)
|
|
|
|
|
2023-01-28 15:13:04 +02:00
|
|
|
agentToken := c.String("grpc-token")
|
2023-07-12 18:51:40 +02:00
|
|
|
authClient := agentRpc.NewAuthGrpcClient(authConn, agentToken, agentConfig.AgentID)
|
2023-01-28 15:13:04 +02: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 20:47:08 +02:00
|
|
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
2022-01-31 16:38:00 +02:00
|
|
|
Time: c.Duration("grpc-keepalive-time"),
|
|
|
|
Timeout: c.Duration("grpc-keepalive-timeout"),
|
2018-01-08 20:47:08 +02:00
|
|
|
}),
|
2023-01-28 15:13:04 +02:00
|
|
|
grpc.WithUnaryInterceptor(authInterceptor.Unary()),
|
|
|
|
grpc.WithStreamInterceptor(authInterceptor.Stream()),
|
2017-03-16 12:14:02 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-03-05 09:56:08 +02:00
|
|
|
}
|
2017-06-28 19:21:22 +02:00
|
|
|
defer conn.Close()
|
|
|
|
|
2023-01-28 15:13:04 +02:00
|
|
|
client := agentRpc.NewGrpcClient(conn)
|
2017-03-05 09:56:08 +02:00
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
sigterm := abool.New()
|
2017-07-19 23:46:03 +02:00
|
|
|
ctx := metadata.NewOutgoingContext(
|
|
|
|
context.Background(),
|
|
|
|
metadata.Pairs("hostname", hostname),
|
|
|
|
)
|
2022-02-28 10:27:31 +02:00
|
|
|
ctx = utils.WithContextSigtermCallback(ctx, func() {
|
2017-03-16 12:14:02 +02:00
|
|
|
println("ctrl+c received, terminating process")
|
|
|
|
sigterm.Set()
|
|
|
|
})
|
2016-09-29 23:45:13 +02:00
|
|
|
|
2023-03-19 23:42:21 +02: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")
|
|
|
|
log.Error().Err(err).Msgf("Server version %s does report grpc version %d but we only understand %d",
|
|
|
|
grpcServerVersion.ServerVersion,
|
|
|
|
grpcServerVersion.GrpcVersion,
|
|
|
|
agentRpc.ClientGrpcVersion)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:24:43 +02:00
|
|
|
backendCtx := context.WithValue(ctx, types.CliContext, c)
|
|
|
|
backend.Init(backendCtx)
|
2022-09-05 06:01:14 +02:00
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
var wg sync.WaitGroup
|
2022-10-28 17:38:53 +02:00
|
|
|
parallel := c.Int("max-workflows")
|
2017-03-16 12:14:02 +02:00
|
|
|
wg.Add(parallel)
|
2016-09-29 23:45:13 +02:00
|
|
|
|
2022-09-03 20:41:23 +02:00
|
|
|
// new engine
|
2023-03-19 21:24:43 +02:00
|
|
|
engine, err := backend.FindEngine(backendCtx, c.String("backend-engine"))
|
2022-09-03 20:41:23 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:51:40 +02:00
|
|
|
agentConfig.AgentID, err = client.RegisterAgent(ctx, platform, engine.Name(), version.String(), parallel)
|
2023-01-28 15:13:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:51:40 +02:00
|
|
|
writeAgentConfig(agentConfig, agentConfigPath)
|
2023-07-02 17:22:05 +02:00
|
|
|
|
2023-04-03 12:30:52 +02:00
|
|
|
labels := map[string]string{
|
|
|
|
"hostname": hostname,
|
|
|
|
"platform": platform,
|
|
|
|
"backend": engine.Name(),
|
|
|
|
"repo": "*", // allow all repos by default
|
|
|
|
}
|
|
|
|
|
2023-04-28 20:41:05 +02:00
|
|
|
if err := stringSliceAddToMap(c.StringSlice("filter"), labels); err != nil {
|
|
|
|
return err
|
2023-04-03 12:30:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
2023-07-12 18:51:40 +02:00
|
|
|
log.Debug().Msgf("Agent registered with ID %d", agentConfig.AgentID)
|
2023-01-28 15:13:04 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if sigterm.IsSet() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ReportHealth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msgf("Failed to report health")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(time.Second * 10)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-07-20 20:39:20 +02:00
|
|
|
// load engine (e.g. init api client)
|
|
|
|
if err := engine.Load(backendCtx); err != nil {
|
|
|
|
log.Error().Err(err).Msg("cannot load backend engine")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("loaded %s backend engine", engine.Name())
|
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
for i := 0; i < parallel; i++ {
|
2023-07-20 20:39:20 +02:00
|
|
|
i := i
|
2017-03-16 12:14:02 +02:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-09-23 16:58:12 +02:00
|
|
|
|
2022-03-08 17:21:43 +02:00
|
|
|
r := agent.NewRunner(client, filter, hostname, counter, &engine)
|
2023-07-20 20:39:20 +02:00
|
|
|
log.Debug().Msgf("created new runner %d", i)
|
2022-03-08 17:21:43 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
if sigterm.IsSet() {
|
2021-11-26 04:34:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
log.Debug().Msg("polling new steps")
|
2021-09-23 16:58:12 +02:00
|
|
|
if err := r.Run(ctx); err != nil {
|
2017-08-03 21:36:22 +02:00
|
|
|
log.Error().Err(err).Msg("pipeline done with error")
|
2017-03-16 12:14:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-04-20 03:37:53 +02:00
|
|
|
}
|
2016-05-24 03:47:44 +02:00
|
|
|
|
2022-09-03 20:41:23 +02:00
|
|
|
log.Info().Msgf(
|
2023-01-28 15:13:04 +02:00
|
|
|
"Starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
|
|
|
|
version.String(), engine.Name(), platform, parallel)
|
2022-09-03 20:41:23 +02:00
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-28 20:41:05 +02:00
|
|
|
|
2023-05-03 13:31:29 +02:00
|
|
|
func runWithRetry(context *cli.Context) error {
|
|
|
|
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 20:41:05 +02: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
|
|
|
|
}
|