This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
114 lines (98 loc) · 3.06 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
package main
import (
"os"
"path"
"strings"
bicmd "github.com/cloudfoundry/bosh-init/cmd"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshlogfile "github.com/cloudfoundry/bosh-utils/logger/file"
boshsys "github.com/cloudfoundry/bosh-utils/system"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
"github.com/pivotal-golang/clock"
"os/signal"
"syscall"
bilog "github.com/cloudfoundry/bosh-init/logger"
biui "github.com/cloudfoundry/bosh-init/ui"
biuifmt "github.com/cloudfoundry/bosh-init/ui/fmt"
)
const mainLogTag = "main"
func main() {
logger := newLogger()
defer logger.HandlePanic("Main")
fileSystem := boshsys.NewOsFileSystemWithStrictTempRoot(logger)
workspaceRootPath := path.Join(os.Getenv("HOME"), ".bosh_init")
ui := biui.NewConsoleUI(logger)
timeService := clock.NewClock()
cmdFactory := bicmd.NewFactory(
fileSystem,
ui,
timeService,
logger,
boshuuid.NewGenerator(),
workspaceRootPath,
)
cmdRunner := bicmd.NewRunner(cmdFactory)
stage := biui.NewStage(ui, timeService, logger)
err := cmdRunner.Run(stage, os.Args[1:]...)
if err != nil {
displayHelpFunc := func() {
if strings.Contains(err.Error(), "Invalid usage") {
ui.ErrorLinef("")
helpErr := cmdRunner.Run(stage, append([]string{"help"}, os.Args[1:]...)...)
if helpErr != nil {
logger.Error(mainLogTag, "Couldn't print help: %s", helpErr.Error())
}
}
}
fail(err, ui, logger, displayHelpFunc)
}
}
func newLogger() boshlog.Logger {
logLevelString := os.Getenv("BOSH_INIT_LOG_LEVEL")
level := boshlog.LevelNone
if logLevelString != "" {
var err error
level, err = boshlog.Levelify(logLevelString)
if err != nil {
err = bosherr.WrapError(err, "Invalid BOSH_INIT_LOG_LEVEL value")
logger := boshlog.NewLogger(boshlog.LevelError)
ui := biui.NewConsoleUI(logger)
fail(err, ui, logger, nil)
}
}
logPath := os.Getenv("BOSH_INIT_LOG_PATH")
if logPath != "" {
return newSignalableFileLogger(logPath, level)
}
return newSignalableLogger(boshlog.NewLogger(level))
}
func newSignalableLogger(logger boshlog.Logger) boshlog.Logger {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP)
signalableLogger, _ := bilog.NewSignalableLogger(logger, c)
return signalableLogger
}
func newSignalableFileLogger(logPath string, level boshlog.LogLevel) boshlog.Logger {
// Log file logger errors to the STDERR logger
logger := boshlog.NewLogger(boshlog.LevelError)
fileSystem := boshsys.NewOsFileSystem(logger)
// log file will be closed by process exit
// log file readable by all
logfileLogger, _, err := boshlogfile.New(level, logPath, boshlogfile.DefaultLogFileMode, fileSystem)
if err != nil {
logger := boshlog.NewLogger(boshlog.LevelError)
ui := biui.NewConsoleUI(logger)
fail(err, ui, logger, nil)
}
return newSignalableLogger(logfileLogger)
}
func fail(err error, ui biui.UI, logger boshlog.Logger, callback func()) {
logger.Error(mainLogTag, err.Error())
ui.ErrorLinef("")
ui.ErrorLinef(biuifmt.MultilineError(err))
if callback != nil {
callback()
}
os.Exit(1)
}