woodpecker/cmd/agent/agent.go

164 lines
3.6 KiB
Go
Raw Normal View History

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"
"crypto/tls"
2017-09-12 18:25:55 +00:00
"net/http"
"os"
2016-04-20 01:37:53 +00:00
"sync"
grpccredentials "google.golang.org/grpc/credentials"
2017-06-28 17:21:22 +00:00
"google.golang.org/grpc"
2018-01-08 15:28:38 +00:00
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
2017-06-28 17:21:22 +00:00
"github.com/woodpecker-ci/woodpecker/agent"
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/backend/docker"
"github.com/woodpecker-ci/woodpecker/cncd/pipeline/pipeline/rpc"
2016-09-26 08:29:05 +00:00
2017-08-03 19:36:22 +00:00
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
2017-03-16 10:14:02 +00:00
"github.com/tevino/abool"
"github.com/urfave/cli"
2017-06-29 23:35:38 +00:00
oldcontext "golang.org/x/net/context"
2016-04-20 01:37:53 +00:00
)
2017-03-16 10:14:02 +00:00
func loop(c *cli.Context) error {
filter := rpc.Filter{
Labels: map[string]string{
"platform": c.String("platform"),
},
Expr: c.String("filter"),
2017-03-16 10:14:02 +00:00
}
2016-04-20 01:37:53 +00:00
hostname := c.String("hostname")
if len(hostname) == 0 {
hostname, _ = os.Hostname()
}
2017-08-03 19:36:22 +00:00
if c.BoolT("debug") {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.WarnLevel)
}
2017-09-12 18:25:55 +00:00
if c.Bool("pretty") {
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: c.BoolT("nocolor"),
},
)
}
2017-09-12 20:40:24 +00:00
counter.Polling = c.Int("max-procs")
counter.Running = 0
2017-09-12 18:25:55 +00:00
if c.BoolT("healthcheck") {
go http.ListenAndServe(":3000", nil)
}
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, ))
var transport = grpc.WithInsecure()
if c.Bool("secure-grpc") {
transport = grpc.WithTransportCredentials(grpccredentials.NewTLS(&tls.Config{InsecureSkipVerify: c.Bool("skip-insecure-grpc")}))
}
2017-06-28 17:21:22 +00:00
conn, err := grpc.Dial(
c.String("server"),
transport,
2017-06-29 23:35:38 +00:00
grpc.WithPerRPCCredentials(&credentials{
username: c.String("username"),
password: c.String("password"),
}),
grpc.WithKeepaliveParams(keepalive.ClientParameters{
2018-03-21 13:02:17 +00:00
Time: c.Duration("keepalive-time"),
Timeout: c.Duration("keepalive-timeout"),
}),
2017-03-16 10:14:02 +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()
ctx := metadata.NewOutgoingContext(
context.Background(),
metadata.Pairs("hostname", hostname),
)
ctx = WithContextFunc(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()
for {
if sigterm.IsSet() {
return
}
// new docker engine
engine, err := docker.NewEnv()
if err != nil {
log.Error().Err(err).Msg("cannot create docker client")
return
2017-09-14 16:39:52 +00:00
}
r := agent.NewRunner(client, filter, hostname, counter, &engine)
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
}
2017-03-16 10:14:02 +00:00
wg.Wait()
return nil
}
2017-06-29 23:35:38 +00:00
type credentials struct {
username string
password string
}
func (c *credentials) GetRequestMetadata(oldcontext.Context, ...string) (map[string]string, error) {
return map[string]string{
"username": c.username,
"password": c.password,
}, nil
}
func (c *credentials) RequireTransportSecurity() bool {
return false
}