forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (108 loc) · 3.1 KB
/
main.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
// Copyright (c) 2016-2019 Cristian Măgherușan-Stanciu
// Licensed under the Open Software License version 3.0
package autospotting
import (
"io/ioutil"
"log"
"os"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
var logger, debug *log.Logger
var hourlySavings float64
var savingsMutex = &sync.RWMutex{}
// Run starts processing all AWS regions looking for AutoScaling groups
// enabled and taking action by replacing more pricy on-demand instances with
// compatible and cheaper spot instances.
func Run(cfg *Config) {
setupLogging(cfg)
debug.Println(*cfg)
// use this only to list all the other regions
ec2Conn := connectEC2(cfg.MainRegion)
addDefaultFilteringMode(cfg)
addDefaultFilter(cfg)
allRegions, err := getRegions(ec2Conn)
if err != nil {
logger.Println(err.Error())
return
}
processRegions(allRegions, cfg)
}
func addDefaultFilteringMode(cfg *Config) {
if cfg.TagFilteringMode != "opt-out" {
debug.Printf("Configured filtering mode: '%s', considering it as 'opt-in'(default)\n",
cfg.TagFilteringMode)
cfg.TagFilteringMode = "opt-in"
} else {
debug.Println("Configured filtering mode: 'opt-out'")
}
}
func addDefaultFilter(cfg *Config) {
if len(strings.TrimSpace(cfg.FilterByTags)) == 0 {
switch cfg.TagFilteringMode {
case "opt-out":
cfg.FilterByTags = "spot-enabled=false"
default:
cfg.FilterByTags = "spot-enabled=true"
}
}
}
func setupLogging(cfg *Config) {
logger = log.New(cfg.LogFile, "", cfg.LogFlag)
if os.Getenv("AUTOSPOTTING_DEBUG") == "true" {
debug = log.New(cfg.LogFile, "", cfg.LogFlag)
} else {
debug = log.New(ioutil.Discard, "", 0)
}
}
// 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 processRegions(regions []string, cfg *Config) {
var wg sync.WaitGroup
for _, r := range regions {
wg.Add(1)
r := region{name: r, conf: cfg}
go func() {
if r.enabled() {
logger.Printf("Enabled to run in %s, processing region.\n", r.name)
r.processRegion()
} else {
debug.Println("Not enabled to run in", r.name)
debug.Println("List of enabled regions:", cfg.Regions)
}
wg.Done()
}()
}
wg.Wait()
}
func connectEC2(region string) *ec2.EC2 {
sess, err := session.NewSession()
if err != nil {
panic(err)
}
return ec2.New(sess,
aws.NewConfig().WithRegion(region))
}
// getRegions generates a list of AWS regions.
func getRegions(ec2conn ec2iface.EC2API) ([]string, error) {
var output []string
logger.Println("Scanning for available AWS regions")
resp, err := ec2conn.DescribeRegions(&ec2.DescribeRegionsInput{})
if err != nil {
logger.Println(err.Error())
return nil, err
}
debug.Println(resp)
for _, r := range resp.Regions {
if r != nil && r.RegionName != nil {
debug.Println("Found region", *r.RegionName)
output = append(output, *r.RegionName)
}
}
return output, nil
}