forked from notaryproject/notary
/
main.go
98 lines (80 loc) · 2.53 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
package main
import (
_ "expvar"
"flag"
"log"
"net/http"
"os"
"os/signal"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
"github.com/theupdateframework/notary/utils"
"github.com/theupdateframework/notary/version"
)
const (
jsonLogFormat = "json"
debugAddr = "localhost:8080"
)
type cmdFlags struct {
debug bool
logFormat string
configFile string
doBootstrap bool
}
func setupFlags(flagStorage *cmdFlags) {
// Setup flags
flag.StringVar(&flagStorage.configFile, "config", "", "Path to configuration file")
flag.BoolVar(&flagStorage.debug, "debug", false, "Run in debug mode, enables Go debug server")
flag.StringVar(&flagStorage.logFormat, "logf", "json", "Set the format of the logs. Only 'json' and 'logfmt' are supported at the moment.")
flag.BoolVar(&flagStorage.doBootstrap, "bootstrap", false, "Do any necessary setup of configured backend storage services")
// this needs to be in init so that _ALL_ logs are in the correct format
if flagStorage.logFormat == jsonLogFormat {
logrus.SetFormatter(new(logrus.JSONFormatter))
}
flag.Usage = usage
}
func main() {
flagStorage := cmdFlags{}
setupFlags(&flagStorage)
flag.Parse()
if flagStorage.debug {
go debugServer(debugAddr)
} else {
// If not in debug mode, stop tracing, core dumps if supported to help protect keys.
if err := protect(); err != nil {
logrus.Fatal(err.Error())
}
}
// when the signer starts print the version for debugging and issue logs later
logrus.Infof("Version: %s, Git commit: %s", version.NotaryVersion, version.GitCommit)
signerConfig, err := parseSignerConfig(flagStorage.configFile, flagStorage.doBootstrap)
if err != nil {
logrus.Fatal(err.Error())
}
grpcServer, lis, err := setupGRPCServer(signerConfig)
if err != nil {
logrus.Fatal(err.Error())
}
if flagStorage.debug {
log.Println("RPC server listening on", signerConfig.GRPCAddr)
}
c := utils.SetupSignalTrap(utils.LogLevelSignalHandle)
if c != nil {
defer signal.Stop(c)
}
grpcServer.Serve(lis)
}
func usage() {
log.Println("usage:", os.Args[0], "<config>")
flag.PrintDefaults()
}
// debugServer starts the debug server with pprof, expvar among other
// endpoints. The addr should not be exposed externally. For most of these to
// work, tls cannot be enabled on the endpoint, so it is generally separate.
func debugServer(addr string) {
logrus.Infof("Debug server listening on %s", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
logrus.Fatalf("error listening on debug interface: %v", err)
}
}