Skip to content

Commit

Permalink
cluster: support using provided topo file besides editing it while ca…
Browse files Browse the repository at this point in the history
…lling edit-config, add show-config command (#1637)
  • Loading branch information
haiboumich authored Nov 25, 2021
1 parent 0ec99bb commit 9edf64e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 26 deletions.
6 changes: 5 additions & 1 deletion components/cluster/command/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package command

import (
"github.com/pingcap/tiup/pkg/cluster/manager"
"github.com/spf13/cobra"
)

func newEditConfigCmd() *cobra.Command {
opt := manager.EditConfigOptions{}
cmd := &cobra.Command{
Use: "edit-config <cluster-name>",
Short: "Edit TiDB cluster config.\nWill use editor from environment variable `EDITOR`, default use vi",
Expand All @@ -30,9 +32,11 @@ func newEditConfigCmd() *cobra.Command {
clusterReport.ID = scrubClusterName(clusterName)
teleCommand = append(teleCommand, scrubClusterName(clusterName))

return cm.EditConfig(clusterName, skipConfirm)
return cm.EditConfig(clusterName, opt, skipConfirm)
},
}

cmd.Flags().StringVarP(&opt.NewTopoFile, "topology-file", "", opt.NewTopoFile, "Use provided topology file to substitute the original one instead of editing it.")

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 @@ -195,6 +195,7 @@ func init() {
newAuditCmd(),
newImportCmd(),
newEditConfigCmd(),
newShowConfigCmd(),
newReloadCmd(),
newPatchCmd(),
newRenameCmd(),
Expand Down
38 changes: 38 additions & 0 deletions components/cluster/command/show_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 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 (
"github.com/spf13/cobra"
)

func newShowConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show-config <cluster-name>",
Short: "Show TiDB cluster config",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Help()
}

clusterName := args[0]
clusterReport.ID = scrubClusterName(clusterName)
teleCommand = append(teleCommand, scrubClusterName(clusterName))

return cm.ShowConfig(clusterName)
},
}

return cmd
}
6 changes: 5 additions & 1 deletion components/dm/command/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
package command

import (
"github.com/pingcap/tiup/pkg/cluster/manager"
"github.com/spf13/cobra"
)

func newEditConfigCmd() *cobra.Command {
opt := manager.EditConfigOptions{}
cmd := &cobra.Command{
Use: "edit-config <cluster-name>",
Short: "Edit DM cluster config",
Expand All @@ -28,9 +30,11 @@ func newEditConfigCmd() *cobra.Command {

clusterName := args[0]

return cm.EditConfig(clusterName, skipConfirm)
return cm.EditConfig(clusterName, opt, skipConfirm)
},
}

cmd.Flags().StringVarP(&opt.NewTopoFile, "topology-file", "", opt.NewTopoFile, "Use provided topology file to substitute the original one instead of editing it.")

return cmd
}
63 changes: 39 additions & 24 deletions pkg/cluster/manager/edit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ import (
"gopkg.in/yaml.v2"
)

// EditConfigOptions contains the options for config edition.
type EditConfigOptions struct {
NewTopoFile string // path to new topology file to substitute the original one
}

// EditConfig lets the user edit the cluster's config.
func (m *Manager) EditConfig(name string, skipConfirm bool) error {
func (m *Manager) EditConfig(name string, opt EditConfigOptions, skipConfirm bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}
Expand All @@ -49,7 +54,7 @@ func (m *Manager) EditConfig(name string, skipConfirm bool) error {
return perrs.AddStack(err)
}

newTopo, err := m.editTopo(topo, data, skipConfirm)
newTopo, err := m.editTopo(topo, data, opt, skipConfirm)
if err != nil {
return err
}
Expand All @@ -69,34 +74,40 @@ func (m *Manager) EditConfig(name string, skipConfirm bool) error {
return nil
}

// If the flag --topology-file is specified, the first 2 steps will be skipped.
// 1. Write Topology to a temporary file.
// 2. Open file in editor.
// 3. Check and update Topology.
// 4. Save meta file.
func (m *Manager) editTopo(origTopo spec.Topology, data []byte, skipConfirm bool) (spec.Topology, error) {
file, err := os.CreateTemp(os.TempDir(), "*")
if err != nil {
return nil, perrs.AddStack(err)
}
func (m *Manager) editTopo(origTopo spec.Topology, data []byte, opt EditConfigOptions, skipConfirm bool) (spec.Topology, error) {
var name string
if opt.NewTopoFile == "" {
file, err := os.CreateTemp(os.TempDir(), "*")
if err != nil {
return nil, perrs.AddStack(err)
}

name := file.Name()
name = file.Name()

_, err = io.Copy(file, bytes.NewReader(data))
if err != nil {
return nil, perrs.AddStack(err)
}
_, err = io.Copy(file, bytes.NewReader(data))
if err != nil {
return nil, perrs.AddStack(err)
}

err = file.Close()
if err != nil {
return nil, perrs.AddStack(err)
}
err = file.Close()
if err != nil {
return nil, perrs.AddStack(err)
}

err = utils.OpenFileInEditor(name)
if err != nil {
return nil, err
err = utils.OpenFileInEditor(name)
if err != nil {
return nil, err
}
} else {
name = opt.NewTopoFile
}

// Now user finish editing the file.
// Now user finish editing the file or user has provided the new topology file
newData, err := os.ReadFile(name)
if err != nil {
return nil, perrs.AddStack(err)
Expand All @@ -107,8 +118,10 @@ func (m *Manager) editTopo(origTopo spec.Topology, data []byte, skipConfirm bool
if err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Infof("Failed to parse topology file: %v", err)
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, skipConfirm)
if opt.NewTopoFile == "" {
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
}
log.Infof("Nothing changed.")
return nil, nil
Expand All @@ -118,8 +131,10 @@ func (m *Manager) editTopo(origTopo spec.Topology, data []byte, skipConfirm bool
if err := utils.ValidateSpecDiff(origTopo, newTopo); err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Errorf("%s", err)
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, skipConfirm)
if opt.NewTopoFile == "" {
if pass, _ := tui.PromptForConfirmNo("Do you want to continue editing? [Y/n]: "); !pass {
return m.editTopo(origTopo, newData, opt, skipConfirm)
}
}
log.Infof("Nothing changed.")
return nil, nil
Expand Down
46 changes: 46 additions & 0 deletions pkg/cluster/manager/show_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 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 manager

import (
"errors"
"fmt"

perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/meta"
"gopkg.in/yaml.v2"
)

// ShowConfig shows the cluster's config.
func (m *Manager) ShowConfig(name string) 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
}

topo := metadata.GetTopology()

data, err := yaml.Marshal(topo)
if err != nil {
return perrs.AddStack(err)
}

fmt.Print(string(data))
return nil
}

0 comments on commit 9edf64e

Please sign in to comment.