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"
|
2017-09-12 18:25:55 +00:00
|
|
|
"net/http"
|
2017-07-19 21:46:03 +00:00
|
|
|
"os"
|
2022-01-17 14:19:30 +00:00
|
|
|
"runtime"
|
2022-05-30 23:12:18 +00:00
|
|
|
"strings"
|
2016-04-20 01:37:53 +00:00
|
|
|
"sync"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/tevino/abool"
|
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"
|
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"
|
2017-06-28 17:21:22 +00:00
|
|
|
|
2021-09-23 14:58:12 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/agent"
|
2021-11-26 02:34:48 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend"
|
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"
|
2016-04-20 01:37:53 +00:00
|
|
|
)
|
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
func loop(c *cli.Context) error {
|
2017-07-19 21:46:03 +00:00
|
|
|
hostname := c.String("hostname")
|
|
|
|
if len(hostname) == 0 {
|
|
|
|
hostname, _ = os.Hostname()
|
|
|
|
}
|
|
|
|
|
2022-05-30 23:12:18 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"hostname": hostname,
|
|
|
|
"platform": runtime.GOOS + "/" + runtime.GOARCH,
|
|
|
|
"repo": "*", // allow all repos by default
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range c.StringSlice("filter-labels") {
|
|
|
|
parts := strings.SplitN(v, "=", 2)
|
|
|
|
labels[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := rpc.Filter{
|
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:25:55 +00:00
|
|
|
if c.Bool("pretty") {
|
|
|
|
log.Logger = log.Output(
|
|
|
|
zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stderr,
|
2021-10-27 19:03:14 +00:00
|
|
|
NoColor: c.Bool("nocolor"),
|
2017-09-12 18:25:55 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-16 22:41:36 +00:00
|
|
|
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
2021-12-08 22:40:00 +00:00
|
|
|
if zerolog.GlobalLevel() <= zerolog.DebugLevel {
|
|
|
|
log.Logger = log.With().Caller().Logger()
|
|
|
|
}
|
2021-10-16 22:41:36 +00: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)
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:40:24 +00:00
|
|
|
counter.Polling = c.Int("max-procs")
|
|
|
|
counter.Running = 0
|
|
|
|
|
2021-10-27 19:03:14 +00:00
|
|
|
if c.Bool("healthcheck") {
|
|
|
|
go func() {
|
|
|
|
if err := http.ListenAndServe(":3000", nil); err != nil {
|
|
|
|
log.Error().Msgf("can not listen on port 3000: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2017-09-12 18:25:55 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
// TODO pass version information to grpc server
|
|
|
|
// TODO authenticate to grpc server
|
|
|
|
|
2017-06-29 23:35:38 +00:00
|
|
|
// grpc.Dial(target, ))
|
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") {
|
2020-05-16 18:56:24 +00:00
|
|
|
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("skip-insecure-grpc")}))
|
2022-01-29 15:04:50 +00:00
|
|
|
} else {
|
|
|
|
transport = grpc.WithTransportCredentials(insecure.NewCredentials())
|
2020-05-16 18:56:24 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 17:21:22 +00:00
|
|
|
conn, err := grpc.Dial(
|
|
|
|
c.String("server"),
|
2020-05-16 18:56:24 +00:00
|
|
|
transport,
|
2017-06-29 23:35:38 +00:00
|
|
|
grpc.WithPerRPCCredentials(&credentials{
|
2022-01-31 14:38:00 +00:00
|
|
|
username: c.String("grpc-username"),
|
|
|
|
password: c.String("grpc-password"),
|
2017-06-29 23:35:38 +00:00
|
|
|
}),
|
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
|
|
|
}),
|
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()
|
|
|
|
|
|
|
|
client := rpc.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),
|
|
|
|
)
|
2022-02-28 08:27:31 +00:00
|
|
|
ctx = utils.WithContextSigtermCallback(ctx, func() {
|
2017-03-16 10:14:02 +00:00
|
|
|
println("ctrl+c received, terminating process")
|
|
|
|
sigterm.Set()
|
|
|
|
})
|
2016-09-29 21:45:13 +00:00
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
parallel := c.Int("max-procs")
|
|
|
|
wg.Add(parallel)
|
2016-09-29 21:45:13 +00:00
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
for i := 0; i < parallel; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-03-08 15:21:43 +00:00
|
|
|
// new engine
|
|
|
|
engine, err := backend.FindEngine(c.String("backend-engine"))
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("cannot find backend engine '%s'", c.String("backend-engine"))
|
|
|
|
return
|
|
|
|
}
|
2021-09-23 14:58:12 +00:00
|
|
|
|
2022-03-08 15:21:43 +00:00
|
|
|
// load engine (e.g. init api client)
|
|
|
|
err = engine.Load()
|
|
|
|
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 02:34:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:21:43 +00:00
|
|
|
log.Debug().Msg("polling new jobs")
|
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
|
|
|
|
2017-03-16 10:14:02 +00:00
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-24 01:47:44 +00:00
|
|
|
|
2017-06-29 23:35:38 +00:00
|
|
|
type credentials struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
|
|
|
|
2021-10-19 09:44:49 +00:00
|
|
|
func (c *credentials) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
|
2017-06-29 23:35:38 +00:00
|
|
|
return map[string]string{
|
|
|
|
"username": c.username,
|
|
|
|
"password": c.password,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *credentials) RequireTransportSecurity() bool {
|
|
|
|
return false
|
|
|
|
}
|