From 616ad7c96a0c49366293a420dbd240506cd5ec74 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Thu, 23 Mar 2023 20:48:33 +0800 Subject: [PATCH 1/8] Clarify labels (#69) The label will follow the format `label[:schema[:args]]`, and the schema will be `host` if it's omitted. So - `ubuntu:docker://node:18`: Run jobs with label `ubuntu` via docker with image `node:18` - `ubuntu:host`: Run jobs with label `ubuntu` on the host directly. - `ubuntu`: Same as `ubuntu:host`. - `ubuntu:vm:ubuntu-latest`: (Just a example, not Implemented) Run jobs with label `ubuntu` via virtual machine with iso `ubuntu-latest`. Reviewed-on: https://gitea.com/gitea/act_runner/pulls/69 Reviewed-by: Zettat123 Reviewed-by: wxiaoguang --- cmd/daemon.go | 17 ++++++------ cmd/register.go | 28 ++++++++++---------- cmd/register_test.go | 10 -------- runtime/label.go | 26 +++++++++++++++++++ runtime/label_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++ runtime/runtime.go | 35 ++++++++++++------------- 6 files changed, 126 insertions(+), 50 deletions(-) delete mode 100644 cmd/register_test.go create mode 100644 runtime/label.go create mode 100644 runtime/label_test.go diff --git a/cmd/daemon.go b/cmd/daemon.go index fafeb04..28c2992 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -3,7 +3,12 @@ package cmd import ( "context" "os" - "strings" + + "github.com/joho/godotenv" + "github.com/mattn/go-isatty" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "golang.org/x/sync/errgroup" "codeberg.org/forgejo/runner/artifactcache" "codeberg.org/forgejo/runner/client" @@ -11,12 +16,6 @@ import ( "codeberg.org/forgejo/runner/engine" "codeberg.org/forgejo/runner/poller" "codeberg.org/forgejo/runner/runtime" - - "github.com/joho/godotenv" - "github.com/mattn/go-isatty" - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "golang.org/x/sync/errgroup" ) func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, args []string) error { @@ -35,8 +34,8 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg // require docker if a runner label uses a docker backend needsDocker := false for _, l := range cfg.Runner.Labels { - splits := strings.SplitN(l, ":", 2) - if len(splits) == 2 && strings.HasPrefix(splits[1], "docker://") { + _, schema, _, _ := runtime.ParseLabel(l) + if schema == "docker" { needsDocker = true break } diff --git a/cmd/register.go b/cmd/register.go index 7bd5662..1b58477 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -1,3 +1,6 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package cmd import ( @@ -6,14 +9,16 @@ import ( "fmt" "os" "os/signal" - "runtime" + goruntime "runtime" "strings" "time" pingv1 "code.gitea.io/actions-proto-go/ping/v1" + "codeberg.org/forgejo/runner/client" "codeberg.org/forgejo/runner/config" "codeberg.org/forgejo/runner/register" + "codeberg.org/forgejo/runner/runtime" "github.com/bufbuild/connect-go" "github.com/joho/godotenv" @@ -34,7 +39,7 @@ func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) fun log.SetLevel(log.DebugLevel) log.Infof("Registering runner, arch=%s, os=%s, version=%s.", - runtime.GOARCH, runtime.GOOS, version) + goruntime.GOARCH, goruntime.GOOS, version) // runner always needs root permission if os.Getuid() != 0 { @@ -118,12 +123,9 @@ func (r *registerInputs) validate() error { func validateLabels(labels []string) error { for _, label := range labels { - values := strings.SplitN(label, ":", 2) - if len(values) > 2 { - return fmt.Errorf("Invalid label: %s", label) + if _, _, _, err := runtime.ParseLabel(label); err != nil { + return err } - // len(values) == 1, label for non docker execution environment - // TODO: validate value format, like docker://node:16-buster } return nil } @@ -164,7 +166,7 @@ func (r *registerInputs) assignToNext(stage registerStage, value string) registe } if validateLabels(r.CustomLabels) != nil { - log.Infoln("Invalid labels, please input again, leave blank to use the default labels (for example, ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster)") + log.Infoln("Invalid labels, please input again, leave blank to use the default labels (for example, ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster,linux_arm:host)") return StageInputCustomLabels } return StageWaitingForRegistration @@ -221,14 +223,14 @@ func printStageHelp(stage registerStage) { case StageOverwriteLocalConfig: log.Infoln("Runner is already registered, overwrite local config? [y/N]") case StageInputInstance: - log.Infoln("Enter the Forgejo instance URL (for example, https://codeberg.org/):") + log.Infoln("Enter the Gitea instance URL (for example, https://gitea.com/):") case StageInputToken: log.Infoln("Enter the runner token:") case StageInputRunnerName: hostname, _ := os.Hostname() - log.Infof("Enter the runner name (if set empty, use hostname:%s ):\n", hostname) + log.Infof("Enter the runner name (if set empty, use hostname: %s):\n", hostname) case StageInputCustomLabels: - log.Infoln("Enter the runner labels, leave blank to use the default labels (comma-separated, for example, self-hosted,ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster):") + log.Infoln("Enter the runner labels, leave blank to use the default labels (comma-separated, for example, ubuntu-20.04:docker://node:16-bullseye,ubuntu-18.04:docker://node:16-buster,linux_arm:host):") case StageWaitingForRegistration: log.Infoln("Waiting for registration...") } @@ -290,11 +292,11 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { } if err != nil { log.WithError(err). - Errorln("Cannot ping the Forgejo instance server") + Errorln("Cannot ping the Gitea instance server") // TODO: if ping failed, retry or exit time.Sleep(time.Second) } else { - log.Debugln("Successfully pinged the Forgejo instance server") + log.Debugln("Successfully pinged the Gitea instance server") break } } diff --git a/cmd/register_test.go b/cmd/register_test.go deleted file mode 100644 index 0179a97..0000000 --- a/cmd/register_test.go +++ /dev/null @@ -1,10 +0,0 @@ -package cmd - -import "testing" - -func TestValidateLabels(t *testing.T) { - labels := []string{"ubuntu-latest:docker://node:16-buster", "self-hosted"} - if err := validateLabels(labels); err != nil { - t.Errorf("validateLabels() error = %v", err) - } -} diff --git a/runtime/label.go b/runtime/label.go new file mode 100644 index 0000000..c7aa001 --- /dev/null +++ b/runtime/label.go @@ -0,0 +1,26 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package runtime + +import ( + "fmt" + "strings" +) + +func ParseLabel(str string) (label, schema, arg string, err error) { + splits := strings.SplitN(str, ":", 3) + label = splits[0] + schema = "host" + arg = "" + if len(splits) >= 2 { + schema = splits[1] + } + if len(splits) >= 3 { + arg = splits[2] + } + if schema != "host" && schema != "docker" { + return "", "", "", fmt.Errorf("unsupported schema: %s", schema) + } + return +} diff --git a/runtime/label_test.go b/runtime/label_test.go new file mode 100644 index 0000000..2bd901a --- /dev/null +++ b/runtime/label_test.go @@ -0,0 +1,60 @@ +package runtime + +import "testing" + +func TestParseLabel(t *testing.T) { + tests := []struct { + args string + wantLabel string + wantSchema string + wantArg string + wantErr bool + }{ + { + args: "ubuntu:docker://node:18", + wantLabel: "ubuntu", + wantSchema: "docker", + wantArg: "//node:18", + wantErr: false, + }, + { + args: "ubuntu:host", + wantLabel: "ubuntu", + wantSchema: "host", + wantArg: "", + wantErr: false, + }, + { + args: "ubuntu", + wantLabel: "ubuntu", + wantSchema: "host", + wantArg: "", + wantErr: false, + }, + { + args: "ubuntu:vm:ubuntu-18.04", + wantLabel: "", + wantSchema: "", + wantArg: "", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.args, func(t *testing.T) { + gotLabel, gotSchema, gotArg, err := ParseLabel(tt.args) + if (err != nil) != tt.wantErr { + t.Errorf("parseLabel() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotLabel != tt.wantLabel { + t.Errorf("parseLabel() gotLabel = %v, want %v", gotLabel, tt.wantLabel) + } + if gotSchema != tt.wantSchema { + t.Errorf("parseLabel() gotSchema = %v, want %v", gotSchema, tt.wantSchema) + } + if gotArg != tt.wantArg { + t.Errorf("parseLabel() gotArg = %v, want %v", gotArg, tt.wantArg) + } + }) + } +} diff --git a/runtime/runtime.go b/runtime/runtime.go index c5a9118..2abf795 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -7,6 +7,7 @@ import ( runnerv1 "code.gitea.io/actions-proto-go/runner/v1" "codeberg.org/forgejo/runner/artifactcache" "codeberg.org/forgejo/runner/client" + log "github.com/sirupsen/logrus" ) // Runner runs the pipeline. @@ -31,28 +32,24 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { } func (s *Runner) platformPicker(labels []string) string { - // "ubuntu-18.04:docker://node:16-buster" - // "self-hosted" - - platforms := make(map[string]string, len(labels)) + platforms := make(map[string]string, len(s.Labels)) for _, l := range s.Labels { - // "ubuntu-18.04:docker://node:16-buster" - splits := strings.SplitN(l, ":", 2) - if len(splits) == 1 { - // identifier for non docker execution environment - platforms[splits[0]] = "-self-hosted" + label, schema, arg, err := ParseLabel(l) + if err != nil { + log.Errorf("invaid label %q: %v", l, err) continue } - // ["ubuntu-18.04", "docker://node:16-buster"] - k, v := splits[0], splits[1] - if prefix := "docker://"; !strings.HasPrefix(v, prefix) { + switch schema { + case "docker": + // TODO "//" will be ignored, maybe we should use 'ubuntu-18.04:docker:node:16-buster' instead + platforms[label] = strings.TrimPrefix(arg, "//") + case "host": + platforms[label] = "-self-hosted" + default: + // It should not happen, because ParseLabel has checked it. continue - } else { - v = strings.TrimPrefix(v, prefix) } - // ubuntu-18.04 => node:16-buster - platforms[k] = v } for _, label := range labels { @@ -67,6 +64,8 @@ func (s *Runner) platformPicker(labels []string) string { // ["with-gpu"] => "linux:with-gpu" // ["ubuntu-22.04", "with-gpu"] => "ubuntu:22.04_with-gpu" - // return default - return "node:16-bullseye" + // return default. + // So the runner receives a task with a label that the runner doesn't have, + // it happens when the user have edited the label of the runner in the web UI. + return "node:16-bullseye" // TODO: it may be not correct, what if the runner is used as host mode only? } From 3463f94119f978ea24cef4c9de70f35df7b7185b Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 24 Mar 2023 15:10:39 +0800 Subject: [PATCH 2/8] Vet code (#73) Reviewed-on: https://gitea.com/gitea/act_runner/pulls/73 --- runtime/label_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/label_test.go b/runtime/label_test.go index 2bd901a..f17c372 100644 --- a/runtime/label_test.go +++ b/runtime/label_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package runtime import "testing" From 3d78433564a28d57c77079cba43a1b8d372c9598 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 24 Mar 2023 17:55:13 +0800 Subject: [PATCH 3/8] Get outbound IP in multiple ways or disable cache server if failed to init (#74) Fix #64 (incompletely). It's still not ideal. It makes more sense to use the gateway IP address of container network as outbound IP of cache server. However, this requires act to cooperate, some think like: - act creates the network for new container, and returns the network to runner. - runner extracts the gateway IP in the network. - runner uses the gateway IP as outbound IP, and pass it to act as cache server endpoint. - act It continues to create the container with the created network. Reviewed-on: https://gitea.com/gitea/act_runner/pulls/74 Reviewed-by: Lunny Xiao --- artifactcache/handler.go | 11 +++++++++-- artifactcache/util.go | 30 +++++++++++++++++++++++++++++- cmd/daemon.go | 14 +++++++------- runtime/runtime.go | 4 +++- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/artifactcache/handler.go b/artifactcache/handler.go index 7885a9b..d29a7ce 100644 --- a/artifactcache/handler.go +++ b/artifactcache/handler.go @@ -18,7 +18,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" "github.com/go-chi/render" - "github.com/nektos/act/pkg/common" log "github.com/sirupsen/logrus" _ "modernc.org/sqlite" "xorm.io/builder" @@ -39,6 +38,8 @@ type Handler struct { gc atomic.Bool gcAt time.Time + + outboundIP string } func NewHandler() (*Handler, error) { @@ -69,6 +70,12 @@ func NewHandler() (*Handler, error) { } h.storage = storage + if ip, err := getOutboundIP(); err != nil { + return nil, err + } else { + h.outboundIP = ip.String() + } + router := chi.NewRouter() router.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: logger})) router.Use(func(handler http.Handler) http.Handler { @@ -113,7 +120,7 @@ func NewHandler() (*Handler, error) { func (h *Handler) ExternalURL() string { // TODO: make the external url configurable if necessary return fmt.Sprintf("http://%s:%d", - common.GetOutboundIP().String(), + h.outboundIP, h.listener.Addr().(*net.TCPAddr).Port) } diff --git a/artifactcache/util.go b/artifactcache/util.go index 0f22c05..eca173a 100644 --- a/artifactcache/util.go +++ b/artifactcache/util.go @@ -5,6 +5,7 @@ package artifactcache import ( "fmt" + "net" "net/http" "strconv" "strings" @@ -44,8 +45,35 @@ func parseContentRange(s string) (int64, int64, error) { return start, stop, nil } +func getOutboundIP() (net.IP, error) { + // FIXME: It makes more sense to use the gateway IP address of container network + if conn, err := net.Dial("udp", "8.8.8.8:80"); err == nil { + defer conn.Close() + return conn.LocalAddr().(*net.UDPAddr).IP, nil + } + if ifaces, err := net.Interfaces(); err == nil { + for _, i := range ifaces { + if addrs, err := i.Addrs(); err == nil { + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if ip.IsGlobalUnicast() { + return ip, nil + } + } + } + } + } + return nil, fmt.Errorf("no outbound IP address found") +} + // engine is a wrapper of *xorm.Engine, with a lock. -// To avoid racing of sqlite, we don't careperformance here. +// To avoid racing of sqlite, we don't care performance here. type engine struct { e *xorm.Engine m sync.Mutex diff --git a/cmd/daemon.go b/cmd/daemon.go index 28c2992..c6a6a82 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -49,12 +49,6 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg } } - handler, err := artifactcache.NewHandler() - if err != nil { - return err - } - log.Infof("cache handler listens on: %v", handler.ExternalURL()) - var g errgroup.Group cli := client.New( @@ -72,7 +66,13 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg Environ: cfg.Runner.Environ, Labels: cfg.Runner.Labels, Version: version, - CacheHandler: handler, + } + + if handler, err := artifactcache.NewHandler(); err != nil { + log.Errorf("cannot init cache server, it will be disabled: %v", err) + } else { + log.Infof("cache handler listens on: %v", handler.ExternalURL()) + runner.CacheHandler = handler } poller := poller.New( diff --git a/runtime/runtime.go b/runtime/runtime.go index 2abf795..9d24caf 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -27,7 +27,9 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { for k, v := range s.Environ { env[k] = v } - env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" + if s.CacheHandler != nil { + env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" + } return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version) } From 63c1734bb57fb6e5ae40e48113998fd5fc066118 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 28 Mar 2023 11:49:09 +0800 Subject: [PATCH 4/8] Fix potential log panic (#82) If a job uses a [reusable workflow](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-of-jobsjob_iduses), the job's steps sequence will be empty. But in log reporter, we don't check the length of `r.state.Steps`, which may cause panic. ``` go if v, ok := entry.Data["stepNumber"]; ok { if v, ok := v.(int); ok { step = r.state.Steps[v] } } ``` Reviewed-on: https://gitea.com/gitea/act_runner/pulls/82 Reviewed-by: Lunny Xiao Co-authored-by: Zettat123 Co-committed-by: Zettat123 --- runtime/reporter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/reporter.go b/runtime/reporter.go index 544852a..51d5e59 100644 --- a/runtime/reporter.go +++ b/runtime/reporter.go @@ -99,7 +99,7 @@ func (r *Reporter) Fire(entry *log.Entry) error { var step *runnerv1.StepState if v, ok := entry.Data["stepNumber"]; ok { - if v, ok := v.(int); ok { + if v, ok := v.(int); ok && len(r.state.Steps) > v { step = r.state.Steps[v] } } From 0d33f8f520a47a480e16adcec1ed41bebbb6a86b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 28 Mar 2023 23:51:38 +0800 Subject: [PATCH 5/8] handle possible panic (#88) Reviewed-on: https://gitea.com/gitea/act_runner/pulls/88 Reviewed-by: Jason Song --- poller/poller.go | 61 +++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/poller/poller.go b/poller/poller.go index 1600dc4..f91f8bf 100644 --- a/poller/poller.go +++ b/poller/poller.go @@ -54,6 +54,40 @@ func (p *Poller) Wait() { p.routineGroup.Wait() } +func (p *Poller) handle(ctx context.Context, l *log.Entry) { + defer func() { + if r := recover(); r != nil { + l.Errorf("handle task panic: %+v", r) + } + }() + + for { + select { + case <-ctx.Done(): + return + default: + task, err := p.pollTask(ctx) + if task == nil || err != nil { + if err != nil { + l.Errorf("can't find the task: %v", err.Error()) + } + time.Sleep(5 * time.Second) + break + } + + p.metric.IncBusyWorker() + p.routineGroup.Run(func() { + defer p.schedule() + defer p.metric.DecBusyWorker() + if err := p.dispatchTask(ctx, task); err != nil { + l.Errorf("execute task: %v", err.Error()) + } + }) + return + } + } +} + func (p *Poller) Poll(ctx context.Context) error { l := log.WithField("func", "Poll") @@ -67,32 +101,7 @@ func (p *Poller) Poll(ctx context.Context) error { case <-ctx.Done(): return nil } - LOOP: - for { - select { - case <-ctx.Done(): - break LOOP - default: - task, err := p.pollTask(ctx) - if task == nil || err != nil { - if err != nil { - l.Errorf("can't find the task: %v", err.Error()) - } - time.Sleep(5 * time.Second) - break - } - - p.metric.IncBusyWorker() - p.routineGroup.Run(func() { - defer p.schedule() - defer p.metric.DecBusyWorker() - if err := p.dispatchTask(ctx, task); err != nil { - l.Errorf("execute task: %v", err.Error()) - } - }) - break LOOP - } - } + p.handle(ctx, l) } } From 028451bf22f935a0af09fdc4f3935619d34dbf9e Mon Sep 17 00:00:00 2001 From: telackey Date: Wed, 29 Mar 2023 09:42:53 +0800 Subject: [PATCH 6/8] Add CLI flag for specifying the Docker image to use. (#83) Since the `exec` command does not use labels from `.runner`, there is no existing way to specify which Docker image to use for task execution. This adds an `--image` flag for specifying it manually. The default remains `node:16-bullseye`. Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/gitea/act_runner/pulls/83 Reviewed-by: Jason Song Reviewed-by: Lunny Xiao Co-authored-by: telackey Co-committed-by: telackey --- cmd/exec.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/exec.go b/cmd/exec.go index cd6b3fc..e058dfc 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -54,6 +54,7 @@ type executeArgs struct { noSkipCheckout bool debug bool dryrun bool + image string cacheHandler *artifactcache.Handler } @@ -385,7 +386,7 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command ContainerNetworkMode: "bridge", DefaultActionInstance: execArgs.defaultActionsUrl, PlatformPicker: func(_ []string) string { - return "node:16-bullseye" + return execArgs.image }, } @@ -461,6 +462,7 @@ func loadExecCmd(ctx context.Context) *cobra.Command { execCmd.PersistentFlags().BoolVarP(&execArg.noSkipCheckout, "no-skip-checkout", "", false, "Do not skip actions/checkout") execCmd.PersistentFlags().BoolVarP(&execArg.debug, "debug", "d", false, "enable debug log") execCmd.PersistentFlags().BoolVarP(&execArg.dryrun, "dryrun", "n", false, "dryrun mode") + execCmd.PersistentFlags().StringVarP(&execArg.image, "image", "i", "node:16-bullseye", "docker image to use") return execCmd } From c817236aa45b1bb7364ad0225a531bd10b2732cc Mon Sep 17 00:00:00 2001 From: Jason Song Date: Sun, 2 Apr 2023 22:41:48 +0800 Subject: [PATCH 7/8] Refactor environment variables to configuration and registration (#90) Close #21. Refactor environment variables to configuration file (config.yaml) and registration file (.runner). The old environment variables are still supported, but warning logs will be printed. Like: ```text $ GITEA_DEBUG=true ./act_runner -c config.yaml daemon INFO[0000] Starting runner daemon WARN[0000] env GITEA_DEBUG has been ignored because config file is used $ GITEA_DEBUG=true ./act_runner daemon INFO[0000] Starting runner daemon WARN[0000] env GITEA_DEBUG will be deprecated, please use config file instead ``` Reviewed-on: https://gitea.com/gitea/act_runner/pulls/90 Reviewed-by: Lunny Xiao --- .gitignore | 1 + artifactcache/handler.go | 20 +++-- client/header.go | 10 +++ client/http.go | 7 +- cmd/cmd.go | 31 ++++--- cmd/daemon.go | 71 +++++++++------- cmd/exec.go | 2 +- cmd/register.go | 75 ++++++++++------ config/config.example.yaml | 38 +++++++++ config/config.go | 169 +++++++++++++++---------------------- config/deprecated.go | 62 ++++++++++++++ config/embed.go | 9 ++ config/registration.go | 54 ++++++++++++ core/runner.go | 18 ---- go.mod | 3 +- go.sum | 2 - poller/poller.go | 15 ++-- register/register.go | 63 -------------- 18 files changed, 376 insertions(+), 274 deletions(-) create mode 100644 client/header.go create mode 100644 config/config.example.yaml create mode 100644 config/deprecated.go create mode 100644 config/embed.go create mode 100644 config/registration.go delete mode 100644 core/runner.go delete mode 100644 register/register.go diff --git a/.gitignore b/.gitignore index 8328ce1..2fe3a1b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ forgejo-runner .runner coverage.txt /gitea-vet +/config.yaml # MS VSCode .vscode diff --git a/artifactcache/handler.go b/artifactcache/handler.go index d29a7ce..86e7aa5 100644 --- a/artifactcache/handler.go +++ b/artifactcache/handler.go @@ -42,14 +42,15 @@ type Handler struct { outboundIP string } -func NewHandler() (*Handler, error) { +func NewHandler(dir, outboundIP string, port uint16) (*Handler, error) { h := &Handler{} - dir := "" // TODO: make the dir configurable if necessary - if home, err := os.UserHomeDir(); err != nil { - return nil, err - } else { - dir = filepath.Join(home, ".cache/actcache") + if dir == "" { + if home, err := os.UserHomeDir(); err != nil { + return nil, err + } else { + dir = filepath.Join(home, ".cache", "actcache") + } } if err := os.MkdirAll(dir, 0o755); err != nil { return nil, err @@ -70,7 +71,9 @@ func NewHandler() (*Handler, error) { } h.storage = storage - if ip, err := getOutboundIP(); err != nil { + if outboundIP != "" { + h.outboundIP = outboundIP + } else if ip, err := getOutboundIP(); err != nil { return nil, err } else { h.outboundIP = ip.String() @@ -102,8 +105,7 @@ func NewHandler() (*Handler, error) { h.gcCache() - // TODO: make the port configurable if necessary - listener, err := net.Listen("tcp", ":0") // random available port + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) // listen on all interfaces if err != nil { return nil, err } diff --git a/client/header.go b/client/header.go new file mode 100644 index 0000000..df8627a --- /dev/null +++ b/client/header.go @@ -0,0 +1,10 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package client + +const ( + UUIDHeader = "x-runner-uuid" + TokenHeader = "x-runner-token" + VersionHeader = "x-runner-version" +) diff --git a/client/http.go b/client/http.go index 0aed490..557b4df 100644 --- a/client/http.go +++ b/client/http.go @@ -8,7 +8,6 @@ import ( "code.gitea.io/actions-proto-go/ping/v1/pingv1connect" "code.gitea.io/actions-proto-go/runner/v1/runnerv1connect" - "codeberg.org/forgejo/runner/core" "github.com/bufbuild/connect-go" ) @@ -32,13 +31,13 @@ func New(endpoint string, insecure bool, uuid, token, runnerVersion string, opts opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { if uuid != "" { - req.Header().Set(core.UUIDHeader, uuid) + req.Header().Set(UUIDHeader, uuid) } if token != "" { - req.Header().Set(core.TokenHeader, token) + req.Header().Set(TokenHeader, token) } if runnerVersion != "" { - req.Header().Set(core.VersionHeader, runnerVersion) + req.Header().Set(VersionHeader, runnerVersion) } return next(ctx, req) } diff --git a/cmd/cmd.go b/cmd/cmd.go index 3a45f9e..3b82c14 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,24 +1,20 @@ +// SPDX-License-Identifier: MIT package cmd import ( "context" + "fmt" "os" "github.com/spf13/cobra" + + "codeberg.org/forgejo/runner/config" ) // the version of act_runner var version = "develop" -type globalArgs struct { - EnvFile string -} - func Execute(ctx context.Context) { - // task := runtime.NewTask("gitea", 0, nil, nil) - - var gArgs globalArgs - // ./act_runner rootCmd := &cobra.Command{ Use: "act_runner [event name to run]\nIf no event name passed, will default to \"on: push\"", @@ -26,9 +22,9 @@ func Execute(ctx context.Context) { Args: cobra.MaximumNArgs(1), Version: version, SilenceUsage: true, - RunE: runDaemon(ctx, gArgs.EnvFile), } - rootCmd.PersistentFlags().StringVarP(&gArgs.EnvFile, "env-file", "", ".env", "Read in a file of environment variables.") + configFile := "" + rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file path") // ./act_runner register var regArgs registerArgs @@ -36,11 +32,10 @@ func Execute(ctx context.Context) { Use: "register", Short: "Register a runner to the server", Args: cobra.MaximumNArgs(0), - RunE: runRegister(ctx, ®Args, gArgs.EnvFile), // must use a pointer to regArgs + RunE: runRegister(ctx, ®Args, &configFile), // must use a pointer to regArgs } registerCmd.Flags().BoolVar(®Args.NoInteractive, "no-interactive", false, "Disable interactive mode") registerCmd.Flags().StringVar(®Args.InstanceAddr, "instance", "", "Forgejo instance address") - registerCmd.Flags().BoolVar(®Args.Insecure, "insecure", false, "If check server's certificate if it's https protocol") registerCmd.Flags().StringVar(®Args.Token, "token", "", "Runner token") registerCmd.Flags().StringVar(®Args.RunnerName, "name", "", "Runner name") registerCmd.Flags().StringVar(®Args.Labels, "labels", "", "Runner tags, comma separated") @@ -51,13 +46,23 @@ func Execute(ctx context.Context) { Use: "daemon", Short: "Run as a runner daemon", Args: cobra.MaximumNArgs(1), - RunE: runDaemon(ctx, gArgs.EnvFile), + RunE: runDaemon(ctx, &configFile), } rootCmd.AddCommand(daemonCmd) // ./act_runner exec rootCmd.AddCommand(loadExecCmd(ctx)) + // ./act_runner config + rootCmd.AddCommand(&cobra.Command{ + Use: "generate-config", + Short: "Generate an example config file", + Args: cobra.MaximumNArgs(0), + Run: func(_ *cobra.Command, _ []string) { + fmt.Printf("%s", config.Example) + }, + }) + // hide completion command rootCmd.CompletionOptions.HiddenDefaultCmd = true diff --git a/cmd/daemon.go b/cmd/daemon.go index c6a6a82..b71815f 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -2,9 +2,9 @@ package cmd import ( "context" + "fmt" "os" - "github.com/joho/godotenv" "github.com/mattn/go-isatty" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -18,22 +18,28 @@ import ( "codeberg.org/forgejo/runner/runtime" ) -func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, args []string) error { +func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { log.Infoln("Starting runner daemon") - _ = godotenv.Load(envFile) - cfg, err := config.FromEnviron() + cfg, err := config.LoadDefault(*configFile) if err != nil { - log.WithError(err). - Fatalln("invalid configuration") + return fmt.Errorf("invalid configuration: %w", err) } initLogging(cfg) + reg, err := config.LoadRegistration(cfg.Runner.File) + if os.IsNotExist(err) { + log.Error("registration file not found, please register the runner first") + return err + } else if err != nil { + return fmt.Errorf("failed to load registration file: %w", err) + } + // require docker if a runner label uses a docker backend needsDocker := false - for _, l := range cfg.Runner.Labels { + for _, l := range reg.Labels { _, schema, _, _ := runtime.ParseLabel(l) if schema == "docker" { needsDocker = true @@ -52,40 +58,40 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg var g errgroup.Group cli := client.New( - cfg.Client.Address, - cfg.Client.Insecure, - cfg.Runner.UUID, - cfg.Runner.Token, + reg.Address, + cfg.Runner.Insecure, + reg.UUID, + reg.Token, version, ) runner := &runtime.Runner{ Client: cli, - Machine: cfg.Runner.Name, - ForgeInstance: cfg.Client.Address, - Environ: cfg.Runner.Environ, - Labels: cfg.Runner.Labels, + Machine: reg.Name, + ForgeInstance: reg.Address, + Environ: cfg.Runner.Envs, + Labels: reg.Labels, Version: version, } - if handler, err := artifactcache.NewHandler(); err != nil { - log.Errorf("cannot init cache server, it will be disabled: %v", err) - } else { - log.Infof("cache handler listens on: %v", handler.ExternalURL()) - runner.CacheHandler = handler + if *cfg.Cache.Enabled { + if handler, err := artifactcache.NewHandler(cfg.Cache.Dir, cfg.Cache.Host, cfg.Cache.Port); err != nil { + log.Errorf("cannot init cache server, it will be disabled: %v", err) + } else { + log.Infof("cache handler listens on: %v", handler.ExternalURL()) + runner.CacheHandler = handler + } } poller := poller.New( cli, runner.Run, - cfg.Runner.Capacity, + cfg, ) g.Go(func() error { l := log.WithField("capacity", cfg.Runner.Capacity). - WithField("endpoint", cfg.Client.Address). - WithField("os", cfg.Platform.OS). - WithField("arch", cfg.Platform.Arch) + WithField("endpoint", reg.Address) l.Infoln("polling the remote server") if err := poller.Poll(ctx); err != nil { @@ -105,17 +111,22 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg } // initLogging setup the global logrus logger. -func initLogging(cfg config.Config) { +func initLogging(cfg *config.Config) { isTerm := isatty.IsTerminal(os.Stdout.Fd()) log.SetFormatter(&log.TextFormatter{ DisableColors: !isTerm, FullTimestamp: true, }) - if cfg.Debug { - log.SetLevel(log.DebugLevel) - } - if cfg.Trace { - log.SetLevel(log.TraceLevel) + if l := cfg.Log.Level; l != "" { + level, err := log.ParseLevel(l) + if err != nil { + log.WithError(err). + Errorf("invalid log level: %q", l) + } + if log.GetLevel() != level { + log.Infof("log level changed to %v", level) + log.SetLevel(level) + } } } diff --git a/cmd/exec.go b/cmd/exec.go index e058dfc..82b365b 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -348,7 +348,7 @@ func runExec(ctx context.Context, execArgs *executeArgs) func(cmd *cobra.Command } // init a cache server - handler, err := artifactcache.NewHandler() + handler, err := artifactcache.NewHandler("", "", 0) if err != nil { return err } diff --git a/cmd/register.go b/cmd/register.go index 1b58477..91c84a8 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -14,21 +14,20 @@ import ( "time" pingv1 "code.gitea.io/actions-proto-go/ping/v1" + runnerv1 "code.gitea.io/actions-proto-go/runner/v1" "codeberg.org/forgejo/runner/client" "codeberg.org/forgejo/runner/config" - "codeberg.org/forgejo/runner/register" "codeberg.org/forgejo/runner/runtime" "github.com/bufbuild/connect-go" - "github.com/joho/godotenv" "github.com/mattn/go-isatty" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) // runRegister registers a runner to the server -func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) func(*cobra.Command, []string) error { +func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string) func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) error { log.SetReportCaller(false) isTerm := isatty.IsTerminal(os.Stdout.Fd()) @@ -48,14 +47,13 @@ func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) fun } if regArgs.NoInteractive { - if err := registerNoInteractive(envFile, regArgs); err != nil { + if err := registerNoInteractive(*configFile, regArgs); err != nil { return err } } else { go func() { - if err := registerInteractive(envFile); err != nil { - // log.Errorln(err) - os.Exit(2) + if err := registerInteractive(*configFile); err != nil { + log.Fatal(err) return } os.Exit(0) @@ -74,7 +72,6 @@ func runRegister(ctx context.Context, regArgs *registerArgs, envFile string) fun type registerArgs struct { NoInteractive bool InstanceAddr string - Insecure bool Token string RunnerName string Labels string @@ -102,7 +99,6 @@ var defaultLabels = []string{ type registerInputs struct { InstanceAddr string - Insecure bool Token string RunnerName string CustomLabels []string @@ -174,16 +170,17 @@ func (r *registerInputs) assignToNext(stage registerStage, value string) registe return StageUnknown } -func registerInteractive(envFile string) error { +func registerInteractive(configFile string) error { var ( reader = bufio.NewReader(os.Stdin) stage = StageInputInstance inputs = new(registerInputs) ) - // check if overwrite local config - _ = godotenv.Load(envFile) - cfg, _ := config.FromEnviron() + cfg, err := config.LoadDefault(configFile) + if err != nil { + return fmt.Errorf("failed to load config: %v", err) + } if f, err := os.Stat(cfg.Runner.File); err == nil && !f.IsDir() { stage = StageOverwriteLocalConfig } @@ -199,7 +196,7 @@ func registerInteractive(envFile string) error { if stage == StageWaitingForRegistration { log.Infof("Registering runner, name=%s, instance=%s, labels=%v.", inputs.RunnerName, inputs.InstanceAddr, inputs.CustomLabels) - if err := doRegister(&cfg, inputs); err != nil { + if err := doRegister(cfg, inputs); err != nil { log.Errorf("Failed to register runner: %v", err) } else { log.Infof("Runner registered successfully.") @@ -236,12 +233,13 @@ func printStageHelp(stage registerStage) { } } -func registerNoInteractive(envFile string, regArgs *registerArgs) error { - _ = godotenv.Load(envFile) - cfg, _ := config.FromEnviron() +func registerNoInteractive(configFile string, regArgs *registerArgs) error { + cfg, err := config.LoadDefault(configFile) + if err != nil { + return err + } inputs := ®isterInputs{ InstanceAddr: regArgs.InstanceAddr, - Insecure: regArgs.Insecure, Token: regArgs.Token, RunnerName: regArgs.RunnerName, CustomLabels: defaultLabels, @@ -258,7 +256,7 @@ func registerNoInteractive(envFile string, regArgs *registerArgs) error { log.WithError(err).Errorf("Invalid input, please re-run act command.") return nil } - if err := doRegister(&cfg, inputs); err != nil { + if err := doRegister(cfg, inputs); err != nil { log.Errorf("Failed to register runner: %v", err) return nil } @@ -272,7 +270,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { // initial http client cli := client.New( inputs.InstanceAddr, - inputs.Insecure, + cfg.Runner.Insecure, "", "", version, @@ -301,9 +299,36 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { } } - cfg.Runner.Name = inputs.RunnerName - cfg.Runner.Token = inputs.Token - cfg.Runner.Labels = inputs.CustomLabels - _, err := register.New(cli).Register(ctx, cfg.Runner) - return err + reg := &config.Registration{ + Name: inputs.RunnerName, + Token: inputs.Token, + Address: inputs.InstanceAddr, + Labels: inputs.CustomLabels, + } + + labels := make([]string, len(reg.Labels)) + for i, v := range reg.Labels { + l, _, _, _ := runtime.ParseLabel(v) + labels[i] = l + } + // register new runner. + resp, err := cli.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ + Name: reg.Name, + Token: reg.Token, + AgentLabels: labels, + })) + if err != nil { + log.WithError(err).Error("poller: cannot register new runner") + return err + } + + reg.ID = resp.Msg.Runner.Id + reg.UUID = resp.Msg.Runner.Uuid + reg.Name = resp.Msg.Runner.Name + reg.Token = resp.Msg.Runner.Token + + if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil { + return fmt.Errorf("failed to save runner config: %w", err) + } + return nil } diff --git a/config/config.example.yaml b/config/config.example.yaml new file mode 100644 index 0000000..10f3e47 --- /dev/null +++ b/config/config.example.yaml @@ -0,0 +1,38 @@ +# Example configuration file, it's safe to copy this as the default config file without any modification. + +log: + # The level of logging, can be trace, debug, info, warn, error, fatal + level: info + +runner: + # Where to store the registration result. + file: .runner + # Execute how many tasks concurrently at the same time. + capacity: 1 + # Extra environment variables to run jobs. + envs: + A_TEST_ENV_NAME_1: a_test_env_value_1 + A_TEST_ENV_NAME_2: a_test_env_value_2 + # Extra environment variables to run jobs from a file. + # It will be ignored if it's empty or the file doesn't exist. + env_file: .env + # The timeout for a job to be finished. + # Please note that the Gitea instance also has a timeout (3h by default) for the job. + # So the job could be stopped by the Gitea instance if it's timeout is shorter than this. + timeout: 3h + # Whether skip verifying the TLS certificate of the Gitea instance. + insecure: false + +cache: + # Enable cache server to use actions/cache. + enabled: true + # The directory to store the cache data. + # If it's empty, the cache data will be stored in $HOME/.cache/actcache. + dir: "" + # The host of the cache server. + # It's not for the address to listen, but the address to connect from job containers. + # So 0.0.0.0 is a bad choice, leave it empty to detect automatically. + host: "" + # The port of the cache server. + # 0 means to use a random available port. + port: 0 diff --git a/config/config.go b/config/config.go index 6f93c9b..f7cc864 100644 --- a/config/config.go +++ b/config/config.go @@ -1,115 +1,84 @@ package config import ( - "encoding/json" - "io" + "fmt" "os" - "runtime" - "strconv" - - "codeberg.org/forgejo/runner/core" + "path/filepath" + "time" "github.com/joho/godotenv" - "github.com/kelseyhightower/envconfig" + "gopkg.in/yaml.v3" ) -type ( - // Config provides the system configuration. - Config struct { - Debug bool `envconfig:"GITEA_DEBUG"` - Trace bool `envconfig:"GITEA_TRACE"` - Client Client - Runner Runner - Platform Platform - } - - Client struct { - Address string `ignored:"true"` - Insecure bool - } - +type Config struct { + Log struct { + Level string `yaml:"level"` + } `yaml:"log"` Runner struct { - UUID string `ignored:"true"` - Name string `envconfig:"GITEA_RUNNER_NAME"` - Token string `ignored:"true"` - Capacity int `envconfig:"GITEA_RUNNER_CAPACITY" default:"1"` - File string `envconfig:"FORGEJO_RUNNER_FILE" default:".runner"` - Environ map[string]string `envconfig:"GITEA_RUNNER_ENVIRON"` - EnvFile string `envconfig:"GITEA_RUNNER_ENV_FILE"` - Labels []string `envconfig:"GITEA_RUNNER_LABELS"` - } + File string `yaml:"file"` + Capacity int `yaml:"capacity"` + Envs map[string]string `yaml:"envs"` + EnvFile string `yaml:"env_file"` + Timeout time.Duration `yaml:"timeout"` + Insecure bool `yaml:"insecure"` + } `yaml:"runner"` + Cache struct { + Enabled *bool `yaml:"enabled"` // pointer to distinguish between false and not set, and it will be true if not set + Dir string `yaml:"dir"` + Host string `yaml:"host"` + Port uint16 `yaml:"port"` + } `yaml:"cache"` +} - Platform struct { - OS string `envconfig:"GITEA_PLATFORM_OS"` - Arch string `envconfig:"GITEA_PLATFORM_ARCH"` - } -) - -// FromEnviron returns the settings from the environment. -func FromEnviron() (Config, error) { - cfg := Config{} - if err := envconfig.Process("", &cfg); err != nil { - return cfg, err - } - - // check runner config exist - f, err := os.Stat(cfg.Runner.File) - if err == nil && !f.IsDir() { - jsonFile, _ := os.Open(cfg.Runner.File) - defer jsonFile.Close() - byteValue, _ := io.ReadAll(jsonFile) - var runner core.Runner - if err := json.Unmarshal(byteValue, &runner); err != nil { - return cfg, err - } - if runner.UUID != "" { - cfg.Runner.UUID = runner.UUID - } - if runner.Name != "" { - cfg.Runner.Name = runner.Name - } - if runner.Token != "" { - cfg.Runner.Token = runner.Token - } - if len(runner.Labels) != 0 { - cfg.Runner.Labels = runner.Labels - } - if runner.Address != "" { - cfg.Client.Address = runner.Address - } - if runner.Insecure != "" { - cfg.Client.Insecure, _ = strconv.ParseBool(runner.Insecure) - } - } else if err != nil { - return cfg, err - } - - // runner config - if cfg.Runner.Environ == nil { - cfg.Runner.Environ = map[string]string{ - "GITHUB_API_URL": cfg.Client.Address + "/api/v1", - "GITHUB_SERVER_URL": cfg.Client.Address, - } - } - if cfg.Runner.Name == "" { - cfg.Runner.Name, _ = os.Hostname() - } - - // platform config - if cfg.Platform.OS == "" { - cfg.Platform.OS = runtime.GOOS - } - if cfg.Platform.Arch == "" { - cfg.Platform.Arch = runtime.GOARCH - } - - if file := cfg.Runner.EnvFile; file != "" { - envs, err := godotenv.Read(file) +// LoadDefault returns the default configuration. +// If file is not empty, it will be used to load the configuration. +func LoadDefault(file string) (*Config, error) { + cfg := &Config{} + if file != "" { + f, err := os.Open(file) if err != nil { - return cfg, err + return nil, err } - for k, v := range envs { - cfg.Runner.Environ[k] = v + defer f.Close() + decoder := yaml.NewDecoder(f) + if err := decoder.Decode(&cfg); err != nil { + return nil, err + } + } + compatibleWithOldEnvs(file != "", cfg) + + if cfg.Runner.EnvFile != "" { + if stat, err := os.Stat(cfg.Runner.EnvFile); err == nil && !stat.IsDir() { + envs, err := godotenv.Read(cfg.Runner.EnvFile) + if err != nil { + return nil, fmt.Errorf("read env file %q: %w", cfg.Runner.EnvFile, err) + } + for k, v := range envs { + cfg.Runner.Envs[k] = v + } + } + } + + if cfg.Log.Level == "" { + cfg.Log.Level = "info" + } + if cfg.Runner.File == "" { + cfg.Runner.File = ".runner" + } + if cfg.Runner.Capacity <= 0 { + cfg.Runner.Capacity = 1 + } + if cfg.Runner.Timeout <= 0 { + cfg.Runner.Timeout = 3 * time.Hour + } + if cfg.Cache.Enabled == nil { + b := true + cfg.Cache.Enabled = &b + } + if *cfg.Cache.Enabled { + if cfg.Cache.Dir == "" { + home, _ := os.UserHomeDir() + cfg.Cache.Dir = filepath.Join(home, ".cache", "actcache") } } diff --git a/config/deprecated.go b/config/deprecated.go new file mode 100644 index 0000000..b5051aa --- /dev/null +++ b/config/deprecated.go @@ -0,0 +1,62 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package config + +import ( + "os" + "strconv" + "strings" + + log "github.com/sirupsen/logrus" +) + +// Deprecated: could be removed in the future. TODO: remove it when Gitea 1.20.0 is released. +// Be compatible with old envs. +func compatibleWithOldEnvs(fileUsed bool, cfg *Config) { + handleEnv := func(key string) (string, bool) { + if v, ok := os.LookupEnv(key); ok { + if fileUsed { + log.Warnf("env %s has been ignored because config file is used", key) + return "", false + } + log.Warnf("env %s will be deprecated, please use config file instead", key) + return v, true + } + return "", false + } + + if v, ok := handleEnv("GITEA_DEBUG"); ok { + if b, _ := strconv.ParseBool(v); b { + cfg.Log.Level = "debug" + } + } + if v, ok := handleEnv("GITEA_TRACE"); ok { + if b, _ := strconv.ParseBool(v); b { + cfg.Log.Level = "trace" + } + } + if v, ok := handleEnv("GITEA_RUNNER_CAPACITY"); ok { + if i, _ := strconv.Atoi(v); i > 0 { + cfg.Runner.Capacity = i + } + } + if v, ok := handleEnv("GITEA_RUNNER_FILE"); ok { + cfg.Runner.File = v + } + if v, ok := handleEnv("GITEA_RUNNER_ENVIRON"); ok { + splits := strings.Split(v, ",") + if cfg.Runner.Envs == nil { + cfg.Runner.Envs = map[string]string{} + } + for _, split := range splits { + kv := strings.SplitN(split, ":", 2) + if len(kv) == 2 && kv[0] != "" { + cfg.Runner.Envs[kv[0]] = kv[1] + } + } + } + if v, ok := handleEnv("GITEA_RUNNER_ENV_FILE"); ok { + cfg.Runner.EnvFile = v + } +} diff --git a/config/embed.go b/config/embed.go new file mode 100644 index 0000000..cf445cf --- /dev/null +++ b/config/embed.go @@ -0,0 +1,9 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package config + +import _ "embed" + +//go:embed config.example.yaml +var Example []byte diff --git a/config/registration.go b/config/registration.go new file mode 100644 index 0000000..be66b4f --- /dev/null +++ b/config/registration.go @@ -0,0 +1,54 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package config + +import ( + "encoding/json" + "os" +) + +const registrationWarning = "This file is automatically generated by act-runner. Do not edit it manually unless you know what you are doing. Removing this file will cause act runner to re-register as a new runner." + +// Registration is the registration information for a runner +type Registration struct { + Warning string `json:"WARNING"` // Warning message to display, it's always the registrationWarning constant + + ID int64 `json:"id"` + UUID string `json:"uuid"` + Name string `json:"name"` + Token string `json:"token"` + Address string `json:"address"` + Labels []string `json:"labels"` +} + +func LoadRegistration(file string) (*Registration, error) { + f, err := os.Open(file) + if err != nil { + return nil, err + } + defer f.Close() + + var reg Registration + if err := json.NewDecoder(f).Decode(®); err != nil { + return nil, err + } + + reg.Warning = "" + + return ®, nil +} + +func SaveRegistration(file string, reg *Registration) error { + f, err := os.Create(file) + if err != nil { + return err + } + defer f.Close() + + reg.Warning = registrationWarning + + enc := json.NewEncoder(f) + enc.SetIndent("", " ") + return enc.Encode(reg) +} diff --git a/core/runner.go b/core/runner.go deleted file mode 100644 index 2975ca5..0000000 --- a/core/runner.go +++ /dev/null @@ -1,18 +0,0 @@ -package core - -const ( - UUIDHeader = "x-runner-uuid" - TokenHeader = "x-runner-token" - VersionHeader = "x-runner-version" -) - -// Runner struct -type Runner struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - Name string `json:"name"` - Token string `json:"token"` - Address string `json:"address"` - Insecure string `json:"insecure"` - Labels []string `json:"labels"` -} diff --git a/go.mod b/go.mod index 2aafba1..92f7b29 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/go-chi/chi/v5 v5.0.8 github.com/go-chi/render v1.0.2 github.com/joho/godotenv v1.5.1 - github.com/kelseyhightower/envconfig v1.4.0 github.com/mattn/go-isatty v0.0.17 github.com/nektos/act v0.0.0 github.com/sirupsen/logrus v1.9.0 @@ -19,6 +18,7 @@ require ( golang.org/x/sync v0.1.0 golang.org/x/term v0.6.0 google.golang.org/protobuf v1.28.1 + gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.14.2 xorm.io/builder v0.3.11-0.20220531020008-1bd24a7dc978 xorm.io/xorm v1.3.2 @@ -91,7 +91,6 @@ require ( golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/uint128 v1.1.1 // indirect modernc.org/cc/v3 v3.35.18 // indirect modernc.org/ccgo/v3 v3.12.82 // indirect diff --git a/go.sum b/go.sum index 5436f69..dcf76d8 100644 --- a/go.sum +++ b/go.sum @@ -302,8 +302,6 @@ github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4d github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= -github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= -github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= diff --git a/poller/poller.go b/poller/poller.go index f91f8bf..9825551 100644 --- a/poller/poller.go +++ b/poller/poller.go @@ -7,22 +7,23 @@ import ( "time" runnerv1 "code.gitea.io/actions-proto-go/runner/v1" - "codeberg.org/forgejo/runner/client" - "github.com/bufbuild/connect-go" log "github.com/sirupsen/logrus" + + "codeberg.org/forgejo/runner/client" + "codeberg.org/forgejo/runner/config" ) var ErrDataLock = errors.New("Data Lock Error") -func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, workerNum int) *Poller { +func New(cli client.Client, dispatch func(context.Context, *runnerv1.Task) error, cfg *config.Config) *Poller { return &Poller{ Client: cli, Dispatch: dispatch, routineGroup: newRoutineGroup(), metric: &metric{}, - workerNum: workerNum, ready: make(chan struct{}, 1), + cfg: cfg, } } @@ -34,13 +35,13 @@ type Poller struct { routineGroup *routineGroup metric *metric ready chan struct{} - workerNum int + cfg *config.Config } func (p *Poller) schedule() { p.Lock() defer p.Unlock() - if int(p.metric.BusyWorkers()) >= p.workerNum { + if int(p.metric.BusyWorkers()) >= p.cfg.Runner.Capacity { return } @@ -148,7 +149,7 @@ func (p *Poller) dispatchTask(ctx context.Context, task *runnerv1.Task) error { } }() - runCtx, cancel := context.WithTimeout(ctx, time.Hour) + runCtx, cancel := context.WithTimeout(ctx, p.cfg.Runner.Timeout) defer cancel() return p.Dispatch(runCtx, task) diff --git a/register/register.go b/register/register.go deleted file mode 100644 index 4458174..0000000 --- a/register/register.go +++ /dev/null @@ -1,63 +0,0 @@ -package register - -import ( - "context" - "encoding/json" - "os" - "strconv" - "strings" - - runnerv1 "code.gitea.io/actions-proto-go/runner/v1" - "codeberg.org/forgejo/runner/client" - "codeberg.org/forgejo/runner/config" - "codeberg.org/forgejo/runner/core" - - "github.com/bufbuild/connect-go" - log "github.com/sirupsen/logrus" -) - -func New(cli client.Client) *Register { - return &Register{ - Client: cli, - } -} - -type Register struct { - Client client.Client -} - -func (p *Register) Register(ctx context.Context, cfg config.Runner) (*core.Runner, error) { - labels := make([]string, len(cfg.Labels)) - for i, v := range cfg.Labels { - labels[i] = strings.SplitN(v, ":", 2)[0] - } - // register new runner. - resp, err := p.Client.Register(ctx, connect.NewRequest(&runnerv1.RegisterRequest{ - Name: cfg.Name, - Token: cfg.Token, - AgentLabels: labels, - })) - if err != nil { - log.WithError(err).Error("poller: cannot register new runner") - return nil, err - } - - data := &core.Runner{ - ID: resp.Msg.Runner.Id, - UUID: resp.Msg.Runner.Uuid, - Name: resp.Msg.Runner.Name, - Token: resp.Msg.Runner.Token, - Address: p.Client.Address(), - Insecure: strconv.FormatBool(p.Client.Insecure()), - Labels: cfg.Labels, - } - - file, err := json.MarshalIndent(data, "", " ") - if err != nil { - log.WithError(err).Error("poller: cannot marshal the json input") - return data, err - } - - // store runner config in .runner file - return data, os.WriteFile(cfg.File, file, 0o644) -} From 7c1801b3027ba5ded8f0b396a83ceb3f4b6d325a Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 4 Apr 2023 14:32:01 +0800 Subject: [PATCH 8/8] Config for container network (#96) Fix #66 Reviewed-on: https://gitea.com/gitea/act_runner/pulls/96 Reviewed-by: Lunny Xiao --- cmd/daemon.go | 1 + config/config.example.yaml | 4 ++++ config/config.go | 6 ++++++ runtime/runtime.go | 3 ++- runtime/task.go | 4 ++-- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/daemon.go b/cmd/daemon.go index b71815f..188491d 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -71,6 +71,7 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, ForgeInstance: reg.Address, Environ: cfg.Runner.Envs, Labels: reg.Labels, + Network: cfg.Container.Network, Version: version, } diff --git a/config/config.example.yaml b/config/config.example.yaml index 10f3e47..1f05f68 100644 --- a/config/config.example.yaml +++ b/config/config.example.yaml @@ -36,3 +36,7 @@ cache: # The port of the cache server. # 0 means to use a random available port. port: 0 + +container: + # Which network to use for the job containers. + network: bridge diff --git a/config/config.go b/config/config.go index f7cc864..40fef9a 100644 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,9 @@ type Config struct { Host string `yaml:"host"` Port uint16 `yaml:"port"` } `yaml:"cache"` + Container struct { + Network string `yaml:"network"` + } } // LoadDefault returns the default configuration. @@ -81,6 +84,9 @@ func LoadDefault(file string) (*Config, error) { cfg.Cache.Dir = filepath.Join(home, ".cache", "actcache") } } + if cfg.Container.Network == "" { + cfg.Container.Network = "bridge" + } return cfg, nil } diff --git a/runtime/runtime.go b/runtime/runtime.go index 9d24caf..068dd4f 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -18,6 +18,7 @@ type Runner struct { Environ map[string]string Client client.Client Labels []string + Network string CacheHandler *artifactcache.Handler } @@ -30,7 +31,7 @@ func (s *Runner) Run(ctx context.Context, task *runnerv1.Task) error { if s.CacheHandler != nil { env["ACTIONS_CACHE_URL"] = s.CacheHandler.ExternalURL() + "/" } - return NewTask(s.ForgeInstance, task.Id, s.Client, env, s.platformPicker).Run(ctx, task, s.Machine, s.Version) + return NewTask(task.Id, s.Client, env, s.Network, s.platformPicker).Run(ctx, task, s.Machine, s.Version) } func (s *Runner) platformPicker(labels []string) string { diff --git a/runtime/task.go b/runtime/task.go index bffe098..ece4ad0 100644 --- a/runtime/task.go +++ b/runtime/task.go @@ -71,11 +71,11 @@ type Task struct { } // NewTask creates a new task -func NewTask(forgeInstance string, buildID int64, client client.Client, runnerEnvs map[string]string, picker func([]string) string) *Task { +func NewTask(buildID int64, client client.Client, runnerEnvs map[string]string, network string, picker func([]string) string) *Task { task := &Task{ Input: &TaskInput{ envs: runnerEnvs, - containerNetworkMode: "bridge", // TODO should be configurable + containerNetworkMode: network, }, BuildID: buildID,