fix: display task progressbar in file. (#2599)

Signed-off-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
liujian 2025-05-29 15:21:30 +08:00 committed by GitHub
parent cec2e1a200
commit 6e0cdbec1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 7 deletions

View File

@ -231,6 +231,7 @@ linters-settings:
excludes:
- G106 # Deferring unsafe method "InsecureIgnoreHostKey" on type "\*ssh"
- G301 # Deferring unsafe method "MkdirAll" on type "\*os.File"
- G302 # Deferring unsafe method "Create" or "Open" on type "\*os.File"
- G304 # Deferring unsafe method "Create" or "Open" on type "\*os.File"
- G306 # Deferring unsafe method "WriteFile" on type "\*os.File"
- G307 # Deferring unsafe method "Close" on type "\*os.File"

View File

@ -45,5 +45,5 @@
when: .kubernetes_install_service.stdout | eq "active"
assert:
that: .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " | eq .kube_version
fail_msg: |
kubernetes has installed with version:{{ .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " }}. but not match kube_version: {{ .kube_version }}
fail_msg: |
kubernetes has installed with version:{{ .kubernetes_install_version.stdout | default "" | trimPrefix "Kubernetes " }}. but not match kube_version: {{ .kube_version }}

View File

@ -231,8 +231,8 @@ func (e *taskExecutor) execTaskHostLogs(ctx context.Context, h string, stdout, s
}
}
// progress bar for task
var bar = progressbar.NewOptions(-1,
progressbar.OptionSetWriter(e.logOutput),
options := []progressbar.Option{
progressbar.OptionSetWriter(os.Stdout),
// progressbar.OptionSpinnerCustom([]string{" "}),
progressbar.OptionSpinnerType(14),
progressbar.OptionEnableColorCodes(true),
@ -242,7 +242,11 @@ func (e *taskExecutor) execTaskHostLogs(ctx context.Context, h string, stdout, s
klog.ErrorS(err, "failed to write output", "host", h)
}
}),
)
}
if e.logOutput != os.Stdout {
options = append(options, progressbar.OptionSetVisibility(false))
}
bar := progressbar.NewOptions(-1, options...)
// run progress
go func() {
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
case *stderr != "":
if e.task.Spec.IgnoreError != nil && *e.task.Spec.IgnoreError { // ignore
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34mignore \033[0m", h, placeholder))
if e.logOutput != os.Stdout {
fmt.Fprintf(e.logOutput, "[%s]%s ignore \n", h, placeholder)
}
} else { // failed
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[31mfailed \033[0m", h, placeholder))
if e.logOutput != os.Stdout {
fmt.Fprintf(e.logOutput, "[%s]%s failed \n", h, placeholder)
}
}
case *stdout == modules.StdoutSkip: // skip
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34mskip \033[0m", h, placeholder))
if e.logOutput != os.Stdout {
fmt.Fprintf(e.logOutput, "[%s]%s skip \n", h, placeholder)
}
default: //success
bar.Describe(fmt.Sprintf("[\033[36m%s\033[0m]%s \033[34msuccess\033[0m", h, placeholder))
if e.logOutput != os.Stdout {
fmt.Fprintf(e.logOutput, "[%s]%s success\n", h, placeholder)
}
}
if err := bar.Finish(); err != nil {
klog.ErrorS(err, "finish bar error")

View File

@ -2,6 +2,7 @@ package web
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
@ -303,14 +304,15 @@ func (h handler) createPlaybook(request *restful.Request, response *restful.Resp
return
}
}
file, err := os.Create(filename)
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
klog.ErrorS(err, "failed to open file", "file", filename)
return
}
defer file.Close()
if err := executor.NewPlaybookExecutor(request.Request.Context(), h.client, playbook, file).Exec(request.Request.Context()); err != nil {
ctx := context.TODO()
if err := executor.NewPlaybookExecutor(ctx, h.client, playbook, file).Exec(ctx); err != nil {
klog.ErrorS(err, "failed to exec playbook", "playbook", playbook.Name)
}
}()