This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
stack.go
343 lines (285 loc) · 10.8 KB
/
stack.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
package cli
import (
"encoding/json"
"fmt"
"os"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/types"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v1stacks"
"github.com/hortonworks/cb-cli/client_cloudbreak/v2stacks"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
const availabilitySet = "availabilitySet"
var stackHeader []string = []string{"Name", "Description", "CloudPlatform", "StackStatus", "ClusterStatus"}
type stackOut struct {
cloudResourceOut
StackStatus string `json:"StackStatus" yaml:"StackStatus"`
ClusterStatus string `json:"ClusterStatus" yaml:"ClusterStatus"`
}
func (s *stackOut) DataAsStringArray() []string {
arr := s.cloudResourceOut.DataAsStringArray()
arr = append(arr, s.StackStatus)
arr = append(arr, s.ClusterStatus)
return arr
}
type stackDetailsOut struct {
*models_cloudbreak.StackResponse
resp *models_cloudbreak.StackResponse
}
func (s *stackDetailsOut) DataAsStringArray() []string {
return convertResponseToStack(s.resp).DataAsStringArray()
}
func CreateStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "create cluster")
req := assembleStackRequest(c)
cbClient := NewCloudbreakHTTPClientFromContext(c)
utils.CheckClientVersion(cbClient.Cloudbreak.V1util, Version)
cbClient.createStack(req, c.Bool(FlPublicOptional.Name))
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(*req.General.Name, AVAILABLE, AVAILABLE)
}
}
func assembleStackRequest(c *cli.Context) *models_cloudbreak.StackV2Request {
path := c.String(FlInputJson.Name)
if _, err := os.Stat(path); os.IsNotExist(err) {
utils.LogErrorAndExit(err)
}
log.Infof("[assembleStackTemplate] read cluster create json from file: %s", path)
content := utils.ReadFile(path)
var req models_cloudbreak.StackV2Request
err := json.Unmarshal(content, &req)
if err != nil {
msg := fmt.Sprintf(`Invalid json format: %s. Please make sure that the json is valid (check for commas and double quotes).`, err.Error())
utils.LogErrorMessageAndExit(msg)
}
name := c.String(FlName.Name)
if len(name) != 0 {
if req.General == nil {
req.General = &models_cloudbreak.GeneralSettings{}
}
req.General.Name = &name
if req.InstanceGroups != nil {
for _, ig := range req.InstanceGroups {
if _, ok := ig.Parameters[availabilitySet]; ok {
as := ig.Parameters[availabilitySet].(map[string]interface{})
as["name"] = *req.General.Name + "-" + *ig.Group + "-as"
ig.Parameters[availabilitySet] = as
}
}
}
}
if req.General == nil || req.General.Name == nil || len(*req.General.Name) == 0 {
utils.LogErrorMessageAndExit("Name of the cluster must be set either in the template or with the --name command line option.")
}
ambariPassword := c.String(FlAmbariPasswordOptional.Name)
if len(ambariPassword) != 0 {
if req.Cluster != nil && req.Cluster.Ambari != nil {
req.Cluster.Ambari.Password = &ambariPassword
} else {
utils.LogErrorMessageAndExit("Missing clusterRequest.ambariRequest node in JSON")
}
}
return &req
}
func convertResponseToStack(s *models_cloudbreak.StackResponse) *stackOut {
return &stackOut{
cloudResourceOut{*s.Name, s.Cluster.Description, s.CloudPlatform},
s.Status,
s.Cluster.Status,
}
}
func DescribeStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "describe stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
resp, err := cbClient.Cloudbreak.V1stacks.GetPublicStack(v1stacks.NewGetPublicStackParams().WithName(c.String(FlName.Name)))
if err != nil {
utils.LogErrorAndExit(err)
}
output.Write(stackHeader, &stackDetailsOut{resp.Payload, resp.Payload})
}
type getPublicStack interface {
GetPublicStack(client *v1stacks.GetPublicStackParams) (*v1stacks.GetPublicStackOK, error)
}
func fetchStack(name string, client getPublicStack) *models_cloudbreak.StackResponse {
resp, err := client.GetPublicStack(v1stacks.NewGetPublicStackParams().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
return resp.Payload
}
func ScaleStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
desiredCount, err := strconv.Atoi(c.String(FlDesiredNodeCount.Name))
if err != nil {
utils.LogErrorMessageAndExit("Unable to parse as number: " + c.String(FlDesiredNodeCount.Name))
}
defer utils.TimeTrack(time.Now(), "scale stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
req := &models_cloudbreak.StackScaleRequestV2{
DesiredCount: &(&types.I32{I: int32(desiredCount)}).I,
Group: &(&types.S{S: c.String(FlGroupName.Name)}).S,
}
name := c.String(FlName.Name)
log.Infof("[ScaleStack] scaling stack, name: %s", name)
err = cbClient.Cloudbreak.V2stacks.PutscalingStackV2(v2stacks.NewPutscalingStackV2Params().WithName(name).WithBody(req))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[ScaleStack] stack scaled, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, AVAILABLE, AVAILABLE)
}
}
func StartStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "start stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[StartStack] starting stack, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.PutstartStackV2(v2stacks.NewPutstartStackV2Params().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[StartStack] stack started, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, AVAILABLE, AVAILABLE)
}
}
func StopStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "stop stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[StopStack] stopping stack, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.PutstopStackV2(v2stacks.NewPutstopStackV2Params().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[StopStack] stack stopted, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, STOPPED, STOPPED)
}
}
func SyncStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "sync stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[SyncStack] syncing stack, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.PutsyncStackV2(v2stacks.NewPutsyncStackV2Params().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[SyncStack] stack synced, name: %s", name)
}
func RepairStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "repair stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[RepairStack] repairing stack, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.PutrepairStackV2(v2stacks.NewPutrepairStackV2Params().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[RepairStack] stack repaired, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, AVAILABLE, AVAILABLE)
}
}
func RetryCluster(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "retry cluster creation")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[RetryCluster] retrying cluster creation, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.RetryStack(v2stacks.NewRetryStackParams().WithName(name))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[RetryCluster] cluster creation retried, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, AVAILABLE, AVAILABLE)
}
}
func ReinstallStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "reinstall stack")
req := assembleReinstallRequest(c)
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[RepairStack] reinstalling stack, name: %s", name)
err := cbClient.Cloudbreak.V2stacks.PutreinstallStackV2(v2stacks.NewPutreinstallStackV2Params().WithName(name).WithBody(req))
if err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[ReinstallStack] stack reinstalled, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, AVAILABLE, AVAILABLE)
}
}
func assembleReinstallRequest(c *cli.Context) *models_cloudbreak.ReinstallRequestV2 {
path := c.String(FlInputJson.Name)
if _, err := os.Stat(path); os.IsNotExist(err) {
utils.LogErrorAndExit(err)
}
log.Infof("[assembleReinstallRequest] read cluster reinstall json from file: %s", path)
content := utils.ReadFile(path)
var req models_cloudbreak.ReinstallRequestV2
err := json.Unmarshal(content, &req)
if err != nil {
msg := fmt.Sprintf(`Invalid json format: %s. Please make sure that the json is valid (check for commas and double quotes).`, err.Error())
utils.LogErrorMessageAndExit(msg)
}
bpName := c.String(FlBlueprintNameOptional.Name)
if len(bpName) != 0 {
req.BlueprintName = &bpName
}
if req.BlueprintName == nil || len(*req.BlueprintName) == 0 {
utils.LogErrorMessageAndExit("Name of the blueprint must be set either in the template or with the --blueprint-name command line option.")
}
req.KerberosPassword = c.String(FlKerberosPasswordOptional.Name)
req.KerberosPrincipal = c.String(FlKerberosPrincipalOptional.Name)
return &req
}
func DeleteStack(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "delete stack")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
stack := cbClient.getStackByName(name)
cbClient.deleteStack(name, c.Bool(FlForceOptional.Name), *stack.Public)
log.Infof("[DeleteStack] stack deleted, name: %s", name)
if c.Bool(FlWaitOptional.Name) {
cbClient.waitForOperationToFinish(name, DELETE_COMPLETED, SKIP)
}
}
func ListStacks(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list stacks")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listStacksImpl(cbClient.Cloudbreak.V1stacks, output.WriteList)
}
type getPublicsStackClient interface {
GetPublicsStack(*v1stacks.GetPublicsStackParams) (*v1stacks.GetPublicsStackOK, error)
}
func listStacksImpl(client getPublicsStackClient, writer func([]string, []utils.Row)) {
log.Infof("[listStacksImpl] sending stack list request")
stacksResp, err := client.GetPublicsStack(v1stacks.NewGetPublicsStackParams())
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, stack := range stacksResp.Payload {
tableRows = append(tableRows, convertResponseToStack(stack))
}
writer(stackHeader, tableRows)
}