/
pump.go
227 lines (203 loc) · 6.05 KB
/
pump.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
// 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 spec
import (
"context"
"crypto/tls"
"fmt"
"os"
"path/filepath"
"time"
"github.com/luyomo/OhMyTiUP/pkg/aws/template/scripts"
"github.com/luyomo/OhMyTiUP/pkg/ctxt"
"github.com/luyomo/OhMyTiUP/pkg/meta"
)
// PumpSpec represents the Pump topology specification in topology.yaml
type PumpSpec struct {
Host string `yaml:"host"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
Port int `yaml:"port" default:"8250"`
DeployDir string `yaml:"deploy_dir,omitempty"`
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
Offline bool `yaml:"offline,omitempty"`
NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"`
Config map[string]interface{} `yaml:"config,omitempty" validate:"config:ignore"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
}
// Role returns the component role of the instance
func (s *PumpSpec) Role() string {
return ComponentPump
}
// SSH returns the host and SSH port of the instance
func (s *PumpSpec) SSH() (string, int) {
return s.Host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *PumpSpec) GetMainPort() int {
return s.Port
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *PumpSpec) IsImported() bool {
return s.Imported
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *PumpSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// PumpComponent represents Pump component.
type PumpComponent struct{ Topology *Specification }
// Name implements Component interface.
func (c *PumpComponent) Name() string {
return ComponentPump
}
// Role implements Component interface.
func (c *PumpComponent) Role() string {
return ComponentPump
}
// Instances implements Component interface.
func (c *PumpComponent) Instances() []Instance {
ins := make([]Instance, 0, len(c.Topology.PumpServers))
for _, s := range c.Topology.PumpServers {
s := s
ins = append(ins, &PumpInstance{BaseInstance{
InstanceSpec: s,
Name: c.Name(),
Host: s.Host,
Port: s.Port,
SSHP: s.SSHPort,
Ports: []int{
s.Port,
},
Dirs: []string{
s.DeployDir,
s.DataDir,
},
StatusFn: func(tlsCfg *tls.Config, _ ...string) string {
return statusByHost(s.Host, s.Port, "/status", tlsCfg)
},
UptimeFn: func(tlsCfg *tls.Config) time.Duration {
return UptimeByHost(s.Host, s.Port, tlsCfg)
},
}, c.Topology})
}
return ins
}
// PumpInstance represent the Pump instance.
type PumpInstance struct {
BaseInstance
topo Topology
}
// ScaleConfig deploy temporary config on scaling
func (i *PumpInstance) ScaleConfig(
ctx context.Context,
e ctxt.Executor,
topo Topology,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) error {
s := i.topo
defer func() {
i.topo = s
}()
i.topo = mustBeClusterTopo(topo)
return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths)
}
// InitConfig implements Instance interface.
func (i *PumpInstance) InitConfig(
ctx context.Context,
e ctxt.Executor,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) error {
topo := i.topo.(*Specification)
if err := i.BaseInstance.InitConfig(ctx, e, topo.GlobalOptions, deployUser, paths); err != nil {
return err
}
enableTLS := topo.GlobalOptions.TLSEnabled
spec := i.InstanceSpec.(*PumpSpec)
nodeID := i.ID()
// keep origin node id if is imported
if i.IsImported() {
nodeID = ""
}
cfg := scripts.NewPumpScript(
nodeID,
i.GetHost(),
paths.Deploy,
paths.Data[0],
paths.Log,
).WithPort(spec.Port).WithNumaNode(spec.NumaNode).AppendEndpoints(topo.Endpoints(deployUser)...)
fp := filepath.Join(paths.Cache, fmt.Sprintf("run_pump_%s_%d.sh", i.GetHost(), i.GetPort()))
if err := cfg.ConfigToFile(fp); err != nil {
return err
}
dst := filepath.Join(paths.Deploy, "scripts", "run_pump.sh")
if err := e.Transfer(ctx, fp, dst, false, 0); err != nil {
return err
}
if _, _, err := e.Execute(ctx, "chmod +x "+dst, false); err != nil {
return err
}
globalConfig := topo.ServerConfigs.Pump
// merge config files for imported instance
if i.IsImported() {
configPath := ClusterPath(
clusterName,
AnsibleImportedConfigPath,
fmt.Sprintf(
"%s-%s-%d.toml",
i.ComponentName(),
i.GetHost(),
i.GetPort(),
),
)
importConfig, err := os.ReadFile(configPath)
if err != nil {
return err
}
globalConfig, err = mergeImported(importConfig, globalConfig)
if err != nil {
return err
}
}
// set TLS configs
if enableTLS {
if spec.Config == nil {
spec.Config = make(map[string]interface{})
}
spec.Config["security.ssl-ca"] = fmt.Sprintf(
"%s/tls/%s",
paths.Deploy,
TLSCACert,
)
spec.Config["security.ssl-cert"] = fmt.Sprintf(
"%s/tls/%s.crt",
paths.Deploy,
i.Role())
spec.Config["security.ssl-key"] = fmt.Sprintf(
"%s/tls/%s.pem",
paths.Deploy,
i.Role())
}
return i.MergeServerConfig(ctx, e, globalConfig, spec.Config, paths)
}