-
Notifications
You must be signed in to change notification settings - Fork 2
/
upgrade.go
248 lines (217 loc) · 6.71 KB
/
upgrade.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// 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 (
"context"
"fmt"
"os"
"github.com/fatih/color"
"github.com/joomcode/errorx"
"github.com/luyomo/OhMyTiUP/pkg/aws/clusterutil"
operator "github.com/luyomo/OhMyTiUP/pkg/aws/operation"
"github.com/luyomo/OhMyTiUP/pkg/aws/spec"
"github.com/luyomo/OhMyTiUP/pkg/aws/task"
"github.com/luyomo/OhMyTiUP/pkg/ctxt"
"github.com/luyomo/OhMyTiUP/pkg/environment"
"github.com/luyomo/OhMyTiUP/pkg/logger/log"
"github.com/luyomo/OhMyTiUP/pkg/meta"
"github.com/luyomo/OhMyTiUP/pkg/repository"
"github.com/luyomo/OhMyTiUP/pkg/tui"
"github.com/luyomo/OhMyTiUP/pkg/utils"
perrs "github.com/pingcap/errors"
"golang.org/x/mod/semver"
)
// 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
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
var (
downloadCompTasks []task.Task // tasks which are used to download components
copyCompTasks []task.Task // tasks which are used to copy components to remote host
uniqueComps = map[string]struct{}{}
)
if err := versionCompare(base.Version, clusterVersion); err != nil {
return err
}
if !skipConfirm {
if err := tui.PromptForConfirmOrAbortError(
"This operation will upgrade %s %s cluster %s to %s.\nDo you want to continue? [y/N]:",
m.sysName,
color.HiYellowString(base.Version),
color.HiYellowString(name),
color.HiYellowString(clusterVersion)); err != nil {
return err
}
log.Infof("Upgrading cluster...")
}
hasImported := false
for _, comp := range topo.ComponentsByUpdateOrder() {
for _, inst := range comp.Instances() {
compName := inst.ComponentName()
// ignore monitor agents for instances marked as ignore_exporter
switch compName {
case spec.ComponentNodeExporter,
spec.ComponentBlackboxExporter:
if inst.IgnoreMonitorAgent() {
continue
}
}
version := m.bindVersion(inst.ComponentName(), clusterVersion)
// Download component from repository
key := fmt.Sprintf("%s-%s-%s-%s", compName, version, inst.OS(), inst.Arch())
if _, found := uniqueComps[key]; !found {
uniqueComps[key] = struct{}{}
t := task.NewBuilder().
Download(inst.ComponentName(), inst.OS(), inst.Arch(), version).
Build()
downloadCompTasks = append(downloadCompTasks, t)
}
deployDir := spec.Abs(base.User, inst.DeployDir())
// data dir would be empty for components which don't need it
dataDirs := spec.MultiDirAbs(base.User, inst.DataDir())
// log dir will always be with values, but might not used by the component
logDir := spec.Abs(base.User, inst.LogDir())
// Deploy component
tb := task.NewBuilder()
// for some component, dataDirs might need to be created due to upgrade
// eg: TiCDC support DataDir since v4.0.13
tb = tb.Mkdir(topo.BaseTopo().GlobalOptions.User, inst.GetHost(), dataDirs...)
if inst.IsImported() {
switch inst.ComponentName() {
case spec.ComponentPrometheus, spec.ComponentGrafana, spec.ComponentAlertmanager:
tb.CopyComponent(
inst.ComponentName(),
inst.OS(),
inst.Arch(),
version,
"", // use default srcPath
inst.GetHost(),
deployDir,
)
}
hasImported = true
}
// backup files of the old version
tb = tb.BackupComponent(inst.ComponentName(), base.Version, inst.GetHost(), deployDir)
if deployerInstance, ok := inst.(DeployerInstance); ok {
deployerInstance.Deploy(tb, "", deployDir, version, name, clusterVersion)
} else {
// copy dependency component if needed
switch inst.ComponentName() {
case spec.ComponentTiSpark:
env := environment.GlobalEnv()
sparkVer, _, err := env.V1Repository().WithOptions(repository.Options{
GOOS: inst.OS(),
GOARCH: inst.Arch(),
}).LatestStableVersion(spec.ComponentSpark, false)
if err != nil {
return err
}
tb = tb.DeploySpark(inst, sparkVer.String(), "" /* default srcPath */, deployDir)
default:
tb = tb.CopyComponent(
inst.ComponentName(),
inst.OS(),
inst.Arch(),
version,
"", // use default srcPath
inst.GetHost(),
deployDir,
)
}
}
tb.InitConfig(
name,
clusterVersion,
m.specManager,
inst,
base.User,
opt.IgnoreConfigCheck,
meta.DirPaths{
Deploy: deployDir,
Data: dataDirs,
Log: logDir,
Cache: m.specManager.Path(name, spec.TempConfigPath),
},
)
copyCompTasks = append(copyCompTasks, tb.Build())
}
}
// handle dir scheme changes
if hasImported {
if err := spec.HandleImportPathMigration(name); err != nil {
return err
}
}
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
b, err := m.sshTaskBuilder(name, topo, base.User, opt)
if err != nil {
return err
}
t := b.
Parallel(false, downloadCompTasks...).
Parallel(opt.Force, copyCompTasks...).
Func("UpgradeCluster", func(ctx context.Context) error {
if offline {
return nil
}
return operator.Upgrade(ctx, topo, opt, tlsCfg)
}).
Build()
if err := t.Execute(ctxt.New(context.Background(), opt.Concurrency)); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
// clear patched packages and tags
if err := os.RemoveAll(m.specManager.Path(name, "patch")); err != nil {
return perrs.Trace(err)
}
topo.IterInstance(func(ins spec.Instance) {
if ins.IsPatched() {
ins.SetPatched(false)
}
})
metadata.SetVersion(clusterVersion)
if err := m.specManager.SaveMeta(name, metadata); err != nil {
return err
}
log.Infof("Upgraded cluster `%s` successfully", name)
return nil
}
func versionCompare(curVersion, newVersion string) error {
// Can always upgrade to 'nightly' event the current version is 'nightly'
if newVersion == utils.NightlyVersionAlias {
return nil
}
switch semver.Compare(curVersion, newVersion) {
case -1:
return nil
case 0, 1:
return perrs.Errorf("please specify a higher version than %s", curVersion)
default:
return perrs.Errorf("unreachable")
}
}