-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.go
451 lines (374 loc) · 11.5 KB
/
register.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package commands
import (
"bufio"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers/ssh"
"gitlab.com/gitlab-org/gitlab-runner/network"
)
type configTemplate struct {
*common.Config
ConfigFile string `long:"config" env:"TEMPLATE_CONFIG_FILE" description:"Path to the configuration template file"`
}
func (c *configTemplate) Enabled() bool {
return c.ConfigFile != ""
}
func (c *configTemplate) MergeTo(config *common.RunnerConfig) error {
err := c.loadConfigTemplate()
if err != nil {
return errors.Wrap(err, "couldn't load configuration template file")
}
if len(c.Runners) != 1 {
return errors.New("configuration template must contain exactly one [[runners]] entry")
}
err = mergo.Merge(config, c.Runners[0])
if err != nil {
return errors.Wrap(err, "error while merging configuration with configuration template")
}
return nil
}
func (c *configTemplate) loadConfigTemplate() error {
config := common.NewConfig()
err := config.LoadConfig(c.ConfigFile)
if err != nil {
return err
}
c.Config = config
return nil
}
type RegisterCommand struct {
context *cli.Context
network common.Network
reader *bufio.Reader
registered bool
configOptions
ConfigTemplate configTemplate `namespace:"template"`
TagList string `long:"tag-list" env:"RUNNER_TAG_LIST" description:"Tag list"`
NonInteractive bool `short:"n" long:"non-interactive" env:"REGISTER_NON_INTERACTIVE" description:"Run registration unattended"`
LeaveRunner bool `long:"leave-runner" env:"REGISTER_LEAVE_RUNNER" description:"Don't remove runner if registration fails"`
RegistrationToken string `short:"r" long:"registration-token" env:"REGISTRATION_TOKEN" description:"Runner's registration token"`
RunUntagged bool `long:"run-untagged" env:"REGISTER_RUN_UNTAGGED" description:"Register to run untagged builds; defaults to 'true' when 'tag-list' is empty"`
Locked bool `long:"locked" env:"REGISTER_LOCKED" description:"Lock Runner for current project, defaults to 'true'"`
AccessLevel string `long:"access-level" env:"REGISTER_ACCESS_LEVEL" description:"Set access_level of the runner to not_protected or ref_protected; defaults to not_protected"`
MaximumTimeout int `long:"maximum-timeout" env:"REGISTER_MAXIMUM_TIMEOUT" description:"What is the maximum timeout (in seconds) that will be set for job when using this Runner"`
Paused bool `long:"paused" env:"REGISTER_PAUSED" description:"Set Runner to be paused, defaults to 'false'"`
common.RunnerConfig
}
type AccessLevel string
const (
NotProtected AccessLevel = "not_protected"
RefProtected AccessLevel = "ref_protected"
)
const (
defaultDockerWindowCacheDir = "c:\\cache"
)
func (s *RegisterCommand) askOnce(prompt string, result *string, allowEmpty bool) bool {
println(prompt)
if *result != "" {
print("["+*result, "]: ")
}
if s.reader == nil {
s.reader = bufio.NewReader(os.Stdin)
}
data, _, err := s.reader.ReadLine()
if err != nil {
panic(err)
}
newResult := string(data)
newResult = strings.TrimSpace(newResult)
if newResult != "" {
*result = newResult
return true
}
if allowEmpty || *result != "" {
return true
}
return false
}
func (s *RegisterCommand) ask(key, prompt string, allowEmptyOptional ...bool) string {
allowEmpty := len(allowEmptyOptional) > 0 && allowEmptyOptional[0]
result := s.context.String(key)
result = strings.TrimSpace(result)
if s.NonInteractive || prompt == "" {
if result == "" && !allowEmpty {
logrus.Panicln("The", key, "needs to be entered")
}
return result
}
for {
if s.askOnce(prompt, &result, allowEmpty) {
break
}
}
return result
}
func (s *RegisterCommand) askExecutor() {
for {
names := common.GetExecutors()
executors := strings.Join(names, ", ")
s.Executor = s.ask("executor", "Please enter the executor: "+executors+":", true)
if common.GetExecutor(s.Executor) != nil {
return
}
message := "Invalid executor specified"
if s.NonInteractive {
logrus.Panicln(message)
} else {
logrus.Errorln(message)
}
}
}
func (s *RegisterCommand) askDocker() {
s.askBasicDocker("ruby:2.6")
for _, volume := range s.Docker.Volumes {
parts := strings.Split(volume, ":")
if parts[len(parts)-1] == "/cache" {
return
}
}
s.Docker.Volumes = append(s.Docker.Volumes, "/cache")
}
func (s *RegisterCommand) askDockerWindows() {
s.askBasicDocker("mcr.microsoft.com/windows/servercore:1809")
for _, volume := range s.Docker.Volumes {
// This does not cover all the possibilities since we don't have access
// to volume parsing package since it's internal.
if strings.Contains(volume, defaultDockerWindowCacheDir) {
return
}
}
s.Docker.Volumes = append(s.Docker.Volumes, defaultDockerWindowCacheDir)
}
func (s *RegisterCommand) askBasicDocker(exampleHelperImage string) {
if s.Docker == nil {
s.Docker = &common.DockerConfig{}
}
s.Docker.Image = s.ask("docker-image", fmt.Sprintf("Please enter the default Docker image (e.g. %s):", exampleHelperImage))
}
func (s *RegisterCommand) askParallels() {
s.Parallels.BaseName = s.ask("parallels-base-name", "Please enter the Parallels VM (e.g. my-vm):")
}
func (s *RegisterCommand) askVirtualBox() {
s.VirtualBox.BaseName = s.ask("virtualbox-base-name", "Please enter the VirtualBox VM (e.g. my-vm):")
}
func (s *RegisterCommand) askSSHServer() {
s.SSH.Host = s.ask("ssh-host", "Please enter the SSH server address (e.g. my.server.com):")
s.SSH.Port = s.ask("ssh-port", "Please enter the SSH server port (e.g. 22):", true)
}
func (s *RegisterCommand) askSSHLogin() {
s.SSH.User = s.ask("ssh-user", "Please enter the SSH user (e.g. root):")
s.SSH.Password = s.ask("ssh-password", "Please enter the SSH password (e.g. docker.io):", true)
s.SSH.IdentityFile = s.ask("ssh-identity-file", "Please enter path to SSH identity file (e.g. /home/user/.ssh/id_rsa):", true)
}
func (s *RegisterCommand) addRunner(runner *common.RunnerConfig) {
s.config.Runners = append(s.config.Runners, runner)
}
func (s *RegisterCommand) askRunner() {
s.URL = s.ask("url", "Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):")
if s.Token != "" {
logrus.Infoln("Token specified trying to verify runner...")
logrus.Warningln("If you want to register use the '-r' instead of '-t'.")
if !s.network.VerifyRunner(s.RunnerCredentials) {
logrus.Panicln("Failed to verify this runner. Perhaps you are having network problems")
}
} else {
// we store registration token as token, since we pass that to RunnerCredentials
s.Token = s.ask("registration-token", "Please enter the gitlab-ci token for this runner:")
s.Name = s.ask("name", "Please enter the gitlab-ci description for this runner:")
s.TagList = s.ask("tag-list", "Please enter the gitlab-ci tags for this runner (comma separated):", true)
if s.TagList == "" {
s.RunUntagged = true
}
parameters := common.RegisterRunnerParameters{
Description: s.Name,
Tags: s.TagList,
Locked: s.Locked,
AccessLevel: s.AccessLevel,
RunUntagged: s.RunUntagged,
MaximumTimeout: s.MaximumTimeout,
Active: !s.Paused,
}
result := s.network.RegisterRunner(s.RunnerCredentials, parameters)
if result == nil {
logrus.Panicln("Failed to register this runner. Perhaps you are having network problems")
}
s.Token = result.Token
s.registered = true
}
}
func (s *RegisterCommand) askExecutorOptions() {
kubernetes := s.Kubernetes
machine := s.Machine
docker := s.Docker
ssh := s.SSH
parallels := s.Parallels
virtualbox := s.VirtualBox
custom := s.Custom
s.Kubernetes = nil
s.Machine = nil
s.Docker = nil
s.SSH = nil
s.Parallels = nil
s.VirtualBox = nil
s.Custom = nil
executorFns := map[string]func(){
"kubernetes": func() {
s.Kubernetes = kubernetes
},
"docker+machine": func() {
s.Machine = machine
s.Docker = docker
s.askDocker()
},
"docker-ssh+machine": func() {
s.Machine = machine
s.Docker = docker
s.SSH = ssh
s.askDocker()
s.askSSHLogin()
},
"docker": func() {
s.Docker = docker
s.askDocker()
},
"docker-windows": func() {
s.Docker = docker
s.askDockerWindows()
},
"docker-ssh": func() {
s.Docker = docker
s.SSH = ssh
s.askDocker()
s.askSSHLogin()
},
"ssh": func() {
s.SSH = ssh
s.askSSHServer()
s.askSSHLogin()
},
"parallels": func() {
s.SSH = ssh
s.Parallels = parallels
s.askParallels()
s.askSSHServer()
},
"virtualbox": func() {
s.SSH = ssh
s.VirtualBox = virtualbox
s.askVirtualBox()
s.askSSHLogin()
},
"shell": func() {
if runtime.GOOS == "windows" && s.RunnerConfig.Shell == "" {
s.Shell = "powershell"
}
},
"custom": func() {
s.Custom = custom
},
}
executorFn, ok := executorFns[s.Executor]
if ok {
executorFn()
}
}
func (s *RegisterCommand) Execute(context *cli.Context) {
userModeWarning(true)
s.context = context
err := s.loadConfig()
if err != nil {
logrus.Panicln(err)
}
validAccessLevels := []AccessLevel{NotProtected, RefProtected}
if !accessLevelValid(validAccessLevels, AccessLevel(s.AccessLevel)) {
logrus.Panicln("Given access-level is not valid. " +
"Please refer to gitlab-runner register -h for the correct options.")
}
s.askRunner()
if !s.LeaveRunner {
defer func() {
// De-register runner on panic
if r := recover(); r != nil {
if s.registered {
s.network.UnregisterRunner(s.RunnerCredentials)
}
// pass panic to next defer
panic(r)
}
}()
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
go func() {
signal := <-signals
s.network.UnregisterRunner(s.RunnerCredentials)
logrus.Fatalf("RECEIVED SIGNAL: %v", signal)
}()
}
if s.config.Concurrent < s.Limit {
logrus.Warningf("Specified limit (%d) larger then current concurrent limit (%d). Concurrent limit will not be enlarged.", s.Limit, s.config.Concurrent)
}
s.askExecutor()
s.askExecutorOptions()
s.mergeTemplate()
s.addRunner(&s.RunnerConfig)
err = s.saveConfig()
if err != nil {
logrus.Panicln(err)
}
logrus.Printf("Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!")
}
func (s *RegisterCommand) mergeTemplate() {
if !s.ConfigTemplate.Enabled() {
return
}
logrus.Infof("Merging configuration from template file %q", s.ConfigTemplate.ConfigFile)
err := s.ConfigTemplate.MergeTo(&s.RunnerConfig)
if err != nil {
logrus.WithError(err).Fatal("Could not handle configuration merging from template file")
}
}
func getHostname() string {
hostname, _ := os.Hostname()
return hostname
}
func newRegisterCommand() *RegisterCommand {
return &RegisterCommand{
RunnerConfig: common.RunnerConfig{
Name: getHostname(),
RunnerSettings: common.RunnerSettings{
Kubernetes: &common.KubernetesConfig{},
Cache: &common.CacheConfig{},
Machine: &common.DockerMachine{},
Docker: &common.DockerConfig{},
SSH: &ssh.Config{},
Parallels: &common.ParallelsConfig{},
VirtualBox: &common.VirtualBoxConfig{},
},
},
Locked: true,
Paused: false,
network: network.NewGitLabClient(),
}
}
func accessLevelValid(levels []AccessLevel, givenLevel AccessLevel) bool {
if givenLevel == "" {
return true
}
for _, level := range levels {
if givenLevel == level {
return true
}
}
return false
}
func init() {
common.RegisterCommand2("register", "register a new runner", newRegisterCommand())
}