forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
264 lines (222 loc) · 5.82 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"flag"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"time"
)
const logo = `
8888888 .d888 888 8888888b. 888888b.
888 d88P" 888 888 "Y88b 888 "88b
888 888 888 888 888 888 .88P
888 88888b. 888888 888 888 888 888 888 888 888 8888888K.
888 888 "88b 888 888 888 888 Y8bd8P' 888 888 888 "Y88b
888 888 888 888 888 888 888 X88K 888 888 888 888
888 888 888 888 888 Y88b 888 .d8""8b. 888 .d88P 888 d88P
8888888 888 888 888 888 "Y88888 888 888 8888888P" 8888888P"
`
// These variables are populated via the Go linker.
var (
version string = "0.9"
commit string
)
// Various constants used by the main package.
const (
messagingClientFile string = "messaging"
)
func main() {
log.SetFlags(0)
rand.Seed(time.Now().UnixNano())
// If commit not set, make that clear.
if commit == "" {
commit = "unknown"
}
// Shift binary name off argument list.
args := os.Args[1:]
// Retrieve command name as first argument.
var cmd string
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
cmd = args[0]
}
// Special case -h immediately following binary name
if len(args) > 0 && args[0] == "-h" {
cmd = "help"
}
// If command is "help" and has an argument then rewrite args to use "-h".
if cmd == "help" && len(args) > 1 {
args[0], args[1] = args[1], "-h"
cmd = args[0]
}
// Extract name from args.
switch cmd {
case "run":
execRun(args[1:])
case "":
execRun(args)
case "backup":
cmd := NewBackupCommand()
if err := cmd.Run(args[1:]...); err != nil {
log.Fatalf("backup: %s", err)
}
case "restore":
cmd := NewRestoreCommand()
if err := cmd.Run(args[1:]...); err != nil {
log.Fatalf("restore: %s", err)
}
case "version":
execVersion(args[1:])
case "config":
execConfig(args[1:])
case "help":
execHelp(args[1:])
default:
log.Fatalf(`influxd: unknown command "%s"`+"\n"+`Run 'influxd help' for usage`+"\n\n", cmd)
}
}
// execRun runs the "run" command.
func execRun(args []string) {
// Parse command flags.
fs := flag.NewFlagSet("", flag.ExitOnError)
var (
configPath = fs.String("config", "", "")
pidPath = fs.String("pidfile", "", "")
hostname = fs.String("hostname", "", "")
join = fs.String("join", "", "")
cpuprofile = fs.String("cpuprofile", "", "")
memprofile = fs.String("memprofile", "", "")
)
fs.Usage = printRunUsage
fs.Parse(args)
// Start profiling, if set.
startProfiling(*cpuprofile, *memprofile)
defer stopProfiling()
// Print sweet InfluxDB logo and write the process id to file.
log.Print(logo)
log.SetPrefix(`[srvr] `)
log.SetFlags(log.LstdFlags)
writePIDFile(*pidPath)
// Parse configuration file from disk.
config, err := parseConfig(*configPath, *hostname)
if err != nil {
log.Fatal(err)
} else if *configPath == "" {
log.Println("No config provided, using default settings")
}
// Create a logging writer.
logWriter := os.Stderr
if config.Logging.File != "" {
var err error
logWriter, err = os.OpenFile(config.Logging.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0660)
if err != nil {
log.Fatalf("unable to open log file %s: %s", config.Logging.File, err.Error())
}
}
log.SetOutput(logWriter)
Run(config, *join, version, logWriter)
// Wait indefinitely.
<-(chan struct{})(nil)
}
// execVersion runs the "version" command.
// Prints the commit SHA1 if set by the build process.
func execVersion(args []string) {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.Usage = func() {
log.Println(`usage: version
version displays the InfluxDB version and build git commit hash
`)
}
fs.Parse(args)
s := fmt.Sprintf("InfluxDB v%s", version)
if commit != "" {
s += fmt.Sprintf(" (git: %s)", commit)
}
log.Print(s)
}
// execConfig parses and prints the current config loaded.
func execConfig(args []string) {
// Parse command flags.
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.Usage = func() {
log.Println(`usage: config
config displays the default configiguration
`)
}
var (
configPath = fs.String("config", "", "")
hostname = fs.String("hostname", "", "")
)
fs.Parse(args)
config, err := parseConfig(*configPath, *hostname)
if err != nil {
log.Fatalf("parse config: %s", err)
}
config.Write(os.Stdout)
}
// execHelp runs the "help" command.
func execHelp(args []string) {
fmt.Println(`
Configure and start an InfluxDB server.
Usage:
influxd [[command] [arguments]]
The commands are:
config display the default configuration
join-cluster create a new node that will join an existing cluster
run run node with existing configuration
version displays the InfluxDB version
"run" is the default command.
Use "influxd help [command]" for more information about a command.
`)
}
type Stopper interface {
Stop()
}
type State struct {
Mode string `json:"mode"`
}
var prof struct {
cpu *os.File
mem *os.File
}
func startProfiling(cpuprofile, memprofile string) {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatalf("cpuprofile: %v", err)
}
prof.cpu = f
pprof.StartCPUProfile(prof.cpu)
}
if memprofile != "" {
f, err := os.Create(memprofile)
if err != nil {
log.Fatalf("memprofile: %v", err)
}
prof.mem = f
runtime.MemProfileRate = 4096
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
stopProfiling()
os.Exit(0)
}()
}
func stopProfiling() {
if prof.cpu != nil {
pprof.StopCPUProfile()
prof.cpu.Close()
}
if prof.mem != nil {
pprof.Lookup("heap").WriteTo(prof.mem, 0)
prof.mem.Close()
}
}
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }