-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgres.go
353 lines (290 loc) · 12.4 KB
/
postgres.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
// 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"
"errors"
"fmt"
"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"
awsutils "github.com/luyomo/OhMyTiUP/pkg/aws/utils"
"github.com/luyomo/OhMyTiUP/pkg/crypto"
"github.com/luyomo/OhMyTiUP/pkg/ctxt"
"github.com/luyomo/OhMyTiUP/pkg/executor"
"github.com/luyomo/OhMyTiUP/pkg/logger"
"github.com/luyomo/OhMyTiUP/pkg/meta"
"github.com/luyomo/OhMyTiUP/pkg/tui"
"github.com/luyomo/OhMyTiUP/pkg/utils"
perrs "github.com/pingcap/errors"
elbtypes "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
ws "github.com/luyomo/OhMyTiUP/pkg/workstation"
)
// DeployOptions contains the options for scale out.
type PostgresDeployOptions struct {
User string // username to login to the SSH server
IdentityFile string // path to the private key file
UsePassword bool // use password instead of identity file for ssh connection
IgnoreConfigCheck bool // ignore config check result
}
// Deploy a cluster.
func (m *Manager) PostgresDeploy(
name string,
topoFile string,
opt PostgresDeployOptions,
afterDeploy func(b *task.Builder, newPart spec.Topology),
skipConfirm bool,
gOpt operator.Options,
) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}
var timer awsutils.ExecutionTimer
timer.Initialize([]string{"Step", "Duration(s)"})
exist, err := m.specManager.Exist(name)
if err != nil {
return err
}
if exist {
// FIXME: When change to use args, the suggestion text need to be updatem.
return errDeployNameDuplicate.
New("Cluster name '%s' is duplicated", name).
WithProperty(tui.SuggestionFromFormat("Please specify another cluster name"))
}
metadata := m.specManager.NewMetadata()
topo := metadata.GetTopology()
if err := spec.ParseTopologyYaml(topoFile, topo); err != nil {
return err
}
spec.ExpandRelativeDir(topo)
base := topo.BaseTopo()
if sshType := gOpt.SSHType; sshType != "" {
base.GlobalOptions.SSHType = sshType
}
var (
sshConnProps *tui.SSHConnectionProps = &tui.SSHConnectionProps{}
sshProxyProps *tui.SSHConnectionProps = &tui.SSHConnectionProps{}
)
if gOpt.SSHType != executor.SSHTypeNone {
var err error
if sshConnProps, err = tui.ReadIdentityFileOrPassword(opt.IdentityFile, opt.UsePassword); err != nil {
return err
}
if len(gOpt.SSHProxyHost) != 0 {
if sshProxyProps, err = tui.ReadIdentityFileOrPassword(gOpt.SSHProxyIdentity, gOpt.SSHProxyUsePassword); err != nil {
return err
}
}
}
if err := m.fillHostArch(sshConnProps, sshProxyProps, topo, &gOpt, opt.User); err != nil {
return err
}
var (
envInitTasks []*task.StepDisplay // tasks which are used to initialize environment
//downloadCompTasks []*task.StepDisplay // tasks which are used to download components
//deployCompTasks []*task.StepDisplay // tasks which are used to copy components to remote host
)
// Initialize environment
// uniqueHosts := make(map[string]hostInfo) // host -> ssh-port, os, arch
// noAgentHosts := set.NewStringSet()
globalOptions := base.GlobalOptions
// generate CA and client cert for TLS enabled cluster
var ca *crypto.CertificateAuthority
if globalOptions.TLSEnabled {
// generate CA
tlsPath := m.specManager.Path(name, spec.TLSCertKeyDir)
if err := utils.CreateDir(tlsPath); err != nil {
return err
}
ca, err = genAndSaveClusterCA(name, tlsPath)
if err != nil {
return err
}
// generate client cert
if err = genAndSaveClientCert(ca, name, tlsPath); err != nil {
return err
}
}
var clusterInfo task.ClusterInfo
sexecutor, err := executor.New(executor.SSHTypeNone, false, executor.SSHConfig{Host: "127.0.0.1", User: utils.CurrentUser()}, []string{})
if err != nil {
return err
}
ctx := context.WithValue(context.Background(), "clusterName", name)
ctx = context.WithValue(ctx, "clusterType", "ohmytiup-postgres")
fpMakeWSContext := func() error {
if err := m.makeExeContext(ctx, nil, &gOpt, INC_WS, ws.EXC_AWS_ENV); err != nil {
return err
}
return nil
}
timer.Take("Resource preparation")
if base.AwsPostgresConfigs.DBParameterFamilyGroup != "" {
var workstationInfo task.ClusterInfo
t5 := task.NewBuilder().
CreateTransitGateway(&sexecutor).
CreateWorkstationCluster(&sexecutor, "workstation", base.AwsWSConfigs, &workstationInfo, &m.wsExe, &gOpt, fpMakeWSContext).
CreateTransitGatewayVpcAttachment(&sexecutor, "workstation", task.NetworkTypePublic).
CreateTransitGatewayVpcAttachment(&sexecutor, "postgres", task.NetworkTypePrivate).
CreatePostgres(&sexecutor, base.AwsWSConfigs, base.AwsPostgresConfigs, &clusterInfo).
CreateRouteTgw(&sexecutor, "workstation", []string{"postgres"}).
CreateRouteTgw(&sexecutor, "postgres", []string{"workstation"}).
BuildAsStep(fmt.Sprintf(" - Preparing postgres ... ..."))
envInitTasks = append(envInitTasks, t5)
}
builder := task.NewBuilder().
ParallelStep("+ Initialize target host environments", false, envInitTasks...)
if afterDeploy != nil {
afterDeploy(builder, topo)
}
t := builder.Build()
if err := t.Execute(ctxt.New(ctx, gOpt.Concurrency)); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return err
}
logger.OutputDebugLog("aws-nodes")
timer.Print()
return nil
}
// Cluster represents a clsuter
// ListCluster list the clusters.
func (m *Manager) ListPostgresCluster(clusterName string, opt DeployOptions) error {
var listTasks []*task.StepDisplay // tasks which are used to initialize environment
ctx := context.WithValue(context.Background(), "clusterName", clusterName)
ctx = context.WithValue(ctx, "clusterType", "ohmytiup-postgres")
sexecutor, err := executor.New(executor.SSHTypeNone, false, executor.SSHConfig{Host: "127.0.0.1", User: utils.CurrentUser()}, []string{})
if err != nil {
return err
}
// 001. VPC listing
tableVPC := [][]string{{"Component Name", "VPC ID", "CIDR", "Status"}}
t1 := task.NewBuilder().ListVPC(&sexecutor, &tableVPC).BuildAsStep(fmt.Sprintf(" - Listing VPC"))
listTasks = append(listTasks, t1)
// 002. subnets
tableSubnets := [][]string{{"Component Name", "Zone", "Subnet ID", "CIDR", "State", "VPC ID"}}
t2 := task.NewBuilder().ListNetwork(&sexecutor, &tableSubnets).BuildAsStep(fmt.Sprintf(" - Listing Subnets"))
listTasks = append(listTasks, t2)
// 003. subnets
tableRouteTables := [][]string{{"Component Name", "Route Table ID", "DestinationCidrBlock", "TransitGatewayId", "GatewayId", "State", "Origin"}}
t3 := task.NewBuilder().ListRouteTable(&sexecutor, &tableRouteTables).BuildAsStep(fmt.Sprintf(" - Listing Route Tables"))
listTasks = append(listTasks, t3)
// 004. Security Groups
tableSecurityGroups := [][]string{{"Component Name", "Ip Protocol", "Source Ip Range", "From Port", "To Port"}}
t4 := task.NewBuilder().ListSecurityGroup(&sexecutor, &tableSecurityGroups).BuildAsStep(fmt.Sprintf(" - Listing Security Groups"))
listTasks = append(listTasks, t4)
// 005. Transit gateway
var transitGateway task.TransitGateway
t5 := task.NewBuilder().ListTransitGateway(&sexecutor, &transitGateway).BuildAsStep(fmt.Sprintf(" - Listing Transit gateway "))
listTasks = append(listTasks, t5)
// 006. Transit gateway vpc attachment
tableTransitGatewayVpcAttachments := [][]string{{"Component Name", "VPC ID", "State"}}
t6 := task.NewBuilder().ListTransitGatewayVpcAttachment(&sexecutor, &tableTransitGatewayVpcAttachments).BuildAsStep(fmt.Sprintf(" - Listing Transit gateway vpc attachment"))
listTasks = append(listTasks, t6)
// 007. EC2
tableECs := [][]string{{"Component Name", "Component Cluster", "State", "Instance ID", "Instance Type", "Private IP", "Public IP", "Image ID"}}
t7 := task.NewBuilder().ListEC(&sexecutor, &tableECs).BuildAsStep(fmt.Sprintf(" - Listing EC2"))
listTasks = append(listTasks, t7)
// 008. NLB
var nlb elbtypes.LoadBalancer
t8 := task.NewBuilder().ListNLB(&sexecutor, "tidb", &nlb).BuildAsStep(fmt.Sprintf(" - Listing Load Balancer "))
listTasks = append(listTasks, t8)
// 009. Postgres
tablePostgres := [][]string{{"Physical Name", "Host Name", "Port", "DB User", "Engine", "Engine Version", "Instance Type", "Security Group"}}
t9 := task.NewBuilder().ListPostgres(&sexecutor, &tablePostgres).BuildAsStep(fmt.Sprintf(" - Listing Postgres"))
listTasks = append(listTasks, t9)
// *********************************************************************
builder := task.NewBuilder().ParallelStep("+ Listing aws resources", false, listTasks...)
t := builder.Build()
if err := t.Execute(ctxt.New(ctx, 10)); err != nil {
return err
}
titleFont := color.New(color.FgRed, color.Bold)
fmt.Printf("Cluster Type: %s\n", titleFont.Sprint("ohmytiup-postgres"))
fmt.Printf("Cluster Name : %s\n\n", titleFont.Sprint(clusterName))
cyan := color.New(color.FgCyan, color.Bold)
fmt.Printf("Resource Type: %s\n", cyan.Sprint("VPC"))
tui.PrintTable(tableVPC, true)
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("Subnet"))
tui.PrintTable(tableSubnets, true)
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("Route Table"))
tui.PrintTable(tableRouteTables, true)
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("Security Group"))
tui.PrintTable(tableSecurityGroups, true)
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("Transit Gateway"))
fmt.Printf("Resource ID : %s State: %s \n", cyan.Sprint(transitGateway.TransitGatewayId), cyan.Sprint(transitGateway.State))
tui.PrintTable(tableTransitGatewayVpcAttachments, true)
fmt.Printf("\nLoad Balancer: %s", cyan.Sprint(nlb.DNSName))
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("EC2"))
tui.PrintTable(tableECs, true)
fmt.Printf("\nResource Type: %s\n", cyan.Sprint("Postgres"))
tui.PrintTable(tablePostgres, true)
return nil
}
func (m *Manager) DestroyPostgresCluster(name string, gOpt operator.Options, destroyOpt operator.Options, skipConfirm bool) error {
_, err := m.meta(name)
if err != nil && !errors.Is(perrs.Cause(err), meta.ErrValidate) &&
!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) &&
!errors.Is(perrs.Cause(err), spec.ErrMultipleTiSparkMaster) &&
!errors.Is(perrs.Cause(err), spec.ErrMultipleTisparkWorker) {
return err
}
clusterType := "ohmytiup-postgres"
sexecutor, err := executor.New(executor.SSHTypeNone, false, executor.SSHConfig{Host: "127.0.0.1", User: utils.CurrentUser()}, []string{})
if err != nil {
return err
}
t0 := task.NewBuilder().
DestroyTransitGateways(&sexecutor).
// DestroyVpcPeering(&sexecutor).
BuildAsStep(fmt.Sprintf(" - Prepare %s:%d", "127.0.0.1", 22))
builder := task.NewBuilder().
ParallelStep("+ Destroying postgres solution service ... ...", false, t0)
t := builder.Build()
ctx := context.WithValue(context.Background(), "clusterName", name)
ctx = context.WithValue(ctx, "clusterType", clusterType)
if err := t.Execute(ctxt.New(ctx, 1)); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return err
}
var destroyTasks []*task.StepDisplay
t1 := task.NewBuilder().
DestroyPostgres(&sexecutor).
BuildAsStep(fmt.Sprintf(" - Destroying postgres nodes cluster %s ", name))
destroyTasks = append(destroyTasks, t1)
t4 := task.NewBuilder().
DestroyEC2Nodes(&sexecutor, "workstation").
BuildAsStep(fmt.Sprintf(" - Destroying workstation cluster %s ", name))
destroyTasks = append(destroyTasks, t4)
builder = task.NewBuilder().
ParallelStep("+ Destroying all the componets", false, destroyTasks...)
t = builder.Build()
tailctx := context.WithValue(context.Background(), "clusterName", name)
tailctx = context.WithValue(tailctx, "clusterType", clusterType)
if err := t.Execute(ctxt.New(tailctx, 5)); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return err
}
return nil
}