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

Comments all lines from files in ansible-imported-configs after import from ansible #1350

Merged
merged 4 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions components/cluster/command/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func newImportCmd() *cobra.Command {
return err
}

// comment config to avoid duplicated copy
if err = ansible.CommentConfig(clsName); err != nil {
return err
}

// backup ansible files
if noBackup {
// rename original TiDB-Ansible inventory file
Expand Down
27 changes: 27 additions & 0 deletions pkg/cluster/ansible/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ package ansible
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"

"github.com/BurntSushi/toml"
"github.com/pingcap/errors"
Expand Down Expand Up @@ -179,6 +181,31 @@ func diffConfigs(configs []map[string]interface{}) (global map[string]interface{
return
}

// CommentConfig add `#` to the head of each lines for imported configs
func CommentConfig(clsName string) error {
dir := spec.ClusterPath(clsName, spec.AnsibleImportedConfigPath)
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.HasSuffix(info.Name(), ".toml") {
return nil
}

content, err := os.ReadFile(path)
if err != nil {
return errors.Annotatef(err, "read config file %s", path)
}
lines := strings.Split(string(content), "\n")
for idx := range lines {
lines[idx] = "# " + lines[idx]
}
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0644); err != nil {
return errors.Annotatef(err, "write config file %s", path)
}

return nil
})
return errors.Annotate(err, "comment imported config")
}

// LoadConfig files to clusterMeta, include tidbservers, tikvservers, pdservers pumpservers and drainerservers
func LoadConfig(clsName string, cls *spec.ClusterMeta) error {
// deal with tidb config
Expand Down