forked from LeanerCloud/AutoSpotting
/
spot_instance_request.go
78 lines (60 loc) · 2.07 KB
/
spot_instance_request.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
package autospotting
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type spotInstanceRequest struct {
*ec2.SpotInstanceRequest
region *region
asg *autoScalingGroup
}
// This function returns an Instance ID
func (s *spotInstanceRequest) waitForAndTagSpotInstance() {
logger.Println(s.asg.name, "Waiting for spot instance for spot instance request",
*s.SpotInstanceRequestId)
ec2Client := s.region.services.ec2
params := ec2.DescribeSpotInstanceRequestsInput{
SpotInstanceRequestIds: []*string{s.SpotInstanceRequestId},
}
err := ec2Client.WaitUntilSpotInstanceRequestFulfilled(¶ms)
if err != nil {
logger.Println(s.asg.name, "Error waiting for instance:", err.Error())
return
}
logger.Println(s.asg.name, "Done waiting for an instance.")
// Now we try to get the InstanceID of the instance we got
requestDetails, err := ec2Client.DescribeSpotInstanceRequests(¶ms)
if err != nil {
logger.Println(s.asg.name, "Failed to describe spot instance requests")
}
// due to the waiter we can now safely assume all this data is available
spotInstanceID := requestDetails.SpotInstanceRequests[0].InstanceId
logger.Println(s.asg.name, "found new spot instance", *spotInstanceID,
"\nTagging it to match the other instances from the group")
// we need to re-scan in order to have the information a
s.region.scanInstances()
tags := s.asg.propagatedInstanceTags()
s.region.instances.get(*spotInstanceID).tag(tags)
}
func (s *spotInstanceRequest) tag(asgName string) {
svc := s.region.services.ec2
_, err := svc.CreateTags(&ec2.CreateTagsInput{
Resources: []*string{s.SpotInstanceRequestId},
Tags: []*ec2.Tag{
{
Key: aws.String("launched-for-asg"),
Value: aws.String(asgName),
},
},
})
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
logger.Println(asgName,
"Failed to create tags for the spot instance request",
err.Error())
return
}
logger.Println(asgName, "successfully tagged spot instance request",
*s.SpotInstanceRequestId)
}