-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (92 loc) · 2.5 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
//go:generate goversioninfo
package main
import (
"flag"
"log"
"os"
"embed"
"github.com/kardianos/service"
"github.com/rechecked/rcagent/internal/server"
"github.com/rechecked/rcagent/internal/sender"
"github.com/rechecked/rcagent/internal/config"
)
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 {
// Set up server configuration and run
go server.Run(logger)
// If we have a sender (passive checks)
go sender.Run()
// Do work here
return nil
}
func (p *program) Stop(s service.Service) error {
// Stop should not block. Return with a few seconds.
return nil
}
var logger service.Logger
//go:embed build/package/config.yml
var defaultConfigFile 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")
version := flag.Bool("v", false, "Show version of rcagent")
flag.Parse()
// Show version and quit
if *version {
log.Printf("ReChecked Agent, version: %s\n", config.Version)
os.Exit(0)
}
var deps []string
if runtime.GOOS == "linux" {
deps = []string{
"Requires=network.target",
"After=network-online.target syslog.target",
}
}
svcConfig := &service.Config{
Name: "rcagent",
DisplayName: "RCAgent",
Description: "ReChecked system status and monitoring agent",
Dependencies: deps,
}
// Initialize config settings (no config.yml on install)
if *action == "run" {
err := config.ParseFile(*configFile, defaultConfigFile)
if err != nil {
log.Fatal(err)
}
}
// Initialize service
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
// Initialize service logger
logger, err = s.Logger(nil)
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 {
logger.Error(err)
os.Exit(1)
}
}