forked from rqlite/rqlite
/
main.go
160 lines (135 loc) · 3.88 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
/*
rqlite -- a replicated SQLite database.
rqlite is a distributed system that provides a replicated SQLite database.
rqlite is written in Go and uses Raft to achieve consensus across all the
instances of the SQLite databases. rqlite ensures that every change made to
the database is made to a majority of underlying SQLite files, or none-at-all.
*/
package main
import (
"bytes"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"time"
"github.com/otoolep/rqlite/log"
"github.com/otoolep/rqlite/server"
)
var host string
var port int
var join string
var dbfile string
var cpuprofile string
var logFile string
var logLevel string
var snapAfter int
var disableReporting bool
func init() {
flag.StringVar(&host, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 4001, "port")
flag.StringVar(&join, "join", "", "host:port of leader to join")
flag.StringVar(&dbfile, "dbfile", "db.sqlite", "sqlite filename")
flag.StringVar(&cpuprofile, "cpuprofile", "", "write CPU profile to file")
flag.StringVar(&logFile, "logfile", "stdout", "log file path")
flag.StringVar(&logLevel, "loglevel", "INFO", "log level (ERROR|WARN|INFO|DEBUG|TRACE)")
flag.IntVar(&snapAfter, "s", 100, "Snapshot and compact after this number of new log entries")
flag.BoolVar(&disableReporting, "noreport", false, "Disable anonymised launch reporting")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [arguments] <data-path> \n", os.Args[0])
flag.PrintDefaults()
}
}
func main() {
flag.Parse()
// Set up profiling, if requested.
if cpuprofile != "" {
log.Info("Profiling enabled")
f, err := os.Create(cpuprofile)
if err != nil {
log.Errorf("Unable to create path: %s", err.Error())
}
defer closeFile(f)
err = pprof.StartCPUProfile(f)
if err != nil {
log.Errorf("Unable to start CPU Profile: %s", err.Error())
}
defer pprof.StopCPUProfile()
}
// Set logging
log.SetLevel(logLevel)
if logFile != "stdout" {
f := createFile(logFile)
defer closeFile(f)
log.Infof("Redirecting logging to %s", logFile)
log.SetOutput(f)
}
// Set the data directory.
if flag.NArg() == 0 {
flag.Usage()
println("Data path argument required")
log.Error("No data path supplied -- aborting")
os.Exit(1)
}
dataPath := flag.Arg(0)
createDir(dataPath)
s := server.NewServer(dataPath, dbfile, snapAfter, host, port)
go func() {
log.Error(s.ListenAndServe(join).Error())
}()
if !disableReporting {
reportLaunch()
}
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt)
<-terminate
log.Info("rqlite server stopped")
}
func closeFile(f *os.File) {
if err := f.Close(); err != nil {
log.Errorf("Unable to close file: %s", err.Error())
os.Exit(1)
}
}
func createFile(path string) *os.File {
usr, _ := user.Current()
dir := usr.HomeDir
// Check in case of paths like "/something/~/something/"
if path[:2] == "~/" {
path = strings.Replace(path, "~/", dir+"/", 1)
}
if err := os.MkdirAll(filepath.Dir(path), 0744); err != nil {
log.Errorf("Unable to create path: %s", err.Error())
os.Exit(1)
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil && !strings.Contains(err.Error(), "is a directory") {
log.Errorf("Unable to open file: %s", err.Error())
os.Exit(1)
}
return f
}
func createDir(path string) {
if err := os.MkdirAll(path, 0744); err != nil {
log.Errorf("Unable to create path: %s", err.Error())
os.Exit(1)
}
}
func reportLaunch() {
json := fmt.Sprintf(`{"os": "%s", "arch": "%s", "app": "rqlite"}`, runtime.GOOS, runtime.GOARCH)
data := bytes.NewBufferString(json)
client := http.Client{Timeout: time.Duration(5 * time.Second)}
go func() {
_, err := client.Post("https://logs-01.loggly.com/inputs/8a0edd84-92ba-46e4-ada8-c529d0f105af/tag/reporting/",
"application/json", data)
if err != nil {
log.Errorf("Report launch failed: %s", err.Error())
}
}()
}