forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
266 lines (230 loc) · 5.33 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
265
266
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"github.com/spf13/cobra"
"github.com/stellar/go/tools/stellar-archivist/internal"
)
func status(a string, opts *Options) {
arch := archivist.MustConnect(a, opts.ConnectOpts)
state, e := arch.GetRootHAS()
if e != nil {
log.Fatal(e)
}
buckets := state.Buckets()
summ, nz := state.LevelSummary()
fmt.Printf("\n")
fmt.Printf(" Archive: %s\n", a)
fmt.Printf(" Server: %s\n", state.Server)
fmt.Printf(" CurrentLedger: %d (0x%8.8x)\n", state.CurrentLedger, state.CurrentLedger)
fmt.Printf("CurrentBuckets: %s (%d nonzero levels)\n", summ, nz)
fmt.Printf(" Newest bucket: %s\n", buckets[0])
fmt.Printf("\n")
}
type Options struct {
Low int
High uint32
Last int
Profile bool
CommandOpts archivist.CommandOptions
ConnectOpts archivist.ConnectOptions
}
func (opts *Options) SetRange(arch *archivist.Archive) {
if arch != nil && opts.Last != -1 {
state, e := arch.GetRootHAS()
if e == nil {
low := state.CurrentLedger - uint32(opts.Last)
opts.CommandOpts.Range =
archivist.MakeRange(low, state.CurrentLedger)
return
}
}
opts.CommandOpts.Range =
archivist.MakeRange(uint32(opts.Low),
uint32(opts.High))
}
func (opts *Options) MaybeProfile() {
if opts.Profile {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
}
func scan(a string, opts *Options) {
arch := archivist.MustConnect(a, opts.ConnectOpts)
opts.SetRange(arch)
e1 := arch.Scan(&opts.CommandOpts)
e2 := arch.ReportMissing(&opts.CommandOpts)
e3 := arch.ReportInvalid(&opts.CommandOpts)
if e1 != nil {
log.Fatal(e1)
}
if e2 != nil {
log.Fatal(e2)
}
if e3 != nil {
log.Fatal(e3)
}
}
func mirror(src string, dst string, opts *Options) {
srcArch := archivist.MustConnect(src, opts.ConnectOpts)
dstArch := archivist.MustConnect(dst, opts.ConnectOpts)
opts.SetRange(srcArch)
log.Printf("mirroring %v -> %v\n", src, dst)
e := archivist.Mirror(srcArch, dstArch, &opts.CommandOpts)
if e != nil {
log.Fatal(e)
}
}
func repair(src string, dst string, opts *Options) {
srcArch := archivist.MustConnect(src, opts.ConnectOpts)
dstArch := archivist.MustConnect(dst, opts.ConnectOpts)
opts.SetRange(srcArch)
log.Printf("repairing %v -> %v\n", src, dst)
e := archivist.Repair(srcArch, dstArch, &opts.CommandOpts)
if e != nil {
log.Fatal(e)
}
}
func main() {
var opts Options
rootCmd := &cobra.Command{
Use: "stellar-archivist",
Short: "inspect stellar history archive",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
os.Exit(0)
},
}
rootCmd.PersistentFlags().IntVar(
&opts.Low,
"low",
0,
"first ledger to act on",
)
rootCmd.PersistentFlags().Uint32Var(
&opts.High,
"high",
uint32(0xffffffff),
"last ledger to act on",
)
rootCmd.PersistentFlags().IntVar(
&opts.Last,
"last",
-1,
"number of recent ledgers to act on",
)
rootCmd.PersistentFlags().IntVarP(
&opts.CommandOpts.Concurrency,
"concurrency",
"c",
32,
"number of files to operate on concurrently",
)
rootCmd.PersistentFlags().StringVar(
&opts.ConnectOpts.S3Region,
"s3region",
"us-east-1",
"S3 region to connect to",
)
rootCmd.PersistentFlags().StringVar(
&opts.ConnectOpts.S3Endpoint,
"s3endpoint",
"",
"S3 endpoint to use",
)
rootCmd.PersistentFlags().BoolVarP(
&opts.CommandOpts.DryRun,
"dryrun",
"n",
false,
"describe file-writes, but do not perform any",
)
rootCmd.PersistentFlags().BoolVarP(
&opts.CommandOpts.Force,
"force",
"f",
false,
"overwrite existing files",
)
rootCmd.PersistentFlags().BoolVar(
&opts.CommandOpts.Verify,
"verify",
false,
"verify file contents",
)
rootCmd.PersistentFlags().BoolVar(
&opts.CommandOpts.Thorough,
"thorough",
false,
"decode and re-encode all buckets",
)
rootCmd.PersistentFlags().BoolVar(
&opts.Profile,
"profile",
false,
"collect and serve profile locally",
)
rootCmd.AddCommand(&cobra.Command{
Use: "status",
Run: func(cmd *cobra.Command, args []string) {
status(firstArg(args), &opts)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "scan",
Run: func(cmd *cobra.Command, args []string) {
opts.MaybeProfile()
scan(firstArg(args), &opts)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "mirror",
Run: func(cmd *cobra.Command, args []string) {
opts.MaybeProfile()
src, dst := srcDst(args)
mirror(src, dst, &opts)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "repair",
Run: func(cmd *cobra.Command, args []string) {
opts.MaybeProfile()
src, dst := srcDst(args)
repair(src, dst, &opts)
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "dumpxdr",
Run: func(cmd *cobra.Command, args []string) {
err := archivist.DumpXdrAsJson(args)
if err != nil {
log.Fatal(err)
}
},
})
rootCmd.Execute()
}
func firstArg(args []string) string {
if len(args) == 0 {
return ""
}
return args[0]
}
func srcDst(args []string) (string, string) {
if len(args) != 2 {
log.Fatal("require exactly 2 arguments")
}
src := args[0]
dst := args[1]
if src == "" || dst == "" {
log.Fatal("require exactly 2 arguments")
}
return src, dst
}