-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
374 lines (308 loc) · 10.5 KB
/
main.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/plunder-app/plunder/pkg/apiserver"
"github.com/plunder-app/plunder/pkg/plunderlogging"
"github.com/plunder-app/plunder/pkg/services"
"github.com/spf13/cobra"
)
var logLevel int
// leaveDeploymentFlag - will stop the removal of the deployment once it's been destroyed
var leaveDeploymentFlag bool
var managermentCluster struct {
mac string
address string
}
var machine struct {
address string
}
func init() {
cappctlCmd.PersistentFlags().IntVar(&logLevel, "logLevel", int(log.InfoLevel), "Set the logging level [0=panic, 3=warning, 5=debug]")
initClusterCmd.Flags().StringVarP(&managermentCluster.mac, "mac", "m", "", "The Mac address of the node to use for provisioning")
initClusterCmd.Flags().StringVarP(&managermentCluster.address, "address", "a", "", "The IP address to provision the management cluster with")
destroyMachine.Flags().StringVarP(&machine.address, "address", "a", "", "Address of a machine to destroy")
destroyMachine.Flags().BoolVarP(&leaveDeploymentFlag, "leave", "l", false, "Leave the deployment after it's been destroyed")
cappctlCmd.AddCommand(destroyMachine)
cappctlCmd.AddCommand(initClusterCmd)
}
func main() {
if err := cappctlCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var cappctlCmd = &cobra.Command{
Use: "cappctl",
Short: "Cluster API Plunder control",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
return
},
}
var initClusterCmd = &cobra.Command{
Use: "init-mgmt-cluster",
Short: "Initialise Kubernetes Management Cluster",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
fmt.Println("Beginning the deployment of a new host")
if managermentCluster.mac == "" {
// Print a warning
fmt.Printf("Will select an unleased server at random for management cluster in 5 seconds\n")
// Wait to give the user time to cancel with ctrl+c
time.Sleep(5 * time.Second)
// Get a mac address
u, c, err := apiserver.BuildEnvironmentFromConfig("plunderclient.yaml", "")
if err != nil {
log.Fatalf("%s", err.Error())
}
ep, resp := apiserver.FindFunctionEndpoint(u, c, "dhcp", http.MethodGet)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = path.Join(u.Path, ep.Path+"/unleased")
response, err := apiserver.ParsePlunderGet(u, c)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Fatalf("%s", err.Error())
}
var unleased []services.Lease
err = json.Unmarshal(response.Payload, &unleased)
if err != nil {
log.Fatalf("%s", err.Error())
}
// Iterate through all known addresses and find a free one that looks "recent"
for i := range unleased {
if time.Since(unleased[i].Expiry).Minutes() < 10 {
managermentCluster.mac = unleased[i].Nic
}
}
// Hopefully we found one!
if managermentCluster.mac == "" {
log.Fatalf("No free hardware could be found to provison")
}
}
u, c, err := apiserver.BuildEnvironmentFromConfig("plunderclient.yaml", "")
if err != nil {
log.Fatalf("%s", err.Error())
}
d := services.DeploymentConfig{
ConfigName: "preseed",
MAC: managermentCluster.mac,
ConfigHost: services.HostConfig{
IPAddress: managermentCluster.address,
ServerName: "Manager01",
},
}
ep, resp := apiserver.FindFunctionEndpoint(u, c, "deployment", http.MethodPost)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path
b, err := json.Marshal(d)
if err != nil {
log.Fatalf("%s", err.Error())
}
response, err := apiserver.ParsePlunderPost(u, c, b)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
newMap := uptimeCommand(managermentCluster.address)
// Marshall the parlay submission (runs the uptime command)
b, err = json.Marshal(newMap)
if err != nil {
log.Fatalf("%s", err.Error())
}
// Create the string that will be used to get the logs
dashAddress := strings.Replace(managermentCluster.address, ".", "-", -1)
// Get the time
t := time.Now()
for {
// Set Parlay API path and POST
ep, resp = apiserver.FindFunctionEndpoint(u, c, "parlay", http.MethodPost)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path
response, err := apiserver.ParsePlunderPost(u, c, b)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
// Sleep for five seconds
time.Sleep(5 * time.Second)
// Set the parlay API get logs path and GET
ep, resp = apiserver.FindFunctionEndpoint(u, c, "parlayLog", http.MethodGet)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path + "/" + dashAddress
response, err = apiserver.ParsePlunderGet(u, c)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
var logs plunderlogging.JSONLog
err = json.Unmarshal(response.Payload, &logs)
if err != nil {
log.Fatalf("%s", err.Error())
}
if logs.State == "Completed" {
fmt.Printf("\r\033[32mHost has been succesfully provisioned OS in\033[m %s Seconds\n", time.Since(t).Round(time.Second))
break
} else {
fmt.Printf("\r\033[36mWaiting for Host to complete OS provisioning \033[m%.0f Seconds", time.Since(t).Seconds())
}
}
fmt.Printf("This process can be exited with ctrl+c and monitored with pldrctl get logs %s -w 5\n", managermentCluster.address)
// Begin the Kubernetes installation //
fmt.Println("Beginning the installation and initialisation of Kubernetes")
// Get the Kubernetes Installation commands
kubeMap := kubeCreateHostCommand(managermentCluster.address)
// Add the kubeadm steps
kubeMap.Deployments[0].Actions = append(kubeMap.Deployments[0].Actions, kubeadmActions()...)
// Marshall the parlay submission (runs the uptime command)
b, err = json.Marshal(kubeMap)
if err != nil {
log.Fatalf("%s", err.Error())
}
// Set Parlay API path and POST
ep, resp = apiserver.FindFunctionEndpoint(u, c, "parlay", http.MethodPost)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path
response, err = apiserver.ParsePlunderPost(u, c, b)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
// Get the time
t = time.Now()
for {
// Sleep for five seconds
time.Sleep(5 * time.Second)
// Set the parlay API get logs path and GET
ep, resp = apiserver.FindFunctionEndpoint(u, c, "parlayLog", http.MethodGet)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path + "/" + dashAddress
response, err = apiserver.ParsePlunderGet(u, c)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
var logs plunderlogging.JSONLog
err = json.Unmarshal(response.Payload, &logs)
if err != nil {
log.Fatalf("%s", err.Error())
}
if logs.State == "Completed" {
fmt.Printf("\r\033[32mKubernetes has been succesfully installed on host %s in\033[m %s Seconds\n", managermentCluster.address, time.Since(t).Round(time.Second))
break
} else if logs.State == "Failed" {
log.Fatalln("Kubernetes has failed to install")
} else {
fmt.Printf("\r\033[36mWaiting for Kubernetes to complete installation \033[m%.0f Seconds", time.Since(t).Seconds())
}
}
return
},
}
var destroyMachine = &cobra.Command{
Use: "destroy",
Short: "Destroy a machine",
Run: func(cmd *cobra.Command, args []string) {
log.SetLevel(log.Level(logLevel))
fmt.Println("Destroying a node")
if machine.address == "" {
// Print a warning and exit
cmd.Help()
log.Fatalf("No address specified for host")
}
u, c, err := apiserver.BuildEnvironmentFromConfig("plunderclient.yaml", "")
if err != nil {
log.Fatalf("%s", err.Error())
}
destroyMap := destroyCommand(machine.address)
// Marshall the parlay submission (runs the uptime command)
b, err := json.Marshal(destroyMap)
if err != nil {
log.Fatalf("%s", err.Error())
}
// Set Parlay API path and POST
ep, resp := apiserver.FindFunctionEndpoint(u, c, "parlay", http.MethodPost)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path
response, err := apiserver.ParsePlunderPost(u, c, b)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
fmt.Println("Node will now reset (through sysrq)")
if !leaveDeploymentFlag {
fmt.Println("Removing node from configuration so it wont be re-provisioned")
// Set Parlay API path and POST
ep, resp := apiserver.FindFunctionEndpoint(u, c, "deploymentAddress", http.MethodDelete)
if resp.Error != "" {
log.Debug(resp.Error)
log.Fatalf(resp.FriendlyError)
}
u.Path = ep.Path + "/" + strings.Replace(machine.address, ".", "-", -1)
response, err = apiserver.ParsePlunderDelete(u, c)
if err != nil {
log.Fatalf("%s", err.Error())
}
// If an error has been returned then handle the error gracefully and terminate
if response.FriendlyError != "" || response.Error != "" {
log.Debugln(response.Error)
log.Fatalln(response.FriendlyError)
}
}
return
},
}