-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
142 lines (118 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
package main
import (
"log"
"math/rand"
"time"
"github.com/GeertJohan/go.rice"
"github.com/blockassets/bam_agent/fetcher"
"github.com/blockassets/bam_agent/monitor"
"github.com/blockassets/bam_agent/service/agent"
"github.com/blockassets/bam_agent/service/miner"
"github.com/blockassets/bam_agent/service/miner/cgminer"
"github.com/blockassets/bam_agent/service/os"
"github.com/blockassets/bam_agent/tool"
"github.com/jpillora/overseer"
"go.uber.org/fx"
)
var (
cmdLine tool.CmdLine
monitorManager monitor.Manager
webManager *WebServer
)
const (
ghUser = "blockassets"
ghRepo = "bam_agent"
)
func setup(version agent.Version) {
rand.Seed(time.Now().UTC().UnixNano())
log.Printf("Agent version: %s ", version.V)
}
func webServer(ws *WebServer) {
webManager = ws
ws.Start()
}
func monitors(mgr monitor.Manager) {
monitorManager = mgr
mgr.Start()
}
func program(state overseer.State) {
cmdLineProvider := fx.Provide(func() tool.CmdLine {
return cmdLine
})
stateProvider := fx.Provide(func() overseer.State {
return state
})
staticRiceBox := fx.Provide(func() tool.StaticRiceBox {
return rice.MustFindBox("static")
})
confRiceBox := fx.Provide(func() tool.ConfRiceBox {
return rice.MustFindBox("conf")
})
app := fx.New(
cmdLineProvider,
stateProvider,
staticRiceBox,
confRiceBox,
agent.ConfigModule,
agent.VersionModule,
cgminer.ConfigModule,
miner.ClientModule,
miner.VersionModule,
os.MemInfoModule,
os.MinerModule,
os.NetInfoModule,
os.NetworkingModule,
os.NtpdateModule,
os.RebootModule,
os.StatRetrieverModule,
os.UptimeModule,
monitor.Module,
WebServerModule,
fx.Invoke(setup, webServer, monitors),
)
app.Run()
}
/*
main() gets called 2x when we use overseer. It first starts up a master process and then
starts the app again as a child process. This means that the command line arguments need to
be parsed twice. So, we cache them and then make them available to fx injection in the program() function
by creating a provider for them.
*/
func main() {
cmdLine = tool.NewCmdLine()
if cmdLine.NoUpdate {
program(overseer.State{
Address: cmdLine.Port,
GracefulShutdown: make(chan bool, 1),
})
} else {
overseerRun(cmdLine.Port)
}
}
func overseerRun(port string) {
interval := time.Duration(rand.Intn(5)+1) * time.Hour // within the next 6 hours
overseer.Run(overseer.Config{
Debug: true,
NoRestart: true, // We allow the OS to restart things
Program: program,
Address: port,
// The default is to check on startup, but we really just want to check in the next interval
// in order to prevent DDOS'ing the whole network if we restart all machines. I copied the overseer
// version of the github fetcher into this project and modified the logic there.
Fetcher: &fetcher.Github{
User: ghUser,
Repo: ghRepo,
Interval: interval,
},
// Try to prevent /reboot from being called during an upgrade
PreUpgrade: func(tempBinaryPath string) error {
if monitorManager != nil {
monitorManager.Stop()
}
if webManager != nil {
webManager.Stop()
}
return nil
},
})
}