/
jenkins.go
302 lines (269 loc) · 9.56 KB
/
jenkins.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
package opts
import (
"fmt"
"net/url"
"os"
"sort"
"strings"
"time"
gojenkins "github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/builds"
jxjenkins "github.com/jenkins-x/jx/v2/pkg/jenkins"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/kube/services"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// JenkinsSelectorOptions used to represent the options used to refer to a Jenkins.
// if nothing is specified it assumes the current team is using a static Jenkins server as its execution engine.
// otherwise we can refer to other additional Jenkins Apps to implement custom Jenkins servers
type JenkinsSelectorOptions struct {
UseCustomJenkins bool
CustomJenkinsName string
// cached client
cachedCustomJenkinsClient gojenkins.JenkinsClient
}
// AddFlags add the command flags for picking a custom Jenkins App to work with
func (o *JenkinsSelectorOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&o.UseCustomJenkins, "custom", "m", false, "Use a custom Jenkins App instead of the default execution engine in Jenkins X")
cmd.Flags().StringVarP(&o.CustomJenkinsName, "jenkins-name", "j", "", "The name of the custom Jenkins App if you don't wish to use the default execution engine in Jenkins X")
}
// IsCustom returns true if a custom Jenkins App is specified
func (o *JenkinsSelectorOptions) IsCustom() bool {
return o.UseCustomJenkins || o.CustomJenkinsName != ""
}
// GetAllPipelineJobNames returns all the pipeline job names
func (o *CommonOptions) GetAllPipelineJobNames(jenkinsClient gojenkins.JenkinsClient, jobNames *[]string, jobName string) error {
job, err := jenkinsClient.GetJob(jobName)
if err != nil {
return err
}
if len(job.Jobs) == 0 {
*jobNames = append(*jobNames, job.FullName)
}
for _, j := range job.Jobs {
err = o.GetAllPipelineJobNames(jenkinsClient, jobNames, job.FullName+"/"+j.Name)
if err != nil {
return err
}
}
return nil
}
// SetJenkinsClient sets the JenkinsClient - usually used in testing
func (o *CommonOptions) SetJenkinsClient(jenkinsClient gojenkins.JenkinsClient) {
o.jenkinsClient = jenkinsClient
}
// JenkinsClient returns the Jenkins client
func (o *CommonOptions) JenkinsClient() (gojenkins.JenkinsClient, error) {
if o.jenkinsClient == nil {
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, err
}
o.factory.SetBatch(o.BatchMode)
jenkins, err := o.factory.CreateJenkinsClient(kubeClient, ns, o.GetIOFileHandles())
if err != nil {
return nil, err
}
o.jenkinsClient = jenkins
}
return o.jenkinsClient, nil
}
// CustomJenkinsClient returns the Jenkins client for the custom jenkins app
func (o *CommonOptions) CustomJenkinsClient(jenkinsServiceName string) (gojenkins.JenkinsClient, error) {
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, err
}
o.factory.SetBatch(o.BatchMode)
return o.factory.CreateCustomJenkinsClient(kubeClient, ns, jenkinsServiceName, o.GetIOFileHandles())
}
// CustomJenkinsURL returns the default or the custom Jenkins URL
func (o *CommonOptions) CustomJenkinsURL(jenkinsSelector *JenkinsSelectorOptions, kubeClient kubernetes.Interface, ns string) (string, error) {
if !jenkinsSelector.UseCustomJenkins {
return o.factory.GetJenkinsURL(kubeClient, ns)
}
customJenkinsName, err := o.PickCustomJenkinsName(jenkinsSelector, kubeClient, ns)
if err != nil {
return "", err
}
return o.factory.GetCustomJenkinsURL(kubeClient, ns, customJenkinsName)
}
// PickCustomJenkinsName picks the name of a custom jenkins server App if available
func (o *CommonOptions) PickCustomJenkinsName(jenkinsSelector *JenkinsSelectorOptions, kubeClient kubernetes.Interface, ns string) (string, error) {
if !jenkinsSelector.UseCustomJenkins {
return "", nil
}
customJenkinsName := jenkinsSelector.CustomJenkinsName
if customJenkinsName == "" {
serviceInterface := kubeClient.CoreV1().Services(ns)
selector := kube.LabelKind + "=" + kube.ValueKindJenkins
serviceList, err := serviceInterface.List(metav1.ListOptions{
LabelSelector: selector,
})
if err != nil {
return "", errors.Wrapf(err, "failed to list Jenkins services in namespace %s with selector %s", ns, selector)
}
switch len(serviceList.Items) {
case 0:
return "", fmt.Errorf("No Jenkins App services found in namespace %s with selector %s\nAre you sure you installed a Jenkins App in this namespace?\nTry jx add app jx-app-jenkins", ns, selector)
case 1:
customJenkinsName = serviceList.Items[0].Name
default:
names := []string{}
for _, svc := range serviceList.Items {
names = append(names, svc.Name)
}
sort.Strings(names)
if o.BatchMode {
return "", util.MissingOptionWithOptions("jenkins-name", names)
}
customJenkinsName, err = util.PickName(names, "Pick which custom Jenkins App you wish to use: ", "Jenkins Apps are a way to add custom Jenkins servers into Jenkins X", o.GetIOFileHandles())
if err != nil {
return "", err
}
}
}
jenkinsSelector.CustomJenkinsName = customJenkinsName
if customJenkinsName == "" {
return "", fmt.Errorf("failed to find a csutom Jenkins App name in namespace %s", ns)
}
return customJenkinsName, nil
}
// CreateCustomJenkinsClient creates either a regular Jenkins client or if useCustom is true creates a JenkinsClient
// for a custom jenkins App. If no customJenkinsName is specified and there is only one available it is used. Otherwise
// the user is prompted to pick the Jenkins App to use if not in batch mode.
func (o *CommonOptions) CreateCustomJenkinsClient(jenkinsSelector *JenkinsSelectorOptions) (gojenkins.JenkinsClient, error) {
isProw, err := o.IsProw()
if err != nil {
return nil, err
}
if isProw {
jenkinsSelector.UseCustomJenkins = true
}
if jenkinsSelector == nil || !jenkinsSelector.UseCustomJenkins {
return o.JenkinsClient()
}
if jenkinsSelector.cachedCustomJenkinsClient != nil {
return jenkinsSelector.cachedCustomJenkinsClient, nil
}
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return nil, err
}
customJenkinsName, err := o.PickCustomJenkinsName(jenkinsSelector, kubeClient, ns)
if err != nil {
return nil, err
}
jenkinsClient, err := o.CustomJenkinsClient(customJenkinsName)
if err == nil {
jenkinsSelector.cachedCustomJenkinsClient = jenkinsClient
}
return jenkinsClient, err
}
// GetJenkinsJobs returns the existing Jenkins jobs
func (o *CommonOptions) GetJenkinsJobs(jenkinsSelector *JenkinsSelectorOptions, filter string) (map[string]gojenkins.Job, error) {
jobMap := map[string]gojenkins.Job{}
jenkins, err := o.CreateCustomJenkinsClient(jenkinsSelector)
if err != nil {
return jobMap, err
}
jobs, err := jenkins.GetJobs()
if err != nil {
return jobMap, err
}
o.AddJenkinsJobs(jenkins, &jobMap, filter, "", jobs)
return jobMap, nil
}
// AddJenkinsJobs add the given jobs to Jenkins
func (o *CommonOptions) AddJenkinsJobs(jenkins gojenkins.JenkinsClient, jobMap *map[string]gojenkins.Job, filter string, prefix string, jobs []gojenkins.Job) {
for _, j := range jobs {
job := j
name := jxjenkins.JobName(prefix, &job)
if jxjenkins.IsPipeline(&job) {
if filter == "" || strings.Contains(name, filter) {
(*jobMap)[name] = job
continue
}
}
if job.Jobs != nil {
o.AddJenkinsJobs(jenkins, jobMap, filter, name, job.Jobs)
} else {
job2, err := jenkins.GetJob(name)
if err == nil && job2.Jobs != nil {
o.AddJenkinsJobs(jenkins, jobMap, filter, name, job2.Jobs)
}
}
}
}
// TailJenkinsBuildLog tail the build log of the given Jenkins jobs name
func (o *CommonOptions) TailJenkinsBuildLog(jenkinsSelector *JenkinsSelectorOptions, jobName string, build *gojenkins.Build) error {
jenkins, err := o.CreateCustomJenkinsClient(jenkinsSelector)
if err != nil {
return nil
}
u, err := url.Parse(build.Url)
if err != nil {
return err
}
buildPath := u.Path
log.Logger().Infof("%s %s", "tailing the log of", fmt.Sprintf("%s #%d", jobName, build.Number))
// TODO Logger
return jenkins.TailLog(buildPath, o.Out, time.Second, time.Hour*100)
}
// GetJenkinsJobName returns the Jenkins job name
func (o *CommonOptions) GetJenkinsJobName() string {
owner := os.Getenv("REPO_OWNER")
repo := os.Getenv("REPO_NAME")
branch := o.GetBranchName("")
if owner != "" && repo != "" && branch != "" {
return fmt.Sprintf("%s/%s/%s", owner, repo, branch)
}
job := os.Getenv("JOB_NAME")
if job != "" {
return job
}
return ""
}
func (o *CommonOptions) GetBranchName(dir string) string {
branch := builds.GetBranchName()
if branch == "" {
if dir == "" {
dir = "."
}
var err error
branch, err = o.Git().Branch(dir)
if err != nil {
log.Logger().Warnf("failed to get the git branch name in dir %s", dir)
}
}
return branch
}
// UpdateJenkinsURL updates the Jenkins URL
func (o *CommonOptions) UpdateJenkinsURL(namespaces []string) error {
client, err := o.KubeClient()
if err != nil {
return err
}
// loop over each namespace and update the Jenkins URL if a Jenkins service is found
for _, n := range namespaces {
externalURL, err := services.GetServiceURLFromName(client, "jenkins", n)
if err != nil {
// skip namespace if no Jenkins service found
continue
}
log.Logger().Infof("Updating Jenkins with new external URL details %s", externalURL)
jenkins, err := o.factory.CreateJenkinsClient(client, n, o.GetIOFileHandles())
if err != nil {
return err
}
data := url.Values{}
data.Add("script", fmt.Sprintf(groovy, externalURL))
err = jenkins.Post("/scriptText", data, nil)
}
return nil
}