/
frutaskrunner.go
343 lines (275 loc) · 8.67 KB
/
frutaskrunner.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
//
// Copyright (c) 2017 Dell Inc. or its subsidiaries. All Rights Reserved.
// Dell EMC Confidential/Proprietary Information
//
//
package frutaskrunner
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/dellemc-symphony/workflow-cli/auth"
"github.com/dellemc-symphony/workflow-cli/models"
"github.com/dellemc-symphony/workflow-cli/transport"
"github.com/dellemc-symphony/workflow-cli/utils"
"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
)
// RunTask starts the logic to run tasks and act on the results
func RunTask(r models.Response, target string) error {
if len(r.Links) == 0 {
return fmt.Errorf("Server error: No next-step or retry-step received")
}
client, clientErr := transport.NewClient(target)
if clientErr != nil {
log.Fatalf(clientErr.Error())
}
for {
var index int
for i, link := range r.Links {
if link.Rel == models.StepNext {
index = i
break
}
}
hrefSplit := strings.Split(r.Links[index].Href, "/")
log.Infof("Next Step: %s", hrefSplit[len(hrefSplit)-1])
endpointBody := models.Endpoint{}
var postBody io.Reader
// Parse for what we are getting credentials to
typeSplit := strings.Split(r.Links[index].Type, ".")
if typeSplit[len(typeSplit)-1] == "endpoint+json" {
if len(typeSplit) < 4 {
log.Printf("r is %+v", r)
log.Warnf("Expecting Type to be of format 'application/vnd.dellemc.SOMETHING.endpoint+json', not: %s", r.Links[index].Type)
}
authTarget := typeSplit[len(typeSplit)-2]
// Call auth
endpointURL, userName, password, err := auth.TargetAuth(authTarget)
if err != nil {
log.Warnf("error getting creds: %s", err)
return err
}
endpointBody.EndpointURL = endpointURL
endpointBody.Username = userName
endpointBody.Password = password
//Format post body
postBody, err = utils.EncodeBody(endpointBody)
if err != nil {
log.Warnf("Could not encode: %+v", endpointBody)
return fmt.Errorf("error encoding credentials body: %s", err)
}
} else if r.Links[index].Type == "application/vnd.dellemc.nodes.list.add+json" {
nodeSelected, err := PresentNodesToUser(models.ActionAddNode, r.Nodes)
if err != nil {
return err
}
postBody, err = utils.EncodeBody(nodeSelected)
if err != nil {
log.Warnf("Could not encode: %+v", nodeSelected)
return fmt.Errorf("error encoding credentials body: %s", err)
}
} else if r.Links[index].Type == "application/vnd.dellemc.nodes.list.remove+json" {
nodeSelected, err := PresentNodesToUser(models.ActionRemoveNode, r.Nodes)
if err != nil {
return err
}
postBody, err = utils.EncodeBody(nodeSelected)
if err != nil {
log.Warnf("Could not encode: %+v", nodeSelected)
return fmt.Errorf("error encoding credentials body: %s", err)
}
} else if r.Links[index].Type == "application/vnd.dellemc.cpsd.removenode" {
fmt.Printf("\nIt is now safe to remove the failed node from the rack.\n")
fmt.Println("When the new node has been racked and cabled, please power it on and type CONTINUE ...")
ok := false
for !ok {
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if err := scanner.Err(); err != nil {
return fmt.Errorf("Error reading user input: %s", err)
}
input := strings.ToUpper(scanner.Text())
if input == "CONTINUE" {
ok = true
} else {
fmt.Println("Type CONTINUE when ready ...")
}
}
}
reqNext, err := http.NewRequest(r.Links[index].Method, r.Links[index].Href, postBody)
if err != nil {
log.Warnf("%s", err)
}
reqNext.Header.Set("content-type", r.Links[index].Type)
resp, err := client.Do(reqNext)
if err != nil {
return fmt.Errorf("error sending HTTP request: %s", err)
}
if resp.StatusCode < 200 || 300 < resp.StatusCode {
log.Warnf("Request was: %+v\n", reqNext)
log.Warnf("Response is: %+v\n", resp)
rBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Warnf("Readall error %s", readErr)
}
log.Warnf("Response body is: %s\n", rBody)
return fmt.Errorf("Non-Success status(%d): %s", resp.StatusCode, resp.Status)
}
r = models.Response{}
if err = utils.DecodeBody(resp, &r); err != nil {
log.Warnf("Resp is %s\n", resp)
return fmt.Errorf("error decoding response: %s", err)
}
for i, link := range r.Links {
if link.Rel == models.StepNext {
index = i
break
}
}
// if r.Links is empty, assume end of steps
if len(r.Links) == 0 {
log.Infof("Step Complete: %s", r.CurrentStep)
log.Printf("No next-step recieved...Workflow complete")
return nil
}
delay := r.Links[index].Delay
switch r.Links[index].Rel {
case models.StepNext:
if delay != 0 {
log.Infof("This task is in progress. Please wait...")
time.Sleep(time.Duration(delay) * time.Second)
} else {
log.Infof("Step Complete: %s", r.CurrentStep)
}
fmt.Print("\n")
case models.StepRetry:
log.Warnf("Step Failed: %s", r.CurrentStep)
if delay != 0 {
log.Infof("Waiting for %d seconds before retrying", delay)
time.Sleep(time.Duration(delay) * time.Second)
} else {
log.Warnf("Attempting retry: %s\n", r.CurrentStep)
}
default:
log.Warnf("Status Unknown: %+v\n", r)
}
}
}
// InitiateWorkflow starts the workflow
func InitiateWorkflow(target string) (models.Response, error) {
client, err := transport.NewClient(target)
if err != nil {
log.Fatalf(err.Error())
}
quantaReplace := models.WorkflowRequest{
Workflow: "quanta-replacement-d51b-esxi",
}
body, err := utils.EncodeBody(quantaReplace)
if err != nil {
return models.Response{}, fmt.Errorf("Error encoding request: %s", err)
}
log.Printf("Server target is %s\n", target)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf(target+"/fru/api/workflow"), body)
if err != nil {
return models.Response{}, fmt.Errorf("Error creating new request: %s", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Fatalf("%s", err)
}
if resp.StatusCode < 200 || 300 < resp.StatusCode {
log.Warnf("Non-Success status(%d): %s", resp.StatusCode, resp.Status)
log.Warnf("Request was: %+v\n", req)
log.Warnf("Response is: %+v\n", resp)
rBody, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Warnf("Readall error %s", readErr)
}
log.Warnf("Response body is: %s\n", rBody)
return models.Response{}, fmt.Errorf("Non-Success status(%d): %s", resp.StatusCode, resp.Status)
}
r := models.Response{}
if err = utils.DecodeBody(resp, &r); err != nil {
return models.Response{}, fmt.Errorf("Decoding Response to Start Workflow: %s", err)
}
return r, nil
}
//PresentNodesToUser presents the user with a list of nodes and asks them to choose one
func PresentNodesToUser(action string, nodes models.Nodes) (models.Node, error) {
var err error
var selector int
var selectedNode models.Node
ok := false
for !ok {
// Print result
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Select", "Hostname", "Serial Number", "Mgmt IP", "Status"})
for index, node := range nodes {
table.Append([]string{
fmt.Sprintf("%d", index+1),
node.Hostname,
node.SerialNumber,
node.ManagementIP,
node.Status,
})
}
table.Render()
scanner := bufio.NewScanner(os.Stdin)
selector = 0
for selector < 1 || selector > len(nodes) {
okNodeSelect := false
for !okNodeSelect {
fmt.Printf("Select a node for action '%s': ", action)
scanner.Scan()
if err = scanner.Err(); err != nil {
log.Errorf("Error reading user input: %s", err)
continue
}
input := scanner.Text()
selector, err = strconv.Atoi(input)
if err != nil {
log.Errorf("Error parsing user input: \"%s\"\n", input)
continue
}
okNodeSelect = true
}
if selector < 1 || selector > len(nodes) {
log.Warnf("Invalid node selection: %d", selector)
}
}
// subtract 1 because nodes is an array and we counted from 1 in the table
selectedNode = nodes[selector-1]
selectedTable := tablewriter.NewWriter(os.Stdout)
selectedTable.SetHeader([]string{"Hostname", "Serial Number", "Mgmt IP", "Status"})
selectedTable.Append([]string{
selectedNode.Hostname,
selectedNode.SerialNumber,
selectedNode.ManagementIP,
selectedNode.Status,
})
fmt.Printf("\n")
selectedTable.Render()
fmt.Printf("Is this the correct node? [Y/N] or Q to quit: ")
scanner.Scan()
fmt.Printf("\n")
if err := scanner.Err(); err != nil {
return models.Node{}, fmt.Errorf("Error reading user input: %s", err)
}
input := scanner.Text()
if strings.ToLower(input) == "q" {
return models.Node{}, fmt.Errorf("User selected quit")
}
if strings.ToLower(input) == "y" {
ok = true
}
}
return selectedNode, nil
}