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 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
42 changes: 42 additions & 0 deletions components/cluster/command/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

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
27 changes: 27 additions & 0 deletions pkg/cluster/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ import (
var (
errNSDeploy = errorx.NewNamespace("deploy")
errDeployNameDuplicate = errNSDeploy.NewType("name_dup", errutil.ErrTraitPreCheck)

errNSRename = errorx.NewNamespace("rename")
errorRenameNameNotExist = errNSRename.NewType("name_not_exist", errutil.ErrTraitPreCheck)
errorRenameNameDuplicate = errNSRename.NewType("name_dup", errutil.ErrTraitPreCheck)
)

// Manager to deploy a cluster.
Expand Down Expand Up @@ -524,6 +528,29 @@ 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 !utils.IsExist(m.specManager.Path(clusterName)) {
return errorRenameNameNotExist.
New("Cluster name '%s' not exist", clusterName).
WithProperty(cliutil.SuggestionFromFormat("Please double check your cluster name"))
}
if utils.IsExist(m.specManager.Path(newName)) {
return errorRenameNameDuplicate.
New("Cluster name '%s' is duplicated", newName).
WithProperty(cliutil.SuggestionFromFormat("Please specify another cluster name"))
}

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