Refactor to new framework #98
No files matched your search
+3
-5
@@ -15,8 +15,8 @@ import (
|
||||
|
||||
"gitea.com/gitea/act_runner/artifactcache"
|
||||
"gitea.com/gitea/act_runner/client"
|
||||
"gitea.com/gitea/act_runner/engine"
|
||||
"gitea.com/gitea/act_runner/internal/pkg/config"
|
||||
"gitea.com/gitea/act_runner/internal/pkg/envcheck"
|
||||
"gitea.com/gitea/act_runner/internal/pkg/labels"
|
||||
"gitea.com/gitea/act_runner/poller"
|
||||
"gitea.com/gitea/act_runner/runtime"
|
||||
@@ -55,10 +55,8 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
|
||||
}
|
||||
|
||||
if ls.RequireDocker() {
|
||||
// try to connect to docker daemon
|
||||
// if failed, exit with error
|
||||
if err := engine.Start(ctx); err != nil {
|
||||
log.WithError(err).Fatalln("failed to connect docker daemon engine")
|
||||
if err := envcheck.CheckIfDockerRunning(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
type Docker struct {
|
||||
client client.APIClient
|
||||
hidePull bool
|
||||
}
|
||||
|
||||
func New(opts ...Option) (*Docker, error) {
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
srv := &Docker{
|
||||
client: cli,
|
||||
}
|
||||
|
||||
// Loop through each option
|
||||
for _, opt := range opts {
|
||||
// Call the option giving the instantiated
|
||||
opt.Apply(srv)
|
||||
}
|
||||
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
// Ping pings the Docker daemon.
|
||||
func (e *Docker) Ping(ctx context.Context) error {
|
||||
_, err := e.client.Ping(ctx)
|
||||
return err
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Start start docker engine api loop
|
||||
func Start(ctx context.Context) error {
|
||||
engine, err := New()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
count := 0
|
||||
for {
|
||||
err := engine.Ping(ctx)
|
||||
if err == context.Canceled {
|
||||
break
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
if err != nil {
|
||||
log.WithError(err).
|
||||
Errorln("cannot ping the docker daemon")
|
||||
count++
|
||||
if count == 5 {
|
||||
return fmt.Errorf("retry connect to docker daemon failed: %d times", count)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
} else {
|
||||
log.Infoln("successfully ping the docker daemon")
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package engine
|
||||
|
||||
import "github.com/docker/docker/client"
|
||||
|
||||
// An Option configures a mutex.
|
||||
type Option interface {
|
||||
Apply(*Docker)
|
||||
}
|
||||
|
||||
// OptionFunc is a function that configure a value.
|
||||
type OptionFunc func(*Docker)
|
||||
|
||||
// Apply calls f(option)
|
||||
func (f OptionFunc) Apply(docker *Docker) {
|
||||
f(docker)
|
||||
}
|
||||
|
||||
// WithClient set custom client
|
||||
func WithClient(c client.APIClient) Option {
|
||||
return OptionFunc(func(q *Docker) {
|
||||
q.client = c
|
||||
})
|
||||
}
|
||||
|
||||
// WithHidePull hide pull event.
|
||||
func WithHidePull(v bool) Option {
|
||||
return OptionFunc(func(q *Docker) {
|
||||
q.hidePull = v
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package envcheck provides a simple way to check if the environment is ready to run jobs.
|
||||
package envcheck
|
||||
@@ -0,0 +1,24 @@
|
||||
package envcheck
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
func CheckIfDockerRunning(ctx context.Context) error {
|
||||
// TODO: if runner support configures to use docker, we need config.Config to pass in
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
_, err = cli.Ping(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot ping the docker daemon, does it running? %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user