2019-06-25 11:42:17 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-07-09 12:08:43 +00:00
|
|
|
"context"
|
2019-12-09 12:36:56 +00:00
|
|
|
"fmt"
|
2019-07-23 18:49:09 +00:00
|
|
|
"net/http"
|
2019-07-09 12:08:43 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-12-09 12:36:56 +00:00
|
|
|
"strings"
|
2019-07-09 12:08:43 +00:00
|
|
|
"syscall"
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-11-21 16:10:04 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-12-09 12:36:56 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2019-07-10 17:28:49 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
2019-07-26 11:45:25 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-06-25 11:42:17 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2019-07-10 17:28:49 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
|
|
|
"github.com/filecoin-project/lotus/lib/jsonrpc"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2019-06-25 11:42:17 +00:00
|
|
|
)
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
var log = logging.Logger("cli")
|
|
|
|
|
2019-07-09 12:08:43 +00:00
|
|
|
const (
|
2019-07-25 20:19:43 +00:00
|
|
|
metadataTraceConetxt = "traceContext"
|
2019-07-09 12:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApiConnector returns API instance
|
2019-07-24 00:09:34 +00:00
|
|
|
type ApiConnector func() api.FullNode
|
2019-07-08 19:07:16 +00:00
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
type APIInfo struct {
|
|
|
|
Addr multiaddr.Multiaddr
|
|
|
|
Token []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a APIInfo) DialArgs() (string, error) {
|
|
|
|
_, addr, err := manet.DialArgs(a.Addr)
|
|
|
|
|
|
|
|
return "ws://" + addr + "/rpc/v0", err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a APIInfo) AuthHeader() http.Header {
|
|
|
|
if len(a.Token) != 0 {
|
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("Authorization", "Bearer "+string(a.Token))
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
log.Warn("API Token not set and requested, capabilities might be limited.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func flagForRepo(t repo.RepoType) string {
|
|
|
|
switch t {
|
|
|
|
case repo.FullNode:
|
|
|
|
return "repo"
|
|
|
|
case repo.StorageMiner:
|
|
|
|
return "storagerepo"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown repo type: %v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func envForRepo(t repo.RepoType) string {
|
|
|
|
switch t {
|
|
|
|
case repo.FullNode:
|
|
|
|
return "FULLNODE_API_INFO"
|
|
|
|
case repo.StorageMiner:
|
|
|
|
return "STORAGE_API_INFO"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown repo type: %v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|
|
|
if env, ok := os.LookupEnv(envForRepo(t)); ok {
|
|
|
|
sp := strings.SplitN(env, ":", 2)
|
|
|
|
if len(sp) != 2 {
|
|
|
|
log.Warnf("invalid env(%s) value, missing token or address", envForRepo(t))
|
|
|
|
} else {
|
|
|
|
ma, err := multiaddr.NewMultiaddr(sp[1])
|
|
|
|
if err != nil {
|
|
|
|
return APIInfo{}, xerrors.Errorf("could not parse multiaddr from env(%s): %w", envForRepo(t), err)
|
|
|
|
}
|
|
|
|
return APIInfo{
|
|
|
|
Addr: ma,
|
|
|
|
Token: []byte(sp[0]),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoFlag := flagForRepo(t)
|
|
|
|
|
2019-11-21 16:10:04 +00:00
|
|
|
p, err := homedir.Expand(ctx.String(repoFlag))
|
2019-07-10 17:28:49 +00:00
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
return APIInfo{}, xerrors.Errorf("cound not exand home dir (%s): %w", repoFlag, err)
|
2019-11-21 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := repo.NewFS(p)
|
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
return APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err)
|
2019-07-10 17:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ma, err := r.APIEndpoint()
|
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
return APIInfo{}, xerrors.Errorf("could not get api enpoint: %w", err)
|
2019-07-10 17:28:49 +00:00
|
|
|
}
|
2019-12-09 12:36:56 +00:00
|
|
|
|
|
|
|
token, err := r.APIToken()
|
2019-11-21 16:10:04 +00:00
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
log.Warnf("Couldn't load CLI token, capabilities may be limited: %v", err)
|
2019-11-21 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
return APIInfo{
|
|
|
|
Addr: ma,
|
|
|
|
Token: token,
|
|
|
|
}, nil
|
2019-11-21 16:10:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
func GetRawAPI(ctx *cli.Context, t repo.RepoType) (string, http.Header, error) {
|
2019-11-21 16:10:04 +00:00
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
ainfo, err := GetAPIInfo(ctx, t)
|
2019-07-11 11:52:07 +00:00
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
return "", nil, xerrors.Errorf("could not get API info: %w", err)
|
2019-07-11 11:52:07 +00:00
|
|
|
}
|
2019-11-21 16:10:04 +00:00
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
addr, err := ainfo.DialArgs()
|
2019-07-23 18:49:09 +00:00
|
|
|
if err != nil {
|
2019-12-09 12:36:56 +00:00
|
|
|
return "", nil, xerrors.Errorf("could not get DialArgs: %w", err)
|
2019-07-23 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
return addr, ainfo.AuthHeader(), nil
|
2019-07-27 12:18:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
func GetAPI(ctx *cli.Context) (api.Common, jsonrpc.ClientCloser, error) {
|
2019-12-09 12:36:56 +00:00
|
|
|
ti, ok := ctx.App.Metadata["repoType"]
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("unknown repo type, are you sure you want to use GetAPI?")
|
|
|
|
ti = repo.FullNode
|
|
|
|
}
|
|
|
|
t, ok := ti.(repo.RepoType)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("repoType type does not match the type of repo.RepoType")
|
2019-08-02 16:18:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 12:36:56 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, t)
|
2019-08-02 16:18:44 +00:00
|
|
|
if err != nil {
|
2019-10-03 18:12:30 +00:00
|
|
|
return nil, nil, err
|
2019-08-02 16:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewCommonRPC(addr, headers)
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
func GetFullNodeAPI(ctx *cli.Context) (api.FullNode, jsonrpc.ClientCloser, error) {
|
2019-12-09 12:36:56 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.FullNode)
|
2019-07-27 12:18:36 +00:00
|
|
|
if err != nil {
|
2019-10-03 18:12:30 +00:00
|
|
|
return nil, nil, err
|
2019-07-27 12:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewFullNodeRPC(addr, headers)
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
func GetStorageMinerAPI(ctx *cli.Context) (api.StorageMiner, jsonrpc.ClientCloser, error) {
|
2019-12-09 12:36:56 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.StorageMiner)
|
2019-07-27 12:18:36 +00:00
|
|
|
if err != nil {
|
2019-10-03 18:12:30 +00:00
|
|
|
return nil, nil, err
|
2019-07-27 12:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return client.NewStorageMinerRPC(addr, headers)
|
2019-07-09 12:08:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 18:36:06 +00:00
|
|
|
func DaemonContext(cctx *cli.Context) context.Context {
|
|
|
|
if mtCtx, ok := cctx.App.Metadata[metadataTraceConetxt]; ok {
|
|
|
|
return mtCtx.(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
2019-07-18 23:16:23 +00:00
|
|
|
// ReqContext returns context for cli execution. Calling it for the first time
|
2019-07-09 12:08:43 +00:00
|
|
|
// installs SIGTERM handler that will close returned context.
|
|
|
|
// Not safe for concurrent execution.
|
2019-07-18 23:16:23 +00:00
|
|
|
func ReqContext(cctx *cli.Context) context.Context {
|
2019-09-17 18:36:06 +00:00
|
|
|
tCtx := DaemonContext(cctx)
|
2019-07-25 20:19:43 +00:00
|
|
|
|
|
|
|
ctx, done := context.WithCancel(tCtx)
|
2019-07-09 12:08:43 +00:00
|
|
|
sigChan := make(chan os.Signal, 2)
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
|
|
|
done()
|
|
|
|
}()
|
|
|
|
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
|
|
|
|
return ctx
|
2019-07-08 19:07:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 11:42:17 +00:00
|
|
|
var Commands = []*cli.Command{
|
2019-09-18 17:53:48 +00:00
|
|
|
authCmd,
|
2019-07-09 15:19:27 +00:00
|
|
|
chainCmd,
|
2019-07-20 21:55:51 +00:00
|
|
|
clientCmd,
|
2019-08-13 04:27:54 +00:00
|
|
|
createMinerCmd,
|
2019-10-04 19:55:33 +00:00
|
|
|
fetchParamCmd,
|
2019-07-20 21:55:51 +00:00
|
|
|
mpoolCmd,
|
2019-07-08 19:07:16 +00:00
|
|
|
netCmd,
|
2019-08-13 04:27:54 +00:00
|
|
|
paychCmd,
|
|
|
|
sendCmd,
|
|
|
|
stateCmd,
|
2019-09-30 21:06:47 +00:00
|
|
|
syncCmd,
|
2019-06-25 11:42:17 +00:00
|
|
|
versionCmd,
|
2019-07-13 00:41:32 +00:00
|
|
|
walletCmd,
|
2019-06-28 09:03:28 +00:00
|
|
|
}
|