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

Feature/confirm reload patch rename #1263

Merged
merged 10 commits into from
Apr 1, 2021
2 changes: 1 addition & 1 deletion components/cluster/command/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/cluster/command/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/cluster/command/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/cluster/command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/cluster/command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/dm/command/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/dm/command/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func newReloadCmd() *cobra.Command {

clusterName := args[0]

return cm.Reload(clusterName, gOpt, skipRestart)
return cm.Reload(clusterName, gOpt, skipRestart, skipConfirm)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/dm/command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newRestartCmd() *cobra.Command {

clusterName := args[0]

return cm.RestartCluster(clusterName, gOpt)
return cm.RestartCluster(clusterName, gOpt, skipConfirm)
},
}

Expand Down
2 changes: 1 addition & 1 deletion components/dm/command/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newStopCmd() *cobra.Command {

clusterName := args[0]

return cm.StopCluster(clusterName, gOpt)
return cm.StopCluster(clusterName, gOpt, skipConfirm)
},
}

Expand Down
32 changes: 30 additions & 2 deletions pkg/cluster/manager/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion pkg/cluster/manager/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion pkg/cluster/manager/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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()

Expand Down
14 changes: 12 additions & 2 deletions pkg/cluster/manager/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions tests/tiup-cluster/script/cmd_subtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;'"
Expand Down
2 changes: 1 addition & 1 deletion tests/tiup-cluster/script/scale_core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down