Skip to content

Commit 6e0cdbe

Browse files
authored
fix: display task progressbar in file. (#2599)
Signed-off-by: joyceliu <[email protected]>
1 parent cec2e1a commit 6e0cdbe

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

.golangci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ linters-settings:
231231
excludes:
232232
- G106 # Deferring unsafe method "InsecureIgnoreHostKey" on type "\*ssh"
233233
- G301 # Deferring unsafe method "MkdirAll" on type "\*os.File"
234+
- G302 # Deferring unsafe method "Create" or "Open" on type "\*os.File"
234235
- G304 # Deferring unsafe method "Create" or "Open" on type "\*os.File"
235236
- G306 # Deferring unsafe method "WriteFile" on type "\*os.File"
236237
- G307 # Deferring unsafe method "Close" on type "\*os.File"

builtin/core/roles/precheck/env_check/tasks/kubernetes.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@
4545
when: .kubernetes_install_service.stdout | eq "active"
4646
assert:
4747
that: .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " | eq .kube_version
48-
fail_msg: |
49-
kubernetes has installed with version:{{ .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " }}. but not match kube_version: {{ .kube_version }}
48+
fail_msg: |
49+
kubernetes has installed with version:{{ .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " }}. but not match kube_version: {{ .kube_version }}

pkg/executor/task_executor.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ func (e *taskExecutor) execTaskHostLogs(ctx context.Context, h string, stdout, s
231231
}
232232
}
233233
// progress bar for task
234-
var bar = progressbar.NewOptions(-1,
235-
progressbar.OptionSetWriter(e.logOutput),
234+
options := []progressbar.Option{
235+
progressbar.OptionSetWriter(os.Stdout),
236236
// progressbar.OptionSpinnerCustom([]string{" "}),
237237
progressbar.OptionSpinnerType(14),
238238
progressbar.OptionEnableColorCodes(true),
@@ -242,7 +242,11 @@ func (e *taskExecutor) execTaskHostLogs(ctx context.Context, h string, stdout, s
242242
klog.ErrorS(err, "failed to write output", "host", h)
243243
}
244244
}),
245-
)
245+
}
246+
if e.logOutput != os.Stdout {
247+
options = append(options, progressbar.OptionSetVisibility(false))
248+
}
249+
bar := progressbar.NewOptions(-1, options...)
246250
// run progress
247251
go func() {
248252
err := wait.PollUntilContextCancel(ctx, 100*time.Millisecond, true, func(context.Context) (bool, error) {
@@ -265,13 +269,25 @@ func (e *taskExecutor) execTaskHostLogs(ctx context.Context, h string, stdout, s
265269
case *stderr != "":
266270
if e.task.Spec.IgnoreError != nil && *e.task.Spec.IgnoreError { // ignore
267271
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34mignore \033[0m", h, placeholder))
272+
if e.logOutput != os.Stdout {
273+
fmt.Fprintf(e.logOutput, "[%s]%s ignore \n", h, placeholder)
274+
}
268275
} else { // failed
269276
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[31mfailed \033[0m", h, placeholder))
277+
if e.logOutput != os.Stdout {
278+
fmt.Fprintf(e.logOutput, "[%s]%s failed \n", h, placeholder)
279+
}
270280
}
271281
case *stdout == modules.StdoutSkip: // skip
272282
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34mskip \033[0m", h, placeholder))
283+
if e.logOutput != os.Stdout {
284+
fmt.Fprintf(e.logOutput, "[%s]%s skip \n", h, placeholder)
285+
}
273286
default: //success
274287
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34msuccess\033[0m", h, placeholder))
288+
if e.logOutput != os.Stdout {
289+
fmt.Fprintf(e.logOutput, "[%s]%s success\n", h, placeholder)
290+
}
275291
}
276292
if err := bar.Finish(); err != nil {
277293
klog.ErrorS(err, "finish bar error")

pkg/web/handler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package web
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -303,14 +304,15 @@ func (h handler) createPlaybook(request *restful.Request, response *restful.Resp
303304
return
304305
}
305306
}
306-
file, err := os.Create(filename)
307+
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
307308
if err != nil {
308309
klog.ErrorS(err, "failed to open file", "file", filename)
309310
return
310311
}
311312
defer file.Close()
312313

313-
if err := executor.NewPlaybookExecutor(request.Request.Context(), h.client, playbook, file).Exec(request.Request.Context()); err != nil {
314+
ctx := context.TODO()
315+
if err := executor.NewPlaybookExecutor(ctx, h.client, playbook, file).Exec(ctx); err != nil {
314316
klog.ErrorS(err, "failed to exec playbook", "playbook", playbook.Name)
315317
}
316318
}()

0 commit comments

Comments
 (0)