This commit is contained in:
sillyguodong 2023-06-06 16:57:34 +08:00
parent e89f91316c
commit 71a61d161b
6 changed files with 95 additions and 18 deletions

View file

@ -12,6 +12,7 @@ import (
"strconv"
"strings"
"github.com/bufbuild/connect-go"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -22,6 +23,7 @@ import (
"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/internal/pkg/utils"
)
func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command, args []string) error {
@ -42,11 +44,13 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
return fmt.Errorf("failed to load registration file: %w", err)
}
var overwrite bool
if len(cfg.Runner.Labels) > 0 {
// overwirte the labels in the config file to the state file.
reg.Labels = cfg.Runner.Labels
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
return fmt.Errorf("failed to save runner config: %w", err)
// Determine if the labels in the `.runner` file are the same as the labels in config.
if isEqual := utils.AreStrSlicesElemsEqual(cfg.Runner.Labels, reg.Labels); !isEqual {
// If not, set `overwrite` to true, and will overwrite `.runner` after declaring successfully.
overwrite = true
reg.Labels = cfg.Runner.Labels
}
}
@ -78,10 +82,24 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
runner := run.NewRunner(cfg, reg, cli)
// declare the labels of the runner before fetching tasks
err = runner.Declare(ctx, ls.Names())
if err != nil {
return fmt.Errorf("failed to declare runner: %w", err)
resp, err := runner.Declare(ctx, ls.Names())
if err != nil && connect.CodeOf(err) == connect.CodeUnimplemented {
// Gitea instance is older version. skip declare step.
log.Warn("Because the Gitea instance is an old version, skip declare labels and version.")
} else if err != nil {
log.WithError(err).Error("fail to invoke Declare")
return err
} else {
log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully",
resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels)
if overwrite {
// overwrite .runner file
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
return fmt.Errorf("failed to save runner config: %w", err)
}
}
}
poller := poll.New(cfg, cli, runner)
poller.Poll(ctx)

View file

@ -46,7 +46,6 @@ func runRegister(ctx context.Context, regArgs *registerArgs, configFile *string)
log.Warnf("Runner in user-mode.")
}
log.Warnf("configFile: %s", *configFile)
if regArgs.NoInteractive {
if err := registerNoInteractive(*configFile, regArgs); err != nil {
return err

View file

@ -216,15 +216,9 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
return execErr
}
func (r *Runner) Declare(ctx context.Context, labels []string) error {
resp, err := r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{
func (r *Runner) Declare(ctx context.Context, labels []string) (*connect.Response[runnerv1.DeclareResponse], error) {
return r.client.Declare(ctx, connect.NewRequest(&runnerv1.DeclareRequest{
Version: ver.Version(),
Labels: labels,
}))
if err != nil {
return err
}
log.Infof("runner: %s, with version: %s, with labels: %v, declare successfully",
resp.Msg.Runner.Name, resp.Msg.Runner.Version, resp.Msg.Runner.Labels)
return nil
}

View file

@ -27,8 +27,10 @@ runner:
# The interval for fetching the job from the Gitea instance.
fetch_interval: 2s
# The labels of a runner are used to determine which jobs the runner can run, and how to run them.
# Please enter sequences (equivalent to lists or arrays).
labels:
# Like: ["macos-arm64:host", "ubuntu-latest:docker://node:16-bullseye", "ubuntu-22.04:docker://node:16-bullseye"]
# If it's empty when registering, will use default labels.
# If it's empty when execute `deamon`, will use labels in `.runner` file.
labels: []
cache:
# Enable cache server to use actions/cache.

View file

@ -0,0 +1,23 @@
package utils
import "reflect"
// AreStrSlicesElemsEqual determines whether elements in two slices are equal (ignoring order).
func AreStrSlicesElemsEqual(s1, s2 []string) bool {
set1 := make(map[string]struct{})
set2 := make(map[string]struct{})
for _, elem := range s1 {
set1[elem] = struct{}{}
}
for _, elem := range s2 {
set2[elem] = struct{}{}
}
if len(set1) != len(set2) {
return false
}
return reflect.DeepEqual(set1, set2)
}

View file

@ -0,0 +1,41 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAreStrSlicesElemsEqual(t *testing.T) {
tests := []struct {
s1 []string
s2 []string
expect bool
}{
{
s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"},
s2: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"},
expect: true,
},
{
s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"},
s2: []string{"macos-arm64:host", "windows-amd64:host"},
expect: false,
},
{
s1: []string{"macos-arm64:host", "windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye"},
s2: []string{"windows-amd64:host", "ubuntu-latest:docker://node:16-bullseye", "macos-arm64:host"},
expect: true,
},
}
for _, tt := range tests {
t.Run("test", func(t *testing.T) {
actual := AreStrSlicesElemsEqual(tt.s1, tt.s2)
assert.Equal(t, actual, tt.expect)
})
}
}