2018-08-23 12:12:55 +00:00
|
|
|
// The ipfs-cluster-ctl application.
|
2016-12-22 16:14:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-14 08:49:49 +00:00
|
|
|
"context"
|
2018-03-15 16:15:23 +00:00
|
|
|
"encoding/hex"
|
2017-12-05 20:44:22 +00:00
|
|
|
"errors"
|
2016-12-22 16:14:15 +00:00
|
|
|
"fmt"
|
2017-10-13 21:12:46 +00:00
|
|
|
"io"
|
2016-12-22 16:14:15 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2018-08-08 19:11:26 +00:00
|
|
|
"sync"
|
2016-12-22 16:14:15 +00:00
|
|
|
"time"
|
|
|
|
|
2018-03-15 16:15:23 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
"github.com/ipfs/ipfs-cluster/api/rest/client"
|
2018-07-23 09:14:43 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2018-03-15 16:15:23 +00:00
|
|
|
|
2016-12-22 16:14:15 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log"
|
2017-01-30 12:12:25 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2017-01-24 14:06:17 +00:00
|
|
|
cli "github.com/urfave/cli"
|
2016-12-22 16:14:15 +00:00
|
|
|
)
|
|
|
|
|
2017-01-23 12:34:22 +00:00
|
|
|
const programName = `ipfs-cluster-ctl`
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2017-01-25 17:10:18 +00:00
|
|
|
// Version is the cluster-ctl tool version. It should match
|
|
|
|
// the IPFS cluster's version
|
2018-11-01 18:49:07 +00:00
|
|
|
const Version = "0.7.0"
|
2017-01-24 14:06:17 +00:00
|
|
|
|
2016-12-22 16:14:15 +00:00
|
|
|
var (
|
2018-03-28 14:04:21 +00:00
|
|
|
defaultHost = "/ip4/127.0.0.1/tcp/9094"
|
2018-08-08 19:11:26 +00:00
|
|
|
defaultTimeout = 0
|
2018-03-28 14:04:21 +00:00
|
|
|
defaultUsername = ""
|
|
|
|
defaultPassword = ""
|
|
|
|
defaultWaitCheckFreq = time.Second
|
2018-08-06 10:44:44 +00:00
|
|
|
defaultAddParams = api.DefaultAddParams()
|
2016-12-22 16:14:15 +00:00
|
|
|
)
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
var logger = logging.Logger("cluster-ctl")
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2018-08-30 23:14:06 +00:00
|
|
|
var globalClient client.Client
|
2017-12-05 20:44:22 +00:00
|
|
|
|
2016-12-22 16:14:15 +00:00
|
|
|
// Description provides a short summary of the functionality of this tool
|
|
|
|
var Description = fmt.Sprintf(`
|
2017-01-24 14:06:17 +00:00
|
|
|
%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.
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2017-01-23 13:21:26 +00:00
|
|
|
%s uses the IPFS Cluster API to perform requests and display
|
2016-12-22 16:14:15 +00:00
|
|
|
responses in a user-readable format. The location of the IPFS
|
|
|
|
Cluster server is assumed to be %s, but can be
|
2018-03-16 18:37:52 +00:00
|
|
|
configured with the --host option. To use the secure libp2p-http
|
|
|
|
API endpoint, use "--host" with the full cluster libp2p listener
|
2018-03-20 18:35:42 +00:00
|
|
|
address (including the "/ipfs/<peerID>" part), and --secret (the
|
|
|
|
32-byte cluster secret as it appears in the cluster configuration).
|
2016-12-22 16:14:15 +00:00
|
|
|
|
|
|
|
For feedback, bug reports or any additional information, visit
|
|
|
|
https://github.com/ipfs/ipfs-cluster.
|
|
|
|
`,
|
|
|
|
programName,
|
|
|
|
programName,
|
|
|
|
programName,
|
|
|
|
programName,
|
|
|
|
defaultHost)
|
|
|
|
|
2017-01-30 12:12:25 +00:00
|
|
|
type peerAddBody struct {
|
|
|
|
Addr string `json:"peer_multiaddress"`
|
|
|
|
}
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
func out(m string, a ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, m, a...)
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
func checkErr(doing string, err error) {
|
|
|
|
if err != nil {
|
|
|
|
out("error %s: %s\n", doing, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
func main() {
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = programName
|
|
|
|
app.Usage = "CLI for IPFS Cluster"
|
2017-07-03 16:36:00 +00:00
|
|
|
app.Description = Description
|
2017-01-24 14:06:17 +00:00
|
|
|
app.Version = Version
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "host, l",
|
|
|
|
Value: defaultHost,
|
2018-03-16 18:37:52 +00:00
|
|
|
Usage: "Cluster's HTTP or LibP2P-HTTP API endpoint",
|
2018-03-15 16:15:23 +00:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "secret",
|
|
|
|
Value: "",
|
|
|
|
Usage: "cluster secret (32 byte pnet-key) as needed. Only when using the LibP2P endpoint",
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "https, s",
|
|
|
|
Usage: "use https to connect to the API",
|
|
|
|
},
|
2017-07-21 21:06:10 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-check-certificate",
|
2017-08-29 01:41:54 +00:00
|
|
|
Usage: "do not verify server TLS certificate. only valid with --https flag",
|
2017-07-21 21:06:10 +00:00
|
|
|
},
|
2017-02-08 18:52:34 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "encoding, enc",
|
|
|
|
Value: "text",
|
|
|
|
Usage: "output format encoding [text, json]",
|
|
|
|
},
|
2017-01-24 14:06:17 +00:00
|
|
|
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",
|
|
|
|
},
|
2017-08-29 01:41:54 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "basic-auth",
|
|
|
|
Usage: `<username>[:<password>] specify BasicAuth credentials for server that
|
|
|
|
requires authorization. implies --https, which you can disable with --force-http`,
|
|
|
|
EnvVar: "CLUSTER_CREDENTIALS",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "force-http, f",
|
|
|
|
Usage: "force HTTP. only valid when using BasicAuth",
|
|
|
|
},
|
2017-01-24 14:06:17 +00:00
|
|
|
}
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
app.Before = func(c *cli.Context) error {
|
2017-12-05 20:44:22 +00:00
|
|
|
cfg := &client.Config{}
|
2018-03-15 16:15:23 +00:00
|
|
|
|
|
|
|
if c.Bool("debug") {
|
|
|
|
logging.SetLogLevel("cluster-ctl", "debug")
|
|
|
|
cfg.LogLevel = "debug"
|
|
|
|
logger.Debug("debug level enabled")
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
addr, err := ma.NewMultiaddr(c.String("host"))
|
|
|
|
checkErr("parsing host multiaddress", err)
|
2018-03-15 16:15:23 +00:00
|
|
|
|
2018-08-10 13:10:16 +00:00
|
|
|
cfg.APIAddr = addr
|
|
|
|
if hexSecret := c.String("secret"); hexSecret != "" {
|
|
|
|
secret, err := hex.DecodeString(hexSecret)
|
|
|
|
checkErr("parsing secret", err)
|
|
|
|
cfg.ProtectorKey = secret
|
2018-03-15 16:15:23 +00:00
|
|
|
}
|
2017-12-05 20:44:22 +00:00
|
|
|
|
|
|
|
cfg.Timeout = time.Duration(c.Int("timeout")) * time.Second
|
2018-03-15 16:15:23 +00:00
|
|
|
|
2018-08-10 13:10:16 +00:00
|
|
|
if client.IsPeerAddress(cfg.APIAddr) && c.Bool("https") {
|
2018-03-15 16:15:23 +00:00
|
|
|
logger.Warning("Using libp2p-http. SSL flags will be ignored")
|
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
cfg.SSL = c.Bool("https")
|
|
|
|
cfg.NoVerifyCert = c.Bool("no-check-certificate")
|
|
|
|
user, pass := parseCredentials(c.String("basic-auth"))
|
|
|
|
cfg.Username = user
|
|
|
|
cfg.Password = pass
|
|
|
|
if user != "" && !cfg.SSL && !c.Bool("force-http") {
|
|
|
|
logger.Warning("SSL automatically enabled with basic auth credentials. Set \"force-http\" to disable")
|
|
|
|
cfg.SSL = true
|
2017-01-24 14:06:17 +00:00
|
|
|
}
|
2017-12-05 20:44:22 +00:00
|
|
|
|
2017-12-07 11:00:28 +00:00
|
|
|
enc := c.String("encoding")
|
|
|
|
if enc != "text" && enc != "json" {
|
|
|
|
checkErr("", errors.New("unsupported encoding"))
|
|
|
|
}
|
|
|
|
|
2018-08-30 23:14:06 +00:00
|
|
|
globalClient, err = client.NewDefaultClient(cfg)
|
2017-12-05 20:44:22 +00:00
|
|
|
checkErr("creating API client", err)
|
2017-01-24 14:06:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-22 16:14:15 +00:00
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
app.Commands = []cli.Command{
|
2017-01-24 15:19:23 +00:00
|
|
|
{
|
|
|
|
Name: "id",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Retrieve peer information",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
|
|
|
This command displays information about the peer that the tool is contacting
|
|
|
|
(usually running in localhost).
|
2017-01-24 15:19:23 +00:00
|
|
|
`,
|
2017-12-05 20:44:22 +00:00
|
|
|
Flags: []cli.Flag{},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-12-05 20:44:22 +00:00
|
|
|
resp, cerr := globalClient.ID()
|
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 15:19:23 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-24 14:06:17 +00:00
|
|
|
{
|
2017-07-03 16:36:00 +00:00
|
|
|
Name: "peers",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "List and manage IPFS Cluster peers",
|
|
|
|
Description: "List and manage IPFS Cluster peers",
|
2017-01-24 14:06:17 +00:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "ls",
|
|
|
|
Usage: "list the nodes participating in the IPFS Cluster",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
|
|
|
This command provides a list of the ID information of all the peers in the Cluster.
|
2017-01-30 12:12:25 +00:00
|
|
|
`,
|
2017-12-05 20:44:22 +00:00
|
|
|
Flags: []cli.Flag{},
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: " ",
|
2017-01-30 12:12:25 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-12-05 20:44:22 +00:00
|
|
|
resp, cerr := globalClient.Peers()
|
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-30 12:12:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rm",
|
|
|
|
Usage: "remove a peer from the Cluster",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2017-01-30 12:12:25 +00:00
|
|
|
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>",
|
2017-12-05 20:44:22 +00:00
|
|
|
Flags: []cli.Flag{},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-01-30 12:12:25 +00:00
|
|
|
pid := c.Args().First()
|
2017-12-05 20:44:22 +00:00
|
|
|
p, err := peer.IDB58Decode(pid)
|
2017-01-30 12:12:25 +00:00
|
|
|
checkErr("parsing peer ID", err)
|
2017-12-05 20:44:22 +00:00
|
|
|
cerr := globalClient.PeerRm(p)
|
|
|
|
formatResponse(c, nil, cerr)
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-07-23 09:14:43 +00:00
|
|
|
{
|
|
|
|
Name: "add",
|
|
|
|
Usage: "Add a file or directory to ipfs and pin it in the cluster",
|
|
|
|
ArgsUsage: "<path>",
|
|
|
|
Description: `
|
2018-08-08 19:11:26 +00:00
|
|
|
Add allows to add and replicate content to several ipfs daemons, performing
|
2018-08-19 17:39:08 +00:00
|
|
|
a Cluster Pin operation on success. It takes elements from local paths as
|
|
|
|
well as from web URLs (accessed with a GET request). Providing several
|
|
|
|
arguments will automatically set --wrap-in-directory.
|
2018-07-23 09:14:43 +00:00
|
|
|
|
|
|
|
Cluster Add is equivalent to "ipfs add" in terms of DAG building, and supports
|
2018-08-19 17:39:08 +00:00
|
|
|
the same options for adjusting the chunker, the DAG layout etc. However,
|
|
|
|
it will allocate the content and send it directly to the allocated peers (among
|
|
|
|
which may not necessarily be the local ipfs daemon).
|
|
|
|
|
|
|
|
Once the adding process is finished, the content is fully added to all
|
|
|
|
allocations and pinned in them. This makes cluster add slower than a local
|
|
|
|
ipfs add, but the result is a fully replicated CID on completion.
|
|
|
|
If you prefer faster adding, add directly to the local IPFS and trigger a
|
|
|
|
cluster "pin add".
|
|
|
|
|
2018-07-23 09:14:43 +00:00
|
|
|
`,
|
2018-08-19 17:39:08 +00:00
|
|
|
/*
|
|
|
|
Cluster Add supports handling huge files and sharding the resulting DAG among
|
|
|
|
several ipfs daemons (--shard). In this case, a single ipfs daemon will not
|
|
|
|
contain the full dag, but only parts of it (shards). Desired shard size can
|
|
|
|
be provided with the --shard-size flag.
|
|
|
|
|
|
|
|
We recommend setting a --name for sharded pins. Otherwise, it will be
|
|
|
|
automatically generated.
|
|
|
|
*/
|
2018-07-23 09:14:43 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "recursive, r",
|
|
|
|
Usage: "Add directory paths recursively",
|
|
|
|
},
|
2018-08-08 19:11:26 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quiet, q",
|
|
|
|
Usage: "Write only hashes to output (one per line)",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "quieter, Q",
|
|
|
|
Usage: "Write only final hash to output",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "layout",
|
|
|
|
Value: defaultAddParams.Layout,
|
|
|
|
Usage: "Dag layout to use for dag generation: balanced or trickle",
|
|
|
|
},
|
2018-08-08 23:16:30 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "wrap-with-directory, w",
|
2018-08-19 17:39:08 +00:00
|
|
|
Usage: "Wrap a with a directory object.",
|
2018-08-08 23:16:30 +00:00
|
|
|
},
|
2018-08-08 19:11:26 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "hidden, H",
|
|
|
|
Usage: "Include files that are hidden. Only takes effect on recursive add",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "chunker, s",
|
|
|
|
Usage: "'size-<size>' or 'rabin-<min>-<avg>-<max>'",
|
|
|
|
Value: defaultAddParams.Chunker,
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "raw-leaves",
|
|
|
|
Usage: "Use raw blocks for leaves (experimental)",
|
|
|
|
},
|
2018-08-20 14:25:26 +00:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "cid-version",
|
|
|
|
Usage: "CID version. Non default implies raw-leaves",
|
|
|
|
Value: defaultAddParams.CidVersion,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "hash",
|
|
|
|
Usage: "Hash function to use. Implies cid-version=1.",
|
|
|
|
Value: defaultAddParams.HashFun,
|
|
|
|
},
|
2018-07-23 09:14:43 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name, n",
|
2018-08-06 10:44:44 +00:00
|
|
|
Value: defaultAddParams.Name,
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Sets a name for this pin",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "replication-min, rmin",
|
2018-08-06 10:44:44 +00:00
|
|
|
Value: defaultAddParams.ReplicationFactorMin,
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Sets the minimum replication factor for pinning this file",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "replication-max, rmax",
|
2018-08-06 10:44:44 +00:00
|
|
|
Value: defaultAddParams.ReplicationFactorMax,
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Sets the maximum replication factor for pinning this file",
|
|
|
|
},
|
2018-08-08 19:11:26 +00:00
|
|
|
// TODO: Uncomment when sharding is supported.
|
2018-07-23 09:14:43 +00:00
|
|
|
// cli.BoolFlag{
|
2018-08-08 19:11:26 +00:00
|
|
|
// Name: "shard",
|
|
|
|
// Usage: "Break the file into pieces (shards) and distributed among peers",
|
2018-07-23 09:14:43 +00:00
|
|
|
// },
|
2018-08-08 19:11:26 +00:00
|
|
|
// cli.Uint64Flag{
|
|
|
|
// Name: "shard-size",
|
|
|
|
// Value: defaultAddParams.ShardSize,
|
|
|
|
// Usage: "Sets the maximum replication factor for pinning this file",
|
|
|
|
// },
|
|
|
|
// TODO: Figure progress over total bar.
|
2018-07-23 09:14:43 +00:00
|
|
|
// cli.BoolFlag{
|
2018-08-08 19:11:26 +00:00
|
|
|
// Name: "progress, p",
|
|
|
|
// Usage: "Stream progress data",
|
2018-07-23 09:14:43 +00:00
|
|
|
// },
|
2018-08-08 19:11:26 +00:00
|
|
|
|
2018-07-23 09:14:43 +00:00
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
shard := c.Bool("shard")
|
|
|
|
name := c.String("name")
|
|
|
|
if shard && name == "" {
|
|
|
|
randName, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// take only first letters
|
|
|
|
name = "sharded-" + strings.Split(randName.String(), "-")[0]
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:39:08 +00:00
|
|
|
// Read arguments (paths)
|
2018-07-23 09:14:43 +00:00
|
|
|
paths := make([]string, c.NArg(), c.NArg())
|
|
|
|
for i, path := range c.Args() {
|
|
|
|
paths[i] = path
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(paths) == 0 {
|
|
|
|
checkErr("", errors.New("need at least one path"))
|
|
|
|
}
|
|
|
|
|
2018-08-19 17:39:08 +00:00
|
|
|
// Setup AddParams
|
2018-08-06 10:44:44 +00:00
|
|
|
p := api.DefaultAddParams()
|
|
|
|
p.ReplicationFactorMin = c.Int("replication-min")
|
|
|
|
p.ReplicationFactorMax = c.Int("replication-max")
|
|
|
|
p.Name = name
|
2018-08-08 19:11:26 +00:00
|
|
|
//p.Shard = shard
|
|
|
|
//p.ShardSize = c.Uint64("shard-size")
|
|
|
|
p.Shard = false
|
2018-08-19 17:39:08 +00:00
|
|
|
p.Recursive = c.Bool("recursive")
|
2018-08-06 10:44:44 +00:00
|
|
|
p.Layout = c.String("layout")
|
|
|
|
p.Chunker = c.String("chunker")
|
|
|
|
p.RawLeaves = c.Bool("raw-leaves")
|
|
|
|
p.Hidden = c.Bool("hidden")
|
2018-08-08 23:16:30 +00:00
|
|
|
p.Wrap = c.Bool("wrap-with-directory") || len(paths) > 1
|
2018-08-20 14:25:26 +00:00
|
|
|
p.CidVersion = c.Int("cid-version")
|
|
|
|
p.HashFun = c.String("hash")
|
|
|
|
if p.HashFun != defaultAddParams.HashFun {
|
|
|
|
p.CidVersion = 1
|
|
|
|
}
|
|
|
|
if p.CidVersion > 0 {
|
|
|
|
p.RawLeaves = true
|
|
|
|
}
|
2018-08-06 20:49:28 +00:00
|
|
|
|
|
|
|
out := make(chan *api.AddedOutput, 1)
|
2018-08-08 19:11:26 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
2018-08-06 20:49:28 +00:00
|
|
|
go func() {
|
2018-08-08 19:11:26 +00:00
|
|
|
defer wg.Done()
|
|
|
|
var last string
|
2018-08-06 20:49:28 +00:00
|
|
|
for v := range out {
|
2018-08-08 19:11:26 +00:00
|
|
|
// Print everything when doing json
|
|
|
|
if c.GlobalString("encoding") != "text" {
|
|
|
|
formatResponse(c, *v, nil)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print last hash only
|
|
|
|
if c.Bool("quieter") {
|
2018-10-03 21:03:30 +00:00
|
|
|
last = v.Cid
|
2018-08-08 19:11:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print hashes only
|
|
|
|
if c.Bool("quiet") {
|
2018-10-03 21:03:30 +00:00
|
|
|
fmt.Println(v.Cid)
|
2018-08-08 19:11:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format normal text representation of AddedOutput
|
|
|
|
formatResponse(c, *v, nil)
|
|
|
|
}
|
|
|
|
if last != "" {
|
|
|
|
fmt.Println(last)
|
2018-08-06 20:49:28 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-08-19 17:39:08 +00:00
|
|
|
cerr := globalClient.Add(paths, p, out)
|
2018-08-08 19:11:26 +00:00
|
|
|
wg.Wait()
|
2018-07-23 09:14:43 +00:00
|
|
|
formatResponse(c, nil, cerr)
|
2018-08-08 19:11:26 +00:00
|
|
|
return cerr
|
2018-07-23 09:14:43 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-24 14:06:17 +00:00
|
|
|
{
|
2017-07-03 16:36:00 +00:00
|
|
|
Name: "pin",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Pin and unpin and list items in IPFS Cluster",
|
|
|
|
Description: "Pin and unpin and list items in IPFS Cluster",
|
2017-01-24 14:06:17 +00:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "add",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Cluster Pin",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2016-12-22 16:14:15 +00:00
|
|
|
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.
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
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".
|
2017-03-08 17:51:03 +00:00
|
|
|
|
|
|
|
An optional replication factor can be provided: -1 means "pin everywhere"
|
|
|
|
and 0 means use cluster's default setting. Positive values indicate how many
|
|
|
|
peers should pin this content.
|
2017-01-24 14:06:17 +00:00
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: "<CID>",
|
2017-03-08 17:51:03 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "replication, r",
|
|
|
|
Value: 0,
|
2018-01-15 18:32:54 +00:00
|
|
|
Usage: "Sets a custom replication factor (overrides -rmax and -rmin)",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "replication-min, rmin",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "Sets the minimum replication factor for this pin",
|
|
|
|
},
|
|
|
|
cli.IntFlag{
|
|
|
|
Name: "replication-max, rmax",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "Sets the maximum replication factor for this pin",
|
2017-03-08 17:51:03 +00:00
|
|
|
},
|
2017-11-28 20:06:12 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name, n",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Sets a name for this pin",
|
|
|
|
},
|
2018-03-06 07:00:05 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-status, ns",
|
|
|
|
Usage: "Prevents fetching pin status after pinning (faster, quieter)",
|
|
|
|
},
|
2018-03-14 08:49:49 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "wait, w",
|
|
|
|
Usage: "Wait for all nodes to report a status of pinned before returning",
|
|
|
|
},
|
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "wait-timeout, wt",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "How long to --wait (in seconds), default is indefinitely",
|
|
|
|
},
|
2017-03-08 17:51:03 +00:00
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-01-24 14:06:17 +00:00
|
|
|
cidStr := c.Args().First()
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-01-24 14:06:17 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-01-15 18:32:54 +00:00
|
|
|
|
|
|
|
rpl := c.Int("replication")
|
|
|
|
rplMin := c.Int("replication-min")
|
|
|
|
rplMax := c.Int("replication-max")
|
|
|
|
if rpl != 0 {
|
|
|
|
rplMin = rpl
|
|
|
|
rplMax = rpl
|
|
|
|
}
|
|
|
|
|
2018-10-01 10:53:42 +00:00
|
|
|
cerr := globalClient.Pin(ci, rplMin, rplMax, c.String("name"))
|
2017-12-05 20:44:22 +00:00
|
|
|
if cerr != nil {
|
|
|
|
formatResponse(c, nil, cerr)
|
|
|
|
return nil
|
2017-03-13 10:10:53 +00:00
|
|
|
}
|
2018-03-14 08:49:49 +00:00
|
|
|
|
2018-03-29 08:33:09 +00:00
|
|
|
handlePinResponseFormatFlags(
|
2018-03-28 14:04:21 +00:00
|
|
|
c,
|
|
|
|
ci,
|
|
|
|
api.TrackerStatusPinned,
|
|
|
|
)
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "rm",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Cluster Unpin",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2016-12-22 16:14:15 +00:00
|
|
|
This command tells IPFS Cluster to no longer manage a CID. This will
|
|
|
|
trigger unpinning operations in all the IPFS nodes holding the content.
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
When the request has succeeded, the command returns the status of the CID
|
2017-01-25 17:10:18 +00:00
|
|
|
in the cluster. The CID should disappear from the list offered by "pin ls",
|
2017-01-24 14:06:17 +00:00
|
|
|
although unpinning operations in the cluster may take longer or fail.
|
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: "<CID>",
|
2018-03-06 07:00:05 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-status, ns",
|
|
|
|
Usage: "Prevents fetching pin status after unpinning (faster, quieter)",
|
|
|
|
},
|
2018-03-14 08:49:49 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "wait, w",
|
|
|
|
Usage: "Wait for all nodes to report a status of unpinned before returning",
|
|
|
|
},
|
|
|
|
cli.DurationFlag{
|
|
|
|
Name: "wait-timeout, wt",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "How long to --wait (in seconds), default is indefinitely",
|
|
|
|
},
|
2018-03-06 07:00:05 +00:00
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-01-24 14:06:17 +00:00
|
|
|
cidStr := c.Args().First()
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-01-24 14:06:17 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-10-01 10:53:42 +00:00
|
|
|
cerr := globalClient.Unpin(ci)
|
2017-12-05 20:44:22 +00:00
|
|
|
if cerr != nil {
|
|
|
|
formatResponse(c, nil, cerr)
|
|
|
|
return nil
|
2017-03-13 10:10:53 +00:00
|
|
|
}
|
2018-03-14 08:49:49 +00:00
|
|
|
|
2018-03-29 08:33:09 +00:00
|
|
|
handlePinResponseFormatFlags(
|
2018-03-28 14:04:21 +00:00
|
|
|
c,
|
|
|
|
ci,
|
|
|
|
api.TrackerStatusUnpinned,
|
|
|
|
)
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ls",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "List items in the cluster pinset",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2017-02-13 15:46:53 +00:00
|
|
|
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
|
2017-04-06 02:27:02 +00:00
|
|
|
any monitoring information about the IPFS status of the CIDs, it
|
|
|
|
merely represents the list of pins which are part of the shared state of
|
|
|
|
the cluster. For IPFS-status information about the pins, use "status".
|
2018-07-23 09:14:43 +00:00
|
|
|
|
2018-08-10 12:39:44 +00:00
|
|
|
The filter only takes effect when listing all pins. The possible values are:
|
|
|
|
- all
|
|
|
|
- pin
|
|
|
|
- meta-pin
|
|
|
|
- clusterdag-pin
|
|
|
|
- shard-pin
|
2017-01-24 14:06:17 +00:00
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: "[CID]",
|
2018-03-18 19:29:02 +00:00
|
|
|
Flags: []cli.Flag{
|
2018-08-10 12:39:44 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "filter",
|
|
|
|
Usage: "Comma separated list of pin types. See help above.",
|
|
|
|
Value: "pin",
|
2018-03-18 19:29:02 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-04-06 02:27:02 +00:00
|
|
|
cidStr := c.Args().First()
|
|
|
|
if cidStr != "" {
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-04-06 02:27:02 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-10-01 10:53:42 +00:00
|
|
|
resp, cerr := globalClient.Allocation(ci)
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
|
|
|
} else {
|
2018-08-10 12:53:46 +00:00
|
|
|
var filter api.PinType
|
2018-08-10 12:39:44 +00:00
|
|
|
strFilter := strings.Split(c.String("filter"), ",")
|
|
|
|
for _, f := range strFilter {
|
|
|
|
filter |= api.PinTypeFromString(f)
|
2018-03-18 19:29:02 +00:00
|
|
|
}
|
2018-08-10 12:39:44 +00:00
|
|
|
|
2018-03-18 19:29:02 +00:00
|
|
|
resp, cerr := globalClient.Allocations(filter)
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
2017-04-06 02:27:02 +00:00
|
|
|
}
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "Retrieve the status of tracked items",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2016-12-22 16:14:15 +00:00
|
|
|
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
|
2018-03-18 19:29:02 +00:00
|
|
|
item. Metadata CIDs are included in the status response
|
2017-01-25 18:38:23 +00:00
|
|
|
|
|
|
|
The status of a CID may not be accurate. A manual sync can be triggered
|
|
|
|
with "sync".
|
2017-12-01 11:56:26 +00:00
|
|
|
|
|
|
|
When the --local flag is passed, it will only fetch the status from the
|
|
|
|
contacted cluster peer. By default, status will be fetched from all peers.
|
Status filters for `ipfs-cluster-ctl status`
Added filter option to `ipfs-cluster-ctl status`
When the --filter is passed, it will only fetch the peer information
where status of the pin matches with the filter value.
Valid filter values are tracker status types(i.e., "pinned",
"pin_error", "unpinning" etc), an alias of tracker status type (i.e.,
"queued" or "error"), comma separated list of tracker status type
and/or it aliases(i.e., "error,pinning")
On passing invalid filter value no status information will be shown
In particular, the filter would remove elements from []GlobalPinInfo
when none of the peers in GlobalPinInfo match the filter. If one peer
in the GlobalPinInfo matches the filter, the whole object is returned,
including the information for the other peers which may or not match it.
filter option works on statusAll("GET /pins"). For fetching pin status
for a CID("GET /pins/<cid>"), filter option would have no effect
Fixes #445
License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
2018-12-20 08:02:26 +00:00
|
|
|
|
|
|
|
When the --filter is passed, it will only fetch the peer information
|
|
|
|
where status of the pin matches with the filter value.
|
2018-12-26 05:45:30 +00:00
|
|
|
Valid filter values are tracker status types(full list of tracker status
|
|
|
|
types includes "bug", "cluster_error", "pin_error", "unpin_error", "pinned",
|
|
|
|
"pinning", "unpinning", "remote", "pin_queued" and "unpin_queued"), an
|
|
|
|
alias of tracker status type ("queued" or "error"), comma separated list of
|
|
|
|
tracker status type and/or it aliases ("error,pinning"). On passing invalid
|
|
|
|
filter value will throw an error.
|
2017-01-24 14:06:17 +00:00
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: "[CID]",
|
2017-12-01 11:56:26 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
localFlag(),
|
Status filters for `ipfs-cluster-ctl status`
Added filter option to `ipfs-cluster-ctl status`
When the --filter is passed, it will only fetch the peer information
where status of the pin matches with the filter value.
Valid filter values are tracker status types(i.e., "pinned",
"pin_error", "unpinning" etc), an alias of tracker status type (i.e.,
"queued" or "error"), comma separated list of tracker status type
and/or it aliases(i.e., "error,pinning")
On passing invalid filter value no status information will be shown
In particular, the filter would remove elements from []GlobalPinInfo
when none of the peers in GlobalPinInfo match the filter. If one peer
in the GlobalPinInfo matches the filter, the whole object is returned,
including the information for the other peers which may or not match it.
filter option works on statusAll("GET /pins"). For fetching pin status
for a CID("GET /pins/<cid>"), filter option would have no effect
Fixes #445
License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
2018-12-20 08:02:26 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "filter",
|
2018-12-26 05:45:30 +00:00
|
|
|
Usage: "Comma-separated list of filters",
|
Status filters for `ipfs-cluster-ctl status`
Added filter option to `ipfs-cluster-ctl status`
When the --filter is passed, it will only fetch the peer information
where status of the pin matches with the filter value.
Valid filter values are tracker status types(i.e., "pinned",
"pin_error", "unpinning" etc), an alias of tracker status type (i.e.,
"queued" or "error"), comma separated list of tracker status type
and/or it aliases(i.e., "error,pinning")
On passing invalid filter value no status information will be shown
In particular, the filter would remove elements from []GlobalPinInfo
when none of the peers in GlobalPinInfo match the filter. If one peer
in the GlobalPinInfo matches the filter, the whole object is returned,
including the information for the other peers which may or not match it.
filter option works on statusAll("GET /pins"). For fetching pin status
for a CID("GET /pins/<cid>"), filter option would have no effect
Fixes #445
License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
2018-12-20 08:02:26 +00:00
|
|
|
},
|
2017-12-01 11:56:26 +00:00
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-01-24 14:06:17 +00:00
|
|
|
cidStr := c.Args().First()
|
|
|
|
if cidStr != "" {
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-01-24 14:06:17 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-10-01 10:53:42 +00:00
|
|
|
resp, cerr := globalClient.Status(ci, c.Bool("local"))
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
|
|
|
} else {
|
Status filters for `ipfs-cluster-ctl status`
Added filter option to `ipfs-cluster-ctl status`
When the --filter is passed, it will only fetch the peer information
where status of the pin matches with the filter value.
Valid filter values are tracker status types(i.e., "pinned",
"pin_error", "unpinning" etc), an alias of tracker status type (i.e.,
"queued" or "error"), comma separated list of tracker status type
and/or it aliases(i.e., "error,pinning")
On passing invalid filter value no status information will be shown
In particular, the filter would remove elements from []GlobalPinInfo
when none of the peers in GlobalPinInfo match the filter. If one peer
in the GlobalPinInfo matches the filter, the whole object is returned,
including the information for the other peers which may or not match it.
filter option works on statusAll("GET /pins"). For fetching pin status
for a CID("GET /pins/<cid>"), filter option would have no effect
Fixes #445
License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
2018-12-20 08:02:26 +00:00
|
|
|
resp, cerr := globalClient.StatusAll(c.String("filter"), c.Bool("local"))
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-24 14:06:17 +00:00
|
|
|
}
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "sync",
|
2017-01-25 18:38:23 +00:00
|
|
|
Usage: "Sync status of tracked items",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2017-01-25 18:38:23 +00:00
|
|
|
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.
|
2017-03-08 17:51:03 +00:00
|
|
|
If a CID is provided, the sync and recover operations will be limited to
|
2017-01-25 18:38:23 +00:00
|
|
|
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".
|
2017-12-01 11:56:26 +00:00
|
|
|
|
|
|
|
When the --local flag is passed, it will only trigger sync
|
|
|
|
operations on the contacted peer. By default, all peers will sync.
|
2017-01-24 14:06:17 +00:00
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: "[CID]",
|
2017-12-01 11:56:26 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
localFlag(),
|
|
|
|
},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-01-24 14:06:17 +00:00
|
|
|
cidStr := c.Args().First()
|
|
|
|
if cidStr != "" {
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-01-24 14:06:17 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-10-01 10:53:42 +00:00
|
|
|
resp, cerr := globalClient.Sync(ci, c.Bool("local"))
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-25 18:38:23 +00:00
|
|
|
} else {
|
2017-12-05 20:44:22 +00:00
|
|
|
resp, cerr := globalClient.SyncAll(c.Bool("local"))
|
|
|
|
formatResponse(c, resp, cerr)
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 14:06:17 +00:00
|
|
|
},
|
|
|
|
},
|
2017-01-25 18:38:23 +00:00
|
|
|
{
|
|
|
|
Name: "recover",
|
|
|
|
Usage: "Recover tracked items in error state",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2017-11-30 00:53:31 +00:00
|
|
|
This command asks Cluster peers to re-track or re-forget CIDs in
|
2017-01-25 18:38:23 +00:00
|
|
|
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
|
2017-11-30 00:53:31 +00:00
|
|
|
of the item upon completion. Note that, when running on the full sets of tracked
|
2017-12-04 12:59:48 +00:00
|
|
|
CIDs (without argument), it may take a considerably long time.
|
2017-11-30 00:53:31 +00:00
|
|
|
|
2017-12-01 11:56:26 +00:00
|
|
|
When the --local flag is passed, it will only trigger recover
|
|
|
|
operations on the contacted peer (as opposed to on every peer).
|
2017-01-25 18:38:23 +00:00
|
|
|
`,
|
2017-11-30 00:53:31 +00:00
|
|
|
ArgsUsage: "[CID]",
|
|
|
|
Flags: []cli.Flag{
|
2017-12-01 11:56:26 +00:00
|
|
|
localFlag(),
|
2017-11-30 00:53:31 +00:00
|
|
|
},
|
2017-01-25 18:38:23 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
cidStr := c.Args().First()
|
|
|
|
if cidStr != "" {
|
2017-12-05 20:44:22 +00:00
|
|
|
ci, err := cid.Decode(cidStr)
|
2017-01-25 18:38:23 +00:00
|
|
|
checkErr("parsing cid", err)
|
2018-10-01 10:53:42 +00:00
|
|
|
resp, cerr := globalClient.Recover(ci, c.Bool("local"))
|
2017-12-05 20:44:22 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-25 18:38:23 +00:00
|
|
|
} else {
|
2017-12-05 20:44:22 +00:00
|
|
|
resp, cerr := globalClient.RecoverAll(c.Bool("local"))
|
|
|
|
formatResponse(c, resp, cerr)
|
2017-01-25 18:38:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2018-01-18 02:49:35 +00:00
|
|
|
|
2017-01-24 15:24:46 +00:00
|
|
|
{
|
|
|
|
Name: "version",
|
|
|
|
Usage: "Retrieve cluster version",
|
2017-07-03 16:36:00 +00:00
|
|
|
Description: `
|
2017-01-24 15:24:46 +00:00
|
|
|
This command retrieves the IPFS Cluster version and can be used
|
|
|
|
to check that it matches the CLI version (shown by -v).
|
|
|
|
`,
|
2017-07-03 16:36:00 +00:00
|
|
|
ArgsUsage: " ",
|
2017-12-05 20:44:22 +00:00
|
|
|
Flags: []cli.Flag{},
|
2017-01-24 18:55:06 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-12-05 20:44:22 +00:00
|
|
|
resp, cerr := globalClient.Version()
|
|
|
|
formatResponse(c, resp, cerr)
|
2017-02-08 18:52:34 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2018-01-18 02:49:35 +00:00
|
|
|
{
|
|
|
|
Name: "health",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "Cluster monitoring information",
|
|
|
|
Description: "Cluster monitoring information",
|
2018-01-18 02:49:35 +00:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
{
|
|
|
|
Name: "graph",
|
2018-07-23 09:14:43 +00:00
|
|
|
Usage: "create a graph displaying connectivity of cluster peers",
|
2018-01-18 02:49:35 +00:00
|
|
|
Description: `
|
2018-01-23 13:33:12 +00:00
|
|
|
This command queries all connected cluster peers and their ipfs peers to generate a
|
2018-01-18 02:49:35 +00:00
|
|
|
graph of the connections. Output is a dot file encoding the cluster's connection state.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "file, f",
|
|
|
|
Value: "",
|
|
|
|
Usage: "sets an output dot-file for the connectivity graph",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all-ipfs-peers",
|
|
|
|
Usage: "causes the graph to mark nodes for ipfs peers not directly in the cluster",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
resp, cerr := globalClient.GetConnectGraph()
|
|
|
|
if cerr != nil {
|
|
|
|
formatResponse(c, resp, cerr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var w io.WriteCloser
|
|
|
|
var err error
|
|
|
|
outputPath := c.String("file")
|
|
|
|
if outputPath == "" {
|
|
|
|
w = os.Stdout
|
|
|
|
} else {
|
|
|
|
w, err = os.Create(outputPath)
|
|
|
|
checkErr("creating output file", err)
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
err = makeDot(resp, w, c.Bool("all-ipfs-peers"))
|
|
|
|
checkErr("printing graph", err)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2018-10-07 16:32:46 +00:00
|
|
|
{
|
|
|
|
Name: "metrics",
|
2018-10-21 06:34:50 +00:00
|
|
|
Usage: "List latest metrics logged by this peer",
|
2018-10-07 16:32:46 +00:00
|
|
|
Description: `
|
2018-10-21 06:34:50 +00:00
|
|
|
This commands displays the latest valid metrics of the given type logged
|
|
|
|
by this peer for all current cluster peers.
|
2018-10-23 18:21:27 +00:00
|
|
|
|
|
|
|
Currently supported metrics depend on the informer component used,
|
|
|
|
but usually are:
|
|
|
|
|
|
|
|
- freespace
|
|
|
|
- ping
|
2018-10-07 16:32:46 +00:00
|
|
|
`,
|
2018-10-23 18:21:27 +00:00
|
|
|
ArgsUsage: "<metric name>",
|
2018-10-07 16:32:46 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2018-10-23 18:21:27 +00:00
|
|
|
metric := c.Args().First()
|
|
|
|
if metric == "" {
|
|
|
|
checkErr("", errors.New("provide a metric name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, cerr := globalClient.Metrics(metric)
|
2018-10-07 16:32:46 +00:00
|
|
|
formatResponse(c, resp, cerr)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
2018-01-18 02:49:35 +00:00
|
|
|
},
|
|
|
|
},
|
2017-02-08 18:52:34 +00:00
|
|
|
{
|
2017-07-03 16:36:00 +00:00
|
|
|
Name: "commands",
|
|
|
|
Usage: "List all commands",
|
|
|
|
ArgsUsage: " ",
|
|
|
|
Hidden: true,
|
2017-02-08 18:52:34 +00:00
|
|
|
Action: func(c *cli.Context) error {
|
2017-07-03 16:36:00 +00:00
|
|
|
walkCommands(c.App.Commands, "ipfs-cluster-ctl")
|
2017-01-24 18:55:06 +00:00
|
|
|
return nil
|
2017-01-24 15:24:46 +00:00
|
|
|
},
|
|
|
|
},
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:06:17 +00:00
|
|
|
app.Run(os.Args)
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 18:52:34 +00:00
|
|
|
func parseFlag(t int) cli.IntFlag {
|
|
|
|
return cli.IntFlag{
|
|
|
|
Name: "parseAs",
|
|
|
|
Value: t,
|
|
|
|
Hidden: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 11:56:26 +00:00
|
|
|
func localFlag() cli.BoolFlag {
|
|
|
|
return cli.BoolFlag{
|
|
|
|
Name: "local",
|
|
|
|
Usage: "run operation only on the contacted peer",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 16:36:00 +00:00
|
|
|
func walkCommands(cmds []cli.Command, parentHelpName string) {
|
2017-02-08 18:52:34 +00:00
|
|
|
for _, c := range cmds {
|
2017-07-14 19:22:52 +00:00
|
|
|
h := c.HelpName
|
|
|
|
// Sometimes HelpName is empty
|
|
|
|
if h == "" {
|
2017-07-03 16:36:00 +00:00
|
|
|
h = fmt.Sprintf("%s %s", parentHelpName, c.FullName())
|
|
|
|
}
|
2017-07-14 19:22:52 +00:00
|
|
|
fmt.Println(h)
|
2017-07-03 16:36:00 +00:00
|
|
|
walkCommands(c.Subcommands, h)
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:12:57 +00:00
|
|
|
func formatResponse(c *cli.Context, resp interface{}, err error) {
|
2017-12-05 20:44:22 +00:00
|
|
|
enc := c.GlobalString("encoding")
|
|
|
|
if resp == nil && err == nil {
|
|
|
|
return
|
2017-08-29 01:41:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:44:22 +00:00
|
|
|
if err != nil {
|
2017-12-08 15:12:57 +00:00
|
|
|
cerr, ok := err.(*api.Error)
|
|
|
|
if !ok {
|
2018-08-20 10:08:49 +00:00
|
|
|
checkErr("", err)
|
2017-12-08 15:12:57 +00:00
|
|
|
}
|
2017-02-08 18:52:34 +00:00
|
|
|
switch enc {
|
|
|
|
case "text":
|
2017-12-08 15:12:57 +00:00
|
|
|
textFormatPrintError(cerr)
|
2017-12-05 20:44:22 +00:00
|
|
|
case "json":
|
2017-12-08 15:12:57 +00:00
|
|
|
jsonFormatPrint(cerr)
|
2017-02-08 18:52:34 +00:00
|
|
|
default:
|
2017-12-05 20:44:22 +00:00
|
|
|
checkErr("", errors.New("unsupported encoding selected"))
|
2017-02-08 18:52:34 +00:00
|
|
|
}
|
2017-12-08 15:12:57 +00:00
|
|
|
if cerr.Code == 0 {
|
2017-12-07 11:00:28 +00:00
|
|
|
os.Exit(1) // problem with the call
|
|
|
|
} else {
|
|
|
|
os.Exit(2) // call went fine, response has an error
|
|
|
|
}
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
2017-12-05 20:44:22 +00:00
|
|
|
|
|
|
|
switch enc {
|
|
|
|
case "text":
|
|
|
|
textFormatObject(resp)
|
|
|
|
case "json":
|
|
|
|
jsonFormatObject(resp)
|
|
|
|
default:
|
|
|
|
checkErr("", errors.New("unsupported encoding selected"))
|
|
|
|
}
|
2016-12-22 16:14:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 01:41:54 +00:00
|
|
|
func parseCredentials(userInput string) (string, string) {
|
|
|
|
credentials := strings.SplitN(userInput, ":", 2)
|
|
|
|
switch len(credentials) {
|
|
|
|
case 1:
|
|
|
|
// only username passed in (with no trailing `:`), return empty password
|
|
|
|
return credentials[0], ""
|
|
|
|
case 2:
|
|
|
|
return credentials[0], credentials[1]
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("invalid <username>[:<password>] input")
|
|
|
|
checkErr("parsing credentials", err)
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 14:04:21 +00:00
|
|
|
|
2018-03-29 08:33:09 +00:00
|
|
|
func handlePinResponseFormatFlags(
|
2018-03-28 14:04:21 +00:00
|
|
|
c *cli.Context,
|
2018-09-22 01:00:10 +00:00
|
|
|
ci cid.Cid,
|
2018-03-28 14:04:21 +00:00
|
|
|
target api.TrackerStatus,
|
|
|
|
) {
|
|
|
|
|
|
|
|
var status api.GlobalPinInfo
|
|
|
|
var cerr error
|
|
|
|
|
2018-03-29 08:33:09 +00:00
|
|
|
if c.Bool("wait") {
|
|
|
|
status, cerr = waitFor(ci, target, c.Duration("wait-timeout"))
|
2018-03-28 14:04:21 +00:00
|
|
|
checkErr("waiting for pin status", cerr)
|
|
|
|
}
|
|
|
|
|
2018-03-29 08:33:09 +00:00
|
|
|
if c.Bool("no-status") {
|
2018-03-28 14:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-22 01:00:10 +00:00
|
|
|
if status.Cid == cid.Undef { // no status from "wait"
|
2018-03-29 08:33:09 +00:00
|
|
|
time.Sleep(time.Second)
|
2018-10-01 10:53:42 +00:00
|
|
|
status, cerr = globalClient.Status(ci, false)
|
2018-03-28 14:04:21 +00:00
|
|
|
}
|
|
|
|
formatResponse(c, status, cerr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitFor(
|
2018-09-22 01:00:10 +00:00
|
|
|
ci cid.Cid,
|
2018-03-28 14:04:21 +00:00
|
|
|
target api.TrackerStatus,
|
|
|
|
timeout time.Duration,
|
|
|
|
) (api.GlobalPinInfo, error) {
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if timeout > defaultWaitCheckFreq {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, timeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
fp := client.StatusFilterParams{
|
|
|
|
Cid: ci,
|
|
|
|
Local: false,
|
|
|
|
Target: target,
|
|
|
|
CheckFreq: defaultWaitCheckFreq,
|
|
|
|
}
|
|
|
|
|
2018-08-30 23:14:06 +00:00
|
|
|
return client.WaitFor(ctx, globalClient, fp)
|
2018-03-28 14:04:21 +00:00
|
|
|
}
|