From 7e1f73b2183f80ff8129aaef70041ab656547604 Mon Sep 17 00:00:00 2001 From: 9547 Date: Thu, 1 Apr 2021 20:11:24 +0800 Subject: [PATCH] Feature/confirm reload patch rename (#1263) --- components/cluster/command/patch.go | 2 +- components/cluster/command/reload.go | 2 +- components/cluster/command/rename.go | 2 +- components/cluster/command/restart.go | 2 +- components/cluster/command/stop.go | 2 +- components/dm/command/patch.go | 2 +- components/dm/command/reload.go | 2 +- components/dm/command/restart.go | 2 +- components/dm/command/stop.go | 2 +- pkg/cluster/manager/basic.go | 32 ++++++++++++++++++++++-- pkg/cluster/manager/patch.go | 18 ++++++++++++- pkg/cluster/manager/reload.go | 19 +++++++++++++- pkg/cluster/manager/rename.go | 14 +++++++++-- tests/tiup-cluster/script/cmd_subtest.sh | 4 +-- tests/tiup-cluster/script/scale_core.sh | 2 +- 15 files changed, 89 insertions(+), 18 deletions(-) diff --git a/components/cluster/command/patch.go b/components/cluster/command/patch.go index 0fda5b7bfe..0be9096cdc 100644 --- a/components/cluster/command/patch.go +++ b/components/cluster/command/patch.go @@ -41,7 +41,7 @@ func newPatchCmd() *cobra.Command { clusterName := args[0] teleCommand = append(teleCommand, scrubClusterName(clusterName)) - return cm.Patch(clusterName, args[1], gOpt, overwrite, offlineMode) + return cm.Patch(clusterName, args[1], gOpt, overwrite, offlineMode, skipConfirm) }, } diff --git a/components/cluster/command/reload.go b/components/cluster/command/reload.go index abfd92ade2..711faaf87e 100644 --- a/components/cluster/command/reload.go +++ b/components/cluster/command/reload.go @@ -36,7 +36,7 @@ func newReloadCmd() *cobra.Command { clusterName := args[0] teleCommand = append(teleCommand, scrubClusterName(clusterName)) - return cm.Reload(clusterName, gOpt, skipRestart) + return cm.Reload(clusterName, gOpt, skipRestart, skipConfirm) }, } diff --git a/components/cluster/command/rename.go b/components/cluster/command/rename.go index 68ece2590b..0bae70e4a6 100644 --- a/components/cluster/command/rename.go +++ b/components/cluster/command/rename.go @@ -34,7 +34,7 @@ func newRenameCmd() *cobra.Command { newClusterName := args[1] teleCommand = append(teleCommand, scrubClusterName(oldClusterName)) - return cm.Rename(oldClusterName, gOpt, newClusterName) + return cm.Rename(oldClusterName, gOpt, newClusterName, skipConfirm) }, } diff --git a/components/cluster/command/restart.go b/components/cluster/command/restart.go index ce5ea90be6..404da952ad 100644 --- a/components/cluster/command/restart.go +++ b/components/cluster/command/restart.go @@ -33,7 +33,7 @@ func newRestartCmd() *cobra.Command { clusterName := args[0] teleCommand = append(teleCommand, scrubClusterName(clusterName)) - return cm.RestartCluster(clusterName, gOpt) + return cm.RestartCluster(clusterName, gOpt, skipConfirm) }, } diff --git a/components/cluster/command/stop.go b/components/cluster/command/stop.go index b38d81282b..400dd39b88 100644 --- a/components/cluster/command/stop.go +++ b/components/cluster/command/stop.go @@ -33,7 +33,7 @@ func newStopCmd() *cobra.Command { clusterName := args[0] teleCommand = append(teleCommand, scrubClusterName(clusterName)) - return cm.StopCluster(clusterName, gOpt) + return cm.StopCluster(clusterName, gOpt, skipConfirm) }, } diff --git a/components/dm/command/patch.go b/components/dm/command/patch.go index e45ef2bf21..11c662b26b 100644 --- a/components/dm/command/patch.go +++ b/components/dm/command/patch.go @@ -41,7 +41,7 @@ func newPatchCmd() *cobra.Command { clusterName := args[0] - return cm.Patch(clusterName, args[1], gOpt, overwrite, offlineMode) + return cm.Patch(clusterName, args[1], gOpt, overwrite, offlineMode, skipConfirm) }, } diff --git a/components/dm/command/reload.go b/components/dm/command/reload.go index 0d7e526e51..ee77af7ee5 100644 --- a/components/dm/command/reload.go +++ b/components/dm/command/reload.go @@ -35,7 +35,7 @@ func newReloadCmd() *cobra.Command { clusterName := args[0] - return cm.Reload(clusterName, gOpt, skipRestart) + return cm.Reload(clusterName, gOpt, skipRestart, skipConfirm) }, } diff --git a/components/dm/command/restart.go b/components/dm/command/restart.go index ef19a3d774..fc9e08e2d6 100644 --- a/components/dm/command/restart.go +++ b/components/dm/command/restart.go @@ -28,7 +28,7 @@ func newRestartCmd() *cobra.Command { clusterName := args[0] - return cm.RestartCluster(clusterName, gOpt) + return cm.RestartCluster(clusterName, gOpt, skipConfirm) }, } diff --git a/components/dm/command/stop.go b/components/dm/command/stop.go index 2eeb12389f..6c4b7204a7 100644 --- a/components/dm/command/stop.go +++ b/components/dm/command/stop.go @@ -28,7 +28,7 @@ func newStopCmd() *cobra.Command { clusterName := args[0] - return cm.StopCluster(clusterName, gOpt) + return cm.StopCluster(clusterName, gOpt, skipConfirm) }, } diff --git a/pkg/cluster/manager/basic.go b/pkg/cluster/manager/basic.go index 1e597c6b8a..b47574f3e6 100644 --- a/pkg/cluster/manager/basic.go +++ b/pkg/cluster/manager/basic.go @@ -16,9 +16,13 @@ package manager import ( "context" "errors" + "fmt" + "strings" + "github.com/fatih/color" "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" "github.com/pingcap/tiup/pkg/cluster/spec" @@ -115,7 +119,7 @@ func (m *Manager) StartCluster(name string, options operator.Options, fn ...func } // StopCluster stop the cluster. -func (m *Manager) StopCluster(name string, options operator.Options) error { +func (m *Manager) StopCluster(name string, options operator.Options, skipConfirm bool) error { metadata, err := m.meta(name) if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) { return err @@ -129,6 +133,18 @@ func (m *Manager) StopCluster(name string, options operator.Options) error { return err } + if !skipConfirm { + if err := cliutil.PromptForConfirmOrAbortError( + fmt.Sprintf("Will stop the cluster %s with nodes: %s, roles: %s.\nDo you want to continue? [y/N]:", + color.HiYellowString(name), + color.HiRedString(strings.Join(options.Nodes, ",")), + color.HiRedString(strings.Join(options.Roles, ",")), + ), + ); err != nil { + return err + } + } + t := m.sshTaskBuilder(name, topo, base.User, options). Func("StopCluster", func(ctx context.Context) error { return operator.Stop(ctx, topo, options, tlsCfg) @@ -148,7 +164,7 @@ func (m *Manager) StopCluster(name string, options operator.Options) error { } // RestartCluster restart the cluster. -func (m *Manager) RestartCluster(name string, options operator.Options) error { +func (m *Manager) RestartCluster(name string, options operator.Options, skipConfirm bool) error { metadata, err := m.meta(name) if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) { return err @@ -162,6 +178,18 @@ func (m *Manager) RestartCluster(name string, options operator.Options) error { return err } + if !skipConfirm { + if err := cliutil.PromptForConfirmOrAbortError( + fmt.Sprintf("Will restart the cluster %s with nodes: %s roles: %s.\nDo you want to continue? [y/N]:", + color.HiYellowString(name), + color.HiYellowString(strings.Join(options.Nodes, ",")), + color.HiYellowString(strings.Join(options.Roles, ",")), + ), + ); err != nil { + return err + } + } + t := m.sshTaskBuilder(name, topo, base.User, options). Func("RestartCluster", func(ctx context.Context) error { return operator.Restart(ctx, topo, options, tlsCfg) diff --git a/pkg/cluster/manager/patch.go b/pkg/cluster/manager/patch.go index 78feabe43f..fae761e250 100644 --- a/pkg/cluster/manager/patch.go +++ b/pkg/cluster/manager/patch.go @@ -19,10 +19,13 @@ import ( "os" "os/exec" "path" + "strings" + "github.com/fatih/color" "github.com/joomcode/errorx" "github.com/pingcap/errors" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" @@ -33,7 +36,7 @@ import ( ) // Patch the cluster. -func (m *Manager) Patch(name string, packagePath string, opt operator.Options, overwrite, offline bool) error { +func (m *Manager) Patch(name string, packagePath string, opt operator.Options, overwrite, offline, skipConfirm bool) error { if err := clusterutil.ValidateClusterNameOrError(name); err != nil { return err } @@ -50,6 +53,19 @@ func (m *Manager) Patch(name string, packagePath string, opt operator.Options, o return perrs.New("specified package not exists") } + if !skipConfirm { + if err := cliutil.PromptForConfirmOrAbortError( + fmt.Sprintf("Will patch the cluster %s with package path is %s, nodes: %s, roles: %s.\nDo you want to continue? [y/N]:", + color.HiYellowString(name), + color.HiYellowString(packagePath), + color.HiRedString(strings.Join(opt.Nodes, ",")), + color.HiRedString(strings.Join(opt.Roles, ",")), + ), + ); err != nil { + return err + } + } + insts, err := instancesToPatch(topo, opt) if err != nil { return err diff --git a/pkg/cluster/manager/reload.go b/pkg/cluster/manager/reload.go index ab3291b327..757601214c 100644 --- a/pkg/cluster/manager/reload.go +++ b/pkg/cluster/manager/reload.go @@ -15,9 +15,13 @@ package manager import ( "context" + "fmt" + "strings" + "github.com/fatih/color" "github.com/joomcode/errorx" perrs "github.com/pingcap/errors" + "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/clusterutil" "github.com/pingcap/tiup/pkg/cluster/ctxt" operator "github.com/pingcap/tiup/pkg/cluster/operation" @@ -26,7 +30,7 @@ import ( ) // Reload the cluster. -func (m *Manager) Reload(name string, opt operator.Options, skipRestart bool) error { +func (m *Manager) Reload(name string, opt operator.Options, skipRestart, skipConfirm bool) error { if err := clusterutil.ValidateClusterNameOrError(name); err != nil { return err } @@ -38,6 +42,19 @@ func (m *Manager) Reload(name string, opt operator.Options, skipRestart bool) er return err } + if !skipConfirm { + if err := cliutil.PromptForConfirmOrAbortError( + fmt.Sprintf("Will reload the cluster %s with restart policy is %s, nodes: %s, roles: %s.\nDo you want to continue? [y/N]:", + color.HiYellowString(name), + color.HiRedString(fmt.Sprintf("%v", !skipRestart)), + color.HiRedString(strings.Join(opt.Nodes, ",")), + color.HiRedString(strings.Join(opt.Roles, ",")), + ), + ); err != nil { + return err + } + } + topo := metadata.GetTopology() base := metadata.GetBaseMeta() diff --git a/pkg/cluster/manager/rename.go b/pkg/cluster/manager/rename.go index 0be9f465ed..3a82048aae 100644 --- a/pkg/cluster/manager/rename.go +++ b/pkg/cluster/manager/rename.go @@ -14,8 +14,10 @@ package manager import ( + "fmt" "os" + "github.com/fatih/color" "github.com/pingcap/tiup/pkg/cliutil" "github.com/pingcap/tiup/pkg/cluster/clusterutil" operator "github.com/pingcap/tiup/pkg/cluster/operation" @@ -25,7 +27,7 @@ import ( ) // Rename the cluster -func (m *Manager) Rename(name string, opt operator.Options, newName string) error { +func (m *Manager) Rename(name string, opt operator.Options, newName string, skipConfirm bool) error { if err := clusterutil.ValidateClusterNameOrError(name); err != nil { return err } @@ -44,6 +46,14 @@ func (m *Manager) Rename(name string, opt operator.Options, newName string) erro WithProperty(cliutil.SuggestionFromFormat("Please specify another cluster name")) } + if !skipConfirm { + if err := cliutil.PromptForConfirmOrAbortError( + fmt.Sprintf("Will rename the cluster name from %s to %s.\nDo you confirm this action? [y/N]:", color.HiYellowString(name), color.HiYellowString(newName)), + ); err != nil { + return err + } + } + _, err := m.meta(name) if err != nil { // refuse renaming if current cluster topology is not valid return err @@ -56,5 +66,5 @@ func (m *Manager) Rename(name string, opt operator.Options, newName string) erro log.Infof("Rename cluster `%s` -> `%s` successfully", name, newName) opt.Roles = []string{spec.ComponentGrafana, spec.ComponentPrometheus} - return m.Reload(newName, opt, false) + return m.Reload(newName, opt, false, skipConfirm) } diff --git a/tests/tiup-cluster/script/cmd_subtest.sh b/tests/tiup-cluster/script/cmd_subtest.sh index 3e54c34d98..e34c92e57f 100755 --- a/tests/tiup-cluster/script/cmd_subtest.sh +++ b/tests/tiup-cluster/script/cmd_subtest.sh @@ -116,9 +116,9 @@ function cmd_subtest() { echo "$display_result" | grep "Since" # Test rename - tiup-cluster $client rename $name "tmp-cluster-name" + tiup-cluster $client --yes rename $name "tmp-cluster-name" tiup-cluster $client display "tmp-cluster-name" - tiup-cluster $client rename "tmp-cluster-name" $name + tiup-cluster $client --yes rename "tmp-cluster-name" $name # Test enable & disable tiup-cluster $client exec $name -R tidb --command="systemctl status tidb-4000|grep 'enabled;'" diff --git a/tests/tiup-cluster/script/scale_core.sh b/tests/tiup-cluster/script/scale_core.sh index 508b58a635..05961f8615 100755 --- a/tests/tiup-cluster/script/scale_core.sh +++ b/tests/tiup-cluster/script/scale_core.sh @@ -29,7 +29,7 @@ function scale_core() { tiup-cluster $client display $name - tiup-cluster $client reload $name --skip-restart + tiup-cluster $client --yes reload $name --skip-restart if [ $test_tls = true ]; then total_sub_one=18