support MARKETS_API_INFO env var; support markets-repo, markets-api-url flags.

This commit is contained in:
Raúl Kripalani 2021-07-29 10:55:37 +01:00 committed by Jennifer Wang
parent 7b1a82a3cc
commit 22461f30e9
3 changed files with 28 additions and 71 deletions

View File

@ -113,7 +113,7 @@ var AuthApiInfoToken = &cli.Command{
ti, ok := cctx.App.Metadata["repoType"] ti, ok := cctx.App.Metadata["repoType"]
if !ok { if !ok {
log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?") log.Errorf("unknown repo type, are you sure you want to use GetAPI?")
ti = repo.FullNode ti = repo.FullNode
} }
t, ok := ti.(repo.RepoType) t, ok := ti.(repo.RepoType)
@ -128,7 +128,7 @@ var AuthApiInfoToken = &cli.Command{
// TODO: Log in audit log when it is implemented // TODO: Log in audit log when it is implemented
currentEnv, _, _ := cliutil.EnvsForAPIInfos(t) currentEnv, _ := cliutil.EnvsForRepo(t)
fmt.Printf("%s=%s:%s\n", currentEnv, string(token), ainfo.Addr) fmt.Printf("%s=%s:%s\n", currentEnv, string(token), ainfo.Addr)
return nil return nil
}, },

View File

