This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
111 lines (94 loc) · 2.91 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
package main
import (
"flag"
"fmt"
"os"
"github.com/Sirupsen/logrus"
"github.com/rancher/event-subscriber/events"
"github.com/rancher/go-machine-service/dynamic"
"github.com/rancher/go-machine-service/handlers"
"github.com/rancher/go-machine-service/logging"
)
var (
GITCOMMIT = "HEAD"
)
var logger = logging.Logger()
func main() {
processCmdLineFlags()
logrus.SetLevel(logrus.DebugLevel)
logger.WithField("gitcommit", GITCOMMIT).Info("Starting go-machine-service...")
apiURL := os.Getenv("CATTLE_URL")
accessKey := os.Getenv("CATTLE_ACCESS_KEY")
secretKey := os.Getenv("CATTLE_SECRET_KEY")
ready := make(chan bool, 2)
done := make(chan error)
go func() {
eventHandlers := map[string]events.EventHandler{
"machinedriver.reactivate": handlers.ActivateDriver,
"machinedriver.activate": handlers.ActivateDriver,
"machinedriver.update": handlers.ActivateDriver,
"machinedriver.error": handlers.ErrorDriver,
"machinedriver.deactivate": handlers.DeactivateDriver,
"machinedriver.remove": handlers.RemoveDriver,
"ping": handlers.PingNoOp,
}
router, err := events.NewEventRouter("machine-service", 2000, apiURL, accessKey, secretKey,
nil, eventHandlers, "machineDriver", 250, events.DefaultPingConfig)
if err == nil {
err = router.Start(ready)
}
done <- err
}()
go func() {
eventHandlers := map[string]events.EventHandler{
"host.provision": handlers.CreateMachineAndActivateMachine,
"host.remove": handlers.PurgeMachine,
"ping": handlers.PingNoOp,
}
router, err := events.NewEventRouter("machine-service", 2000, apiURL, accessKey, secretKey,
nil, eventHandlers, "host", 250, events.DefaultPingConfig)
if err == nil {
err = router.Start(ready)
}
done <- err
}()
go func() {
// Can not remove this as nothing will delete the handler entries
eventHandlers := map[string]events.EventHandler{
"ping": handlers.PingNoOp,
}
router, err := events.NewEventRouter("machine-service", 2000, apiURL, accessKey, secretKey,
nil, eventHandlers, "agent", 5, events.DefaultPingConfig)
if err == nil {
err = router.Start(ready)
}
done <- err
}()
go func() {
logger.Infof("Waiting for handler registration (1/2)")
<-ready
logger.Infof("Waiting for handler registration (2/2)")
<-ready
if err := dynamic.ReactivateOldDrivers(); err != nil {
logger.Fatalf("Error reactivating old drivers: %v", err)
}
if err := dynamic.DownloadAllDrivers(); err != nil {
logger.Fatalf("Error updating drivers: %v", err)
}
}()
err := <-done
if err == nil {
logger.Infof("Exiting go-machine-service")
} else {
logger.Fatalf("Exiting go-machine-service: %v", err)
}
}
func processCmdLineFlags() {
// Define command line flags
version := flag.Bool("v", false, "read the version of the go-machine-service")
flag.Parse()
if *version {
fmt.Printf("go-machine-service\t gitcommit=%s\n", GITCOMMIT)
os.Exit(0)
}
}