-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
148 lines (131 loc) · 3.23 KB
/
aws.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"os"
"os/exec"
"os/user"
"path"
"text/tabwriter"
)
type Aws struct {
config Config
svc *ec2.EC2
}
func NewAws(config Config) Aws {
creds := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
region := &config.Region
sess := session.New(&aws.Config{
Credentials: creds,
Region: region,
})
svc := ec2.New(sess)
return Aws{config, svc}
}
func (a Aws) listMachines() {
params := &ec2.DescribeInstancesInput{}
resp, _ := a.svc.DescribeInstances(params)
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)
fmt.Fprintln(w, "Machine ID\tPublic IP\tPrivateIP")
fmt.Fprintln(w, "---\t---\t---")
total := 0
for idx, _ := range resp.Reservations {
for _, inst := range resp.Reservations[idx].Instances {
// Replace public ip with "-" if machine doesn't have one
publicIp := "-"
if inst.PublicIpAddress != nil {
publicIp = *inst.PublicIpAddress
}
// Replace private ip with "-" if machine doesn't have one
privateIp := "-"
if inst.PrivateIpAddress != nil {
privateIp = *inst.PrivateIpAddress
}
fmt.Fprintf(
w,
"%s\t%s\t%s\n",
*inst.InstanceId,
publicIp,
privateIp,
)
total++
}
}
w.Flush()
fmt.Printf("---\nFound %d machines.\n", total)
}
func (a Aws) showMachine(machineId string) {
inst := a.getMachine(machineId)
if inst != nil {
fmt.Printf("%#v\n", inst)
} else {
fmt.Printf("Couldn't find %s\n", machineId)
}
}
func (a Aws) sshMachine(username, machineId string) {
inst := a.getMachine(machineId)
if inst != nil {
var ip string
if !a.config.PrivateNetwork && inst.PublicIpAddress != nil {
ip = *inst.PublicIpAddress
} else if a.config.PrivateNetwork && inst.PrivateIpAddress != nil {
ip = *inst.PrivateIpAddress
} else {
fmt.Println("Can't find an IP for", machineId)
return
}
userHost := fmt.Sprintf("%s@%s", username, ip)
me, err := user.Current()
if err != nil {
fmt.Println("Can't determine username details:", err)
}
keyFile := fmt.Sprintf(".ssh/%s.pem", *inst.KeyName)
keyFile = path.Join(me.HomeDir, keyFile)
if _, err := os.Stat(keyFile); os.IsNotExist(err) {
fmt.Println("Can't find private key:", keyFile)
return
}
cmd := exec.Command("ssh", "-i", keyFile, userHost)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
err = cmd.Run()
if err != nil {
fmt.Println(err)
}
} else {
fmt.Printf("Couldn't find %s\n", machineId)
}
}
func (a Aws) removeMachines(machines []string) {
var instances []*string
for _, inst := range machines {
instances = append(instances, &inst)
}
filter := &ec2.TerminateInstancesInput{
InstanceIds: instances,
}
a.svc.TerminateInstances(filter)
}
func (a Aws) getMachine(machineId string) *ec2.Instance {
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-id"),
Values: []*string{
&machineId,
},
},
},
}
resp, _ := a.svc.DescribeInstances(params)
for idx, _ := range resp.Reservations {
for _, inst := range resp.Reservations[idx].Instances {
return inst
}
}
return nil
}