forked from cloudfoundry/bosh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
95 lines (79 loc) · 2.06 KB
/
agent.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
boshapp "github.com/cloudfoundry/bosh-agent/app"
"github.com/cloudfoundry/bosh-agent/infrastructure/agentlogger"
"github.com/cloudfoundry/bosh-agent/platform"
"github.com/cloudfoundry/bosh-utils/logger"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
const mainLogTag = "main"
func runAgent(opts boshapp.Options, logger logger.Logger) chan error {
errCh := make(chan error, 1)
go func() {
defer logger.HandlePanic("Main")
logger.Debug(mainLogTag, "Starting agent")
fs := boshsys.NewOsFileSystem(logger)
if opts.PlatformName == "dummy" {
fs = platform.DummyWrapFs(fs)
}
app := boshapp.New(logger, fs)
err := app.Setup(opts)
if err != nil {
logger.Error(mainLogTag, "App setup %s", err.Error())
errCh <- err
return
}
err = app.Run()
if err != nil {
logger.Error(mainLogTag, "App run %s", err.Error())
errCh <- err
return
}
}()
return errCh
}
func startAgent(logger logger.Logger) error {
opts, err := boshapp.ParseOptions(os.Args)
if err != nil {
logger.Error(mainLogTag, "Parsing options %s", err.Error())
return err
}
if opts.VersionCheck {
fmt.Println(VersionLabel)
os.Exit(0)
}
sigCh := make(chan os.Signal, 8)
signal.Notify(sigCh, syscall.SIGTERM, os.Interrupt, os.Kill)
errCh := runAgent(opts, logger)
for {
select {
case sig := <-sigCh:
return fmt.Errorf("received signal (%s): stopping now", sig)
case err := <-errCh:
return err
}
}
}
func main() {
asyncLog := boshlog.NewAsyncWriterLogger(boshlog.LevelDebug, os.Stderr)
logger := newSignalableLogger(asyncLog)
exitCode := 0
if err := startAgent(logger); err != nil {
logger.Error(mainLogTag, "Agent exited with error: %s", err)
exitCode = 1
}
logger.FlushTimeout(time.Minute)
os.Exit(exitCode)
}
func newSignalableLogger(logger logger.Logger) logger.Logger {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGSEGV)
signalableLogger, _ := agentlogger.NewSignalableLogger(logger, c)
return signalableLogger
}