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
|
|
|
|
2021-09-23 14:58:12 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/agent"
|
2023-01-28 13:13:04 +00:00
|
|
|
agentRpc "github.com/woodpecker-ci/woodpecker/agent/rpc"
|
2023-08-07 18:47:30 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/cmd/common"
|
2021-11-26 02:34:48 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
2022-09-05 04:01:14 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
2021-09-24 11:18:34 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/rpc"
|
2022-02-28 08:27:31 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/utils"
|
2022-09-03 18:41:23 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/version"
|
2016-04-20 01:37:53 +00:00
|
|
|
)
|
|
|
|
|
2023-05-03 11:31:29 +00:00
|
|
|
func run(c *cli.Context) error {
|
2023-10-24 10:23:42 +00:00
|
|
|
common.SetupGlobalLogger(c, true)
|
2023-08-07 18:47:30 +00:00
|
|
|
|
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 {
|
|
|
|
log.Error().Msgf("cannot listen on address %s: %v", c.String("healthcheck-addr"), err)
|
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() {
|
2023-11-01 23:53:47 +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() {
|
|
|
|
log.Debug().Msg("Unregistering agent from server")
|
|
|
|
err := client.UnregisterAgent(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to unregister agent from server")
|
|
|
|
}
|
|
|
|
}
|
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")
|
|
|
|
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 19:24:43 +00:00
|
|
|
backendCtx := context.WithValue(ctx, types.CliContext, c)
|
|
|
|
backend.Init(backendCtx)
|
2022-09-05 04:01:14 +00:00
|
|
|
|
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
|
|
|
|
2022-09-03 18:41:23 +00:00
|
|
|
// new engine
|
2023-03-19 19:24:43 +00:00
|
|
|
engine, err := backend.FindEngine(backendCtx, c.String("backend-engine"))
|
2022-09-03 18:41:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine"))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-01 14:38:37 +00:00
|
|
|
// load engine (e.g. init api client)
|
|
|
|
engInfo, err := engine.Load(backendCtx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("cannot load backend engine")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debug().Msgf("loaded %s backend engine", engine.Name())
|
|
|
|
|
|
|
|
agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, engine.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-04-03 10:30:52 +00:00
|
|
|
"backend": engine.Name(),
|
|
|
|
"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,
|
|
|
|
}
|
|
|
|
|
2023-07-12 16:51:40 +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() {
|
2023-11-01 23:53:47 +00:00
|
|
|
log.Debug().Msg("Terminating health reporting")
|
2023-01-28 13:13:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.ReportHealth(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msgf("Failed to report health")
|
|
|
|
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
|
|
|
|
2022-03-08 15:21:43 +00:00
|
|
|
r := agent.NewRunner(client, filter, hostname, counter, &engine)
|
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(
|
2023-01-28 13:13:04 +00:00
|
|
|
"Starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
|
2023-11-01 14:38:37 +00:00
|
|
|
version.String(), engine.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-05-03 11:31:29 +00: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 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
|
|
|
|
}
|