-
Notifications
You must be signed in to change notification settings - Fork 3
/
clusters_add_spot_fleet.go
193 lines (160 loc) · 5.88 KB
/
clusters_add_spot_fleet.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
package cmd
import (
"bytes"
"encoding/base64"
"errors"
"html/template"
"strconv"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func clustersAddSpotFleetRun(cmd *cobra.Command, clusters []string) {
clustersDescription, err := ecsI.DescribeClusters(&ecs.DescribeClustersInput{
Clusters: []*string{
aws.String(clusters[0]),
},
})
t.Must(err)
if len(clustersDescription.Clusters) == 0 {
t.Must(errors.New("Cluster informed not found"))
}
c := clustersDescription.Clusters[0]
var userData string
switch platform {
case "windows", "windows-2016", "windows-2019":
userData = windowsUserData
default:
userData = linuxSpotFleetUserData
}
tmpl, err := template.New("UserData").Parse(userData)
t.Must(err)
userDataF := new(bytes.Buffer)
t.Must(tmpl.Execute(userDataF, templateUserData{
Cluster: *c.ClusterName,
Region: aws.StringValue(awsSession.Config.Region),
}))
ami := viper.GetString("ami")
if ami == "latest" || ami == "" {
latestAMI, err := latestAmiEcsOptimized(platform)
t.Must(err)
ami = aws.StringValue(latestAMI.ImageId)
}
// TODO: automaticaly --create-roles if does not exist
spotFleetRoleResponse, err := iamI.GetRole(&iam.GetRoleInput{
RoleName: aws.String(spotFleetRole),
})
t.Must(err)
// TODO: automaticaly --create-roles if does not exist
instanceProfileResponse, err := iamI.GetInstanceProfile(&iam.GetInstanceProfileInput{
InstanceProfileName: aws.String(instanceProfile),
})
t.Must(err)
var SecurityGroups []*ec2.GroupIdentifier
for _, securityGroup := range securityGroups {
sg, err := findSecurityGroup(securityGroup)
t.Must(err)
SecurityGroups = append(SecurityGroups, &ec2.GroupIdentifier{
GroupId: sg.GroupId,
})
}
var subnetsIds []string
for _, subnet := range subnets {
Subnet, err := findSubnet(subnet)
t.Must(err)
subnetsIds = append(subnetsIds, aws.StringValue(Subnet.SubnetId))
}
var LaunchSpecifications []*ec2.SpotFleetLaunchSpecification
for _, instanceTypeAndWeight := range instanceTypes {
iTWSlice := strings.Split(instanceTypeAndWeight, ":")
instanceType := iTWSlice[0]
var weight float64
if len(iTWSlice) > 1 {
weight, err = strconv.ParseFloat(iTWSlice[1], 64)
t.Must(err)
}
SpotFleetLaunchSpecification := ec2.SpotFleetLaunchSpecification{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Arn: instanceProfileResponse.InstanceProfile.Arn,
},
EbsOptimized: aws.Bool(ebs),
ImageId: aws.String(ami),
InstanceType: aws.String(instanceType),
SecurityGroups: SecurityGroups,
SubnetId: aws.String(strings.Join(subnetsIds, ",")),
UserData: aws.String(base64.StdEncoding.EncodeToString(userDataF.Bytes())),
}
if weight > 1 {
SpotFleetLaunchSpecification.WeightedCapacity = aws.Float64(weight)
}
if kernelID != "" {
SpotFleetLaunchSpecification.KernelId = aws.String(kernelID)
}
if key != "" {
SpotFleetLaunchSpecification.KeyName = aws.String(key)
}
if monitoring {
SpotFleetLaunchSpecification.Monitoring = &ec2.SpotFleetMonitoring{Enabled: aws.Bool(monitoring)}
}
if len(tags) > 0 {
SpotFleetLaunchSpecification.TagSpecifications = []*ec2.SpotFleetTagSpecification{
&ec2.SpotFleetTagSpecification{
ResourceType: aws.String("instance"),
Tags: parseTags(tags),
},
}
}
LaunchSpecifications = append(LaunchSpecifications, &SpotFleetLaunchSpecification)
}
SpotFleetRequestConfig := ec2.SpotFleetRequestConfigData{
IamFleetRole: spotFleetRoleResponse.Role.Arn,
LaunchSpecifications: LaunchSpecifications,
TargetCapacity: aws.Int64(targetCapacity),
}
if spotPrice != "" {
SpotFleetRequestConfig.SpotPrice = aws.String(spotPrice)
}
if allocationStrategy != "" {
SpotFleetRequestConfig.AllocationStrategy = aws.String(allocationStrategy)
}
_, err = ec2I.RequestSpotFleet(&ec2.RequestSpotFleetInput{
SpotFleetRequestConfig: &SpotFleetRequestConfig,
})
t.Must(err)
}
var clustersAddSpotFleetCmd = &cobra.Command{
Use: "add-spot-fleet [cluster]",
Short: "Add a new Spot Fleet to informed cluster",
Args: cobra.ExactArgs(1),
Aliases: []string{"add-spotfleet", "add-sf", "addsf", "asf"},
Run: clustersAddSpotFleetRun,
PersistentPreRun: persistentPreRun,
}
func init() {
clustersCmd.AddCommand(clustersAddSpotFleetCmd)
flags := clustersAddSpotFleetCmd.Flags()
flags.SortFlags = false
flags.StringSliceVarP(&subnets, "subnet", "n", []string{}, requiredSpec+subnetsSpec)
flags.StringSliceVarP(&instanceTypes, "instance-type", "i", []string{}, requiredSpec+instanceTypesSpec)
flags.StringSliceVarP(&securityGroups, "security-group", "g", []string{}, requiredSpec+securityGroupsSpec)
flags.Int64VarP(&targetCapacity, "target-capacity", "c", 1, targetCapacitySpec)
flags.StringVar(&instanceProfile, "instance-profile", "ecsInstanceRole", instanceProfileSpec)
flags.StringVar(&spotFleetRole, "spot-fleet-role", "ecsSpotFleetRole", spotFleetRoleSpec)
flags.StringVarP(&allocationStrategy, "allocation-strategy", "s", "", allocationStrategySpec)
flags.StringVar(&platform, "platform", "linux", platformSpec)
flags.StringVar(&spotPrice, "spot-price", "", spotPriceSpec)
flags.BoolVar(&monitoring, "monitoring", false, monitoringSpec)
flags.StringVar(&kernelID, "kernel-id", "", kernelIDSpec)
flags.String("ami", "latest", `The Amazon EC2 Image (ID or tag 'Name')
The "latest" refers to the latest ECS Optimized published by the AWS`)
flags.BoolVar(&ebs, "ebs", false, ebsSpec)
flags.StringVarP(&key, "key", "k", "", keySpec)
flags.StringSliceVarP(&tags, "tag", "t", []string{}, tagsSpec)
clustersAddSpotFleetCmd.MarkFlagRequired("subnets")
clustersAddSpotFleetCmd.MarkFlagRequired("instance-types")
clustersAddSpotFleetCmd.MarkFlagRequired("security-groups")
}