This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
forked from yeo/ec2.shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spot.go
175 lines (144 loc) · 3.81 KB
/
spot.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
AWSSpotPriceUrl = "https://website.spot.ec2.aws.a2z.com/spot.js"
)
type SpotPrice struct {
Linux *float64
MSWin *float64
}
type SpotValueColumn struct {
Name string `json:"name"`
RawPrice *RawPrice `json:"prices"`
}
type SpotInstanceTypeSize struct {
Size string `json:"size"`
ValueColumns []*SpotValueColumn `json:"valueColumns"`
}
type SpotInstanceType struct {
Type string `json:"type"`
Sizes []*SpotInstanceTypeSize `json:"sizes"`
}
type SpotRegion struct {
Region string `json:"region"`
InstanceTypes []*SpotInstanceType `json:"instanceTypes"`
}
type SpotPriceResponse struct {
Rate string `json:"rate"`
Regions []*SpotRegion `json:"regions"`
}
type SpotPriceResponseWrap struct {
Config *SpotPriceResponse `json:"config"`
}
type SpotPriceCrawler struct {
client *http.Client
Done chan bool
pricePerRegions map[string]map[string]*SpotPrice
}
func NewSpotPriceCrawler() *SpotPriceCrawler {
client := &http.Client{
Timeout: 30 * time.Second,
}
s := SpotPriceCrawler{
client: client,
Done: make(chan bool),
pricePerRegions: make(map[string]map[string]*SpotPrice),
}
return &s
}
func (s *SpotPriceCrawler) Fetch() error {
t0 := time.Now()
resp, err := s.client.Get(AWSSpotPriceUrl)
if err != nil {
fmt.Println("Error fetching spot price", err)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Fail to read response body", err)
return err
}
body = body[9 : len(body)-2]
var priceWrap SpotPriceResponseWrap
err = json.Unmarshal(body, &priceWrap)
if err != nil {
fmt.Println("Cannot parse json from spot request response", err)
return err
}
price := priceWrap.Config
for _, r := range price.Regions {
s.pricePerRegions[r.Region] = make(map[string]*SpotPrice)
for _, t := range r.InstanceTypes {
for _, size := range t.Sizes {
s.pricePerRegions[r.Region][size.Size] = &SpotPrice{}
for _, vc := range size.ValueColumns {
if vc.RawPrice.USD == "N/A*" {
continue
}
if vc.Name == "linux" {
if v, err := vc.RawPrice.Price(); err == nil {
s.pricePerRegions[r.Region][size.Size].Linux = new(float64)
*s.pricePerRegions[r.Region][size.Size].Linux = v
}
}
if vc.Name == "mswin" {
if v, err := vc.RawPrice.Price(); err == nil {
s.pricePerRegions[r.Region][size.Size].MSWin = new(float64)
*s.pricePerRegions[r.Region][size.Size].MSWin = v
}
}
}
}
}
}
//fmt.Printf("Spot Price %+v", s.pricePerRegions)
fmt.Println("Fetched spot price in", time.Now().Sub(t0), "at", time.Now())
return nil
}
func (s *SpotPriceCrawler) SpotRegionName(region string) string {
// The AWS API we're using has some funky names for some regions; e.g. eu-west-1 is referred to as eu-ireland
// This function maps an "actual" region name to the one in this API call
spotRegionMap := map[string]string{
"us-east-1": "us-east",
"us-west-1": "us-west",
"eu-west-1": "eu-ireland",
"ap-southeast-1": "apac-sin",
"ap-southeast-2": "apac-syd",
"ap-northeast-1": "apac-tokyo",
}
spotRegionName, found := spotRegionMap[region]
if found {
return spotRegionName
} else {
return region
}
}
func (s *SpotPriceCrawler) PriceForInstance(region string, instanceType string) (*SpotPrice, error) {
m := s.pricePerRegions[s.SpotRegionName(region)][instanceType]
if m == nil {
return nil, errors.New("Invalid instance type or region")
}
return m, nil
}
func (s *SpotPriceCrawler) Run() {
ticker := time.NewTicker(150 * time.Second)
go func() {
for {
select {
case <-s.Done:
return
case <-ticker.C:
s.Fetch()
}
}
}()
s.Fetch()
}