-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspotprice.go
322 lines (282 loc) · 9.55 KB
/
spotprice.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
/*
spotprice.go
-John Taylor
2023-12-02
version 2
=========
* Now uses the AWS GO SDK v2
* Output all instance types as well as all regions, use -l
* You can now use regular expressions to match multiple instance types, use -I
* Shortcuts for products, use -prod
* * lin => Linux/UNIX
* * red => Red Hat Enterprise Linux
* * suse => SUSE Linux
* * win => Windows
This code is feature completed, but still needs a few more things such as:
* variable name refactoring for more consistency
* better function documentation
Examples
========
go run .\spotprice.go -I "c\d.xlarge" -max 0.2 -prod "lin" -reg "us-.*-1" -az "[bf]$"
go build -ldflags="-s -w" .\spotprice.go
.\spotprice.exe -I "c\d.xlarge" -max 0.38 -prod "lin,red" -reg "us-.*-1,eu-west.*" -az "[bdf]$" -prof someone
*/
package main
import (
"context"
"flag"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/olekukonko/tablewriter"
"os"
"regexp"
"slices"
"sort"
"strings"
"sync"
"time"
)
const pgmName string = "spotprice"
const pgmVersion string = "2.0.0"
const pgmUrl = "https://github.com/jftuga/spotprice"
const pgmDescription = "Quickly get AWS spot instance pricing across multiple regions"
type pgmArgs struct {
Profile string
Region string
AZ string
Inst string
InstRE string
Prod string
MaxPrice float64
}
type spotInfo struct {
Region string
AZ string
Instance string
Product string
Price string
}
// when given a string slice (hackstack),
// only return items that match a regular expression (needle)
func matcher(haystack []string, needles string) []string {
allRegExprs := strings.Split(needles, ",")
var found []string
for _, a := range haystack {
for _, regExpr := range allRegExprs {
re, err := regexp.Compile(regExpr)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nregexp: %s\n%s\n", regExpr, err)
os.Exit(1)
}
if re.MatchString(a) {
found = append(found, a)
}
}
}
return found
}
func getTime() *time.Time {
utc, _ := time.LoadLocation("UTC")
now := time.Now().In(utc)
return &now
}
func processArgs() (pgmArgs, bool) {
var usedArgs pgmArgs
argsVersion := flag.Bool("v", false, "show program version and then exit")
argsList := flag.Bool("l", false, "List regions & instance types, then exit")
flag.StringVar(&usedArgs.Profile, "prof", "default", "AWS profile to use")
flag.StringVar(&usedArgs.Region, "reg", "", "A comma-separated list of regular-expressions to match regions, eg: us-.*-2,ap-.*east-\\d")
flag.StringVar(&usedArgs.AZ, "az", "", "A comma-separated list of regular-expressions to match AZs (eg: us-*1a)")
flag.StringVar(&usedArgs.Inst, "inst", "", "A comma-separated list of exact Instance Type names, eg: t2.small,t3a.micro,c5.large")
flag.StringVar(&usedArgs.InstRE, "I", "", "A comma-separated list of regular-expressions to match Instance Type names, eg: t2.*,c5(a\\.|n\\.|\\.)4xlarge")
flag.StringVar(&usedArgs.Prod, "prod", "", "A comma-separated list of exact, case-sensitive Product Names (eg: Windows,win,Linux/UNIX,lin,SUSE Linux,Red Hat Enterprise Linux)")
flag.Float64Var(&usedArgs.MaxPrice, "max", 0.00, "Only output if spot price is less than or equal to given amount")
flag.Usage = func() {
pgmName := os.Args[0]
if strings.HasPrefix(os.Args[0], "./") {
pgmName = os.Args[0][2:]
}
fmt.Fprintf(os.Stderr, "\n\n%s: %s\n\n", pgmName, pgmDescription)
fmt.Fprintf(os.Stderr, "usage: %s [options]\n", pgmName)
fmt.Fprintf(os.Stderr, " (required EC2 IAM Permissions: DescribeRegions, DescribeAvailabilityZones, DescribeSpotPriceHistory)\n\n")
flag.PrintDefaults()
}
flag.Parse()
if 1 == len(os.Args) {
flag.Usage()
os.Exit(1)
}
if *argsVersion {
fmt.Printf("%v v%v\n%v\n", pgmName, pgmVersion, pgmUrl)
os.Exit(0)
}
return usedArgs, *argsList
}
func getRegionList(region, profile string, listOnly bool) []string {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region), config.WithSharedConfigProfile(profile))
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(255)
}
ctx := context.TODO()
ec2Client := ec2.NewFromConfig(cfg)
regionInfo, err := ec2Client.DescribeRegions(ctx, &ec2.DescribeRegionsInput{AllRegions: aws.Bool(false)})
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(255)
}
allRegions := make([]string, len(regionInfo.Regions))
for i, regionName := range regionInfo.Regions {
allRegions[i] = *regionName.RegionName
}
sort.Strings(allRegions)
if listOnly {
for _, region := range allRegions {
fmt.Printf("%v\n", region)
}
}
return allRegions
}
// return a sorted list of all EC2 Instance Types
// this make at least 6 AWS API calls, for pagination
func getAllInstanceTypes(region, profile string, listOnly bool) []string {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region), config.WithSharedConfigProfile(profile))
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(255)
}
ctx := context.TODO()
ec2Client := ec2.NewFromConfig(cfg)
var allInstanceNames []string
paginator := ec2.NewDescribeInstanceTypesPaginator(ec2Client, &ec2.DescribeInstanceTypesInput{})
for paginator.HasMorePages() {
instanceTypeInfo, err := paginator.NextPage(ctx)
//fmt.Printf("PAGE: %v\n", len(instanceTypeInfo.InstanceTypes))
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(255)
}
for _, i := range instanceTypeInfo.InstanceTypes {
allInstanceNames = append(allInstanceNames, string(i.InstanceType))
}
}
sort.Strings(allInstanceNames)
if listOnly {
for _, name := range allInstanceNames {
fmt.Printf("%v\n", name)
}
}
return allInstanceNames
}
func getRegionalSpotInfo(region, profile, commaDelimitedInstances, commaDelimitedProducts, commaDelimitedAZ, maxPrice string, all map[string][]spotInfo) {
allInstances := strings.Split(commaDelimitedInstances, ",")
allProducts := strings.Split(commaDelimitedProducts, ",")
for i := range allProducts {
switch allProducts[i] {
case "lin":
allProducts[i] = "Linux/UNIX"
case "red":
allProducts[i] = "Red Hat Enterprise Linux"
case "suse":
allProducts[i] = "SUSE Linux"
case "win":
allProducts[i] = "Windows"
}
}
var allInstanceTypes []types.InstanceType
for _, instance := range allInstances {
allInstanceTypes = append(allInstanceTypes, types.InstanceType(instance))
}
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region), config.WithSharedConfigProfile(profile))
if err != nil {
fmt.Fprintf(os.Stderr, "\n%v\n", err)
os.Exit(255)
}
ctx := context.TODO()
ec2Client := ec2.NewFromConfig(cfg)
input := ec2.DescribeSpotPriceHistoryInput{InstanceTypes: allInstanceTypes, ProductDescriptions: allProducts, StartTime: getTime()}
v, err := ec2Client.DescribeSpotPriceHistory(ctx, &input)
for _, info := range v.SpotPriceHistory {
if *info.SpotPrice > maxPrice && maxPrice != "-1" {
continue
}
var matchedAZ []string
if len(commaDelimitedAZ) > 0 {
matchedAZ = matcher([]string{*info.AvailabilityZone}, commaDelimitedAZ)
}
if len(commaDelimitedAZ) == 0 || (len(commaDelimitedAZ) > 0 && slices.Contains(matchedAZ, *info.AvailabilityZone)) {
all[region] = append(all[region], spotInfo{region, *info.AvailabilityZone, string(info.InstanceType), string(info.ProductDescription), *info.SpotPrice})
}
}
}
func sortResults(allRegionSpotInfo map[string][]spotInfo) []spotInfo {
var mergedSpotInfo []spotInfo
for region := range allRegionSpotInfo {
for _, info := range allRegionSpotInfo[region] {
mergedSpotInfo = append(mergedSpotInfo, info)
}
}
sort.SliceStable(mergedSpotInfo, func(i, j int) bool {
if mergedSpotInfo[i].AZ < mergedSpotInfo[j].AZ {
return true
} else {
return false
}
})
sort.SliceStable(mergedSpotInfo, func(i, j int) bool {
if mergedSpotInfo[i].Price < mergedSpotInfo[j].Price {
return true
} else {
return false
}
})
return mergedSpotInfo
}
func main() {
args, listOnly := processArgs()
if listOnly {
getRegionList(args.Region, args.Profile, listOnly)
fmt.Println("---")
getAllInstanceTypes(args.Region, args.Profile, true)
return
}
var matchedRegions []string
if len(args.Region) > 0 {
matchedRegions = matcher(getRegionList("us-east-1", args.Profile, false), args.Region)
} else {
matchedRegions = getRegionList("us-east-1", args.Profile, false)
}
var matchedInstanceNames string
if len(args.InstRE) > 0 {
matchedInstanceNames = strings.Join(matcher(getAllInstanceTypes("us-east-1", args.Profile, false), args.InstRE), ",")
} else {
matchedInstanceNames = args.Inst
}
var maxPrice string
if args.MaxPrice > 0 {
maxPrice = fmt.Sprintf("%f", args.MaxPrice)
} else {
maxPrice = "-1"
}
allRegionSpotInfo := make(map[string][]spotInfo)
var wg sync.WaitGroup
for _, region := range matchedRegions {
wg.Add(1)
allRegionSpotInfo[region] = []spotInfo{}
go func(region, profile, inst, allProducts, filteredAZ, maxPrice string, allRegionSpotInfo map[string][]spotInfo) {
defer wg.Done()
getRegionalSpotInfo(region, profile, inst, allProducts, filteredAZ, maxPrice, allRegionSpotInfo)
}(region, args.Profile, matchedInstanceNames, args.Prod, args.AZ, maxPrice, allRegionSpotInfo)
}
wg.Wait()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Region", "AZ", "Instance", "Product", "Spot Price"})
sortedSportInfoResults := sortResults(allRegionSpotInfo)
for _, info := range sortedSportInfoResults {
table.Append([]string{string(info.Region), string(info.AZ), string(info.Instance), string(info.Product), string(info.Price)})
}
table.Render()
}