-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.go
158 lines (119 loc) · 3.52 KB
/
run.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
package main
import (
"context"
"encoding/json"
"log"
"strings"
"sync"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
// Init initializes some data structures reusable across multiple event runs
func (e *EBSOptimizer) Init(cfg *Config) {
e.config = cfg
e.config.setupLogging()
// use this only to list all the other regions
debug.Println("Connecting to EC2 in the main region", e.config.MainRegion)
e.mainEC2Conn = e.connectEC2(e.config.MainRegion)
eo = e
err := populateEBSPricing()
if err != nil {
log.Fatalf("failed to get EBS pricing information: %v", err)
}
}
func (e *EBSOptimizer) connectEC2(region string) *ec2.Client {
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
)
if err != nil {
panic(err)
}
return ec2.NewFromConfig(cfg)
}
func (e *EBSOptimizer) run(event *json.RawMessage) {
allRegions, err := e.getRegions()
if err != nil {
log.Println(err.Error())
return
}
e.processRegions(allRegions)
// Print Final Recap
log.Println("####### BEGIN FINAL RECAP #######")
for r, a := range e.config.FinalRecap {
for _, t := range a {
log.Printf("%s %s\n", r, t)
}
}
}
// getRegions generates a list of AWS regions.
func (e *EBSOptimizer) getRegions() ([]string, error) {
var output []string
debug.Println("Scanning for available AWS regions")
resp, err := e.mainEC2Conn.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
if err != nil {
log.Println(err.Error())
return nil, err
}
debug.Println(resp)
for _, r := range resp.Regions {
if r.RegionName != nil {
debug.Println("Found region", *r.RegionName)
output = append(output, *r.RegionName)
}
}
return output, nil
}
// processAllRegions iterates all regions in parallel, and replaces instances
// for each of the ASGs tagged with tags as specified by slice represented by cfg.FilterByTags
// by default this is all asg with the tag 'spot-enabled=true'.
func (e *EBSOptimizer) processRegions(regions []string) {
var wg sync.WaitGroup
var mutex sync.Mutex
var savings float64
for _, reg := range regions {
wg.Add(1)
r := region{name: reg, conf: e.config}
go func() {
debug.Println("Creating connections to the required AWS services in", r.name)
r.api.connect(r.name, r.conf.MainRegion)
r.scanEBSVolumes()
r.calculateHourlySavings()
if r.savings > 0 {
log.Printf("Calculated savings in %s: $%f(monthly), %f(hourly) ", r.name, r.savings*730, r.savings)
}
mutex.Lock()
savings += r.savings
mutex.Unlock()
wg.Done()
}()
}
wg.Wait()
log.Printf("Total savings: %f(monthly), %f(hourly)", savings*730, savings)
if strings.Contains(e.config.Version, "stable") {
log.Println("Running a stable build, submitting AWS marketplace metering data")
if err := meterMarketplaceUsage(savings); err != nil {
log.Println("Failed marketplace metering, exiting... Encountered error:", err.Error())
return
}
} else {
log.Println("Not running a stable build, skipped AWS marketplace metering")
}
for _, reg := range regions {
wg.Add(1)
r := region{name: reg, conf: e.config}
go func() {
debug.Println("Creating connections to the required AWS services in", r.name)
r.api.connect(r.name, r.conf.MainRegion)
r.scanEBSVolumes()
if r.enabled() {
log.Printf("Enabled to run in %s, processing region.\n", r.name)
r.processEBSVolumes()
} else {
debug.Println("Not enabled to run in", r.name)
debug.Println("List of enabled regions:", r.conf.Regions)
}
wg.Done()
}()
}
wg.Wait()
}