-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.go
284 lines (231 loc) · 7.7 KB
/
instance.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
package aws
import (
"fmt"
"log"
"sort"
"time"
"golang.org/x/crypto/ssh"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
// The instance start timeout, in seconds.
const startTimeout = 300
// The instance type to launch.
const instanceType = "t2.nano"
// The SSH user that is used to log into the default image.
const sshUser = "ec2-user"
// amiSearchParameters returns a DescribeImagesInput struct with the details
// necessary to locate the image that the bastion host will launch. The code
// describes an Amazon Linux AMI, which is the default that gets launched.
func amiSearchParameters() *ec2.DescribeImagesInput {
return &ec2.DescribeImagesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("owner-id"),
Values: aws.StringSlice([]string{"137112412989"}),
},
&ec2.Filter{
Name: aws.String("owner-alias"),
Values: aws.StringSlice([]string{"amazon"}),
},
&ec2.Filter{
Name: aws.String("name"),
Values: aws.StringSlice([]string{"amzn-ami-hvm-*.x86_64-gp2"}),
},
&ec2.Filter{
Name: aws.String("description"),
Values: aws.StringSlice([]string{"Amazon Linux AMI * x86_64 HVM GP2"}),
},
},
}
}
// Instance describes an AWS EC2 instance.
type Instance struct {
_ struct{}
// true if the instance has been created.
Created bool `json:"created"`
// The ID of the AMI used to launch the instance.
ImageID string `json:"image_id"`
// The ID of the instance.
InstanceID string `json:"instance_id"`
// The instance type.
InstanceType string `json:"instance_type"`
// The subnet for the instance.
SubnetID string `json:"subnet_id"`
// The key pair name for SSH access.
KeyPairName string `json:"key_pair_name"`
// The security group ID the instance is being launched in.
SecurityGroupID string `json:"security_group_id"`
// The public IP address.
PublicIPAddress string `json:"public_ip_address"`
// The private IP address.
PrivateIPAddress string `json:"private_ip_address"`
// The SSH user to connect to the instance with.
SSHUser string `json:"ssh_user"`
}
// imageSort is an alias type for []*ec2.Image, used for sorting.
type imageSort []*ec2.Image
// Len is the sort.Interface.Len() implementation for imageSort.
func (a imageSort) Len() int { return len(a) }
// Swap is the sort.Interface.Swap() implementation for imageSort.
func (a imageSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Less is the sort.Interface.Less() implementation for imageSort.
func (a imageSort) Less(i, j int) bool {
itime, _ := time.Parse(time.RFC3339, *a[i].CreationDate)
jtime, _ := time.Parse(time.RFC3339, *a[j].CreationDate)
return itime.Unix() < jtime.Unix()
}
// mostRecentAmi returns the most recent AMI out of a slice of images.
func mostRecentAmi(images []*ec2.Image) *ec2.Image {
sortedImages := images
sort.Sort(imageSort(sortedImages))
return sortedImages[len(sortedImages)-1]
}
// waitForInstanceStart waits for the instance to start, and returns the
// properly updated *ec2.Instance object.
func waitForInstanceStart(conn *ec2.EC2, instanceID string, timeout int) (*ec2.Instance, error) {
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("instance-id"),
Values: aws.StringSlice([]string{instanceID}),
},
},
}
start := time.Now()
d := time.Duration(timeout) * time.Second
max := start.Add(d)
for time.Now().After(max) == false {
resp, err := conn.DescribeInstances(params)
if err != nil {
return nil, err
}
if len(resp.Reservations) != 1 {
panic(fmt.Errorf("Expected a single reservation, got %d", len(resp.Reservations)))
}
if len(resp.Reservations[0].Instances) < 1 {
return nil, fmt.Errorf("No instances were found.")
}
if len(resp.Reservations[0].Instances) > 1 {
panic("More than one instance was found when only one was requested")
}
instance := resp.Reservations[0].Instances[0]
if *instance.State.Name == "running" {
return instance, nil
}
}
return nil, fmt.Errorf("Instance was not started after %d seconds", timeout)
}
// waitForSSH waits not only for SSH to be running and open, but also ensures
// that the IP address can be reached via the configured SSH user.
func waitForSSH(addr, user string, key KeyPair, timeout int) error {
signer, err := ssh.ParsePrivateKey([]byte(key.PrivateKeyPEM))
if err != nil {
log.Fatalf("Unable to parse private key: %s", err.Error())
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
}
start := time.Now()
d := time.Duration(timeout) * time.Second
max := start.Add(d)
for time.Now().After(max) == false {
_, err := ssh.Dial("tcp", addr, config)
if err == nil {
return nil
}
}
return fmt.Errorf("SSH could not be connected after %d seconds", timeout)
}
// LocateImage searches for a suitable AMI to launch, based off the
// filters supplied by amiSearchParameters().
func LocateImage(conn *ec2.EC2) (string, error) {
params := amiSearchParameters()
resp, err := conn.DescribeImages(params)
if err != nil {
return "", err
}
if len(resp.Images) < 1 {
return "", fmt.Errorf("No default image found. You may need to update bastion.")
}
// Sort the images and return the most recent AMI found
image := mostRecentAmi(resp.Images)
return *image.ImageId, nil
}
// CreateInstance creates an Amazon EC2 insatnce, and returns an Instance
// struct.
func CreateInstance(conn *ec2.EC2, subnet, securityGroup string, keyPair KeyPair) (Instance, error) {
instance := Instance{
SubnetID: subnet,
KeyPairName: keyPair.KeyName,
SecurityGroupID: securityGroup,
InstanceType: instanceType,
SSHUser: sshUser,
}
// Locate an AMI for the instance
ami, err := LocateImage(conn)
if err != nil {
return instance, err
}
// Attempt to launch the instance.
params := &ec2.RunInstancesInput{
ImageId: aws.String(ami),
InstanceType: aws.String(instanceType),
KeyName: aws.String(keyPair.KeyName),
MaxCount: aws.Int64(1),
MinCount: aws.Int64(1),
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
&ec2.InstanceNetworkInterfaceSpecification{
AssociatePublicIpAddress: aws.Bool(true),
DeleteOnTermination: aws.Bool(true),
DeviceIndex: aws.Int64(0),
Groups: aws.StringSlice([]string{securityGroup}),
SubnetId: aws.String(subnet),
},
},
}
resp, err := conn.RunInstances(params)
if err != nil {
return instance, err
}
if len(resp.Instances) < 1 {
return instance, fmt.Errorf("No instances were launched.")
}
if len(resp.Instances) > 1 {
panic("More than one instance was launched when only one was requested")
}
// Wait for the instance to be started.
newInstance, err := waitForInstanceStart(conn, *resp.Instances[0].InstanceId, startTimeout)
if err != nil {
return instance, err
}
// Wait for SSH off the new instance public IP address
if newInstance.PublicIpAddress == nil {
return instance, fmt.Errorf("Instance ID %s does not have a public IP address.", *newInstance.InstanceId)
}
err = waitForSSH(*newInstance.PublicIpAddress, sshUser, keyPair, startTimeout)
if err != nil {
return instance, err
}
// Done
instance.InstanceID = *newInstance.InstanceId
instance.PublicIPAddress = *newInstance.PublicIpAddress
instance.PrivateIPAddress = *newInstance.PrivateIpAddress
instance.Created = true
return instance, nil
}
// DeleteInstance terminates an Amazon EC2 instance.
func DeleteInstance(conn *ec2.EC2, instance Instance) (Instance, error) {
params := &ec2.TerminateInstancesInput{
InstanceIds: aws.StringSlice([]string{instance.InstanceID}),
}
_, err := conn.TerminateInstances(params)
if err != nil {
return instance, err
}
instance.Created = false
return instance, nil
}