This commit is contained in:
sillyguodong 2023-06-08 15:29:56 +08:00
parent c9a8427124
commit f3209257ae
No known key found for this signature in database
GPG key ID: 4A0646FA86256DF4
7 changed files with 36 additions and 88 deletions

View file

@ -23,7 +23,6 @@ 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"
"gitea.com/gitea/act_runner/internal/pkg/ver"
)
@ -45,18 +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
lbls := reg.Labels
if len(cfg.Runner.Labels) > 0 {
// 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
}
lbls = cfg.Runner.Labels
}
ls := labels.Labels{}
for _, l := range reg.Labels {
for _, l := range lbls {
label, err := labels.Parse(l)
if err != nil {
log.WithError(err).Warnf("ignored invalid label %q", l)
@ -94,11 +88,10 @@ func runDaemon(ctx context.Context, configFile *string) func(cmd *cobra.Command,
} 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)
}
// if declare successfully, overrides the labels in .runner file that are valid in config file.
reg.Labels = ls.ToStrings()
if err := config.SaveRegistration(cfg.Runner.File, reg); err != nil {
return fmt.Errorf("failed to save runner config: %w", err)
}
}

View file

@ -265,12 +265,18 @@ func registerNoInteractive(configFile string, regArgs *registerArgs) error {
if regArgs.Labels != "" {
inputs.Labels = strings.Split(regArgs.Labels, ",")
}
if len(cfg.Runner.Labels) > 0 {
if regArgs.Labels != "" {
log.Warnf("Labels from command will be ignored, use labels defined in config file.")
if configFile != "" {
if len(cfg.Runner.Labels) > 0 {
if regArgs.Labels != "" {
log.Warn("Labels from command will be ignored, use labels defined in config file.")
}
inputs.Labels = cfg.Runner.Labels
} else {
log.Error("Please specify labels in config, and re-regitser.")
return nil
}
inputs.Labels = cfg.Runner.Labels
}
if inputs.RunnerName == "" {
inputs.RunnerName, _ = os.Hostname()
log.Infof("Runner name is empty, use hostname '%s'.", inputs.RunnerName)

View file

@ -4,7 +4,8 @@
package client
const (
UUIDHeader = "x-runner-uuid"
TokenHeader = "x-runner-token"
UUIDHeader = "x-runner-uuid"
TokenHeader = "x-runner-token"
// Deprecated: could be removed after Gitea 1.20 released
VersionHeader = "x-runner-version"
)

View file

@ -28,7 +28,7 @@ runner:
fetch_interval: 2s
# The labels of a runner are used to determine which jobs the runner can run, and how to run them.
# 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 registering, it will ask for inputting labels.
# If it's empty when execute `deamon`, will use labels in `.runner` file.
labels: []

View file

@ -90,3 +90,18 @@ func (l Labels) Names() []string {
}
return names
}
func (l Labels) ToStrings() []string {
ls := make([]string, 0, len(l))
for _, label := range l {
lbl := label.Name
if label.Schema != "" {
lbl += ":" + label.Schema
if label.Arg != "" {
lbl += ":" + label.Arg
}
}
ls = append(ls, lbl)
}
return ls
}

View file

@ -1,26 +0,0 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
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

@ -1,41 +0,0 @@
// 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)
})
}
}