Make agent usable for external backends (#3270)

This commit is contained in:
qwerty287
2024-02-08 16:33:22 +01:00
committed by GitHub
parent e64d596436
commit f92f8b17a3
20 changed files with 142 additions and 104 deletions

View File

@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"context"
@@ -42,14 +42,12 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/rpc"
"go.woodpecker-ci.org/woodpecker/v2/shared/addon"
addonTypes "go.woodpecker-ci.org/woodpecker/v2/shared/addon/types"
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
"go.woodpecker-ci.org/woodpecker/v2/version"
)
func run(c *cli.Context) error {
func run(c *cli.Context, backendEngines []types.Backend) error {
agentConfigPath := c.String("agent-config")
hostname := c.String("hostname")
if len(hostname) == 0 {
@@ -155,11 +153,13 @@ func run(c *cli.Context) error {
// new engine
backendCtx := context.WithValue(ctx, types.CliContext, c)
backendEngine, err := getBackendEngine(backendCtx, c.String("backend-engine"), c.StringSlice("addons"))
backend.Init(backendEngines)
backendName := c.String("backend-engine")
backendEngine, err := backend.FindBackend(backendCtx, 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())
@@ -249,44 +249,27 @@ func run(c *cli.Context) error {
return nil
}
func getBackendEngine(backendCtx context.Context, backendName string, addons []string) (types.Backend, error) {
addonBackend, err := addon.Load[types.Backend](addons, addonTypes.TypeBackend)
if err != nil {
log.Error().Err(err).Msg("cannot load backend addon")
return nil, err
}
if addonBackend != nil {
return addonBackend.Value, nil
}
func runWithRetry(backendEngines []types.Backend) func(context *cli.Context) error {
return func(context *cli.Context) error {
if err := logger.SetupGlobalLogger(context, true); err != nil {
return err
}
backend.Init()
engine, err := backend.FindBackend(backendCtx, backendName)
if err != nil {
log.Error().Err(err).Msgf("cannot find backend engine '%s'", backendName)
return nil, err
}
return engine, nil
}
initHealth()
func runWithRetry(context *cli.Context) error {
if err := logger.SetupGlobalLogger(context, true); err != nil {
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, backendEngines); 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
}
initHealth()
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
}
func stringSliceAddToMap(sl []string, m map[string]string) error {

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"testing"

View File

@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"bytes"

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"os"

View File

@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"os"
@@ -97,9 +97,4 @@ var flags = []cli.Flag{
Usage: "backend to run pipelines on",
Value: "auto-detect",
},
&cli.StringSliceFlag{
EnvVars: []string{"WOODPECKER_ADDONS"},
Name: "addons",
Usage: "list of addon files",
},
}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"encoding/json"

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package main
package core
import (
"testing"

53
cmd/agent/core/run.go Normal file
View File

@@ -0,0 +1,53 @@
// Copyright 2024 Woodpecker Authors
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
package core
import (
"os"
// Load config from .env
_ "github.com/joho/godotenv/autoload"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
backend "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
"go.woodpecker-ci.org/woodpecker/v2/version"
)
func RunAgent(backends []backend.Backend) {
app := cli.NewApp()
app.Name = "woodpecker-agent"
app.Version = version.String()
app.Usage = "woodpecker agent"
app.Action = runWithRetry(backends)
app.Commands = []*cli.Command{
{
Name: "ping",
Usage: "ping the agent",
Action: pinger,
},
}
agentFlags := utils.MergeSlices(flags, logger.GlobalLoggerFlags)
for _, b := range backends {
agentFlags = utils.MergeSlices(agentFlags, b.Flags())
}
app.Flags = agentFlags
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("error running agent") //nolint:forbidigo
}
}

View File

@@ -15,36 +15,17 @@
package main
import (
"os"
_ "github.com/joho/godotenv/autoload"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"go.woodpecker-ci.org/woodpecker/v2/cmd/agent/core"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/docker"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/kubernetes"
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/local"
"go.woodpecker-ci.org/woodpecker/v2/shared/logger"
"go.woodpecker-ci.org/woodpecker/v2/shared/utils"
"go.woodpecker-ci.org/woodpecker/v2/version"
backendTypes "go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
)
func main() {
app := cli.NewApp()
app.Name = "woodpecker-agent"
app.Version = version.String()
app.Usage = "woodpecker agent"
app.Action = runWithRetry
app.Commands = []*cli.Command{
{
Name: "ping",
Usage: "ping the agent",
Action: pinger,
},
}
app.Flags = utils.MergeSlices(flags, logger.GlobalLoggerFlags, docker.Flags, kubernetes.Flags, local.Flags)
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("error running agent") //nolint:forbidigo
}
core.RunAgent([]backendTypes.Backend{
docker.New(),
local.New(),
kubernetes.New(),
})
}