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"
|
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"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/tevino/abool"
|
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"
|
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"
|
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"
|
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
|
|
|
)
|
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
func loop(c *cli.Context) error {
|
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-05-31 01:12:18 +02:00
|
|
|
labels := map[string]string{
|
|
|
|
"hostname": hostname,
|
2023-01-28 15:13:04 +02:00
|
|
|
"platform": platform,
|
2022-05-31 01:12:18 +02:00
|
|
|
"repo": "*", // allow all repos by default
|
|
|
|
}
|
|
|
|
|
2022-08-14 09:48:52 +02:00
|
|
|
for _, v := range c.StringSlice("filter") {
|
2022-05-31 01:12:18 +02:00
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
labels[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:25:55 +02:00
|
|
|
if c.Bool("pretty") {
|
|
|
|
log.Logger = log.Output(
|
|
|
|
zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stderr,
|
2021-10-27 21:03:14 +02:00
|
|
|
NoColor: c.Bool("nocolor"),
|
2017-09-12 20:25:55 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-03 20:41:23 +02:00
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
2021-10-17 00:41:36 +02:00
|
|
|
if c.IsSet("log-level") {
|
|
|
|
logLevelFlag := c.String("log-level")
|
|
|
|
lvl, err := zerolog.ParseLevel(logLevelFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Msgf("unknown logging level: %s", logLevelFlag)
|
|
|
|
}
|
|
|
|
zerolog.SetGlobalLevel(lvl)
|
|
|
|
}
|
2022-09-03 20:41:23 +02:00
|
|
|
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
|
|
|
log.Logger = log.With().Caller().Logger()
|
|
|
|
}
|
2021-10-17 00:41:36 +02:00
|
|
|
|
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") {
|
2020-05-16 20:56:24 +02:00
|
|
|
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("skip-insecure-grpc")}))
|
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()
|
|
|
|
|
|
|
|
agentID := int64(-1) // TODO: store agent id in a file
|
|
|
|
agentToken := c.String("grpc-token")
|
|
|
|
authClient := agentRpc.NewAuthGrpcClient(authConn, agentToken, agentID)
|
|
|
|
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-01-28 15:13:04 +02:00
|
|
|
agentID, err = client.RegisterAgent(ctx, platform, engine.Name(), version.String(), parallel)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().Msgf("Agent registered with ID %d", agentID)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-16 12:14:02 +02:00
|
|
|
for i := 0; i < parallel; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-09-23 16:58:12 +02:00
|
|
|
|
2022-03-08 17:21:43 +02:00
|
|
|
// load engine (e.g. init api client)
|
2023-03-19 21:24:43 +02:00
|
|
|
err = engine.Load(backendCtx)
|
2022-03-08 17:21:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("cannot load backend engine")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := agent.NewRunner(client, filter, hostname, counter, &engine)
|
|
|
|
|
|
|
|
log.Debug().Msgf("loaded %s backend engine", engine.Name())
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|