mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-18 12:25:14 +00: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:
parent
5e6b38e0e7
commit
e072e4cce7
5 changed files with 35 additions and 42 deletions
|
@ -27,7 +27,7 @@ func init() {
|
||||||
func FindEngine(engineName string) (types.Engine, error) {
|
func FindEngine(engineName string) (types.Engine, error) {
|
||||||
if engineName == "auto-detect" {
|
if engineName == "auto-detect" {
|
||||||
for _, engine := range engines {
|
for _, engine := range engines {
|
||||||
if engine.IsAvivable() {
|
if engine.IsAvailable() {
|
||||||
return engine, nil
|
return engine, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,28 +17,31 @@ import (
|
||||||
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
backend "github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type engine struct {
|
type docker struct {
|
||||||
client client.APIClient
|
client client.APIClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure docker implements Engine
|
||||||
|
var _ backend.Engine = &docker{}
|
||||||
|
|
||||||
// New returns a new Docker Engine.
|
// New returns a new Docker Engine.
|
||||||
func New() backend.Engine {
|
func New() backend.Engine {
|
||||||
return &engine{
|
return &docker{
|
||||||
client: nil,
|
client: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) Name() string {
|
func (e *docker) Name() string {
|
||||||
return "docker"
|
return "docker"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) IsAvivable() bool {
|
func (e *docker) IsAvailable() bool {
|
||||||
_, err := os.Stat("/.dockerenv")
|
_, err := os.Stat("/var/run/docker.sock")
|
||||||
return os.IsNotExist(err)
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load new client for Docker Engine using environment variables.
|
// 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)
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -48,7 +51,7 @@ func (e *engine) Load() error {
|
||||||
return nil
|
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 {
|
for _, vol := range conf.Volumes {
|
||||||
_, err := e.client.VolumeCreate(noContext, volume.VolumeCreateBody{
|
_, err := e.client.VolumeCreate(noContext, volume.VolumeCreateBody{
|
||||||
Name: vol.Name,
|
Name: vol.Name,
|
||||||
|
@ -73,7 +76,7 @@ func (e *engine) Setup(_ context.Context, conf *backend.Config) error {
|
||||||
return nil
|
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)
|
config := toConfig(proc)
|
||||||
hostConfig := toHostConfig(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)
|
return e.client.ContainerStart(ctx, proc.Name, startOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) Kill(_ context.Context, proc *backend.Step) error {
|
func (e *docker) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) {
|
||||||
return e.client.ContainerKill(noContext, proc.Name, "9")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State, error) {
|
|
||||||
wait, errc := e.client.ContainerWait(ctx, proc.Name, "")
|
wait, errc := e.client.ContainerWait(ctx, proc.Name, "")
|
||||||
select {
|
select {
|
||||||
case <-wait:
|
case <-wait:
|
||||||
|
@ -170,7 +169,7 @@ func (e *engine) Wait(ctx context.Context, proc *backend.Step) (*backend.State,
|
||||||
}, nil
|
}, 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)
|
logs, err := e.client.ContainerLogs(ctx, proc.Name, logsOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -187,7 +186,7 @@ func (e *engine) Tail(ctx context.Context, proc *backend.Step) (io.ReadCloser, e
|
||||||
return rc, nil
|
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 _, stage := range conf.Stages {
|
||||||
for _, step := range stage.Steps {
|
for _, step := range stage.Steps {
|
||||||
if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil {
|
if err := e.client.ContainerKill(noContext, step.Name, "9"); err != nil {
|
||||||
|
|
|
@ -20,15 +20,15 @@ package docker
|
||||||
// func (p *Pool) Reserve(c context.Context) backend.Engine {
|
// func (p *Pool) Reserve(c context.Context) backend.Engine {
|
||||||
// select {
|
// select {
|
||||||
// case <-c.Done():
|
// case <-c.Done():
|
||||||
// case engine := <-p.queue:
|
// case docker := <-p.queue:
|
||||||
// return engine
|
// return docker
|
||||||
// }
|
// }
|
||||||
// return nil
|
// return nil
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// // Release releases the Docker client back to the pool.
|
// // Release releases the Docker client back to the pool.
|
||||||
// func (p *Pool) Release(engine backend.Engine) {
|
// func (p *Pool) Release(docker backend.Engine) {
|
||||||
// p.queue <- engine
|
// p.queue <- docker
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// pool := docker.Pool(
|
// pool := docker.Pool(
|
||||||
|
|
|
@ -8,68 +8,65 @@ import (
|
||||||
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type engine struct {
|
type kube struct {
|
||||||
namespace string
|
namespace string
|
||||||
endpoint string
|
endpoint string
|
||||||
token string
|
token string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// make sure kube implements Engine
|
||||||
|
var _ types.Engine = &kube{}
|
||||||
|
|
||||||
// New returns a new Kubernetes Engine.
|
// New returns a new Kubernetes Engine.
|
||||||
func New(namespace, endpoint, token string) types.Engine {
|
func New(namespace, endpoint, token string) types.Engine {
|
||||||
return &engine{
|
return &kube{
|
||||||
namespace: namespace,
|
namespace: namespace,
|
||||||
endpoint: endpoint,
|
endpoint: endpoint,
|
||||||
token: token,
|
token: token,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) Name() string {
|
func (e *kube) Name() string {
|
||||||
return "kubernetes"
|
return "kubernetes"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) IsAvivable() bool {
|
func (e *kube) IsAvailable() bool {
|
||||||
host := os.Getenv("KUBERNETES_SERVICE_HOST")
|
host := os.Getenv("KUBERNETES_SERVICE_HOST")
|
||||||
return len(host) > 0
|
return len(host) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) Load() error {
|
func (e *kube) Load() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the pipeline environment.
|
// 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
|
// POST /api/v1/namespaces
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the pipeline step.
|
// Exec the pipeline step.
|
||||||
func (e *engine) Exec(context.Context, *types.Step) error {
|
func (e *kube) Exec(context.Context, *types.Step) error {
|
||||||
// POST /api/v1/namespaces/{namespace}/pods
|
// POST /api/v1/namespaces/{namespace}/pods
|
||||||
return nil
|
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
|
// Wait for the pipeline step to complete and returns
|
||||||
// the completion results.
|
// 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
|
||||||
// GET /api/v1/watch/namespaces/{namespace}/pods/{name}
|
// GET /api/v1/watch/namespaces/{namespace}/pods/{name}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tail the pipeline step logs.
|
// 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
|
// GET /api/v1/namespaces/{namespace}/pods/{name}/log
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destroy the pipeline environment.
|
// 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}
|
// DELETE /api/v1/namespaces/{name}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
type Engine interface {
|
type Engine interface {
|
||||||
Name() string
|
Name() string
|
||||||
|
|
||||||
IsAvivable() bool
|
IsAvailable() bool
|
||||||
|
|
||||||
Load() error
|
Load() error
|
||||||
|
|
||||||
|
@ -20,9 +20,6 @@ type Engine interface {
|
||||||
// Exec start the pipeline step.
|
// Exec start the pipeline step.
|
||||||
Exec(context.Context, *Step) error
|
Exec(context.Context, *Step) error
|
||||||
|
|
||||||
// Kill the pipeline step.
|
|
||||||
Kill(context.Context, *Step) error
|
|
||||||
|
|
||||||
// Wait for the pipeline step to complete and returns
|
// Wait for the pipeline step to complete and returns
|
||||||
// the completion results.
|
// the completion results.
|
||||||
Wait(context.Context, *Step) (*State, error)
|
Wait(context.Context, *Step) (*State, error)
|
||||||
|
|
Loading…
Reference in a new issue