-
Notifications
You must be signed in to change notification settings - Fork 312
/
destroy.go
160 lines (137 loc) · 4.6 KB
/
destroy.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
// 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"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cliutil"
"github.com/pingcap/tiup/pkg/cluster/clusterutil"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
)
// DestroyCluster destroy the cluster.
func (m *Manager) DestroyCluster(name string, gOpt operator.Options, destroyOpt operator.Options, skipConfirm bool) error {
if err := clusterutil.ValidateClusterNameOrError(name); err != nil {
return err
}
metadata, 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
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
if !skipConfirm {
if err := cliutil.PromptForConfirmOrAbortError(
"This operation will destroy %s %s cluster %s and its data.\nDo you want to continue? [y/N]:",
m.sysName,
color.HiYellowString(base.Version),
color.HiYellowString(name)); err != nil {
return err
}
log.Infof("Destroying cluster...")
}
t := m.sshTaskBuilder(name, topo, base.User, gOpt).
Func("StopCluster", func(ctx context.Context) error {
return operator.Stop(ctx, topo, operator.Options{Force: destroyOpt.Force}, tlsCfg)
}).
Func("DestroyCluster", func(ctx context.Context) error {
return operator.Destroy(ctx, topo, destroyOpt)
}).
Build()
if err := t.Execute(ctxt.New(context.Background())); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
if err := m.specManager.Remove(name); err != nil {
return perrs.Trace(err)
}
log.Infof("Destroyed cluster `%s` successfully", name)
return nil
}
// DestroyTombstone destroy and remove instances that is in tombstone state
func (m *Manager) DestroyTombstone(
name string,
gOpt operator.Options,
skipConfirm bool,
) error {
metadata, err := m.meta(name)
// allow specific validation errors so that user can recover a broken
// cluster if it is somehow in a bad state.
if err != nil &&
!errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) {
return err
}
topo := metadata.GetTopology()
base := metadata.GetBaseMeta()
clusterMeta := metadata.(*spec.ClusterMeta)
cluster := clusterMeta.Topology
if !operator.NeedCheckTombstone(cluster) {
return nil
}
tlsCfg, err := topo.TLSConfig(m.specManager.Path(name, spec.TLSCertKeyDir))
if err != nil {
return err
}
b := m.sshTaskBuilder(name, topo, base.User, gOpt)
ctx := ctxt.New(context.Background())
nodes, err := operator.DestroyTombstone(ctx, cluster, true /* returnNodesOnly */, gOpt, tlsCfg)
if err != nil {
return err
}
regenConfigTasks, _ := buildRegenConfigTasks(m, name, topo, base, nodes, true)
t := b.
Func("FindTomestoneNodes", func(ctx context.Context) (err error) {
if !skipConfirm {
err = cliutil.PromptForConfirmOrAbortError(
color.HiYellowString(fmt.Sprintf("Will destroy these nodes: %v\nDo you confirm this action? [y/N]:", nodes)),
)
if err != nil {
return err
}
}
log.Infof("Start destroy Tombstone nodes: %v ...", nodes)
return err
}).
ClusterOperate(cluster, operator.DestroyTombstoneOperation, gOpt, tlsCfg).
UpdateMeta(name, clusterMeta, nodes).
UpdateTopology(name, m.specManager.Path(name), clusterMeta, nodes).
ParallelStep("+ Refresh instance configs", true, regenConfigTasks...).
Parallel(true, buildReloadPromTasks(metadata.GetTopology())...).
Build()
if err := t.Execute(ctx); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
log.Infof("Destroy success")
return nil
}