-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgc-controller.go
89 lines (77 loc) · 2.47 KB
/
gc-controller.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
package main
import (
"fmt"
"github.com/crunchycookie/openstack-gc/gc-controller/internal/configs"
"github.com/crunchycookie/openstack-gc/gc-controller/internal/handler"
"github.com/crunchycookie/openstack-gc/gc-controller/internal/model"
"github.com/crunchycookie/openstack-gc/gc-controller/internal/power"
"github.com/crunchycookie/openstack-gc/gc-controller/internal/serviceerror"
"github.com/gin-gonic/gin"
"log"
"os"
"os/signal"
"strconv"
)
func main() {
log.Println("loading service configurations...")
conf := loadConfigs()
log.Println("creating an api handler...")
apiHandler, err := getAPIHandler(conf)
if err != nil {
fmt.Println("Failed to create an API handler", err)
return
}
attachCleanUponShutdownHandler(apiHandler)
log.Println("configuring api routing...")
router := gin.Default()
router.Use(serviceerror.ErrorHandler())
router.GET("/gc-controller/sleep-info", apiHandler.GetSleepInfo)
router.PUT("/gc-controller/sleep", apiHandler.PutSleepOP)
router.PUT("/gc-controller/wake", apiHandler.PutAwakeOP)
router.PUT("/gc-controller/dev/perf", apiHandler.PutPoolFreq)
router.GET("/gc-controller/dev/green-score", apiHandler.GetGreenScore)
log.Println("begin serving...")
err = router.Run(conf.Host.Name + ":" + strconv.Itoa(conf.Host.Port))
if err != nil {
fmt.Println("Unable to start the gc-controller", err)
return
}
}
func attachCleanUponShutdownHandler(apiHandler *handler.SleepAPIHandler) {
// Set cleanup.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("[service quit signal received]")
cleanup(apiHandler)
os.Exit(0)
}()
}
func cleanup(handler *handler.SleepAPIHandler) {
log.Println("restoring core power management...")
err := handler.Clean()
if err != nil {
log.Fatal("unable to restore the system. there can be residue of core power management that might "+
"degrade hardware. consider rebooting to properly hand-over power management to the os. ", err)
}
log.Println("power management safely handed over to the os. Goodbye!")
}
func getAPIHandler(conf *model.ConfYaml) (*handler.SleepAPIHandler, error) {
controller, err := power.NewSleepController(conf)
if err != nil {
return nil, fmt.Errorf("failed to initialize sleep controller: %w", err)
}
sleepHandler := handler.SleepAPIHandler{
Controller: controller,
}
return &sleepHandler, nil
}
func loadConfigs() *model.ConfYaml {
path := ""
if len(os.Args) > 1 {
path = os.Args[1]
}
conf := configs.NewConfigs(path)
return conf
}