Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: add significant warning when --force is set for scale-in #1629

Merged
merged 2 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/cluster/manager/scale_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ func (m *Manager) ScaleIn(
)
if !skipConfirm {
if force {
if err := tui.PromptForConfirmOrAbortError(
log.Warnf(color.HiRedString(tui.ASCIIArtWarning))
if err := tui.PromptForAnswerOrAbortError(
"Yes, I know my data might be lost.",
color.HiRedString("Forcing scale in is unsafe and may result in data loss for stateful components.\n"+
"The process is irreversible and could NOT be cancelled.\n") +
"Only use `--force` when some of the servers are already permanently offline.\n" +
"Are you sure to continue? [y/N]:",
"DO NOT use `--force` if you have any component in ")+
color.YellowString("Pending Offline")+color.HiRedString(" status.\n")+
color.HiRedString("The process is irreversible and could NOT be cancelled.\n")+
"Only use `--force` when some of the servers are already permanently offline.\n"+
"Are you sure to continue?",
); err != nil {
return err
}
Expand Down
31 changes: 31 additions & 0 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"syscall"

"github.com/AstroProfundis/tabby"
"github.com/fatih/color"
"github.com/pingcap/tiup/pkg/utils/mock"
"golang.org/x/term"
)
Expand Down Expand Up @@ -59,6 +60,17 @@ func addRow(t *tabby.Tabby, rawLine []string, header bool) {
}
}

// pre-defined ascii art strings
const (
ASCIIArtWarning = `
██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████
██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██
██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███
██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████
`
)

// Prompt accepts input from console by user
func Prompt(prompt string) string {
if prompt != "" {
Expand Down Expand Up @@ -106,6 +118,25 @@ func PromptForConfirmOrAbortError(format string, a ...interface{}) error {
return nil
}

// PromptForConfirmAnswer accepts string from console by user, default to empty and only return
// true if the user input is exactly the same as pre-defined answer.
func PromptForConfirmAnswer(answer string, format string, a ...interface{}) (bool, string) {
ans := Prompt(fmt.Sprintf(format, a...) + fmt.Sprintf("\n(Type \"%s\" to continue)\n:", color.CyanString(answer)))
if ans == answer {
return true, ans
}
return false, ans
}

// PromptForAnswerOrAbortError accepts string from console by user, generates AbortError if user does
// not input the pre-defined answer.
func PromptForAnswerOrAbortError(answer string, format string, a ...interface{}) error {
if pass, ans := PromptForConfirmAnswer(answer, format, a...); !pass {
return errOperationAbort.New("Operation aborted by user (with incorrect answer '%s')", ans)
}
return nil
}

// PromptForPassword reads a password input from console
func PromptForPassword(format string, a ...interface{}) string {
defer fmt.Println("")
Expand Down