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

Support rename cluster #671

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions components/cluster/command/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package command
lucklove marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/spf13/cobra"
)

func newRenameCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "rename <old-cluster-name> <new-cluster-name>",
Short: "Rename the cluster",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return cmd.Help()
}

if err := validRoles(gOpt.Roles); err != nil {
return err
}

oldClusterName := args[0]
newClusterName := args[1]
teleCommand = append(teleCommand, scrubClusterName(oldClusterName))

return manager.Rename(oldClusterName, gOpt, newClusterName)
},
}

return cmd
}
1 change: 1 addition & 0 deletions components/cluster/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func init() {
newEditConfigCmd(),
newReloadCmd(),
newPatchCmd(),
newRenameCmd(),
newTestCmd(), // hidden command for test internally
newTelemetryCmd(),
)
Expand Down
12 changes: 12 additions & 0 deletions pkg/cluster/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,18 @@ func (m *Manager) EditConfig(clusterName string, skipConfirm bool) error {
return nil
}

// Rename the cluster
func (m *Manager) Rename(clusterName string, opt operator.Options, newName string) error {
if err := os.Rename(m.specManager.Path(clusterName), m.specManager.Path(newName)); err != nil {
lucklove marked this conversation as resolved.
Show resolved Hide resolved
return perrs.AddStack(err)
}

log.Infof("Rename cluster `%s` -> `%s` successfully", clusterName, newName)

opt.Roles = []string{spec.ComponentGrafana, spec.ComponentPrometheus}
return m.Reload(newName, opt, false)
}

// Reload the cluster.
func (m *Manager) Reload(clusterName string, opt operator.Options, skipRestart bool) error {
sshTimeout := opt.SSHTimeout
Expand Down
5 changes: 5 additions & 0 deletions tests/tiup-cluster/script/cmd_subtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function cmd_subtest() {

tiup-cluster $client display $name

# Test rename
tiup-cluster $client rename $name "tmp-cluster-name"
tiup-cluster $client display "tmp-cluster-name"
tiup-cluster $client rename "tmp-cluster-name" $name

tiup-cluster $client --yes clean $name --data --all --ignore-node 172.19.0.101:9090

echo "checking cleanup data and log"
Expand Down