2021-03-05 20:08:14 +00:00
|
|
|
package cliutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2021-04-01 12:17:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2021-04-06 11:36:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
2021-03-05 20:08:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
metadataTraceContext = "traceContext"
|
|
|
|
)
|
|
|
|
|
2021-07-29 11:54:23 +00:00
|
|
|
// flagsForAPI returns flags passed on the command line with the listen address
|
|
|
|
// of the API server (only used by the tests), in the order of precedence they
|
|
|
|
// should be applied for the requested kind of node.
|
2021-07-29 09:55:37 +00:00
|
|
|
func flagsForAPI(t repo.RepoType) []string {
|
2021-03-05 20:08:14 +00:00
|
|
|
switch t {
|
|
|
|
case repo.FullNode:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"api-url"}
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.StorageMiner:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"miner-api-url"}
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.Worker:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"worker-api-url"}
|
|
|
|
case repo.Markets:
|
|
|
|
// support split markets-miner and monolith deployments.
|
|
|
|
return []string{"markets-api-url", "miner-api-url"}
|
2021-03-05 20:08:14 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown repo type: %v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
func flagsForRepo(t repo.RepoType) []string {
|
2021-03-05 20:08:14 +00:00
|
|
|
switch t {
|
|
|
|
case repo.FullNode:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"repo"}
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.StorageMiner:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"miner-repo"}
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.Worker:
|
2021-07-29 09:55:37 +00:00
|
|
|
return []string{"worker-repo"}
|
|
|
|
case repo.Markets:
|
|
|
|
// support split markets-miner and monolith deployments.
|
|
|
|
return []string{"markets-repo", "miner-repo"}
|
2021-03-05 20:08:14 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown repo type: %v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
// EnvsForAPIInfos returns the environment variables to use in order of precedence
|
2021-07-29 09:55:37 +00:00
|
|
|
// to determine the API endpoint of the specified node type.
|
|
|
|
//
|
|
|
|
// It returns the current variables and deprecated ones separately, so that
|
|
|
|
// the user can log a warning when deprecated ones are found to be in use.
|
2021-07-29 15:10:04 +00:00
|
|
|
func EnvsForAPIInfos(t repo.RepoType) (primary string, fallbacks []string, deprecated []string) {
|
2021-03-05 20:08:14 +00:00
|
|
|
switch t {
|
|
|
|
case repo.FullNode:
|
2021-07-29 15:10:04 +00:00
|
|
|
return "FULLNODE_API_INFO", nil, nil
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.StorageMiner:
|
2021-07-29 09:55:37 +00:00
|
|
|
// TODO remove deprecated deprecation period
|
2021-07-29 15:10:04 +00:00
|
|
|
return "MINER_API_INFO", nil, []string{"STORAGE_API_INFO"}
|
2021-03-05 20:08:14 +00:00
|
|
|
case repo.Worker:
|
2021-07-29 15:10:04 +00:00
|
|
|
return "WORKER_API_INFO", nil, nil
|
2021-07-29 09:55:37 +00:00
|
|
|
case repo.Markets:
|
|
|
|
// support split markets-miner and monolith deployments.
|
2021-07-29 15:10:04 +00:00
|
|
|
return "MARKETS_API_INFO", []string{"MINER_API_INFO"}, nil
|
2021-03-05 20:08:14 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown repo type: %v", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
// GetAPIInfo returns the API endpoint to use for the specified kind of repo.
|
|
|
|
//
|
|
|
|
// The order of precedence is as follows:
|
|
|
|
//
|
|
|
|
// 1. *-api-url command line flags.
|
|
|
|
// 2. *_API_INFO environment variables
|
|
|
|
// 3. deprecated *_API_INFO environment variables
|
|
|
|
// 4. *-repo command line flags.
|
2021-03-05 20:08:14 +00:00
|
|
|
func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
|
|
|
|
// Check if there was a flag passed with the listen address of the API
|
|
|
|
// server (only used by the tests)
|
2021-07-29 09:55:37 +00:00
|
|
|
apiFlags := flagsForAPI(t)
|
|
|
|
for _, f := range apiFlags {
|
|
|
|
if !ctx.IsSet(f) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
strma := ctx.String(f)
|
2021-03-05 20:08:14 +00:00
|
|
|
strma = strings.TrimSpace(strma)
|
|
|
|
|
|
|
|
return APIInfo{Addr: strma}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
//
|
|
|
|
// Note: it is not correct/intuitive to prefer environment variables over
|
|
|
|
// CLI flags (repo flags below).
|
|
|
|
//
|
|
|
|
primaryEnv, fallbacksEnvs, deprecatedEnvs := EnvsForAPIInfos(t)
|
|
|
|
env, ok := os.LookupEnv(primaryEnv)
|
|
|
|
if ok {
|
|
|
|
return ParseApiInfo(env), nil
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
2021-07-29 09:55:37 +00:00
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
for _, env := range deprecatedEnvs {
|
2021-07-29 09:55:37 +00:00
|
|
|
env, ok := os.LookupEnv(env)
|
|
|
|
if ok {
|
2021-07-29 15:10:04 +00:00
|
|
|
log.Warnf("Using deprecated env(%s) value, please use env(%s) instead.", env, primaryEnv)
|
2021-07-29 09:55:37 +00:00
|
|
|
return ParseApiInfo(env), nil
|
|
|
|
}
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
repoFlags := flagsForRepo(t)
|
|
|
|
for _, f := range repoFlags {
|
2021-07-29 12:49:47 +00:00
|
|
|
// cannot use ctx.IsSet because it ignores default values
|
2021-07-29 13:36:04 +00:00
|
|
|
path := ctx.String(f)
|
|
|
|
if path == "" {
|
2021-07-29 09:55:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-05 20:08:14 +00:00
|
|
|
|
2021-07-29 13:36:04 +00:00
|
|
|
p, err := homedir.Expand(path)
|
2021-07-29 09:55:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return APIInfo{}, xerrors.Errorf("could not expand home dir (%s): %w", f, err)
|
|
|
|
}
|
2021-03-05 20:08:14 +00:00
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
r, err := repo.NewFS(p)
|
|
|
|
if err != nil {
|
|
|
|
return APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err)
|
|
|
|
}
|
2021-03-05 20:08:14 +00:00
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
ma, err := r.APIEndpoint()
|
|
|
|
if err != nil {
|
|
|
|
return APIInfo{}, xerrors.Errorf("could not get api endpoint: %w", err)
|
|
|
|
}
|
2021-03-05 20:08:14 +00:00
|
|
|
|
2021-07-29 09:55:37 +00:00
|
|
|
token, err := r.APIToken()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("Couldn't load CLI token, capabilities may be limited: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return APIInfo{
|
|
|
|
Addr: ma.String(),
|
|
|
|
Token: token,
|
|
|
|
}, nil
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
for _, env := range fallbacksEnvs {
|
|
|
|
env, ok := os.LookupEnv(env)
|
|
|
|
if ok {
|
|
|
|
return ParseApiInfo(env), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:21:55 +00:00
|
|
|
return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %v", t)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 18:15:44 +00:00
|
|
|
func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.Header, error) {
|
2021-03-05 20:08:14 +00:00
|
|
|
ainfo, err := GetAPIInfo(ctx, t)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, xerrors.Errorf("could not get API info: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:15:44 +00:00
|
|
|
addr, err := ainfo.DialArgs(version)
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil, xerrors.Errorf("could not get DialArgs: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintf(ctx.App.Writer, "using raw API %s endpoint: %s\n", version, addr)
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:08:14 +00:00
|
|
|
return addr, ainfo.AuthHeader(), nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:37:29 +00:00
|
|
|
func GetCommonAPI(ctx *cli.Context) (api.CommonNet, jsonrpc.ClientCloser, error) {
|
2021-03-05 20:08:14 +00:00
|
|
|
ti, ok := ctx.App.Metadata["repoType"]
|
|
|
|
if !ok {
|
2021-07-29 11:37:29 +00:00
|
|
|
log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?")
|
2021-03-05 20:08:14 +00:00
|
|
|
ti = repo.FullNode
|
|
|
|
}
|
|
|
|
t, ok := ti.(repo.RepoType)
|
|
|
|
if !ok {
|
|
|
|
log.Errorf("repoType type does not match the type of repo.RepoType")
|
|
|
|
}
|
|
|
|
|
|
|
|
if tn, ok := ctx.App.Metadata["testnode-storage"]; ok {
|
|
|
|
return tn.(api.StorageMiner), func() {}, nil
|
|
|
|
}
|
|
|
|
if tn, ok := ctx.App.Metadata["testnode-full"]; ok {
|
|
|
|
return tn.(api.FullNode), func() {}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:15:44 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, t, "v0")
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
return client.NewCommonRPCV0(ctx.Context, addr, headers)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 12:17:22 +00:00
|
|
|
func GetFullNodeAPI(ctx *cli.Context) (v0api.FullNode, jsonrpc.ClientCloser, error) {
|
2021-03-05 20:08:14 +00:00
|
|
|
if tn, ok := ctx.App.Metadata["testnode-full"]; ok {
|
2021-04-05 19:34:03 +00:00
|
|
|
return &v0api.WrapperV1Full{FullNode: tn.(v1api.FullNode)}, func() {}, nil
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:07:56 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v0")
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using full node API v0 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-04-01 12:17:22 +00:00
|
|
|
return client.NewFullNodeRPCV0(ctx.Context, addr, headers)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:47:10 +00:00
|
|
|
func GetFullNodeAPIV1(ctx *cli.Context) (v1api.FullNode, jsonrpc.ClientCloser, error) {
|
|
|
|
if tn, ok := ctx.App.Metadata["testnode-full"]; ok {
|
|
|
|
return tn.(v1api.FullNode), func() {}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v1")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using full node API v1 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:47:10 +00:00
|
|
|
return client.NewFullNodeRPCV1(ctx.Context, addr, headers)
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:08:14 +00:00
|
|
|
type GetStorageMinerOptions struct {
|
|
|
|
PreferHttp bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetStorageMinerOption func(*GetStorageMinerOptions)
|
|
|
|
|
|
|
|
func StorageMinerUseHttp(opts *GetStorageMinerOptions) {
|
|
|
|
opts.PreferHttp = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStorageMinerAPI(ctx *cli.Context, opts ...GetStorageMinerOption) (api.StorageMiner, jsonrpc.ClientCloser, error) {
|
|
|
|
var options GetStorageMinerOptions
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tn, ok := ctx.App.Metadata["testnode-storage"]; ok {
|
|
|
|
return tn.(api.StorageMiner), func() {}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:15:44 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.StorageMiner, "v0")
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.PreferHttp {
|
|
|
|
u, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, xerrors.Errorf("parsing miner api URL: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch u.Scheme {
|
|
|
|
case "ws":
|
|
|
|
u.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
u.Scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = u.String()
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using miner API v0 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
return client.NewStorageMinerRPCV0(ctx.Context, addr, headers)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func GetWorkerAPI(ctx *cli.Context) (api.Worker, jsonrpc.ClientCloser, error) {
|
2021-03-23 18:15:44 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.Worker, "v0")
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using worker API v0 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
return client.NewWorkerRPCV0(ctx.Context, addr, headers)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:37:29 +00:00
|
|
|
func GetMarketsAPI(ctx *cli.Context) (api.StorageMiner, jsonrpc.ClientCloser, error) {
|
2021-07-29 12:21:55 +00:00
|
|
|
// to support lotus-miner cli tests.
|
|
|
|
if tn, ok := ctx.App.Metadata["testnode-storage"]; ok {
|
|
|
|
return tn.(api.StorageMiner), func() {}, nil
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:37:29 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.Markets, "v0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if IsVeryVerbose {
|
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using markets API v0 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the markets node is a specialised miner's node, supporting only the
|
|
|
|
// markets API, which is a subset of the miner API. All non-markets
|
|
|
|
// operations will error out with "unsupported".
|
|
|
|
return client.NewStorageMinerRPCV0(ctx.Context, addr, headers)
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:42:56 +00:00
|
|
|
func GetGatewayAPI(ctx *cli.Context) (api.Gateway, jsonrpc.ClientCloser, error) {
|
2021-04-20 16:42:12 +00:00
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v1")
|
2021-03-05 20:08:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using gateway API v1 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
return client.NewGatewayRPCV1(ctx.Context, addr, headers)
|
2021-03-05 20:08:14 +00:00
|
|
|
}
|
|
|
|
|
2021-04-20 16:42:12 +00:00
|
|
|
func GetGatewayAPIV0(ctx *cli.Context) (v0api.Gateway, jsonrpc.ClientCloser, error) {
|
|
|
|
addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:46:02 +00:00
|
|
|
if IsVeryVerbose {
|
2021-07-27 13:25:28 +00:00
|
|
|
_, _ = fmt.Fprintln(ctx.App.Writer, "using gateway API v0 endpoint:", addr)
|
|
|
|
}
|
|
|
|
|
2021-04-20 16:42:12 +00:00
|
|
|
return client.NewGatewayRPCV0(ctx.Context, addr, headers)
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:08:14 +00:00
|
|
|
func DaemonContext(cctx *cli.Context) context.Context {
|
|
|
|
if mtCtx, ok := cctx.App.Metadata[metadataTraceContext]; ok {
|
|
|
|
return mtCtx.(context.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReqContext returns context for cli execution. Calling it for the first time
|
|
|
|
// installs SIGTERM handler that will close returned context.
|
|
|
|
// Not safe for concurrent execution.
|
|
|
|
func ReqContext(cctx *cli.Context) context.Context {
|
|
|
|
tCtx := DaemonContext(cctx)
|
|
|
|
|
|
|
|
ctx, done := context.WithCancel(tCtx)
|
|
|
|
sigChan := make(chan os.Signal, 2)
|
|
|
|
go func() {
|
|
|
|
<-sigChan
|
|
|
|
done()
|
|
|
|
}()
|
|
|
|
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|