forked from LeanerCloud/AutoSpotting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautospotting.go
More file actions
126 lines (99 loc) · 3.2 KB
/
Copy pathautospotting.go
File metadata and controls
126 lines (99 loc) · 3.2 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/cristim/autospotting/core"
"github.com/cristim/ec2-instances-info"
"github.com/eawsy/aws-lambda-go-core/service/lambda/runtime"
"github.com/namsral/flag"
)
type cfgData struct {
*autospotting.Config
}
var conf *cfgData
//These variables are set by the build system using some ldflags parameters.
var Version string
var ExpirationDate string
func main() {
run()
}
func run() {
log.Println("Starting autospotting agent, build ", Version, "expiring on", ExpirationDate)
if isExpired(ExpirationDate) {
log.Fatalln("Autospotting expired, please install a newer version.")
return
}
log.Printf("Parsed command line flags: "+
"regions='%s' "+
"min_on_demand_number=%d "+
"min_on_demand_percentage=%.1f "+
"allowed_instance_types=%v",
conf.Regions,
conf.MinOnDemandNumber,
conf.MinOnDemandPercentage,
conf.AllowedInstanceTypes)
autospotting.Run(conf.Config)
log.Println("Execution completed, nothing left to do")
}
// this is the equivalent of a main for when running from Lambda, but on Lambda
// the run() is executed within the handler function every time we have an event
func init() {
var region string
if r := os.Getenv("AWS_REGION"); r != "" {
region = r
} else {
region = endpoints.UsEast1RegionID
}
conf = &cfgData{
&autospotting.Config{
LogFile: os.Stdout,
LogFlag: log.Ldate | log.Ltime | log.Lshortfile,
MainRegion: region,
SleepMultiplier: 1,
},
}
conf.initialize()
}
// Handle implements the AWS Lambda handler
func Handle(evt json.RawMessage, ctx *runtime.Context) (interface{}, error) {
run()
return nil, nil
}
// Configuration handling
func (c *cfgData) initialize() {
c.parseCommandLineFlags()
data, err := ec2instancesinfo.Data()
if err != nil {
log.Fatal(err.Error())
}
c.InstanceData = data
}
func (c *cfgData) parseCommandLineFlags() {
flag.StringVar(&c.Regions, "regions", "",
"Regions where it should be activated (comma or whitespace separated list, "+
"also supports globs), by default it runs on all regions.\n\t"+
"Example: ./autospotting -regions 'eu-*,us-east-1'")
flag.Int64Var(&c.MinOnDemandNumber, "min_on_demand_number", 0,
"On-demand capacity (as absolute number) ensured to be running in each of your groups.\n\t"+
"Can be overridden on a per-group basis using the tag "+
autospotting.OnDemandNumberLong)
flag.Float64Var(&c.MinOnDemandPercentage, "min_on_demand_percentage", 0.0,
"On-demand capacity (percentage of the total number of instances in the group) "+
"ensured to be running in each of your groups.\n\t"+
"Can be overridden on a per-group basis using the tag "+
autospotting.OnDemandPercentageLong+
"\n\tIt is ignored if min_on_demand_number is also set.")
flag.StringVar(&c.AllowedInstanceTypes, "allowed_instance_types", "",
"If specified, the spot instances will have a specific instance type:\n"+
"\tcurrent: the same as initial on-demand instances\n"+
"\t<instance-type>: the actual instance type to use")
v := flag.Bool("version", false, "Print version number and exit.")
flag.Parse()
if *v {
fmt.Println("AutoSpotting build:", Version)
os.Exit(0)
}
}