-
Notifications
You must be signed in to change notification settings - Fork 312
/
basic.go
179 lines (146 loc) · 4.59 KB
/
basic.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
// 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 (
"errors"
"github.com/joomcode/errorx"
perrs "github.com/pingcap/errors"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/cluster/task"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
)
// EnableCluster enable/disable the service in a cluster
func (m *Manager) EnableCluster(name string, options operator.Options, isEnable bool) error {
if isEnable {
log.Infof("Enabling cluster %s...", name)
} else {
log.Infof("Disabling cluster %s...", name)
}
metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) {
return err
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
b := m.sshTaskBuilder(name, topo, base.User, options)
if isEnable {
b = b.Func("EnableCluster", func(ctx *task.Context) error {
return operator.Enable(ctx, topo, options, isEnable)
})
} else {
b = b.Func("DisableCluster", func(ctx *task.Context) error {
return operator.Enable(ctx, topo, options, isEnable)
})
}
t := b.Build()
if err := t.Execute(task.NewContext()); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
if isEnable {
log.Infof("Enabled cluster `%s` successfully", name)
} else {
log.Infof("Disabled cluster `%s` successfully", name)
}
return nil
}
// StartCluster start the cluster with specified name.
func (m *Manager) StartCluster(name string, options operator.Options, fn ...func(b *task.Builder, metadata spec.Metadata)) error {
log.Infof("Starting cluster %s...", name)
metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) {
return err
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
b := m.sshTaskBuilder(name, topo, base.User, options).
Func("StartCluster", func(ctx *task.Context) error {
return operator.Start(ctx, topo, options, tlsCfg)
})
for _, f := range fn {
f(b, metadata)
}
t := b.Build()
if err := t.Execute(task.NewContext()); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
log.Infof("Started cluster `%s` successfully", name)
return nil
}
// StopCluster stop the cluster.
func (m *Manager) StopCluster(name string, options operator.Options) error {
metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) {
return err
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
t := m.sshTaskBuilder(name, topo, base.User, options).
Func("StopCluster", func(ctx *task.Context) error {
return operator.Stop(ctx, topo, options, tlsCfg)
}).
Build()
if err := t.Execute(task.NewContext()); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
log.Infof("Stopped cluster `%s` successfully", name)
return nil
}
// RestartCluster restart the cluster.
func (m *Manager) RestartCluster(name string, options operator.Options) error {
metadata, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) {
return err
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
t := m.sshTaskBuilder(name, topo, base.User, options).
Func("RestartCluster", func(ctx *task.Context) error {
return operator.Restart(ctx, topo, options, tlsCfg)
}).
Build()
if err := t.Execute(task.NewContext()); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
log.Infof("Restarted cluster `%s` successfully", name)
return nil
}