-
Notifications
You must be signed in to change notification settings - Fork 312
/
edit_config.go
145 lines (122 loc) · 3.67 KB
/
edit_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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 manager
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/fatih/color"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
"gopkg.in/yaml.v2"
)
// EditConfig lets the user edit the cluster's config.
func (m *Manager) EditConfig(name string, skipConfirm bool) error {
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)
}
newTopo, err := m.editTopo(topo, data, skipConfirm)
if err != nil {
return err
}
if newTopo == nil {
return nil
}
log.Infof("Applying changes...")
metadata.SetTopology(newTopo)
err = m.specManager.SaveMeta(name, metadata)
if err != nil {
return perrs.Annotate(err, "failed to save meta")
}
log.Infof("Applied successfully, please use `%s reload %s [-N <nodes>] [-R <roles>]` to reload config.", cliutil.OsArgs0(), name)
return nil
}
// 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 := ioutil.TempFile(os.TempDir(), "*")
if err != nil {
return nil, perrs.AddStack(err)
}
name := file.Name()
_, 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 = utils.OpenFileInEditor(name)
if err != nil {
return nil, err
}
// Now user finish editing the file.
newData, err := ioutil.ReadFile(name)
if err != nil {
return nil, perrs.AddStack(err)
}
newTopo := m.specManager.NewMetadata().GetTopology()
err = yaml.UnmarshalStrict(newData, newTopo)
if err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Infof("Failed to parse topology file: %v", err)
if !cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: ") {
return m.editTopo(origTopo, newData, skipConfirm)
}
log.Infof("Nothing changed.")
return nil, nil
}
// report error if immutable field has been changed
if err := utils.ValidateSpecDiff(origTopo, newTopo); err != nil {
fmt.Print(color.RedString("New topology could not be saved: "))
log.Errorf("%s", err)
if !cliutil.PromptForConfirmNo("Do you want to continue editing? [Y/n]: ") {
return m.editTopo(origTopo, newData, skipConfirm)
}
log.Infof("Nothing changed.")
return nil, nil
}
origData, err := yaml.Marshal(origTopo)
if err != nil {
return nil, perrs.AddStack(err)
}
if bytes.Equal(origData, newData) {
log.Infof("The file has nothing changed")
return nil, nil
}
utils.ShowDiff(string(origData), string(newData), os.Stdout)
if !skipConfirm {
if err := cliutil.PromptForConfirmOrAbortError(
color.HiYellowString("Please check change highlight above, do you want to apply the change? [y/N]:"),
); err != nil {
return nil, err
}
}
return newTopo, nil
}