-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathkeeper.go
503 lines (474 loc) · 13.4 KB
/
keeper.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
package deploy
import (
"encoding/gob"
"fmt"
"os"
"path"
"strings"
"sync"
"time"
"github.com/housepower/ckman/ckconfig"
"github.com/housepower/ckman/service/zookeeper"
"github.com/housepower/ckman/config"
"github.com/pkg/errors"
"github.com/housepower/ckman/common"
"github.com/housepower/ckman/log"
"github.com/housepower/ckman/model"
)
func init() {
gob.Register(KeeperDeploy{})
}
const (
KeeperSvrName string = "clickhouse-keeper"
)
type KeeperDeploy struct {
DeployBase
Conf *model.CKManClickHouseConfig
HostInfos []ckconfig.HostInfo
Ext model.CkDeployExt
}
func NewKeeperDeploy(conf model.CKManClickHouseConfig, packages Packages) *KeeperDeploy {
return &KeeperDeploy{
Conf: &conf,
DeployBase: DeployBase{
Packages: packages,
},
}
}
func (d *KeeperDeploy) Init() error {
d.Conf.Normalize()
d.HostInfos = make([]ckconfig.HostInfo, len(d.Conf.KeeperConf.KeeperNodes))
var lastError error
var wg sync.WaitGroup
d.Ext.Ipv6Enable = true
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
kpath := ""
if !d.Conf.NeedSudo {
kpath = path.Join(d.Conf.Cwd, d.Conf.Path, "clickhouse-keeper")
} else {
kpath = path.Join(d.Conf.Path, "clickhouse-keeper")
}
cmd1 := fmt.Sprintf("mkdir -p %s ; chown -R clickhouse:clickhouse %s", kpath, kpath)
_, err := common.RemoteExecute(sshOpts, cmd1)
if err != nil {
lastError = err
return
}
if d.Ext.Ipv6Enable {
cmd2 := "grep lo /proc/net/if_inet6 >/dev/null 2>&1; echo $?"
output, err := common.RemoteExecute(sshOpts, cmd2)
if err != nil {
lastError = err
return
}
ipv6Enable := strings.Trim(output, "\n")
if ipv6Enable != "0" {
//file not exists, return 2, file exists but empty, return 1
d.Ext.Ipv6Enable = false
}
}
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("init done")
return nil
}
func (d *KeeperDeploy) Prepare() error {
d.Conf.Normalize()
file := path.Join(config.GetWorkDirectory(), common.DefaultPackageDirectory, d.Packages.Keeper)
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
if err := common.ScpUploadFiles([]string{file}, common.TmpWorkDirectory, sshOpts); err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s prepare done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("prepare done")
return nil
}
func (d *KeeperDeploy) Install() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
cmds := make([]string, 0)
cmds = append(cmds, cmdIns.InstallCmd(KeeperSvrName, d.Packages))
cmds = append(cmds, fmt.Sprintf("rm -rf %s/* %s/*", d.Conf.KeeperConf.LogPath, d.Conf.KeeperConf.SnapshotPath))
if d.Conf.NeedSudo {
cmds = append(cmds, fmt.Sprintf("chown clickhouse.clickhouse %s %s -R", d.Conf.KeeperConf.LogPath, d.Conf.KeeperConf.SnapshotPath))
}
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
cmd1 := cmdIns.StopCmd(KeeperSvrName, d.Conf.Cwd)
_, _ = common.RemoteExecute(sshOpts, cmd1)
cmd2 := strings.Join(cmds, ";")
_, err := common.RemoteExecute(sshOpts, cmd2)
if err != nil {
lastError = err
return
}
if d.Conf.Cwd != "" {
//tgz deployment, try to add auto start
pkg := d.Packages.Keeper
lastIndex := strings.LastIndex(pkg, "-")
extractDir := pkg[:lastIndex]
cmd3 := fmt.Sprintf("cp /tmp/%s/lib/systemd/system/clickhouse-keeper.service /etc/systemd/system/", extractDir)
sshOpts.NeedSudo = true
_, err = common.RemoteExecute(sshOpts, cmd3)
if err != nil {
log.Logger.Warnf("try to config autorestart failed:%v", err)
}
}
log.Logger.Debugf("host %s install done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("install done")
return nil
}
func (d *KeeperDeploy) Uninstall() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
cmds := make([]string, 0)
cmds = append(cmds, cmdIns.Uninstall(KeeperSvrName, d.Packages, d.Conf.Version))
cmds = append(cmds, fmt.Sprintf("rm -rf %s/* %s/*", d.Conf.KeeperConf.LogPath, d.Conf.KeeperConf.SnapshotPath))
if d.Conf.NeedSudo {
cmds = append(cmds, "rm -rf /etc/clickhouse-keeper")
}
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
cmd := strings.Join(cmds, ";")
_, err := common.RemoteExecute(sshOpts, cmd)
if err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s uninstall done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("uninstall done")
return nil
}
func (d *KeeperDeploy) Upgrade() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
cmd := cmdIns.UpgradeCmd(KeeperSvrName, d.Packages)
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
_, err := common.RemoteExecute(sshOpts, cmd)
if err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s upgrade done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("upgrade done")
return nil
}
func (d *KeeperDeploy) Config() error {
d.Conf.Normalize()
confFiles := make([]string, 0)
var remotePath string
if d.Conf.NeedSudo {
remotePath = "/etc/clickhouse-keeper"
} else {
remotePath = path.Join(d.Conf.Cwd, "etc", "clickhouse-keeper")
}
var lastError error
var wg sync.WaitGroup
for index, host := range d.Conf.KeeperConf.KeeperNodes {
innerIndex := index
innerHost := host
confFiles := confFiles
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
if d.Conf.NeedSudo {
//clear config first
cmd := "cp /etc/clickhouse-keeper/keeper_config.xml /etc/clickhouse-keeper/keeper_config.xml.last"
if _, err := common.RemoteExecute(sshOpts, cmd); err != nil {
lastError = err
return
}
}
keeperFile, err := common.NewTempFile(path.Join(config.GetWorkDirectory(), "package"), "keeper_config")
if err != nil {
lastError = err
return
}
defer os.Remove(keeperFile.FullName)
keeperXml, err := ckconfig.GenerateKeeperXML(keeperFile.FullName, d.Conf, d.Ext.Ipv6Enable, innerIndex+1)
if err != nil {
lastError = err
return
}
confFiles = append(confFiles, keeperXml)
if err := common.ScpUploadFiles(confFiles, remotePath, sshOpts); err != nil {
lastError = err
return
}
cmds := make([]string, 0)
cmds = append(cmds, fmt.Sprintf("mv %s %s", path.Join(remotePath, keeperFile.BaseName), path.Join(remotePath, "keeper_config.xml")))
if d.Conf.NeedSudo {
cmds = append(cmds, "chown -R clickhouse:clickhouse /etc/clickhouse-keeper")
}
cmds = append(cmds, "rm -rf /tmp/keeper_config*")
cmd := strings.Join(cmds, ";")
if _, err = common.RemoteExecute(sshOpts, cmd); err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s config done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("config done")
return nil
}
func (d *KeeperDeploy) Start() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
// if strings.HasSuffix(d.Conf.PkgType, common.PkgSuffixTgz) {
// // try to modify ulimit nofiles
// sshOpts.NeedSudo = true
// cmds := []string{
// fmt.Sprintf("sed -i '/%s soft nofile/d' /etc/security/limits.conf", d.Conf.SshUser),
// fmt.Sprintf("sed -i '/%s hard nofile/d' /etc/security/limits.conf", d.Conf.SshUser),
// fmt.Sprintf("echo \"%s soft nofile 500000\" >> /etc/security/limits.conf", d.Conf.SshUser),
// fmt.Sprintf("echo \"%s hard nofile 500000\" >> /etc/security/limits.conf", d.Conf.SshUser),
// }
// _, err := common.RemoteExecute(sshOpts, strings.Join(cmds, ";"))
// if err != nil {
// log.Logger.Warnf("[%s] set ulimit -n failed: %v", host, err)
// }
// sshOpts.NeedSudo = d.Conf.NeedSudo
// }
cmd := cmdIns.StartCmd(KeeperSvrName, d.Conf.Cwd)
_, err := common.RemoteExecute(sshOpts, cmd)
if err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s start done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("start done")
return nil
}
func (d *KeeperDeploy) Stop() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
cmd := cmdIns.StopCmd(KeeperSvrName, d.Conf.Cwd)
_, err := common.RemoteExecute(sshOpts, cmd)
if err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s stop done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("stop done")
return nil
}
func (d *KeeperDeploy) Restart() error {
d.Conf.Normalize()
cmdIns := GetSuitableCmdAdpt(d.Conf.PkgType)
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
sshOpts := common.SshOptions{
User: d.Conf.SshUser,
Password: d.Conf.SshPassword,
Port: d.Conf.SshPort,
Host: innerHost,
NeedSudo: d.Conf.NeedSudo,
AuthenticateType: d.Conf.AuthenticateType,
}
cmd := cmdIns.RestartCmd(KeeperSvrName, d.Conf.Cwd)
_, err := common.RemoteExecute(sshOpts, cmd)
if err != nil {
lastError = err
return
}
log.Logger.Debugf("host %s restart done", innerHost)
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("restart done")
return nil
}
func (d *KeeperDeploy) Check(timeout int) error {
d.Conf.Normalize()
var lastError error
var wg sync.WaitGroup
for _, host := range d.Conf.KeeperConf.KeeperNodes {
innerHost := host
wg.Add(1)
_ = common.Pool.Submit(func() {
defer wg.Done()
// Golang <-time.After() is not garbage collected before expiry.
ticker := time.NewTicker(5 * time.Second)
ticker2 := time.NewTicker(time.Duration(timeout) * time.Second)
defer ticker.Stop()
defer ticker2.Stop()
for {
select {
case <-ticker.C:
res, err := zookeeper.ZkMetric(innerHost, d.Conf.KeeperConf.TcpPort, "ruok")
if err == nil && string(res) == "imok" {
log.Logger.Debugf("host %s check done", innerHost)
return
}
case <-ticker2.C:
lastError = errors.Wrapf(model.CheckTimeOutErr, "clickhouse-keeper may start failed, please check the clickhouse-keeper log")
return
}
}
})
}
wg.Wait()
if lastError != nil {
return lastError
}
log.Logger.Infof("check done")
return nil
}