-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloud_control.go
403 lines (356 loc) · 13.3 KB
/
cloud_control.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"mdibl_cloud_control/datamodels"
"mdibl_cloud_control/utils"
"os"
"strconv"
"strings"
"github.com/vaughan0/go-ini"
)
func main() {
// Boolean flags
var help,
listInstances,
listInstanceTypes,
stopAllInstances,
stopInstances,
startAllInstances,
startInstances,
launchInstances *bool
// String flags
var awsConf *string
// Declare boolean flags
help = flag.Bool("help", false, "Show full help message")
listInstances = flag.Bool("list-instances", false, "List all running instances")
listInstanceTypes = flag.Bool("list-instance-types", false, "List available instance types for region")
stopAllInstances = flag.Bool("stop-all-instances", false, "Stop all running instances")
stopInstances = flag.Bool("stop-instances", false, "Stop instances specified in instance report")
startAllInstances = flag.Bool("start-all-instances", false, "Start all stopped instances")
startInstances = flag.Bool("start-instances", false, "Start all instances specified in instance report")
launchInstances = flag.Bool("launch-instances", false, "Launch instances from a config file")
// Declare string flags
awsConf = flag.String("aws-config", ".aws/config", "Path to aws config folder.")
flag.Parse()
/* -------------------------------------------------------------------------
* Check for the help param and display help message if provided.
* ---------------------------------------------------------------------- */
if *help {
fmt.Println("\nWill show a help message eventually.\n")
os.Exit(0)
}
/* -------------------------------------------------------------------------
* Proceed with other operations
* ---------------------------------------------------------------------- */
// Get the default AWS region
region, err := utils.DefaultAWSRegion(*awsConf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Create new EC@ client with specified credentials
creds, err := utils.CreateNewEC2ClientCredentials(*awsConf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ec2Client := utils.CreateNewEC2Client(creds, region)
// List all instance types for the specified region
if *listInstanceTypes {
dryRun := false
params := utils.CreateInstanceTypeOfferingFilterParams(dryRun)
describeInstanceTypeOutput, _ := ec2Client.DescribeInstanceTypeOfferings(params)
offerings := utils.ParseInstanceTypeOfferings(describeInstanceTypeOutput)
outputFileName, err := utils.WriteInstanceTypeOfferings(region, offerings)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("\nOutput written to %s\n", outputFileName)
os.Exit(0)
}
// List all instances in the user's account. Running, stopped or pending will be returned.
if *listInstances {
// Create filter parameters
params := utils.CreateEC2InstanceFilterParams("instance-state-name", []string{"stopped", "running", "pending"})
// Query AWS for all instances that match the filter params
describeInstanceOutput, _ := ec2Client.DescribeInstances(params)
// Parse instance details
instanceReport := utils.ParseDescribeInstanceOutput(describeInstanceOutput)
// Print instance details to screen
utils.PrintEC2InstanceReport(instanceReport)
// Write instance report to file
outputFileName, err := utils.WriteInstanceDetailsReport(instanceReport, "all")
fmt.Printf("\nOutput written to %s\n", outputFileName)
if err != nil {
panic(err)
}
}
/* -------------------------------------------------------------------------
* Stop all running instances
* ---------------------------------------------------------------------- */
if *stopAllInstances {
// Get report of all running instances.
// Create filter parameters. Only care about running instances
params := utils.CreateEC2InstanceFilterParams("instance-state-name", []string{"running"})
// Query AWS for all instances that match the filter params
describeInstanceOutput, _ := ec2Client.DescribeInstances(params)
// Print instance information
report := utils.ParseDescribeInstanceOutput(describeInstanceOutput)
if len(report.Instances) == 0 {
// Abort if there are no instances to stop
fmt.Println("No running instances")
os.Exit(0)
}
// List running instances for user.
// Build array of image ids at the same time.
var instanceIDs = make([]string, 0)
var response string
// Prompt the user before proceeding
fmt.Printf("\nThe following instances will be stopped:\n")
fmt.Printf("----------------------------------------\n\n")
for _, instance := range report.Instances {
// Append id string.
instanceIDs = append(instanceIDs, instance.InstanceID)
// Display it to the user.
fmt.Printf("Name: %s, ID: %s, Instance type: %s\n", instance.Name, instance.InstanceID, instance.InstanceType)
}
fmt.Printf("\nContinue: (y/n) ")
fmt.Scanln(&response)
// Stop all running instances
if strings.ToLower(response) == "y" {
fmt.Println("Stopping instances...")
// Create stop instance param object
stopInstanceParams := utils.CreateEC2StopInstanceParams(instanceIDs, true, false, false)
// Throw away the response for now.
_, err := ec2Client.StopInstances(stopInstanceParams)
// Only panic on an actual error. Not dry run operation report.
if err != nil && !strings.Contains(err.Error(), "DryRunOperation") {
panic(err)
}
fmt.Println("Done!")
} else {
fmt.Println("Bye!")
}
os.Exit(0)
}
/* -------------------------------------------------------------------------
* Stop all specified instances
* ---------------------------------------------------------------------- */
if *stopInstances {
if len(flag.Args()) == 0 {
fmt.Println("Instance details file required but not supplied")
os.Exit(1)
}
// Grab the instance config file from the command line.
instanceReport := flag.Args()[0]
// Make sure the config file exists. If configuration is not present, abort.
if _, err := os.Stat(instanceReport); os.IsNotExist(err) {
fmt.Printf("No instance report file found at: %s\n", instanceReport)
os.Exit(1)
}
// Read json file
jsonData, err := ioutil.ReadFile(instanceReport)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Unmarshal the json
ec2ReportObj := datamodels.EC2InstanceReport{}
err = json.Unmarshal(jsonData, &ec2ReportObj)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var response string
// Warn user about stopping all images. Prompt for continue.
// Generate the instance id array at the same time.
var instanceIDs = make([]string, 0)
fmt.Printf("\nThe following instances will be stopped:\n")
fmt.Printf("----------------------------------------\n\n")
for _, instance := range ec2ReportObj.Instances {
// Append id string.
instanceIDs = append(instanceIDs, instance.InstanceID)
// Display it to the user.
fmt.Printf("Name: %s, ID: %s, Instance type: %s\n", instance.Name, instance.InstanceID, instance.InstanceType)
}
fmt.Printf("\nContinue: (y/n) ")
fmt.Scanln(&response)
// Stop all running instances
if strings.ToLower(response) == "y" {
fmt.Println("Stopping specified instances...")
// Create stop instance param object
stopInstanceParams := utils.CreateEC2StopInstanceParams(instanceIDs, false, false, false)
// Throw away the response for now.
_, err := ec2Client.StopInstances(stopInstanceParams)
// Only panic on an actual error. Not dry run operation report.
if err != nil && !strings.Contains(err.Error(), "DryRunOperation") {
panic(err)
}
fmt.Println("Done!")
} else {
fmt.Println("Bye!")
}
os.Exit(0)
}
/* -------------------------------------------------------------------------
* Start all stopped instances.
* NOTE: This will not create new instances. Only start existing instances.
* ---------------------------------------------------------------------- */
if *startAllInstances {
// Create filter parameters. Only care about stopped instances
params := utils.CreateEC2InstanceFilterParams("instance-state-name", []string{"stopped"})
// Query AWS for all instances that match the filter params
describeInstanceOutput, _ := ec2Client.DescribeInstances(params)
// Print instance information
report := utils.ParseDescribeInstanceOutput(describeInstanceOutput)
if len(report.Instances) == 0 {
// Abort if there are no instances to start
fmt.Println("No stopped instances")
os.Exit(0)
}
// List running instances for user.
// Build array of image ids at the same time.
var instanceIDs = make([]string, 0)
var response string
fmt.Printf("\nThe following instances will be started:\n")
fmt.Printf("------------------------------------------\n\n")
for _, instance := range report.Instances {
// Append id string.
instanceIDs = append(instanceIDs, instance.InstanceID)
// Display it to the user.
fmt.Printf("Name: %s, ID: %s, Instance type: %s\n", instance.Name, instance.InstanceID, instance.InstanceType)
}
fmt.Printf("\nContinue: (y/n) ")
fmt.Scanln(&response)
// Start all stopped instances
if strings.ToLower(response) == "y" {
fmt.Println("Starting all instances...")
// Create stop instance param object
startInstanceParams := utils.CreateEC2StartInstanceParams(instanceIDs, true)
// Throw away the response for now.
_, err := ec2Client.StartInstances(startInstanceParams)
// Only panic on an actual error. Not dry run operation report.
if err != nil && !strings.Contains(err.Error(), "DryRunOperation") {
panic(err)
}
fmt.Println("Done!")
} else {
fmt.Println("Bye!")
}
os.Exit(0)
}
/* -------------------------------------------------------------------------
* Start all stopped instances.
* NOTE: This will not create new instances. Only start existing instances.
* ---------------------------------------------------------------------- */
if *startInstances {
if len(flag.Args()) == 0 {
fmt.Println("Instance details file required but not supplied")
os.Exit(1)
}
// Grab the instance config file from the command line.
instanceReport := flag.Args()[0]
// Make sure the config file exists. If configuration is not present, abort.
if _, err := os.Stat(instanceReport); os.IsNotExist(err) {
fmt.Printf("No config file found at: %s\n", instanceReport)
os.Exit(1)
}
// Read json file
jsonData, err := ioutil.ReadFile(instanceReport)
if err != nil {
panic(err)
}
// Unmarshal the json
ec2ReportObj := datamodels.EC2InstanceReport{}
err = json.Unmarshal(jsonData, &ec2ReportObj)
if err != nil {
panic(err)
}
var response string
// Warn user about stopping all images. Prompt for continue.
// Generate the instance id array at the same time.
var instanceIDs = make([]string, 0)
fmt.Printf("\nThe following instances will be started:\n")
fmt.Printf("----------------------------------------\n\n")
for _, instance := range ec2ReportObj.Instances {
// Append id string.
instanceIDs = append(instanceIDs, instance.InstanceID)
// Display it to the user.
fmt.Printf("Name: %s, ID: %s, Instance type: %s\n", instance.Name, instance.InstanceID, instance.InstanceType)
}
fmt.Printf("\nContinue: (y/n) ")
fmt.Scanln(&response)
// Start all instances
if strings.ToLower(response) == "y" {
fmt.Println("Starting specified instances...")
// Create stop instance param object
startInstanceParams := utils.CreateEC2StartInstanceParams(instanceIDs, false)
// Throw away the response for now.
_, err := ec2Client.StartInstances(startInstanceParams)
// Only panic on an actual error. Not dry run operation report.
if err != nil && !strings.Contains(err.Error(), "DryRunOperation") {
panic(err)
}
fmt.Println("Done!")
} else {
fmt.Println("Bye!")
}
os.Exit(0)
}
if *launchInstances {
if len(flag.Args()) == 0 {
fmt.Println("Instance config file required but not supplied")
os.Exit(1)
}
// Grab the instance config file from the command line.
instanceConf := flag.Args()[0]
// Make sure the config file exists. If configuration is not present, abort.
if _, err := os.Stat(instanceConf); os.IsNotExist(err) {
fmt.Printf("No instance config file found at: %s\n", instanceConf)
os.Exit(1)
}
// Load the config file and extract the parameters.
configFile, err := ini.LoadFile(instanceConf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
amiID, _ := configFile.Get("instance", "ami_id")
amiName, _ := configFile.Get("instance", "ami_name")
instanceType, _ := configFile.Get("instance", "instance_type")
countString, _ := configFile.Get("instance", "count")
count, _ := strconv.ParseInt(countString, 10, 64)
// Display launch request to user
var response string
fmt.Println("\nLaunch request details:")
fmt.Printf("-----------------------\n\n")
fmt.Printf("AMI Name: %s\nInstance type: %s\nCount: %d\n\n", amiName, instanceType, count)
fmt.Printf("Continue: (y/n) ")
fmt.Scanln(&response)
if strings.ToLower(response) == "y" {
// Create specified instances.
createInstanceParams := utils.CreateEC2RunInstanceParams(amiID, instanceType, count)
runResponse, err := ec2Client.RunInstances(createInstanceParams)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Generate a launch report and write it to disk.
reportType := "launch"
report := utils.GetInstanceDetails(runResponse)
_, err = utils.WriteInstanceDetailsReport(report, reportType)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Done!")
os.Exit(0)
}
fmt.Println("Bye!")
os.Exit(0)
}
}