-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
main.go
181 lines (158 loc) · 4.44 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
// The influx_tools command displays detailed information about InfluxDB data files.
package main
import (
"errors"
"fmt"
"io"
"os"
"time"
"github.com/influxdata/influxdb/cmd"
"github.com/influxdata/influxdb/cmd/influx_tools/compact"
"github.com/influxdata/influxdb/cmd/influx_tools/export"
genexec "github.com/influxdata/influxdb/cmd/influx_tools/generate/exec"
geninit "github.com/influxdata/influxdb/cmd/influx_tools/generate/init"
"github.com/influxdata/influxdb/cmd/influx_tools/help"
"github.com/influxdata/influxdb/cmd/influx_tools/importer"
"github.com/influxdata/influxdb/cmd/influx_tools/server"
"github.com/influxdata/influxdb/cmd/influxd/run"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/tsdb"
_ "github.com/influxdata/influxdb/tsdb/engine"
"go.uber.org/zap"
)
func main() {
m := NewMain()
if err := m.Run(os.Args[1:]...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Main represents the program execution.
type Main struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewMain returns a new instance of Main.
func NewMain() *Main {
return &Main{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// Run determines and runs the command specified by the CLI args.
func (m *Main) Run(args ...string) error {
name, args := cmd.ParseCommandName(args)
// Extract name from args.
switch name {
case "", "help":
if err := help.NewCommand().Run(args...); err != nil {
return fmt.Errorf("help failed: %s", err)
}
case "compact-shard":
c := compact.NewCommand()
if err := c.Run(args); err != nil {
return fmt.Errorf("compact-shard failed: %s", err)
}
case "export":
c := export.NewCommand(&ossServer{logger: zap.NewNop()})
if err := c.Run(args); err != nil {
return fmt.Errorf("export failed: %s", err)
}
case "import":
c := importer.NewCommand(&ossServer{logger: zap.NewNop()})
if err := c.Run(args); err != nil {
return fmt.Errorf("import failed: %s", err)
}
case "gen-init":
c := geninit.NewCommand(&ossServer{logger: zap.NewNop()})
if err := c.Run(args); err != nil {
return fmt.Errorf("gen-init failed: %s", err)
}
case "gen-exec":
deps := genexec.Dependencies{Server: &ossServer{logger: zap.NewNop()}}
c := genexec.NewCommand(deps)
if err := c.Run(args); err != nil {
return fmt.Errorf("gen-exec failed: %s", err)
}
default:
return fmt.Errorf(`unknown command "%s"`+"\n"+`Run 'influx-tools help' for usage`+"\n\n", name)
}
return nil
}
type ossServer struct {
logger *zap.Logger
config *run.Config
noClient bool
client *meta.Client
mc server.MetaClient
}
func (s *ossServer) Open(path string) (err error) {
s.config, err = s.parseConfig(path)
if err != nil {
return err
}
// Validate the configuration.
if err = s.config.Validate(); err != nil {
return fmt.Errorf("validate config: %s", err)
}
if s.noClient {
return nil
}
s.client = meta.NewClient(s.config.Meta)
if err = s.client.Open(); err != nil {
s.client = nil
return err
}
s.mc = &ossMetaClient{s.client}
return nil
}
func (s *ossServer) Close() {
if s.client != nil {
s.client.Close()
s.client = nil
}
}
func (s *ossServer) MetaClient() server.MetaClient { return s.mc }
func (s *ossServer) TSDBConfig() tsdb.Config { return s.config.Data }
func (s *ossServer) Logger() *zap.Logger { return s.logger }
func (s *ossServer) NodeID() uint64 { return 0 }
// ParseConfig parses the config at path.
// It returns a demo configuration if path is blank.
func (s *ossServer) parseConfig(path string) (*run.Config, error) {
path = s.resolvePath(path)
// Use demo configuration if no config path is specified.
if path == "" {
return nil, errors.New("missing config file")
}
config := run.NewConfig()
if err := config.FromTomlFile(path); err != nil {
return nil, err
}
return config, nil
}
func (s *ossServer) resolvePath(path string) string {
if path != "" {
if path == os.DevNull {
return ""
}
return path
}
for _, p := range []string{
os.ExpandEnv("${HOME}/.influxdb/influxdb.conf"),
"/etc/influxdb/influxdb.conf",
} {
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}
type ossMetaClient struct {
*meta.Client
}
func (*ossMetaClient) NodeID() uint64 { return 0 }
func (c *ossMetaClient) NodeShardGroupsByTimeRange(database, policy string, min, max time.Time) (a []meta.ShardGroupInfo, err error) {
return c.ShardGroupsByTimeRange(database, policy, min, max)
}