-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
331 lines (283 loc) · 7.75 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
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"text/tabwriter"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/awserr"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
func usage() {
fmt.Fprintf(os.Stderr, `Usage: %s [options] {instance id|private IPv4 address|name}
Options:
-v be verbose (passes -v to underlying SSH invocation)
-p path to SSH key files
-l, --list list running and pending AWS instances
-c, --command run a command on the remote server
`, filepath.Base(os.Args[0]))
os.Exit(1)
}
var verboseFlag bool
var remoteCommand string
var listInstances bool
var kp string
var instIdRe = regexp.MustCompile(`i-[0-9a-fA-F]{8,17}$`)
type Instance struct {
Name string
Id string
Ip string
}
type Instances []*Instance
func (s Instances) Len() int {
return len(s)
}
func (s Instances) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Instances) Less(i, j int) bool {
switch strings.Compare(s[i].Name, s[j].Name) {
case -1:
return true
case 1:
return false
}
return s[i].Name > s[j].Name
}
func debugf(format string, args ...interface{}) {
if verboseFlag {
log.Printf(format, args...)
}
}
func printError(err error) {
if awsErr, ok := err.(awserr.Error); ok {
log.Println("Error:", awsErr.Code(), awsErr.Message())
} else {
log.Println("Error:", err.Error())
}
os.Exit(1)
}
func reservationsToInstances(reservations []ec2.RunInstancesOutput) []*Instance {
var instances []*Instance
for _, reservation := range reservations {
for _, instance := range reservation.Instances {
name := "[None]"
for _, keys := range instance.Tags {
if *keys.Key == "Name" {
name = url.QueryEscape(*keys.Value)
}
}
instances = append(instances, &Instance{Name: name, Id: *instance.InstanceId, Ip: *instance.PrivateIpAddress})
}
}
sort.Sort(Instances(instances))
return instances
}
func printInstanceList(instances []*Instance) {
if len(instances) == 0 {
printError(errors.New("Found no instances"))
} else {
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 4, ' ', tabwriter.TabIndent)
fmt.Fprintln(writer, "Name\tInstance ID\tPrivate IP")
fmt.Fprintln(writer, "----\t-----------\t----------")
for _, instance := range instances {
fmt.Fprintf(writer, "%s\t%s\t%s\n", instance.Name, instance.Id, instance.Ip)
}
writer.Flush()
}
}
// Formats a slice of instance pointers into a table and returns it
func fmtInstanceList(instances []*Instance) string {
var buf bytes.Buffer
writer := tabwriter.NewWriter(&buf, 4, 4, 4, ' ', tabwriter.TabIndent)
fmt.Fprintln(writer, "n\tName\tInstance ID\tPrivate IP")
fmt.Fprintln(writer, "-\t----\t-----------\t----------")
for i, instance := range instances {
fmt.Fprintf(writer, "%d\t%s\t%s\t%s\n", i+1, instance.Name, instance.Id, instance.Ip)
}
writer.Flush()
return buf.String()
}
func init() {
// default key path to home dir, inherit if env var if set
p := os.Getenv("HOME") + "/.ssh/"
if s := os.Getenv("AWS_KEY_PATH"); s != "" {
p = s
}
flag.StringVar(&kp, "p", p, "path to directory with SSH keys, default is $HOME/.ssh")
flag.BoolVar(&verboseFlag, "v", false, "be verbose")
flag.BoolVar(&listInstances, "l", false, "show list of running instances and exit")
flag.BoolVar(&listInstances, "list", false, "show list of running instances and exit")
flag.StringVar(&remoteCommand, "c", "", "A command to run on the remote server")
flag.StringVar(&remoteCommand, "command", "", "A command to run on the remote server")
}
// Given an instance name and Id, and a reservation list, return the ec2.Instance
// that matches
func findInstance(instance *Instance, reservations []ec2.RunInstancesOutput) (*ec2.Instance, error) {
for _, reservation := range reservations {
for _, ec2Instance := range reservation.Instances {
if *ec2Instance.InstanceId == instance.Id {
return &ec2Instance, nil
}
}
}
return nil, fmt.Errorf("Unable to find instance %#v", instance)
}
// Accepts the user's query and a slice of reservations that match the query.
// Shows the user the instance IDs and allows them to choose one on the command
// line, and returns a pointer to the instance that was chosen
func chooseInstance(lookup string, reservations []ec2.RunInstancesOutput) *ec2.Instance {
var instanceList = reservationsToInstances(reservations)
fmt.Printf(`Found more than one instance for '%s'.
Available instances:
%s
Which would you like to connect to? [1]
>>> `, lookup, fmtInstanceList(instanceList))
var which string
_, err := fmt.Scanln(&which)
if err == io.EOF {
// We're currently in the middle of a line; print a newline to clean up
// the user's terminal
fmt.Println("")
os.Exit(0)
}
idx := 1
if len(which) > 0 {
idx, err = strconv.Atoi(which)
if err != nil {
printError(err)
}
}
if idx < 1 || idx > len(instanceList) {
printError(fmt.Errorf("Invalid index %d", idx))
}
instance, err := findInstance(instanceList[idx-1], reservations)
if err != nil {
printError(err)
}
return instance
}
func main() {
log.SetFlags(0)
log.SetPrefix(filepath.Base(os.Args[0]) + ": ")
flag.Usage = usage
flag.Parse()
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
printError(err)
}
svc := ec2.New(cfg)
var instanceStateFilter = ec2.Filter{
Name: aws.String("instance-state-name"),
Values: []string{
"running",
"pending",
},
}
if flag.NArg() != 1 {
if listInstances {
debugf("aws api: describing instances")
var params *ec2.DescribeInstancesInput
params = &ec2.DescribeInstancesInput{
Filters: []ec2.Filter{
instanceStateFilter,
},
}
req := svc.DescribeInstancesRequest(params)
resp, err := req.Send()
if err != nil {
printError(err)
}
printInstanceList(reservationsToInstances(resp.Reservations))
os.Exit(0)
} else {
flag.Usage()
}
}
lookup := flag.Arg(0)
var params *ec2.DescribeInstancesInput
if ip := net.ParseIP(lookup); ip != nil {
params = &ec2.DescribeInstancesInput{
Filters: []ec2.Filter{
{
Name: aws.String("private-ip-address"),
Values: []string{
lookup,
},
},
instanceStateFilter,
},
}
} else if instIdRe.MatchString(lookup) {
debugf("describing instance(s) by ID")
params = &ec2.DescribeInstancesInput{
InstanceIds: []string{lookup},
Filters: []ec2.Filter{
instanceStateFilter,
},
}
} else {
debugf("describing instance(s) by name")
params = &ec2.DescribeInstancesInput{
Filters: []ec2.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{lookup},
},
instanceStateFilter,
},
}
}
debugf("aws api: describing instances")
req := svc.DescribeInstancesRequest(params)
resp, err := req.Send()
if err != nil {
printError(err)
}
debugf("aws api: got %d reservation(s)", len(resp.Reservations))
var instance *ec2.Instance
if len(resp.Reservations) == 0 {
printError(fmt.Errorf("Found no instance '%s'", lookup))
} else if len(resp.Reservations) == 1 {
instance = &resp.Reservations[0].Instances[0]
} else if len(resp.Reservations) > 1 {
instance = chooseInstance(lookup, resp.Reservations)
}
binary, lookErr := exec.LookPath("ssh")
if lookErr != nil {
printError(lookErr)
}
args := []string{"-i", keypath(*instance.KeyName), "-l", "ec2-user", *instance.PrivateIpAddress}
if verboseFlag {
args = append(args, "-v")
}
if len(remoteCommand) > 1 {
args = append(args, remoteCommand)
}
cmd := exec.Command(binary, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
debugf("running command %v", cmd.Args)
if err := cmd.Run(); err != nil {
printError(err)
}
}
func keypath(s string) string {
debugf("key path is: %s", kp)
return path.Join(kp, s+".pem")
}