use fallback api infos last; init service with markets-path.

This commit is contained in:
Raúl Kripalani 2021-07-29 16:10:04 +01:00 committed by Jennifer Wang
parent 23cd561f2e
commit 04b358f340
3 changed files with 28 additions and 17 deletions

View File

@ -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.EnvsForRepo(t) currentEnv, _ := cliutil.EnvsForAPIInfos(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

@ -62,23 +62,23 @@ func flagsForRepo(t repo.RepoType) []string {
} }
} }
// EnvsForRepo returns the environment variables to use in order of precedence // EnvsForAPIInfos 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 EnvsForRepo(t repo.RepoType) (current []string, deprecated []string) { func EnvsForAPIInfos(t repo.RepoType) (primary string, fallbacks []string, deprecated []string) {
switch t { switch t {
case repo.FullNode: case repo.FullNode:
return []string{"FULLNODE_API_INFO"}, nil return "FULLNODE_API_INFO", nil, nil
case repo.StorageMiner: case repo.StorageMiner:
// TODO remove deprecated deprecation period // TODO remove deprecated deprecation period
return []string{"MINER_API_INFO"}, []string{"STORAGE_API_INFO"} return "MINER_API_INFO", nil, []string{"STORAGE_API_INFO"}
case repo.Worker: case repo.Worker:
return []string{"WORKER_API_INFO"}, nil return "WORKER_API_INFO", nil, nil
case repo.Markets: case repo.Markets:
// support split markets-miner and monolith deployments. // support split markets-miner and monolith deployments.
return []string{"MARKETS_API_INFO", "MINER_API_INFO"}, nil return "MARKETS_API_INFO", []string{"MINER_API_INFO"}, nil
default: default:
panic(fmt.Sprintf("Unknown repo type: %v", t)) panic(fmt.Sprintf("Unknown repo type: %v", t))
} }
@ -106,18 +106,20 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
return APIInfo{Addr: strma}, nil return APIInfo{Addr: strma}, nil
} }
currentEnv, deprecatedEnv := EnvsForRepo(t) //
for _, env := range currentEnv { // Note: it is not correct/intuitive to prefer environment variables over
env, ok := os.LookupEnv(env) // CLI flags (repo flags below).
//
primaryEnv, fallbacksEnvs, deprecatedEnvs := EnvsForAPIInfos(t)
env, ok := os.LookupEnv(primaryEnv)
if ok { if ok {
return ParseApiInfo(env), nil return ParseApiInfo(env), nil
} }
}
for _, env := range deprecatedEnv { 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, currentEnv) log.Warnf("Using deprecated env(%s) value, please use env(%s) instead.", env, primaryEnv)
return ParseApiInfo(env), nil return ParseApiInfo(env), nil
} }
} }
@ -156,6 +158,13 @@ func GetAPIInfo(ctx *cli.Context, t repo.RepoType) (APIInfo, error) {
}, nil }, nil
} }
for _, env := range fallbacksEnvs {
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) return APIInfo{}, fmt.Errorf("could not determine API endpoint for node type: %v", t)
} }

View File

@ -22,8 +22,10 @@ import (
var log = logging.Logger("main") var log = logging.Logger("main")
const FlagMinerRepo = "miner-repo" const (
const FlagMarketsRepo = "markets-repo" FlagMinerRepo = "miner-repo"
FlagMarketsRepo = "markets-repo"
)
// TODO remove after deprecation period // TODO remove after deprecation period
const FlagMinerRepoDeprecation = "storagerepo" const FlagMinerRepoDeprecation = "storagerepo"