forked from ipfs-cluster/ipfs-cluster
/
main.go
509 lines (471 loc) · 13.2 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
cid "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
cli "github.com/urfave/cli"
)
const programName = `ipfs-cluster-ctl`
// Version is the cluster-ctl tool version. It should match
// the IPFS cluster's version
const Version = "0.0.2"
var (
defaultHost = fmt.Sprintf("127.0.0.1:%d", 9094)
defaultTimeout = 60
defaultProtocol = "http"
)
var logger = logging.Logger("cluster-ctl")
// Description provides a short summary of the functionality of this tool
var Description = fmt.Sprintf(`
%s is a tool to manage IPFS Cluster nodes.
Use "%s help" to list all available commands and
"%s help <command>" to get usage information for a
specific one.
%s uses the IPFS Cluster API to perform requests and display
responses in a user-readable format. The location of the IPFS
Cluster server is assumed to be %s, but can be
configured with the --host option.
For feedback, bug reports or any additional information, visit
https://github.com/ipfs/ipfs-cluster.
`,
programName,
programName,
programName,
programName,
defaultHost)
type errorResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
type peerAddBody struct {
Addr string `json:"peer_multiaddress"`
}
func out(m string, a ...interface{}) {
fmt.Fprintf(os.Stderr, m, a...)
}
func checkErr(doing string, err error) {
if err != nil {
out("error %s: %s\n", doing, err)
os.Exit(1)
}
}
func main() {
app := cli.NewApp()
app.Name = programName
app.Usage = "CLI for IPFS Cluster"
app.UsageText = Description
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "host, l",
Value: defaultHost,
Usage: "host:port of the IPFS Cluster service API",
},
cli.BoolFlag{
Name: "https, s",
Usage: "use https to connect to the API",
},
cli.StringFlag{
Name: "encoding, enc",
Value: "text",
Usage: "output format encoding [text, json]",
},
cli.IntFlag{
Name: "timeout, t",
Value: defaultTimeout,
Usage: "number of seconds to wait before timing out a request",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "set debug log level",
},
}
app.Before = func(c *cli.Context) error {
defaultHost = c.String("host")
defaultTimeout = c.Int("timeout")
if c.Bool("https") {
defaultProtocol = "https"
}
if c.Bool("debug") {
logging.SetLogLevel("cluster-ctl", "debug")
}
return nil
}
app.Commands = []cli.Command{
{
Name: "id",
Usage: "retrieve cluster member information",
UsageText: `
This command will print out information about the cluster peer used
`,
Flags: []cli.Flag{parseFlag(formatID)},
Action: func(c *cli.Context) error {
resp := request("GET", "/id", nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "peers",
Usage: "list and manage IPFS Cluster peers",
UsageText: `
This command can be used to list and manage IPFS Cluster peers.
`,
Subcommands: []cli.Command{
{
Name: "ls",
Usage: "list the nodes participating in the IPFS Cluster",
UsageText: `
This commands provides a list of the ID information of all the peers in the Cluster.
`,
Flags: []cli.Flag{parseFlag(formatID)},
Action: func(c *cli.Context) error {
resp := request("GET", "/peers", nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "add",
Usage: "add a peer to the Cluster",
UsageText: `
This command adds a new peer to the cluster. In order for the operation to
succeed, the new peer needs to be reachable and any other member of the cluster
should be online. The operation returns the ID information for the new peer.
`,
ArgsUsage: "<multiaddress>",
Flags: []cli.Flag{parseFlag(formatID)},
Action: func(c *cli.Context) error {
addr := c.Args().First()
if addr == "" {
return cli.NewExitError("Error: a multiaddress argument is needed", 1)
}
_, err := ma.NewMultiaddr(addr)
checkErr("parsing multiaddress", err)
addBody := peerAddBody{addr}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.Encode(addBody)
resp := request("POST", "/peers", &buf)
formatResponse(c, resp)
return nil
},
},
{
Name: "rm",
Usage: "remove a peer from the Cluster",
UsageText: `
This command removes a peer from the cluster. If the peer is online, it will
automatically shut down. All other cluster peers should be online for the
operation to succeed, otherwise some nodes may be left with an outdated list of
cluster peers.
`,
ArgsUsage: "<peer ID>",
Flags: []cli.Flag{parseFlag(formatNone)},
Action: func(c *cli.Context) error {
pid := c.Args().First()
_, err := peer.IDB58Decode(pid)
checkErr("parsing peer ID", err)
resp := request("DELETE", "/peers/"+pid, nil)
formatResponse(c, resp)
return nil
},
},
},
},
{
Name: "pin",
Usage: "add, remove or list items managed by IPFS Cluster",
UsageText: `
This command allows to add, remove or list items managed (pinned) by
the Cluster.
`,
Subcommands: []cli.Command{
{
Name: "add",
Usage: "Track a CID (pin)",
UsageText: `
This command tells IPFS Cluster to start managing a CID. Depending on
the pinning strategy, this will trigger IPFS pin requests. The CID will
become part of the Cluster's state and will tracked from this point.
When the request has succeeded, the command returns the status of the CID
in the cluster and should be part of the list offered by "pin ls".
`,
ArgsUsage: "<cid>",
Flags: []cli.Flag{parseFlag(formatGPInfo)},
Action: func(c *cli.Context) error {
cidStr := c.Args().First()
_, err := cid.Decode(cidStr)
checkErr("parsing cid", err)
resp := request("POST", "/pins/"+cidStr, nil)
formatResponse(c, resp)
time.Sleep(500 * time.Millisecond)
resp = request("GET", "/pins/"+cidStr, nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "rm",
Usage: "Stop tracking a CID (unpin)",
UsageText: `
This command tells IPFS Cluster to no longer manage a CID. This will
trigger unpinning operations in all the IPFS nodes holding the content.
When the request has succeeded, the command returns the status of the CID
in the cluster. The CID should disappear from the list offered by "pin ls",
although unpinning operations in the cluster may take longer or fail.
`,
ArgsUsage: "<cid>",
Flags: []cli.Flag{parseFlag(formatGPInfo)},
Action: func(c *cli.Context) error {
cidStr := c.Args().First()
_, err := cid.Decode(cidStr)
checkErr("parsing cid", err)
request("DELETE", "/pins/"+cidStr, nil)
time.Sleep(500 * time.Millisecond)
resp := request("GET", "/pins/"+cidStr, nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "ls",
Usage: "List tracked CIDs",
UsageText: `
This command will list the CIDs which are tracked by IPFS Cluster and to
which peers they are currently allocated. This list does not include
any monitoring information about the
merely represents the list of pins which are part of the global state of
the cluster. For specific information, use "status".
`,
Flags: []cli.Flag{parseFlag(formatCidArg)},
Action: func(c *cli.Context) error {
resp := request("GET", "/pinlist", nil)
formatResponse(c, resp)
return nil
},
},
},
},
{
Name: "status",
Usage: "Retrieve the status of tracked items",
UsageText: `
This command retrieves the status of the CIDs tracked by IPFS
Cluster, including which member is pinning them and any errors.
If a CID is provided, the status will be only fetched for a single
item.
The status of a CID may not be accurate. A manual sync can be triggered
with "sync".
`,
ArgsUsage: "[cid]",
Flags: []cli.Flag{parseFlag(formatGPInfo)},
Action: func(c *cli.Context) error {
cidStr := c.Args().First()
if cidStr != "" {
_, err := cid.Decode(cidStr)
checkErr("parsing cid", err)
}
resp := request("GET", "/pins/"+cidStr, nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "sync",
Usage: "Sync status of tracked items",
UsageText: `
This command asks Cluster peers to verify that the current status of tracked
CIDs is accurate by triggering queries to the IPFS daemons that pin them.
If a CID is provided, the sync and recover operations will be limited to
that single item.
Unless providing a specific CID, the command will output only items which
have changed status because of the sync or are in error state in some node,
therefore, the output should be empty if no operations were performed.
CIDs in error state may be manually recovered with "recover".
`,
ArgsUsage: "[cid]",
Flags: []cli.Flag{parseFlag(formatGPInfo)},
Action: func(c *cli.Context) error {
cidStr := c.Args().First()
var resp *http.Response
if cidStr != "" {
_, err := cid.Decode(cidStr)
checkErr("parsing cid", err)
resp = request("POST", "/pins/"+cidStr+"/sync", nil)
} else {
resp = request("POST", "/pins/sync", nil)
}
formatResponse(c, resp)
return nil
},
},
{
Name: "recover",
Usage: "Recover tracked items in error state",
UsageText: `
This command asks Cluster peers to re-track or re-forget an item which is in
error state, usually because the IPFS pin or unpin operation has failed.
The command will wait for any operations to succeed and will return the status
of the item upon completion.
`,
ArgsUsage: "<cid>",
Flags: []cli.Flag{parseFlag(formatGPInfo)},
Action: func(c *cli.Context) error {
cidStr := c.Args().First()
var resp *http.Response
if cidStr != "" {
_, err := cid.Decode(cidStr)
checkErr("parsing cid", err)
resp = request("POST", "/pins/"+cidStr+"/recover", nil)
formatResponse(c, resp)
} else {
return cli.NewExitError("A CID is required", 1)
}
return nil
},
},
{
Name: "version",
Usage: "Retrieve cluster version",
UsageText: `
This command retrieves the IPFS Cluster version and can be used
to check that it matches the CLI version (shown by -v).
`,
Flags: []cli.Flag{parseFlag(formatVersion)},
Action: func(c *cli.Context) error {
resp := request("GET", "/version", nil)
formatResponse(c, resp)
return nil
},
},
{
Name: "commands",
Usage: "List all commands",
Hidden: true,
Action: func(c *cli.Context) error {
walkCommands(c.App.Commands)
return nil
},
},
}
app.Run(os.Args)
}
func parseFlag(t int) cli.IntFlag {
return cli.IntFlag{
Name: "parseAs",
Value: t,
Hidden: true,
}
}
func walkCommands(cmds []cli.Command) {
for _, c := range cmds {
fmt.Println(c.HelpName)
walkCommands(c.Subcommands)
}
}
func request(method, path string, body io.Reader, args ...string) *http.Response {
ctx, cancel := context.WithTimeout(context.Background(),
time.Duration(defaultTimeout)*time.Second)
defer cancel()
u := defaultProtocol + "://" + defaultHost + path
// turn /a/{param0}/{param1} into /a/this/that
for i, a := range args {
p := fmt.Sprintf("{param%d}", i)
u = strings.Replace(u, p, a, 1)
}
u = strings.TrimSuffix(u, "/")
logger.Debugf("%s: %s", method, u)
r, err := http.NewRequest(method, u, body)
checkErr("creating request", err)
r.WithContext(ctx)
client := &http.Client{}
resp, err := client.Do(r)
checkErr(fmt.Sprintf("performing request to %s", defaultHost), err)
return resp
}
func formatResponse(c *cli.Context, r *http.Response) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
checkErr("reading body", err)
logger.Debugf("Body: %s", body)
switch {
case r.StatusCode > 399:
var e errorResp
err = json.Unmarshal(body, &e)
checkErr("decoding error response", err)
out("Error %d: %s", e.Code, e.Message)
case r.StatusCode == http.StatusAccepted:
out("%s", "Request accepted")
case r.StatusCode == http.StatusNoContent:
out("%s", "Request succeeded\n")
default:
enc := c.GlobalString("encoding")
switch enc {
case "text":
textFormat(body, c.Int("parseAs"))
default:
var resp interface{}
err = json.Unmarshal(body, &resp)
checkErr("decoding response", err)
prettyPrint(body)
}
}
}
// JSON output is nice and allows users to build on top.
func prettyPrint(buf []byte) {
var dst bytes.Buffer
err := json.Indent(&dst, buf, "", " ")
checkErr("indenting json", err)
fmt.Printf("%s", dst.String())
}
/*
// old ugly pretty print
func prettyPrint(obj interface{}, indent int) {
ind := func() string {
var str string
for i := 0; i < indent; i++ {
str += " "
}
return str
}
switch obj.(type) {
case []interface{}:
slice := obj.([]interface{})
for _, elem := range slice {
prettyPrint(elem, indent+2)
}
case map[string]interface{}:
m := obj.(map[string]interface{})
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := m[k]
fmt.Printf(ind()+"%s: ", k)
switch v.(type) {
case []interface{}, map[string]interface{}:
fmt.Println()
prettyPrint(v, indent+4)
default:
prettyPrint(v, indent)
}
}
default:
fmt.Println(obj)
}
}
*/