-
Notifications
You must be signed in to change notification settings - Fork 308
/
builder.go
537 lines (491 loc) · 15.2 KB
/
builder.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// 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 task
import (
"context"
"crypto/tls"
"fmt"
"path/filepath"
"github.com/pingcap/tiup/pkg/cluster/executor"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/crypto"
"github.com/pingcap/tiup/pkg/environment"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/proxy"
"github.com/pingcap/tiup/pkg/tui"
"github.com/pingcap/tiup/pkg/utils"
)
// Builder is used to build TiUP task
type Builder struct {
tasks []Task
Logger *logprinter.Logger
}
// NewBuilder returns a *Builder instance
func NewBuilder(logger *logprinter.Logger) *Builder {
return &Builder{Logger: logger}
}
// RootSSH appends a RootSSH task to the current task collection
func (b *Builder) RootSSH(
host string, port int, user, password, keyFile, passphrase string, sshTimeout, exeTimeout uint64,
proxyHost string, proxyPort int, proxyUser, proxyPassword, proxyKeyFile, proxyPassphrase string, proxySSHTimeout uint64,
sshType, defaultSSHType executor.SSHType,
) *Builder {
if sshType == "" {
sshType = defaultSSHType
}
b.tasks = append(b.tasks, &RootSSH{
host: host,
port: port,
user: user,
password: password,
keyFile: keyFile,
passphrase: passphrase,
timeout: sshTimeout,
exeTimeout: exeTimeout,
proxyHost: proxyHost,
proxyPort: proxyPort,
proxyUser: proxyUser,
proxyPassword: proxyPassword,
proxyKeyFile: proxyKeyFile,
proxyPassphrase: proxyPassphrase,
proxyTimeout: proxySSHTimeout,
sshType: sshType,
})
return b
}
// NewSimpleUerSSH append a UserSSH task to the current task collection with operator.Options and SSHConnectionProps
func NewSimpleUerSSH(logger *logprinter.Logger, host string, port int, user string, gOpt operator.Options, p *tui.SSHConnectionProps, sshType executor.SSHType) *Builder {
return NewBuilder(logger).
UserSSH(
host,
port,
user,
gOpt.SSHTimeout,
gOpt.OptTimeout,
gOpt.SSHProxyHost,
gOpt.SSHProxyPort,
gOpt.SSHProxyUser,
p.Password,
p.IdentityFile,
p.IdentityFilePassphrase,
gOpt.SSHProxyTimeout,
gOpt.SSHType,
sshType,
)
}
// UserSSH append a UserSSH task to the current task collection
func (b *Builder) UserSSH(
host string, port int, deployUser string, sshTimeout, exeTimeout uint64,
proxyHost string, proxyPort int, proxyUser, proxyPassword, proxyKeyFile, proxyPassphrase string, proxySSHTimeout uint64,
sshType, defaultSSHType executor.SSHType,
) *Builder {
if sshType == "" {
sshType = defaultSSHType
}
b.tasks = append(b.tasks, &UserSSH{
host: host,
port: port,
deployUser: deployUser,
timeout: sshTimeout,
exeTimeout: exeTimeout,
proxyHost: proxyHost,
proxyPort: proxyPort,
proxyUser: proxyUser,
proxyPassword: proxyPassword,
proxyKeyFile: proxyKeyFile,
proxyPassphrase: proxyPassphrase,
proxyTimeout: proxySSHTimeout,
sshType: sshType,
})
return b
}
// Func append a func task.
func (b *Builder) Func(name string, fn func(ctx context.Context) error) *Builder {
b.tasks = append(b.tasks, &Func{
name: name,
fn: fn,
})
return b
}
// ClusterSSH init all UserSSH need for the cluster.
func (b *Builder) ClusterSSH(
topo spec.Topology,
deployUser string, sshTimeout, exeTimeout uint64,
proxyHost string, proxyPort int, proxyUser, proxyPassword, proxyKeyFile, proxyPassphrase string, proxySSHTimeout uint64,
sshType, defaultSSHType executor.SSHType,
) *Builder {
if sshType == "" {
sshType = defaultSSHType
}
var tasks []Task
topo.IterInstance(func(inst spec.Instance) {
tasks = append(tasks, &UserSSH{
host: inst.GetManageHost(),
port: inst.GetSSHPort(),
deployUser: deployUser,
timeout: sshTimeout,
exeTimeout: exeTimeout,
proxyHost: proxyHost,
proxyPort: proxyPort,
proxyUser: proxyUser,
proxyPassword: proxyPassword,
proxyKeyFile: proxyKeyFile,
proxyPassphrase: proxyPassphrase,
proxyTimeout: proxySSHTimeout,
sshType: sshType,
})
})
b.tasks = append(b.tasks, &Parallel{inner: tasks})
return b
}
// UpdateMeta maintain the meta information
func (b *Builder) UpdateMeta(cluster string, metadata *spec.ClusterMeta, deletedNodeIds []string) *Builder {
b.tasks = append(b.tasks, &UpdateMeta{
cluster: cluster,
metadata: metadata,
deletedNodeIDs: deletedNodeIds,
})
return b
}
// UpdateTopology maintain the topology information
func (b *Builder) UpdateTopology(cluster, profile string, metadata *spec.ClusterMeta, deletedNodeIds []string) *Builder {
b.tasks = append(b.tasks, &UpdateTopology{
metadata: metadata,
cluster: cluster,
profileDir: profile,
deletedNodeIDs: deletedNodeIds,
tcpProxy: proxy.GetTCPProxy(),
})
return b
}
// CopyFile appends a CopyFile task to the current task collection
func (b *Builder) CopyFile(src, dst, server string, download bool, limit int, compress bool) *Builder {
b.tasks = append(b.tasks, &CopyFile{
src: src,
dst: dst,
remote: server,
download: download,
limit: limit,
compress: compress,
})
return b
}
// Download appends a Downloader task to the current task collection
func (b *Builder) Download(component, os, arch string, version string) *Builder {
b.tasks = append(b.tasks, NewDownloader(component, os, arch, version))
return b
}
// CopyComponent appends a CopyComponent task to the current task collection
func (b *Builder) CopyComponent(component, os, arch string,
version string,
srcPath, dstHost, dstDir string,
) *Builder {
b.tasks = append(b.tasks, &CopyComponent{
component: component,
os: os,
arch: arch,
version: version,
srcPath: srcPath,
host: dstHost,
dstDir: dstDir,
})
return b
}
// InstallPackage appends a InstallPackage task to the current task collection
func (b *Builder) InstallPackage(srcPath, dstHost, dstDir string) *Builder {
b.tasks = append(b.tasks, &InstallPackage{
srcPath: srcPath,
host: dstHost,
dstDir: dstDir,
})
return b
}
// BackupComponent appends a BackupComponent task to the current task collection
func (b *Builder) BackupComponent(component, fromVer string, host, deployDir string) *Builder {
b.tasks = append(b.tasks, &BackupComponent{
component: component,
fromVer: fromVer,
host: host,
deployDir: deployDir,
})
return b
}
// InitConfig appends a CopyComponent task to the current task collection
func (b *Builder) InitConfig(clusterName, clusterVersion string, specManager *spec.SpecManager, inst spec.Instance, deployUser string, ignoreCheck bool, paths meta.DirPaths) *Builder {
// get nightly version
var componentVersion utils.Version
meta := specManager.NewMetadata()
// full version
componentVersion = utils.Version(clusterVersion)
if err := specManager.Metadata(clusterName, meta); err == nil {
// get nightly version
if clusterVersion == utils.NightlyVersionAlias {
componentVersion, _, err = environment.GlobalEnv().V1Repository().LatestNightlyVersion(inst.ComponentSource())
if err != nil {
componentVersion = utils.Version(clusterVersion)
}
}
// dm cluster does not require a full nightly version
if meta.GetTopology().Type() == spec.TopoTypeDM {
componentVersion = utils.Version(clusterVersion)
}
}
b.tasks = append(b.tasks, &InitConfig{
specManager: specManager,
clusterName: clusterName,
clusterVersion: string(componentVersion),
instance: inst,
deployUser: deployUser,
ignoreCheck: ignoreCheck,
paths: paths,
})
return b
}
// ScaleConfig generate temporary config on scaling
func (b *Builder) ScaleConfig(clusterName, clusterVersion string, specManager *spec.SpecManager, topo spec.Topology, inst spec.Instance, deployUser string, paths meta.DirPaths) *Builder {
b.tasks = append(b.tasks, &ScaleConfig{
specManager: specManager,
clusterName: clusterName,
clusterVersion: clusterVersion,
base: topo,
instance: inst,
deployUser: deployUser,
paths: paths,
})
return b
}
// MonitoredConfig appends a CopyComponent task to the current task collection
func (b *Builder) MonitoredConfig(name, comp, host string, globResCtl meta.ResourceControl, options *spec.MonitoredOptions, deployUser string, tlsEnabled bool, paths meta.DirPaths) *Builder {
b.tasks = append(b.tasks, &MonitoredConfig{
name: name,
component: comp,
host: host,
globResCtl: globResCtl,
options: options,
deployUser: deployUser,
tlsEnabled: tlsEnabled,
paths: paths,
})
return b
}
// SSHKeyGen appends a SSHKeyGen task to the current task collection
func (b *Builder) SSHKeyGen(keypath string) *Builder {
b.tasks = append(b.tasks, &SSHKeyGen{
keypath: keypath,
})
return b
}
// SSHKeySet appends a SSHKeySet task to the current task collection
func (b *Builder) SSHKeySet(privKeyPath, pubKeyPath string) *Builder {
b.tasks = append(b.tasks, &SSHKeySet{
privateKeyPath: privKeyPath,
publicKeyPath: pubKeyPath,
})
return b
}
// EnvInit appends a EnvInit task to the current task collection
func (b *Builder) EnvInit(host, deployUser string, userGroup string, skipCreateUser bool) *Builder {
b.tasks = append(b.tasks, &EnvInit{
host: host,
deployUser: deployUser,
userGroup: userGroup,
skipCreateUser: skipCreateUser,
})
return b
}
// RotateSSH appends a RotateSSH task to the current task collection
func (b *Builder) RotateSSH(host, deployUser, newPublicKeyPath string) *Builder {
b.tasks = append(b.tasks, &RotateSSH{
host: host,
deployUser: deployUser,
newPublicKeyPath: newPublicKeyPath,
})
return b
}
// ClusterOperate appends a cluster operation task.
// All the UserSSH needed must be init first.
func (b *Builder) ClusterOperate(
spec *spec.Specification,
op operator.Operation,
options operator.Options,
tlsCfg *tls.Config,
) *Builder {
b.tasks = append(b.tasks, &ClusterOperate{
spec: spec,
op: op,
options: options,
tlsCfg: tlsCfg,
})
return b
}
// Mkdir appends a Mkdir task to the current task collection
func (b *Builder) Mkdir(user, host string, dirs ...string) *Builder {
b.tasks = append(b.tasks, &Mkdir{
user: user,
host: host,
dirs: dirs,
})
return b
}
// Rmdir appends a Rmdir task to the current task collection
func (b *Builder) Rmdir(host string, dirs ...string) *Builder {
b.tasks = append(b.tasks, &Rmdir{
host: host,
dirs: dirs,
})
return b
}
// Shell command on cluster host
func (b *Builder) Shell(host, command, cmdID string, sudo bool) *Builder {
b.tasks = append(b.tasks, &Shell{
host: host,
command: command,
sudo: sudo,
cmdID: cmdID,
})
return b
}
// SystemCtl run systemctl on host
func (b *Builder) SystemCtl(host, unit, action string, daemonReload, checkActive bool) *Builder {
b.tasks = append(b.tasks, &SystemCtl{
host: host,
unit: unit,
action: action,
daemonReload: daemonReload,
checkactive: checkActive,
})
return b
}
// Sysctl set a kernel parameter
func (b *Builder) Sysctl(host, key, val string) *Builder {
b.tasks = append(b.tasks, &Sysctl{
host: host,
key: key,
val: val,
})
return b
}
// Limit set a system limit
func (b *Builder) Limit(host, domain, limit, item, value string) *Builder {
b.tasks = append(b.tasks, &Limit{
host: host,
domain: domain,
limit: limit,
item: item,
value: value,
})
return b
}
// CheckSys checks system information of deploy server
func (b *Builder) CheckSys(host, dir, checkType string, topo *spec.Specification, opt *operator.CheckOptions) *Builder {
b.tasks = append(b.tasks, &CheckSys{
host: host,
topo: topo,
opt: opt,
checkDir: dir,
check: checkType,
})
return b
}
// DeploySpark deployes spark as dependency of TiSpark
func (b *Builder) DeploySpark(inst spec.Instance, sparkVersion, srcPath, deployDir string) *Builder {
sparkSubPath := spec.ComponentSubDir(spec.ComponentSpark, sparkVersion)
return b.CopyComponent(
spec.ComponentSpark,
inst.OS(),
inst.Arch(),
sparkVersion,
srcPath,
inst.GetManageHost(),
deployDir,
).Shell( // spark is under a subdir, move it to deploy dir
inst.GetManageHost(),
fmt.Sprintf(
"cp -rf %[1]s %[2]s/ && cp -rf %[3]s/* %[2]s/ && rm -rf %[1]s %[3]s",
filepath.Join(deployDir, "bin", sparkSubPath),
deployDir,
filepath.Join(deployDir, sparkSubPath),
),
"",
false, // (not) sudo
).CopyComponent(
inst.ComponentName(),
inst.OS(),
inst.Arch(),
"", // use the latest stable version
srcPath,
inst.GetManageHost(),
deployDir,
).Shell( // move tispark jar to correct path
inst.GetManageHost(),
fmt.Sprintf(
"cp -f %[1]s/*.jar %[2]s/jars/ && rm -f %[1]s/*.jar",
filepath.Join(deployDir, "bin"),
deployDir,
),
"",
false, // (not) sudo
)
}
// TLSCert generates certificate for instance and transfers it to the server
func (b *Builder) TLSCert(host, comp, role string, port int, ca *crypto.CertificateAuthority, paths meta.DirPaths) *Builder {
b.tasks = append(b.tasks, &TLSCert{
host: host,
comp: comp,
role: role,
port: port,
ca: ca,
paths: paths,
})
return b
}
// Parallel appends a parallel task to the current task collection
func (b *Builder) Parallel(ignoreError bool, tasks ...Task) *Builder {
if len(tasks) > 0 {
b.tasks = append(b.tasks, &Parallel{ignoreError: ignoreError, inner: tasks})
}
return b
}
// Serial appends the tasks to the tail of queue
func (b *Builder) Serial(tasks ...Task) *Builder {
if len(tasks) > 0 {
b.tasks = append(b.tasks, tasks...)
}
return b
}
// Build returns a task that contains all tasks appended by previous operation
func (b *Builder) Build() Task {
// Serial handles event internally. So the following 3 lines are commented out.
// if len(b.tasks) == 1 {
// return b.tasks[0]
// }
return &Serial{inner: b.tasks}
}
// Step appends a new StepDisplay task, which will print single line progress for inner tasks.
func (b *Builder) Step(prefix string, inner Task, logger *logprinter.Logger) *Builder {
b.Serial(newStepDisplay(prefix, inner, logger))
return b
}
// ParallelStep appends a new ParallelStepDisplay task, which will print multi line progress in parallel
// for inner tasks. Inner tasks must be a StepDisplay task.
func (b *Builder) ParallelStep(prefix string, ignoreError bool, tasks ...*StepDisplay) *Builder {
b.tasks = append(b.tasks, newParallelStepDisplay(prefix, ignoreError, tasks...).SetLogger(b.Logger))
return b
}
// BuildAsStep returns a task that is wrapped by a StepDisplay. The task will print single line progress.
func (b *Builder) BuildAsStep(prefix string) *StepDisplay {
inner := b.Build()
return newStepDisplay(prefix, inner, b.Logger)
}