-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
156 lines (127 loc) · 3.22 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//go:generate goversioninfo
package main
import (
"embed"
"flag"
"fmt"
"log"
"os"
"runtime"
"github.com/kardianos/service"
"github.com/rechecked/rcagent/internal/config"
"github.com/rechecked/rcagent/internal/manager"
"github.com/rechecked/rcagent/internal/sender"
"github.com/rechecked/rcagent/internal/server"
)
type program struct {
exit chan struct{}
}
func (p *program) Start(s service.Service) error {
// Start should not block. Do the actual work async.
go p.run()
return nil
}
func (p *program) run() error {
// Register with the manager on startup
go manager.Register()
// Set up server configuration and run
server.Setup()
c := make(chan struct{})
go runServer(c)
// If we have a sender (passive checks)
go sender.Run()
// Connect to the manager for sync
go manager.Run(c)
return nil
}
func runServer(c chan struct{}) {
restart := make(chan struct{})
go server.Run(restart)
<-c
restart <- struct{}{}
<-restart
go runServer(c)
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
//go:embed build/package/config.yml
var defaultConfigFile embed.FS
//go:embed VERSION
var defaultVersion embed.FS
func main() {
// All actions the service can perform
action := flag.String("a", "run", "Service action to run: 'install', 'uninstall', or 'run'. Default is 'run'.")
configFile := flag.String("f", "", "Config file location")
debugMode := flag.Bool("D", false, "Force debug mode")
version := flag.Bool("v", false, "Show version of rcagent")
machineId := flag.Bool("m", false, "Show the machineID for this system")
flag.Parse()
// Parse/set version then show if someone does -v
config.ParseVersion(defaultVersion)
if *version {
fmt.Printf("ReChecked Agent, version: %s\n", config.Version)
os.Exit(0)
}
// Display the machine id (useful for debugging/dev/testing)
if *machineId {
fmt.Printf("Machine ID: %s\n", manager.GetMachineId())
os.Exit(0)
}
var deps []string
name := "rcagent"
if runtime.GOOS == "linux" {
deps = []string{
"Requires=network.target",
"After=network-online.target syslog.target",
}
}
// Change name on macos to conform to macos
if runtime.GOOS == "darwin" {
name = "io.rechecked.rcagent"
}
svcConfig := &service.Config{
Name: name,
DisplayName: "rcagent",
Description: "ReChecked Agent - System status and monitoring agent",
Dependencies: deps,
}
// Initialize service
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
// Initialize service logger
config.Log, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
// Initialize config settings (no config.yml on install)
if *action == "run" {
config.DebugMode = *debugMode // Force debug mode on if we set it with -D
err := config.InitConfig(*configFile, defaultConfigFile)
if err != nil {
log.Fatal(err)
}
}
// Run actions for the service (run, install, uninstall)
switch *action {
case "install":
err = s.Install()
case "uninstall":
err = s.Uninstall()
default:
err = s.Run()
}
// Exit with error if we hit one
if err != nil {
config.Log.Error(err)
if *action == "install" || *action == "uninstall" {
os.Exit(0)
} else {
os.Exit(1)
}
}
}