mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-23 17:53:23 +02:00
ee969979c6
Closes https://github.com/woodpecker-ci/woodpecker/issues/1617 The `woodpecker exec` auto-detects the backend by iterating over a map of backends. However, since Go 1 the runtime randomizes map iteration order, so a random backend might be chosen during each execution. PR changes to auto-detection to iterate over the backends list with predefined priority: `docker`, `local`, `ssh`, `kubernetes`. --------- Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/docker"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/kubernetes"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/local"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/ssh"
|
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
|
)
|
|
|
|
var (
|
|
enginesByName map[string]types.Engine
|
|
engines []types.Engine
|
|
)
|
|
|
|
func Init(ctx context.Context) {
|
|
engines = []types.Engine{
|
|
docker.New(),
|
|
local.New(),
|
|
ssh.New(),
|
|
kubernetes.New(ctx),
|
|
}
|
|
|
|
enginesByName = make(map[string]types.Engine)
|
|
for _, engine := range engines {
|
|
enginesByName[engine.Name()] = engine
|
|
}
|
|
}
|
|
|
|
func FindEngine(engineName string) (types.Engine, error) {
|
|
if engineName == "auto-detect" {
|
|
for _, engine := range engines {
|
|
if engine.IsAvailable() {
|
|
return engine, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("Can't detect an available backend engine")
|
|
}
|
|
|
|
engine, ok := enginesByName[engineName]
|
|
if !ok {
|
|
return nil, fmt.Errorf("Backend engine '%s' not found", engineName)
|
|
}
|
|
|
|
return engine, nil
|
|
}
|