Skip to content

Commit 92c513d

Browse files
Merge pull request #616 from cloudfoundry/director-fetch-logs-supports-system-and-all
Add --system and --all-logs support to bosh logs --director
2 parents 91bd3c7 + 87b99be commit 92c513d

File tree

6 files changed

+88
-155
lines changed

6 files changed

+88
-155
lines changed

cmd/logs.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ func buildTailCmd(opts LogsOpts) []string {
129129
return cmd
130130
}
131131

132+
func buildLogTypeArgument(opts LogsOpts) string {
133+
var logTypes []string
134+
if opts.All {
135+
logTypes = append(logTypes, "agent")
136+
logTypes = append(logTypes, "job")
137+
logTypes = append(logTypes, "system")
138+
} else {
139+
if opts.Agent {
140+
logTypes = append(logTypes, "agent")
141+
} else if opts.System {
142+
logTypes = append(logTypes, "system")
143+
} else {
144+
logTypes = append(logTypes, "job")
145+
}
146+
}
147+
logType := strings.Join(logTypes, ",")
148+
return logType
149+
}
150+
132151
func (c LogsCmd) fetch(opts LogsOpts) error {
133152
slug := opts.Args.Slug
134153
name := c.deployment.Name()
@@ -141,7 +160,8 @@ func (c LogsCmd) fetch(opts LogsOpts) error {
141160
name += "." + slug.IndexOrID()
142161
}
143162

144-
result, err := c.deployment.FetchLogs(slug, opts.Filters, opts.Agent, opts.System, opts.All)
163+
logType := buildLogTypeArgument(opts)
164+
result, err := c.deployment.FetchLogs(slug, opts.Filters, logType)
145165
if err != nil {
146166
return err
147167
}
@@ -238,10 +258,8 @@ func (c EnvLogsCmd) tail(opts LogsOpts, connOpts boshssh.ConnectionOpts, sshResu
238258
}
239259

240260
func (c EnvLogsCmd) fetch(opts LogsOpts, connOpts boshssh.ConnectionOpts, sshResult boshdir.SSHResult, agentClient biagentclient.AgentClient) error {
241-
logType := "job"
242-
if opts.Agent {
243-
logType = "agent"
244-
}
261+
logType := buildLogTypeArgument(opts)
262+
245263
bundleLogsResult, err := agentClient.BundleLogs(sshResult.Hosts[0].Username, logType, opts.Filters)
246264
if err != nil {
247265
return err

cmd/logs_test.go

+42-16
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ var _ = Describe("Logs", func() {
7979

8080
Expect(deployment.FetchLogsCallCount()).To(Equal(1))
8181

82-
slug, filters, agent, system, all := deployment.FetchLogsArgsForCall(0)
82+
slug, filters, logTypes := deployment.FetchLogsArgsForCall(0)
8383
Expect(slug).To(Equal(boshdir.NewAllOrInstanceGroupOrInstanceSlug("job", "index")))
8484
Expect(filters).To(BeEmpty())
85-
Expect(agent).To(BeFalse())
86-
Expect(system).To(BeFalse())
87-
Expect(all).To(BeFalse())
85+
Expect(logTypes).To(Equal("job"))
8886

8987
Expect(downloader.DownloadCallCount()).To(Equal(1))
9088

@@ -106,12 +104,10 @@ var _ = Describe("Logs", func() {
106104

107105
Expect(deployment.FetchLogsCallCount()).To(Equal(1))
108106

109-
slug, filters, agent, system, all := deployment.FetchLogsArgsForCall(0)
107+
slug, filters, logTypes := deployment.FetchLogsArgsForCall(0)
110108
Expect(slug).To(Equal(boshdir.NewAllOrInstanceGroupOrInstanceSlug("job", "index")))
111109
Expect(filters).To(Equal([]string{"filter1", "filter2"}))
112-
Expect(agent).To(BeTrue())
113-
Expect(system).To(BeFalse())
114-
Expect(all).To(BeFalse())
110+
Expect(logTypes).To(Equal("agent"))
115111
})
116112

117113
It("fetches system logs and allows custom filters", func() {
@@ -125,12 +121,10 @@ var _ = Describe("Logs", func() {
125121

126122
Expect(deployment.FetchLogsCallCount()).To(Equal(1))
127123

128-
slug, filters, agent, system, all := deployment.FetchLogsArgsForCall(0)
124+
slug, filters, logTypes := deployment.FetchLogsArgsForCall(0)
129125
Expect(slug).To(Equal(boshdir.NewAllOrInstanceGroupOrInstanceSlug("job", "index")))
130126
Expect(filters).To(Equal([]string{"filter1", "filter2"}))
131-
Expect(agent).To(BeFalse())
132-
Expect(system).To(BeTrue())
133-
Expect(all).To(BeFalse())
127+
Expect(logTypes).To(Equal("system"))
134128
})
135129

136130
It("fetches all logs and allows custom filters", func() {
@@ -144,12 +138,10 @@ var _ = Describe("Logs", func() {
144138

145139
Expect(deployment.FetchLogsCallCount()).To(Equal(1))
146140

147-
slug, filters, agent, system, all := deployment.FetchLogsArgsForCall(0)
141+
slug, filters, logTypes := deployment.FetchLogsArgsForCall(0)
148142
Expect(slug).To(Equal(boshdir.NewAllOrInstanceGroupOrInstanceSlug("job", "index")))
149143
Expect(filters).To(Equal([]string{"filter1", "filter2"}))
150-
Expect(agent).To(BeFalse())
151-
Expect(system).To(BeFalse())
152-
Expect(all).To(BeTrue())
144+
Expect(logTypes).To(Equal("agent,job,system"))
153145
})
154146

155147
It("fetches logs for more than one instance", func() {
@@ -669,6 +661,40 @@ var _ = Describe("Logs", func() {
669661
Expect(command.Run(opts)).ToNot(HaveOccurred())
670662
})
671663

664+
It("bundles system logs", func() {
665+
opts.Filters = []string{}
666+
opts.System = true
667+
668+
agentClient.EXPECT().BundleLogs(
669+
gomock.Eq(ExpUsername),
670+
gomock.Eq("system"),
671+
gomock.Eq([]string{}),
672+
).
673+
Return(bundleResult, nil).
674+
Times(1)
675+
agentClient.EXPECT().RemoveFile(gomock.Any()).
676+
Times(1)
677+
678+
Expect(command.Run(opts)).ToNot(HaveOccurred())
679+
})
680+
681+
It("bundles all logs", func() {
682+
opts.Filters = []string{}
683+
opts.All = true
684+
685+
agentClient.EXPECT().BundleLogs(
686+
gomock.Eq(ExpUsername),
687+
gomock.Eq("agent,job,system"),
688+
gomock.Eq([]string{}),
689+
).
690+
Return(bundleResult, nil).
691+
Times(1)
692+
agentClient.EXPECT().RemoveFile(gomock.Any()).
693+
Times(1)
694+
695+
Expect(command.Run(opts)).ToNot(HaveOccurred())
696+
})
697+
672698
It("returns error if bundling logs failed", func() {
673699
agentClient.EXPECT().BundleLogs(gomock.Any(), gomock.Any(), gomock.Any()).Return(agentclient.BundleLogsResult{}, errors.New("fake-logs-err"))
674700

director/deployment.go

+4-19
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ func (d DeploymentImpl) Manifest() (string, error) {
117117
return resp.Manifest, nil
118118
}
119119

120-
func (d DeploymentImpl) FetchLogs(slug AllOrInstanceGroupOrInstanceSlug, filters []string, agent bool, system bool, allLogs bool) (LogsResult, error) {
121-
blobID, sha1, err := d.client.FetchLogs(d.name, slug.Name(), slug.IndexOrID(), filters, agent, system, allLogs)
120+
func (d DeploymentImpl) FetchLogs(slug AllOrInstanceGroupOrInstanceSlug, filters []string, logTypes string) (LogsResult, error) {
121+
blobID, sha1, err := d.client.FetchLogs(d.name, slug.Name(), slug.IndexOrID(), filters, logTypes)
122122
if err != nil {
123123
return LogsResult{}, err
124124
}
@@ -254,7 +254,7 @@ func (d DeploymentImpl) Variables() ([]VariableResult, error) {
254254
return response, nil
255255
}
256256

257-
func (c Client) FetchLogs(deploymentName, instance, indexOrID string, filters []string, agent bool, system bool, allLogs bool) (string, string, error) {
257+
func (c Client) FetchLogs(deploymentName, instance, indexOrID string, filters []string, logTypes string) (string, string, error) {
258258
if len(deploymentName) == 0 {
259259
return "", "", bosherr.Error("Expected non-empty deployment name")
260260
}
@@ -273,22 +273,7 @@ func (c Client) FetchLogs(deploymentName, instance, indexOrID string, filters []
273273
query.Add("filters", strings.Join(filters, ","))
274274
}
275275

276-
var logTypes []string
277-
if allLogs {
278-
logTypes = append(logTypes, "agent")
279-
logTypes = append(logTypes, "job")
280-
logTypes = append(logTypes, "system")
281-
} else {
282-
if agent {
283-
logTypes = append(logTypes, "agent")
284-
} else if system {
285-
logTypes = append(logTypes, "system")
286-
} else {
287-
logTypes = append(logTypes, "job")
288-
}
289-
}
290-
291-
query.Add("type", strings.Join(logTypes, ","))
276+
query.Add("type", logTypes)
292277

293278
path := fmt.Sprintf("/deployments/%s/jobs/%s/%s/logs?%s",
294279
deploymentName, instance, indexOrID, query.Encode())

director/deployment_test.go

+8-100
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,12 @@ var _ = Describe("Deployment", func() {
194194
),
195195
)
196196

197-
result, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, false, false)
197+
result, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, "job")
198198
Expect(err).ToNot(HaveOccurred())
199199
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
200200
})
201201

202-
It("returns logs result for all deplotment", func() {
202+
It("returns logs result for all jobs", func() {
203203
ConfigureTaskResult(
204204
ghttp.CombineHandlers(
205205
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/*/*/logs", "type=job"),
@@ -216,15 +216,15 @@ var _ = Describe("Deployment", func() {
216216
),
217217
)
218218

219-
result, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("", ""), nil, false, false, false)
219+
result, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("", ""), nil, "job")
220220
Expect(err).ToNot(HaveOccurred())
221221
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
222222
})
223223

224-
It("is able to apply filters and fetch agent logs", func() {
224+
It("is able to apply filters and passes through the log type params when fetching logs", func() {
225225
ConfigureTaskResult(
226226
ghttp.CombineHandlers(
227-
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=agent&filters=f1,f2"),
227+
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=log%20type%20here&filters=f1,f2"),
228228
ghttp.VerifyBasicAuth("username", "password"),
229229
),
230230
``,
@@ -239,99 +239,7 @@ var _ = Describe("Deployment", func() {
239239
)
240240

241241
result, err := deployment.FetchLogs(
242-
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), []string{"f1", "f2"}, true, false, false)
243-
Expect(err).ToNot(HaveOccurred())
244-
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
245-
})
246-
247-
It("is correctly fetches job, agent, and system logs when --all is passed", func() {
248-
ConfigureTaskResult(
249-
ghttp.CombineHandlers(
250-
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=agent,job,system"),
251-
ghttp.VerifyBasicAuth("username", "password"),
252-
),
253-
``,
254-
server,
255-
)
256-
257-
server.AppendHandlers(
258-
ghttp.CombineHandlers(
259-
ghttp.VerifyRequest("GET", "/tasks/123"),
260-
ghttp.RespondWith(http.StatusOK, `{"result":"logs-blob-id"}`),
261-
),
262-
)
263-
264-
result, err := deployment.FetchLogs(
265-
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, false, true)
266-
Expect(err).ToNot(HaveOccurred())
267-
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
268-
})
269-
270-
It("is correctly only fetches system logs when --system is passed", func() {
271-
ConfigureTaskResult(
272-
ghttp.CombineHandlers(
273-
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=system"),
274-
ghttp.VerifyBasicAuth("username", "password"),
275-
),
276-
``,
277-
server,
278-
)
279-
280-
server.AppendHandlers(
281-
ghttp.CombineHandlers(
282-
ghttp.VerifyRequest("GET", "/tasks/123"),
283-
ghttp.RespondWith(http.StatusOK, `{"result":"logs-blob-id"}`),
284-
),
285-
)
286-
287-
result, err := deployment.FetchLogs(
288-
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, true, false)
289-
Expect(err).ToNot(HaveOccurred())
290-
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
291-
})
292-
293-
It("is correctly only fetches agent logs when --agent is passed", func() {
294-
ConfigureTaskResult(
295-
ghttp.CombineHandlers(
296-
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=agent"),
297-
ghttp.VerifyBasicAuth("username", "password"),
298-
),
299-
``,
300-
server,
301-
)
302-
303-
server.AppendHandlers(
304-
ghttp.CombineHandlers(
305-
ghttp.VerifyRequest("GET", "/tasks/123"),
306-
ghttp.RespondWith(http.StatusOK, `{"result":"logs-blob-id"}`),
307-
),
308-
)
309-
310-
result, err := deployment.FetchLogs(
311-
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, true, false, false)
312-
Expect(err).ToNot(HaveOccurred())
313-
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
314-
})
315-
316-
It("is correctly only fetches job logs when no flags are passed", func() {
317-
ConfigureTaskResult(
318-
ghttp.CombineHandlers(
319-
ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=job"),
320-
ghttp.VerifyBasicAuth("username", "password"),
321-
),
322-
``,
323-
server,
324-
)
325-
326-
server.AppendHandlers(
327-
ghttp.CombineHandlers(
328-
ghttp.VerifyRequest("GET", "/tasks/123"),
329-
ghttp.RespondWith(http.StatusOK, `{"result":"logs-blob-id"}`),
330-
),
331-
)
332-
333-
result, err := deployment.FetchLogs(
334-
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, false, false)
242+
NewAllOrInstanceGroupOrInstanceSlug("job", "id"), []string{"f1", "f2"}, "log type here")
335243
Expect(err).ToNot(HaveOccurred())
336244
Expect(result).To(Equal(LogsResult{BlobstoreID: "logs-blob-id", SHA1: ""}))
337245
})
@@ -348,15 +256,15 @@ var _ = Describe("Deployment", func() {
348256

349257
AppendBadRequest(ghttp.VerifyRequest("GET", "/tasks/123"), server)
350258

351-
_, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, false, false)
259+
_, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, "job")
352260
Expect(err).To(HaveOccurred())
353261
Expect(err.Error()).To(ContainSubstring("Finding task '123'"))
354262
})
355263

356264
It("returns error if response is non-200", func() {
357265
AppendBadRequest(ghttp.VerifyRequest("GET", "/deployments/dep/jobs/job/id/logs", "type=job"), server)
358266

359-
_, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, false, false, false)
267+
_, err := deployment.FetchLogs(NewAllOrInstanceGroupOrInstanceSlug("job", "id"), nil, "job")
360268
Expect(err).To(HaveOccurred())
361269
Expect(err.Error()).To(ContainSubstring("Fetching logs"))
362270
})

0 commit comments

Comments
 (0)