From c2dd9996edfe9a61e2be4959619d8b72bb083fb4 Mon Sep 17 00:00:00 2001 From: lucklove Date: Tue, 11 Aug 2020 19:42:21 +0800 Subject: [PATCH 1/3] Support `tiup cluster reload` command This command will refresh config and restart grafana and prometheus Signed-off-by: lucklove --- components/cluster/command/rename.go | 29 ++++++++++++++++++++++++++++ components/cluster/command/root.go | 1 + pkg/cluster/manager.go | 12 ++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 components/cluster/command/rename.go diff --git a/components/cluster/command/rename.go b/components/cluster/command/rename.go new file mode 100644 index 0000000000..08adbe384f --- /dev/null +++ b/components/cluster/command/rename.go @@ -0,0 +1,29 @@ +package command + +import ( + "github.com/spf13/cobra" +) + +func newRenameCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "rename ", + 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 +} diff --git a/components/cluster/command/root.go b/components/cluster/command/root.go index 9647370d91..57b1859ef2 100644 --- a/components/cluster/command/root.go +++ b/components/cluster/command/root.go @@ -157,6 +157,7 @@ func init() { newEditConfigCmd(), newReloadCmd(), newPatchCmd(), + newRenameCmd(), newTestCmd(), // hidden command for test internally newTelemetryCmd(), ) diff --git a/pkg/cluster/manager.go b/pkg/cluster/manager.go index 6d536f257a..531a17f456 100644 --- a/pkg/cluster/manager.go +++ b/pkg/cluster/manager.go @@ -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 { + 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 From fa1315adb57cae184a4dbb28a56890df74b665ef Mon Sep 17 00:00:00 2001 From: lucklove Date: Tue, 11 Aug 2020 19:47:05 +0800 Subject: [PATCH 2/3] Add test Signed-off-by: lucklove --- tests/tiup-cluster/script/cmd_subtest.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/tiup-cluster/script/cmd_subtest.sh b/tests/tiup-cluster/script/cmd_subtest.sh index e3c2dca8a7..79ab05224b 100755 --- a/tests/tiup-cluster/script/cmd_subtest.sh +++ b/tests/tiup-cluster/script/cmd_subtest.sh @@ -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" From ec02b01e135b03808ca7bab1139ae667767d610d Mon Sep 17 00:00:00 2001 From: lucklove Date: Fri, 14 Aug 2020 10:59:35 +0800 Subject: [PATCH 3/3] Address comment Signed-off-by: lucklove --- components/cluster/command/rename.go | 13 +++++++++++++ pkg/cluster/manager.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/components/cluster/command/rename.go b/components/cluster/command/rename.go index 08adbe384f..e16838a9a3 100644 --- a/components/cluster/command/rename.go +++ b/components/cluster/command/rename.go @@ -1,3 +1,16 @@ +// 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 import ( diff --git a/pkg/cluster/manager.go b/pkg/cluster/manager.go index 531a17f456..5b9b8a198f 100644 --- a/pkg/cluster/manager.go +++ b/pkg/cluster/manager.go @@ -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. @@ -524,6 +528,17 @@ func (m *Manager) EditConfig(clusterName string, skipConfirm bool) error { // 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 { return perrs.AddStack(err) }