-
Notifications
You must be signed in to change notification settings - Fork 785
/
get_build_logs.go
277 lines (237 loc) · 7.29 KB
/
get_build_logs.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
package cmd
import (
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/jenkins-x/golang-jenkins"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/builds"
"github.com/jenkins-x/jx/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"k8s.io/client-go/kubernetes"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
corev1 "k8s.io/api/core/v1"
)
// GetBuildLogsOptions the command line options
type GetBuildLogsOptions struct {
GetOptions
Tail bool
Wait bool
BuildFilter builds.BuildPodInfoFilter
}
var (
get_build_log_long = templates.LongDesc(`
Display a build log
`)
get_build_log_example = templates.Examples(`
# Display a build log - with the user choosing which repo + build to view
jx get build log
# Pick a build to view the log based on the repo cheese
jx get build log --repo cheese
# Pick a pending knative build to view the log based
jx get build log -p
# Pick a pending knative build to view the log based on the repo cheese
jx get build log --repo cheese -p
# Pick a knative build for the 1234 Pull Request on the repo cheese
jx get build log --repo cheese --branch PR-1234
`)
)
// NewCmdGetBuildLogs creates the command
func NewCmdGetBuildLogs(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetBuildLogsOptions{
GetOptions: GetOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "log [flags]",
Short: "Display a build log",
Long: get_build_log_long,
Example: get_build_log_example,
Aliases: []string{"logs"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.Tail, "tail", "t", true, "Tails the build log to the current terminal")
cmd.Flags().BoolVarP(&options.Wait, "wait", "w", false, "Waits for the build to start before failing")
cmd.Flags().BoolVarP(&options.BuildFilter.Pending, "pending", "p", false, "Only display logs which are currently pending to choose from if no build name is supplied")
cmd.Flags().StringVarP(&options.BuildFilter.Filter, "filter", "f", "", "Filters all the available jobs by those that contain the given text")
cmd.Flags().StringVarP(&options.BuildFilter.Owner, "owner", "o", "", "Filters the owner (person/organisation) of the repository")
cmd.Flags().StringVarP(&options.BuildFilter.Repository, "repo", "r", "", "Filters the build repository")
cmd.Flags().StringVarP(&options.BuildFilter.Branch, "branch", "", "", "Filters the branch")
cmd.Flags().StringVarP(&options.BuildFilter.Build, "build", "b", "", "The build number to view")
return cmd
}
// Run implements this command
func (o *GetBuildLogsOptions) Run() error {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
devEnv, err := kube.GetEnrichedDevEnvironment(kubeClient, jxClient, ns)
webhookEngine := devEnv.Spec.WebHookEngine
if webhookEngine == v1.WebHookEngineProw {
return o.getProwBuildLog(kubeClient, jxClient, ns)
}
args := o.Args
if !o.BatchMode && len(args) == 0 {
jobMap, err := o.getJobMap(o.BuildFilter.Filter)
if err != nil {
return err
}
names := []string{}
for k, _ := range jobMap {
names = append(names, k)
}
sort.Strings(names)
if len(names) == 0 {
return fmt.Errorf("No pipelines have been built!")
}
defaultName := ""
for _, n := range names {
if strings.HasSuffix(n, "/master") {
defaultName = n
break
}
}
name, err := util.PickNameWithDefault(names, "Which pipeline do you want to view the logs of?: ", defaultName, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
args = []string{name}
}
if len(args) == 0 {
return fmt.Errorf("No pipeline chosen")
}
name := args[0]
buildNumber := o.BuildFilter.BuildNumber()
last, err := o.getLastJenkinsBuild(name, buildNumber)
if err != nil {
return err
}
log.Infof("%s %s\n", util.ColorStatus("view the log at:"), util.ColorInfo(util.UrlJoin(last.Url, "/console")))
return o.tailBuild(name, &last)
}
func (o *GetBuildLogsOptions) getLastJenkinsBuild(name string, buildNumber int) (gojenkins.Build, error) {
var last gojenkins.Build
jenkinsClient, err := o.JenkinsClient()
if err != nil {
return last, err
}
f := func() error {
var err error
jobMap, err := o.getJobMap(o.BuildFilter.Filter)
if err != nil {
return err
}
job := jobMap[name]
if job.Url == "" {
return fmt.Errorf("No Job exists yet called %s", name)
}
if buildNumber > 0 {
last, err = jenkinsClient.GetBuild(job, buildNumber)
} else {
last, err = jenkinsClient.GetLastBuild(job)
}
if err != nil {
return err
}
if last.Url == "" {
if buildNumber > 0 {
return fmt.Errorf("No build found for name %s number %d", name, buildNumber)
} else {
return fmt.Errorf("No build found for name %s", name)
}
}
return err
}
if o.Wait {
err := o.retry(60, time.Second*2, f)
return last, err
} else {
err := f()
return last, err
}
}
func (o *GetBuildLogsOptions) getProwBuildLog(kubeClient kubernetes.Interface, jxClient versioned.Interface, ns string) error {
pods, err := builds.GetBuildPods(kubeClient, ns)
if err != nil {
log.Warnf("Failed to query pods %s\n", err)
return err
}
buildInfos := []*builds.BuildPodInfo{}
for _, pod := range pods {
initContainers := pod.Spec.InitContainers
if len(initContainers) > 0 {
buildInfo := builds.CreateBuildPodInfo(pod)
if o.BuildFilter.BuildMatches(buildInfo) {
buildInfos = append(buildInfos, buildInfo)
}
}
}
builds.SortBuildPodInfos(buildInfos)
if len(buildInfos) == 0 {
return fmt.Errorf("No knative builds have been triggered which match the current filter!")
}
args := o.Args
names := []string{}
buildMap := map[string]*builds.BuildPodInfo{}
defaultName := ""
for _, build := range buildInfos {
name := build.Pipeline + " #" + build.Build
names = append(names, name)
buildMap[name] = build
if build.Branch == "master" {
defaultName = name
}
}
if len(args) == 0 {
name, err := util.PickNameWithDefault(names, "Which build do you want to view the logs of?: ", defaultName, "", o.In, o.Out, o.Err)
if err != nil {
return err
}
args = []string{name}
}
if len(args) == 0 {
return fmt.Errorf("No pipeline chosen")
}
name := args[0]
build := buildMap[name]
if build == nil {
return fmt.Errorf("No Pipeline found for name %s", name)
}
pod := build.Pod
if pod == nil {
return fmt.Errorf("No Pod found for name %s", name)
}
initContainers := pod.Spec.InitContainers
if len(initContainers) <= 0 {
return fmt.Errorf("No InitContainers for Pod %s for build: %s", pod.Name, name)
}
lastInitC := initContainers[len(initContainers)-1]
log.Infof("Build logs for %s\n", util.ColorInfo(name))
return o.getPodLog(ns, pod, lastInitC)
}
func (o *GetBuildLogsOptions) getPodLog(ns string, pod *corev1.Pod, container corev1.Container) error {
log.Infof("Getting the pod log for pod %s and init container %s\n", pod.Name, container.Name)
return o.tailLogs(ns, pod.Name, container.Name)
}