1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-06 08:16:19 +02:00

Fix pipeline backend autodetect (#545)

* refactor:
 - rename IsAvivable -> IsAvailable
 - drop depricated Kill
 - make sure backends implement interface
 - rename backend struct for ide (better info)

* docker backend fix autodetect
This commit is contained in:
6543 2021-11-27 02:29:14 +01:00 committed by GitHub
parent 5e6b38e0e7
commit e072e4cce7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 42 deletions

View File

@ -27,7 +27,7 @@ func init() {
func FindEngine(engineName string) (types.Engine, error) {
if engineName == "auto-detect" {
for _, engine := range engines {
if engine.IsAvivable() {
if engine.IsAvailable() {
return engine, nil
}
}

View File

@ -17,28 +17,31 @@ import (
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
)
type engine struct {
type docker struct {
client client.APIClient
}
// make sure docker implements Engine
var _ backend.Engine = &docker{}
// New returns a new Docker Engine.
func New() backend.Engine {
return &engine{
return &docker{
client: nil,
}
}
func (e *engine) Name() string {
func (e *docker) Name() string {
return "docker"
}
func (e *engine) IsAvivable() bool {
_, err := os.Stat("/.dockerenv")
return os.IsNotExist(err)
func (e *docker) IsAvailable() bool {
_, err := os.Stat("/var/run/docker.sock")
return err == nil
}
// Load new client for Docker Engine using environment variables.
func (e *engine) Load() error {
func (e *docker) Load() error {
cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return err
@ -48,7 +51,7 @@ func (e *engine) Load() error {
return nil
}
func (e *engine) Setup(_ context.Context, conf *backend.Config) error {
func (e *docker) Setup(_ context.Context, conf *backend.Config) error {
for _, vol := range conf.Volumes {
_, err := e.client.VolumeCreate(noContext, volume.VolumeCreateBody{
Name: vol.Name,
@ -73,7 +76,7 @@ func (e *engine) Setup(_ context.Context, conf *backend.Config) error {
return nil
}
func (e *engine) Exec(ctx context.Context, proc *backend.Step) error {
func (e *docker) Exec(ctx context.Context, proc *backend.Step) error {
config := toConfig(proc)
hostConfig := toHostConfig(proc)
@ -144,11 +147,7 @@ func (e *engine) Exec(ctx context.Context, proc *backend.Step) error {
return e.client.ContainerStart(ctx, proc.Name, startOpts)
}
func (e *engine) Kill(_ context.Context, proc *backend.Step) error {
return e.client.ContainerKill(noContext, proc.Name, "9")
}
func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) {
func (e *docker) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) {
wait, errc := e.client.ContainerWait(ctx, proc.Name, "")
select {
case <-wait:
@ -170,7 +169,7 @@ func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State,
}, nil
}
func (e *engine) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, error) {
func (e *docker) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, error) {
logs, err := e.client.ContainerLogs(ctx, proc.Name, logsOpts)
if err != nil {
return nil, err
@ -187,7 +186,7 @@ func (e *engine) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, e
return rc, nil
}
func (e *engine) Destroy(_ context.Context, conf *backend.Config) error {
func (e *docker) Destroy(_ context.Context, conf *backend.Config) error {
for _, stage := range conf.Stages {
for _, step := range stage.Steps {
if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil {

View File

@ -20,15 +20,15 @@ package docker
// func (p *Pool) Reserve(c context.Context) backend.Engine {
// select {
// case <-c.Done():
// case engine := <-p.queue:
// return engine
// case docker := <-p.queue:
// return docker
// }
// return nil
// }
//
// // Release releases the Docker client back to the pool.
// func (p *Pool) Release(engine backend.Engine) {
// p.queue <- engine
// func (p *Pool) Release(docker backend.Engine) {
// p.queue <- docker
// }
// pool := docker.Pool(

View File

@ -8,68 +8,65 @@ import (
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
)
type engine struct {
type kube struct {
namespace string
endpoint string
token string
}
// make sure kube implements Engine
var _ types.Engine = &kube{}
// New returns a new Kubernetes Engine.
func New(namespace, endpoint, token string) types.Engine {
return &engine{
return &kube{
namespace: namespace,
endpoint: endpoint,
token: token,
}
}
func (e *engine) Name() string {
func (e *kube) Name() string {
return "kubernetes"
}
func (e *engine) IsAvivable() bool {
func (e *kube) IsAvailable() bool {
host := os.Getenv("KUBERNETES_SERVICE_HOST")
return len(host) > 0
}
func (e *engine) Load() error {
func (e *kube) Load() error {
return nil
}
// Setup the pipeline environment.
func (e *engine) Setup(context.Context, *types.Config) error {
func (e *kube) Setup(context.Context, *types.Config) error {
// POST /api/v1/namespaces
return nil
}
// Start the pipeline step.
func (e *engine) Exec(context.Context, *types.Step) error {
// Exec the pipeline step.
func (e *kube) Exec(context.Context, *types.Step) error {
// POST /api/v1/namespaces/{namespace}/pods
return nil
}
// DEPRECATED
// Kill the pipeline step.
func (e *engine) Kill(context.Context, *types.Step) error {
return nil
}
// Wait for the pipeline step to complete and returns
// the completion results.
func (e *engine) Wait(context.Context, *types.Step) (*types.State, error) {
func (e *kube) Wait(context.Context, *types.Step) (*types.State, error) {
// GET /api/v1/watch/namespaces/{namespace}/pods
// GET /api/v1/watch/namespaces/{namespace}/pods/{name}
return nil, nil
}
// Tail the pipeline step logs.
func (e *engine) Tail(context.Context, *types.Step) (io.ReadCloser, error) {
func (e *kube) Tail(context.Context, *types.Step) (io.ReadCloser, error) {
// GET /api/v1/namespaces/{namespace}/pods/{name}/log
return nil, nil
}
// Destroy the pipeline environment.
func (e *engine) Destroy(context.Context, *types.Config) error {
func (e *kube) Destroy(context.Context, *types.Config) error {
// DELETE /api/v1/namespaces/{name}
return nil
}

View File

@ -10,7 +10,7 @@ import (
type Engine interface {
Name() string
IsAvivable() bool
IsAvailable() bool
Load() error
@ -20,9 +20,6 @@ type Engine interface {
// Exec start the pipeline step.
Exec(context.Context, *Step) error
// Kill the pipeline step.
Kill(context.Context, *Step) error
// Wait for the pipeline step to complete and returns
// the completion results.
Wait(context.Context, *Step) (*State, error)