Skip to content

Commit

Permalink
cluster: validate cluster name for all cluster ops
Browse files Browse the repository at this point in the history
  • Loading branch information
AstroProfundis committed Mar 4, 2021
1 parent dcb107f commit 365f343
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/cluster/manager/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"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"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -32,6 +33,10 @@ import (

// CleanCluster cleans the cluster without destroying it
func (m *Manager) CleanCluster(name string, gOpt operator.Options, cleanOpt operator.Options, skipConfirm bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"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"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -31,6 +32,10 @@ import (

// DestroyCluster destroy the cluster.
func (m *Manager) DestroyCluster(name string, gOpt operator.Options, destroyOpt operator.Options, skipConfirm bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) &&
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/executor"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
Expand Down Expand Up @@ -52,6 +53,10 @@ type InstInfo struct {

// Display cluster meta and topology.
func (m *Manager) Display(name string, opt operator.Options) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

clusterInstInfos, err := m.GetClusterTopology(name, opt)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/fatih/color"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
Expand All @@ -32,6 +33,10 @@ import (

// EditConfig lets the user edit the cluster's config.
func (m *Manager) EditConfig(name string, skipConfirm bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/fatih/color"
"github.com/joomcode/errorx"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -37,6 +38,10 @@ type ExecOptions struct {

// Exec shell command on host in the tidb cluster.
func (m *Manager) Exec(name string, opt ExecOptions, gOpt operator.Options) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions pkg/cluster/manager/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (

// Patch the cluster.
func (m *Manager) Patch(name string, packagePath string, opt operator.Options, overwrite, offline bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/joomcode/errorx"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -26,6 +27,10 @@ import (

// Reload the cluster.
func (m *Manager) Reload(name string, opt operator.Options, skipRestart bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

sshTimeout := opt.SSHTimeout

metadata, err := m.meta(name)
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/manager/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"os"

"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/logger/log"
Expand All @@ -25,11 +26,18 @@ import (

// Rename the cluster
func (m *Manager) Rename(name string, opt operator.Options, newName string) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}
if !utils.IsExist(m.specManager.Path(name)) {
return errorRenameNameNotExist.
New("Cluster name '%s' not exist", name).
WithProperty(cliutil.SuggestionFromFormat("Please double check your cluster name"))
}

if err := clusterutil.ValidateClusterNameOrError(newName); err != nil {
return err
}
if utils.IsExist(m.specManager.Path(newName)) {
return errorRenameNameDuplicate.
New("Cluster name '%s' is duplicated", newName).
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/scale_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"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"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -38,6 +39,10 @@ func (m *Manager) ScaleIn(
gOpt operator.Options,
scale func(builer *task.Builder, metadata spec.Metadata, tlsCfg *tls.Config),
) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

var (
force bool = gOpt.Force
nodes []string = gOpt.Nodes
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/scale_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/executor"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
Expand Down Expand Up @@ -51,6 +52,10 @@ func (m *Manager) ScaleOut(
skipConfirm bool,
gOpt operator.Options,
) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
// allow specific validation errors so that user can recover a broken
// cluster if it is somehow in a bad state.
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/google/uuid"
"github.com/joomcode/errorx"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -41,6 +42,10 @@ type TransferOptions struct {

// Transfer copies files from or to host in the tidb cluster.
func (m *Manager) Transfer(name string, opt TransferOptions, gOpt operator.Options) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/manager/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"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"
"github.com/pingcap/tiup/pkg/cluster/spec"
Expand All @@ -35,6 +36,10 @@ import (

// Upgrade the cluster.
func (m *Manager) Upgrade(name string, clusterVersion string, opt operator.Options, skipConfirm, offline bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}

metadata, err := m.meta(name)
if err != nil {
return err
Expand Down

0 comments on commit 365f343

Please sign in to comment.