@ -44,7 +44,7 @@ func GetFullNodeServices(ctx *cli.Context) (ServicesAPI, error) {
var GetAPIInfo = cliutil.GetAPIInfo var GetAPIInfo = cliutil.GetAPIInfo
var GetRawAPI = cliutil.GetRawAPI var GetRawAPI = cliutil.GetRawAPI
var GetAPI = cliutil.GetCommonAPI var GetAPI = cliutil.GetAPI
var DaemonContext = cliutil.DaemonContext var DaemonContext = cliutil.DaemonContext
var ReqContext = cliutil.ReqContext var ReqContext = cliutil.ReqContext
@ -54,7 +54,6 @@ var GetFullNodeAPIV1 = cliutil.GetFullNodeAPIV1
var GetGatewayAPI = cliutil.GetGatewayAPI var GetGatewayAPI = cliutil.GetGatewayAPI
var GetStorageMinerAPI = cliutil.GetStorageMinerAPI var GetStorageMinerAPI = cliutil.GetStorageMinerAPI
var GetMarketsAPI = cliutil.GetMarketsAPI
var GetWorkerAPI = cliutil.GetWorkerAPI var GetWorkerAPI = cliutil.GetWorkerAPI
var CommonCommands = []*cli.Command{ var CommonCommands = []*cli.Command{

View File

@ -2,7 +2,6 @@ package cliutil
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -28,9 +27,9 @@ const (
metadataTraceContext = "traceContext" metadataTraceContext = "traceContext"
) )
// flagsForAPI returns flags passed on the command line with the listen address // The flags passed on the command line with the listen address of the API
// of the API server (only used by the tests), in the order of precedence they // server (only used by the tests), in the order of precedence they should be
// should be applied for the requested kind of node. // applied for the requested kind of node.
func flagsForAPI(t repo.RepoType) []string { func flagsForAPI(t repo.RepoType) []string {
switch t { switch t {
case repo.FullNode: case repo.FullNode:
@ -63,23 +62,23 @@ func flagsForRepo(t repo.RepoType) []string {
} }
} }
// EnvsForAPIInfos returns the environment variables to use in order of precedence // EnvsForRepo returns the environment variables to use in order of precedence
// to determine the API endpoint of the specified node type. // to determine the API endpoint of the specified node type.
// //
// It returns the current variables and deprecated ones separately, so that // 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. // the user can log a warning when deprecated ones are found to be in use.
func EnvsForAPIInfos(t repo.RepoType) (primary string, fallbacks []string, deprecated []string) { func EnvsForRepo(t repo.RepoType) (current []string, deprecated []string) {
switch t { switch t {
case repo.FullNode: case repo.FullNode:
return "FULLNODE_API_INFO", nil, nil return []string{"FULLNODE_API_INFO"}, nil
case repo.StorageMiner: case repo.StorageMiner:
// TODO remove deprecated deprecation period // TODO remove deprecated deprecation period
return "MINER_API_INFO", nil, []string{"STORAGE_API_INFO"} return []string{"MINER_API_INFO"}, []string{"STORAGE_API_INFO"}
case repo.Worker: case repo.Worker:
return "WORKER_API_INFO", nil, nil return []string{"WORKER_API_INFO"}, nil
case repo.Markets: case repo.Markets:
// support split markets-miner and monolith deployments. // support split markets-miner and monolith deployments.
return "MARKETS_API_INFO", []string{"MINER_API_INFO"}, nil return []string{"MARKETS_API_INFO, MINER_API_INFO"}, nil
default: default:
panic(fmt.Sprintf("Unknown repo type: %v", t)) panic(fmt.Sprintf("Unknown repo type: %v", t))
} }
@ -107,33 +106,29 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
return APIInfo{Addr: strma}, nil return APIInfo{Addr: strma}, nil
} }
// currentEnv, deprecatedEnv := EnvsForRepo(t)
// Note: it is not correct/intuitive to prefer environment variables over for _, env := range currentEnv {
// CLI flags (repo flags below).
//
primaryEnv, fallbacksEnvs, deprecatedEnvs := EnvsForAPIInfos(t)
env, ok := os.LookupEnv(primaryEnv)
if ok {
return ParseApiInfo(env), nil
}
for _, env := range deprecatedEnvs {
env, ok := os.LookupEnv(env) env, ok := os.LookupEnv(env)
if ok { if ok {
log.Warnf("Using deprecated env(%s) value, please use env(%s) instead.", env, primaryEnv) return ParseApiInfo(env), nil
}
}
for _, env := range deprecatedEnv {
env, ok := os.LookupEnv(env)
if ok {
log.Warnf("Use deprecation env(%s) value, please use env(%s) instead.", env, currentEnv)
return ParseApiInfo(env), nil return ParseApiInfo(env), nil
} }
} }
repoFlags := flagsForRepo(t) repoFlags := flagsForRepo(t)
for _, f := range repoFlags { for _, f := range repoFlags {
// cannot use ctx.IsSet because it ignores default values if !ctx.IsSet(f) {
path := ctx.String(f)
if path == "" {
continue continue
} }
p, err := homedir.Expand(path) p, err := homedir.Expand(ctx.String(f))
if err != nil { if err != nil {
return APIInfo{}, xerrors.Errorf("could not expand home dir (%s): %w", f, err) return APIInfo{}, xerrors.Errorf("could not expand home dir (%s): %w", f, err)
} }
@ -143,15 +138,6 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
return APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err) return APIInfo{}, xerrors.Errorf("could not open repo at path: %s; %w", p, err)
} }
exists, err := r.Exists()
if err != nil {
return APIInfo{}, xerrors.Errorf("repo.Exists returned an error: %w", err)
}
if !exists {
return APIInfo{}, errors.New("repo directory does not exist. Make sure your configuration is correct")
}
ma, err := r.APIEndpoint() ma, err := r.APIEndpoint()
if err != nil { if err != nil {
return APIInfo{}, xerrors.Errorf("could not get api endpoint: %w", err) return APIInfo{}, xerrors.Errorf("could not get api endpoint: %w", err)
@ -168,14 +154,7 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}, nil }, nil
} }
for _, env := range fallbacksEnvs { return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %s", t)
env, ok := os.LookupEnv(env)
if ok {
return ParseApiInfo(env), nil
}
}
return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %v", t)
} }
func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.Header, error) { func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.Header, error) {
@ -196,10 +175,10 @@ func GetRawAPI(ctx *cli.Context, t repo.RepoType, version string) (string, http.
return addr, ainfo.AuthHeader(), nil return addr, ainfo.AuthHeader(), nil
} }
func GetCommonAPI(ctx *cli.Context) (api.CommonNet, jsonrpc.ClientCloser, error) { func GetAPI(ctx *cli.Context) (api.CommonNet, jsonrpc.ClientCloser, error) {
ti, ok := ctx.App.Metadata["repoType"] ti, ok := ctx.App.Metadata["repoType"]
if !ok { if !ok {
log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?") log.Errorf("unknown repo type, are you sure you want to use GetAPI?")
ti = repo.FullNode ti = repo.FullNode
} }
t, ok := ti.(repo.RepoType) t, ok := ti.(repo.RepoType)
@ -317,27 +296,6 @@ func GetWorkerAPI(ctx *cli.Context) (api.Worker, jsonrpc.ClientCloser, error) {
return client.NewWorkerRPCV0(ctx.Context, addr, headers) return client.NewWorkerRPCV0(ctx.Context, addr, headers)
} }
func GetMarketsAPI(ctx *cli.Context) (api.StorageMiner, jsonrpc.ClientCloser, error) {
// to support lotus-miner cli tests.
if tn, ok := ctx.App.Metadata["testnode-storage"]; ok {
return tn.(api.StorageMiner), func() {}, nil
}
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)
}
func GetGatewayAPI(ctx *cli.Context) (api.Gateway, jsonrpc.ClientCloser, error) { func GetGatewayAPI(ctx *cli.Context) (api.Gateway, jsonrpc.ClientCloser, error) {
addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v1") addr, headers, err := GetRawAPI(ctx, repo.FullNode, "v1")
if err != nil { if err != nil {