/
asgservers.go
125 lines (97 loc) · 3.5 KB
/
asgservers.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
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mitchellh/cli"
)
type ASGServersCommand struct {
ASGName string
Ui cli.Ui
}
// Help function displays detailed help for the asgservers sub command
func (c *ASGServersCommand) Help() string {
return `
Description:
Display the internal IP addresses of auto scaled servers
Usage:
awsgo-tools asgservers [flags]
Flags:
--asg-name <auto scale group> to display ip addresses for that group
No flags to display a list of auto scale group names
`
}
// Synopsis function returns a string with concise details of the sub command
func (c *ASGServersCommand) Synopsis() string {
return "Display auto scale server internal ip addresses"
}
// Run function is the function called by the cli library to run the actual sub command code.
func (c *ASGServersCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("asgservers", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.StringVar(&c.ASGName, "asg-name", "", "Auto scale group name or blank to list all groups")
if err := cmdFlags.Parse(args); err != nil {
return RCERR
}
// Get details of current Auto Scale Groups
// Create empty auto scale group list and append any group name on the command line
asgNames := []*string{}
if len(c.ASGName) > 0 {
asgNames = append(asgNames, &c.ASGName)
}
// Create an Autoscaling service object
// config values keys, sercet key & region read from environment
svcAs := autoscaling.New(session.New(), &aws.Config{MaxRetries: aws.Int(10)})
asgi := autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: asgNames}
resp, err := svcAs.DescribeAutoScalingGroups(&asgi)
if err != nil {
fmt.Printf("Fatal error: DescribeAutoScalingGroups - %s\n", err)
return RCERR
}
// if no asg name provided then display current asg names and exit
if len(c.ASGName) == 0 {
fmt.Println("No Autoscaling Group Name provided. Current Groups:")
for asGroup := range resp.AutoScalingGroups {
fmt.Printf("%s\n", *resp.AutoScalingGroups[asGroup].AutoScalingGroupName)
}
fmt.Println("")
return RCOK
}
if len(resp.AutoScalingGroups) < 1 {
fmt.Printf("No Auto Scale Group info found for %s\n", *asgNames[0])
return RCOK
}
instanceSlice := []*string{}
// extract the instanceid's from the auto scale details and append to a slice
for asGroup := range resp.AutoScalingGroups {
for instance := range resp.AutoScalingGroups[asGroup].Instances {
instanceSlice = append(instanceSlice, resp.AutoScalingGroups[asGroup].Instances[instance].InstanceId)
}
}
if len(instanceSlice) < 1 {
fmt.Printf("No instances in auto scale group %s\n", *asgNames[0])
return RCOK
}
ec2i := ec2.DescribeInstancesInput{InstanceIds: instanceSlice}
// Create an EC2 service object
// config values keys, sercet key & region read from environment
svcEc2 := ec2.New(session.New(), &aws.Config{MaxRetries: aws.Int(10)})
respEc2, err := svcEc2.DescribeInstances(&ec2i)
if err != nil {
fmt.Printf("Fatal error: DescribeAutoScalingGroups - %s\n", err)
return RCERR
}
// extract the private ip address from the instance struct stored in the reservation
for reservation := range respEc2.Reservations {
for instance := range respEc2.Reservations[reservation].Instances {
fmt.Printf("%s\n",
*respEc2.Reservations[reservation].Instances[instance].PrivateIpAddress)
}
}
return RCOK
}
/*
*/