-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconfig.go
More file actions
141 lines (127 loc) · 3.53 KB
/
config.go
File metadata and controls
141 lines (127 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"log/slog"
"net/url"
"os"
"strings"
"github.com/actions/scaleset"
)
type Config struct {
RegistrationURL string
MaxRunners int
MinRunners int
ScaleSetName string
Labels []string
RunnerGroup string
GitHubApp scaleset.GitHubAppAuth
Token string
RunnerImage string
LogLevel string
LogFormat string
}
func (c *Config) defaults() {
if c.RunnerGroup == "" {
c.RunnerGroup = scaleset.DefaultRunnerGroup
}
if c.RunnerImage == "" {
c.RunnerImage = "ghcr.io/actions/actions-runner:latest"
}
}
func (c *Config) Validate() error {
c.defaults()
if _, err := url.ParseRequestURI(c.RegistrationURL); err != nil {
return fmt.Errorf("invalid registration URL: %w, it should be the full URL of where you want to register your scale set, e.g. 'https://github.com/org/repo'", err)
}
appError := c.GitHubApp.Validate()
if c.Token == "" && appError != nil {
return fmt.Errorf("no credentials provided: either GitHub App (client id, installation id and private key) (recommended) or a Personal Access Token are required")
}
if c.ScaleSetName == "" {
return fmt.Errorf("scale set name is required")
}
for i, label := range c.Labels {
if strings.TrimSpace(label) == "" {
return fmt.Errorf("label at index %d is empty", i)
}
}
if c.MaxRunners < c.MinRunners {
return fmt.Errorf("max runners cannot be less than min-runners")
}
if c.RunnerGroup == "" {
return fmt.Errorf("runner group is required")
}
if c.RunnerImage == "" {
return fmt.Errorf("runner image is required")
}
return nil
}
// systemInfo serves as a base system info
func systemInfo(scaleSetID int) scaleset.SystemInfo {
return scaleset.SystemInfo{
System: "dockerscaleset",
Subsystem: "dockerscaleset",
CommitSHA: "NA", // You can leverage build flags to set commit SHA
Version: "0.1.0", // You can leverage build flags to set version
ScaleSetID: scaleSetID,
}
}
func (c *Config) ScalesetClient() (*scaleset.Client, error) {
if err := c.GitHubApp.Validate(); err == nil {
return scaleset.NewClientWithGitHubApp(
scaleset.ClientWithGitHubAppConfig{
GitHubConfigURL: c.RegistrationURL,
GitHubAppAuth: c.GitHubApp,
SystemInfo: systemInfo(0),
},
)
}
return scaleset.NewClientWithPersonalAccessToken(
scaleset.NewClientWithPersonalAccessTokenConfig{
GitHubConfigURL: c.RegistrationURL,
PersonalAccessToken: c.Token,
SystemInfo: systemInfo(0),
},
)
}
func (c *Config) Logger() *slog.Logger {
var lvl slog.Level
switch strings.ToLower(c.LogLevel) {
case "debug":
lvl = slog.LevelDebug
case "info":
lvl = slog.LevelInfo
case "warn":
lvl = slog.LevelWarn
case "error":
lvl = slog.LevelError
default:
lvl = slog.LevelInfo
}
switch c.LogFormat {
case "json":
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: lvl,
}))
case "text":
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: lvl,
}))
default:
return slog.New(slog.DiscardHandler)
}
}
// BuildLabels returns the labels to use for the runner scale set.
// If custom labels are provided, those are used; otherwise, the scale set name is used as the label.
func (c *Config) BuildLabels() []scaleset.Label {
if len(c.Labels) > 0 {
labels := make([]scaleset.Label, len(c.Labels))
for i, name := range c.Labels {
labels[i] = scaleset.Label{Name: strings.TrimSpace(name)}
}
return labels
}
return []scaleset.Label{{Name: c.ScaleSetName}}
}