From ce8d7d7aabcf33b71d6b7be04fa6a2252bc6d85f Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 4 Apr 2023 15:27:56 +0800 Subject: [PATCH] refactor: envcheck --- cmd/daemon.go | 8 +++--- engine/docker.go | 40 ---------------------------- engine/engine.go | 46 --------------------------------- engine/options.go | 33 ----------------------- internal/pkg/envcheck/doc.go | 2 ++ internal/pkg/envcheck/docker.go | 24 +++++++++++++++++ 6 files changed, 29 insertions(+), 124 deletions(-) delete mode 100644 engine/docker.go delete mode 100644 engine/engine.go delete mode 100644 engine/options.go create mode 100644 internal/pkg/envcheck/doc.go create mode 100644 internal/pkg/envcheck/docker.go diff --git a/cmd/daemon.go b/cmd/daemon.go index 2480e2d..ee3c43e 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -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 } } diff --git a/engine/docker.go b/engine/docker.go deleted file mode 100644 index 7f89ffc..0000000 --- a/engine/docker.go +++ /dev/null @@ -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 -} diff --git a/engine/engine.go b/engine/engine.go deleted file mode 100644 index 5580416..0000000 --- a/engine/engine.go +++ /dev/null @@ -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 -} diff --git a/engine/options.go b/engine/options.go deleted file mode 100644 index 7c81a6d..0000000 --- a/engine/options.go +++ /dev/null @@ -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 - }) -} diff --git a/internal/pkg/envcheck/doc.go b/internal/pkg/envcheck/doc.go new file mode 100644 index 0000000..860132a --- /dev/null +++ b/internal/pkg/envcheck/doc.go @@ -0,0 +1,2 @@ +// Package envcheck provides a simple way to check if the environment is ready to run jobs. +package envcheck diff --git a/internal/pkg/envcheck/docker.go b/internal/pkg/envcheck/docker.go new file mode 100644 index 0000000..f76790e --- /dev/null +++ b/internal/pkg/envcheck/docker.go @@ -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 +}