-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathrunner_register.go
More file actions
119 lines (96 loc) · 3.53 KB
/
Copy pathrunner_register.go
File metadata and controls
119 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"io"
"os"
"strings"
"github.com/semaphoreui/semaphore/util"
"github.com/spf13/cobra"
)
var runnerRegisterArgs struct {
stdinRegistrationToken bool
registrationTokenFilePath string
projectID int
name string
tags []string
webhook string
enabled bool
nameSet bool
webhookSet bool
tagsSet bool
}
func init() {
runnerRegisterCmd.PersistentFlags().BoolVar(&runnerRegisterArgs.stdinRegistrationToken, "stdin-registration-token", false, "Read registration token from stdin")
runnerRegisterCmd.PersistentFlags().StringVar(&runnerRegisterArgs.name, "name", "", "Runner name to register with")
runnerRegisterCmd.PersistentFlags().StringSliceVar(&runnerRegisterArgs.tags, "tags", nil, "Runner tags (comma-separated or repeat the flag)")
runnerRegisterCmd.PersistentFlags().StringVar(&runnerRegisterArgs.webhook, "webhook", "", "Runner webhook URL")
runnerRegisterCmd.PersistentFlags().StringVar(&runnerRegisterArgs.registrationTokenFilePath, "registration-token-file", "", "Read registration token from a file")
runnerRegisterCmd.PersistentFlags().BoolVar(&runnerRegisterArgs.enabled, "enabled", true, "Enable or disable the runner on the server")
runnerRegisterCmd.PersistentFlags().IntVar(&runnerRegisterArgs.projectID, "project-id", 0, "Project ID for project-level runner (global runner if not provided)")
runnerCmd.AddCommand(runnerRegisterCmd)
}
func readRegistrationTokenFromFile(path string) {
tokenBytes, err := os.ReadFile(path)
if err != nil {
panic(err)
}
if len(tokenBytes) == 0 {
panic("Empty token")
}
util.Config.Runner.RegistrationToken = strings.TrimSpace(string(tokenBytes))
}
func initRunnerRegistrationToken() {
if runnerRegisterArgs.registrationTokenFilePath != "" {
readRegistrationTokenFromFile(runnerRegisterArgs.registrationTokenFilePath)
return
}
if util.Config.Runner.RegistrationTokenFile != "" {
readRegistrationTokenFromFile(util.Config.Runner.RegistrationTokenFile)
return
}
if !runnerRegisterArgs.stdinRegistrationToken {
return
}
tokenBytes, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
if len(tokenBytes) == 0 {
panic("Empty token")
}
util.Config.Runner.RegistrationToken = strings.TrimSpace(string(tokenBytes))
}
func applyRunnerRegisterFlags(cmd *cobra.Command) {
if cmd.PersistentFlags().Changed("name") {
util.Config.Runner.Name = runnerRegisterArgs.name
}
if cmd.PersistentFlags().Changed("webhook") {
util.Config.Runner.Webhook = runnerRegisterArgs.webhook
}
if cmd.PersistentFlags().Changed("tags") {
util.Config.Runner.Tags = runnerRegisterArgs.tags
}
// Always apply enabled: the flag defaults to true, but util.Config.Runner.Enabled
// stays false (zero value) unless we copy it. Without this, registration creates
// an inactive runner that never receives tasks.
util.Config.Runner.Enabled = runnerRegisterArgs.enabled
}
func registerRunner(cmd *cobra.Command) {
configFile := util.ConfigInit(persistentFlags.configPath, persistentFlags.noConfig)
initRunnerRegistrationToken()
applyRunnerRegisterFlags(cmd)
if runnerRegisterArgs.projectID > 0 {
util.Config.Runner.ProjectID = &runnerRegisterArgs.projectID
}
taskPool := createRunnerJobPool()
err := taskPool.Register(configFile)
if err != nil {
panic(err)
}
}
var runnerRegisterCmd = &cobra.Command{
Use: "register",
Short: "Register runner on the server",
Run: func(cmd *cobra.Command, args []string) {
registerRunner(cmd)
},
}