-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
88 lines (67 loc) · 1.79 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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
var conf Config
// Version represents the build version being used
var Version = "missing version string"
// ExpirationDate represents the date at which the version will expire
var ExpirationDate = "01-Jan-2100"
var eo *EBSOptimizer
var debug *log.Logger
// EBSOptimizer provides the global configuration
type EBSOptimizer struct {
config *Config
mainEC2Conn *ec2.Client
}
func main() {
eventFile := conf.EventFile
if runningFromLambda() {
lambda.Start(Handler)
} else if eventFile != "" {
parseEvent, err := ioutil.ReadFile(eventFile)
if err != nil {
log.Fatal(err)
}
Handler(context.TODO(), parseEvent)
} else {
eventHandler(nil)
}
}
func eventHandler(event *json.RawMessage) {
log.Println("Starting ebs-optimizer, build ", Version)
if isExpired(ExpirationDate) {
log.Println("EBS-Optimizer expired, please install a newer version.")
return
}
log.Printf("Configuration flags: %#v", conf)
eo.run(event)
log.Println("Execution completed, nothing left to do")
}
// this is the equivalent of a main for when running from Lambda, but on Lambda
// the runFromCronEvent() is executed within the handler function every time we have an event
func init() {
conf = Config{Version: Version}
conf.setupLogging()
log.Println("Determining configuration")
conf.ParseCommandlineFlags()
eo = &EBSOptimizer{}
eo.Init(&conf)
}
// Handler implements the AWS Lambda handler interface
func Handler(ctx context.Context, rawEvent json.RawMessage) {
eventHandler(&rawEvent)
}
func runningFromLambda() bool {
if os.Getenv("AWS_LAMBDA_FUNCTION_NAME") != "" {
log.Println("Running from Lambda")
return true
}
return false
}