-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdrunner.go
163 lines (146 loc) · 4.37 KB
/
cmdrunner.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
package main
/*
Package tjob - Test job management utility
Copyright (c) 2014 Ohmu Ltd.
Licensed under the Apache License, Version 2.0 (see LICENSE)
*/
import (
"fmt"
"github.com/ohmu/tjob/config"
"github.com/ohmu/tjob/jenkins"
"github.com/ohmu/tjob/sshcmd"
"github.com/ohmu/tjob/tabout"
"path"
"strconv"
)
// TODO: go-flags support for forcing a lower-case struct to be processed (anonymous members)
// TODO: go-flags support for automatic lowercasing and dashifying (JobStatus -> job-status) as default long names
func init() {
globalParser().AddCommand("runner", "Runner commands", "Runner commands", &struct { // TODO: long desc
Add runnerAddCmd `command:"add" description:"Add a runner"`
List runnerListCmd `command:"list" description:"List runners"`
Update runnerUpdateCmd `command:"update" description:"Update a runner"`
Remove runnerRemoveCmd `command:"rm" description:"Remove a runner"`
}{})
}
type runnerPosArgs struct {
RunnerID string `description:"Runner ID"`
}
// TODO: go-flags support for positional args without the extra struct
type runnerIDCmd struct {
URL string `long:"url" description:"Jenkins URL"`
User string `long:"user" description:"Jenkins/SSH username"`
SSHPort int `long:"ssh-port" description:"Jenkins SSH port"`
SSHKey string `long:"ssh-key" description:"Jenkins SSH private key"`
Insecure string `long:"insecure" description:"Skip TLS server cert validation"`
runnerPosArgs `positional-args:"yes" required:"yes"`
}
type runnerAddCmd runnerIDCmd
func (r *runnerAddCmd) Execute(args []string) error {
conf, err := config.Load(globalFlags.ConfigFile)
if err != nil {
return err
}
if _, exists := conf.Runners[r.RunnerID]; exists {
return fmt.Errorf(
"runner '%s' already exists, use the 'runner update' command",
r.RunnerID)
}
sshPortInt := 54410
if r.SSHPort != 0 {
sshPortInt = r.SSHPort
}
sshPort := sshcmd.SSHPort(sshPortInt)
insecureStr := "false"
if r.Insecure != "" {
insecureStr = r.Insecure
}
insecure, err := strconv.ParseBool(insecureStr)
if err != nil {
return err
}
sshKey := "id_rsa"
if r.SSHKey != "" {
sshKey = r.SSHKey
}
conf.Runners[r.RunnerID] = &config.Runner{
URL: r.URL, SSHPort: sshPort, SSHKey: sshKey, User: r.User,
Insecure: insecure,
}
return conf.Save()
}
type runnerUpdateCmd runnerIDCmd
func (r *runnerUpdateCmd) Execute(args []string) error {
conf, err := config.Load(globalFlags.ConfigFile)
if err != nil {
return err
}
if _, exists := conf.Runners[r.RunnerID]; !exists {
return fmt.Errorf(
"runner '%s' does not exist, use the 'runner add' command",
r.RunnerID)
}
if value := r.URL; value != "" {
conf.Runners[r.RunnerID].URL = value
}
if value := sshcmd.SSHPort(r.SSHPort); value != 0 {
conf.Runners[r.RunnerID].SSHPort = value
}
if value := r.SSHKey; value != "" {
conf.Runners[r.RunnerID].SSHKey = value
}
if value := r.User; value != "" {
conf.Runners[r.RunnerID].User = value
}
if value := r.Insecure; value != "" {
flag, err := strconv.ParseBool(value)
if err != nil {
return err
}
conf.Runners[r.RunnerID].Insecure = flag
}
return conf.Save()
}
type runnerRemoveCmd runnerIDCmd
func (r *runnerRemoveCmd) Execute(args []string) error {
conf, err := config.Load(globalFlags.ConfigFile)
if err != nil {
return err
}
if _, exists := conf.Runners[r.RunnerID]; !exists {
return fmt.Errorf(
"runner '%s' does not exist", r.RunnerID)
}
delete(conf.Runners, r.RunnerID)
return conf.Save()
}
type runnerListCmd struct{}
func (r *runnerListCmd) Execute(args []string) error {
conf, err := config.Load(globalFlags.ConfigFile)
if err != nil {
return err
}
output := tabout.New([]string{"NAME", "URL", "USER", "SSH-PORT",
"SSH-KEY", "INSECURE"}, nil)
for name, runner := range conf.Runners {
output.Write(map[string]string{
"NAME": name, "URL": runner.URL, "USER": runner.User,
"SSH-PORT": runner.SSHPort.String(),
"SSH-KEY": runner.SSHKey,
"INSECURE": strconv.FormatBool(runner.Insecure),
})
}
output.Flush()
return nil
}
func getJenkins(conf *config.Config, runnerID string) (*jenkins.Jenkins, error) {
runner, exists := conf.Runners[runnerID]
if !exists {
return nil, fmt.Errorf(
"runner '%s' does not exist", runnerID)
}
jobCache := jenkins.JobCache{path.Join(conf.Dir(), "cache")}
jenk := jenkins.MakeJenkins(runnerID, runner.URL, runner.Insecure,
&jobCache)
return jenk, nil
}