use lotus cli and GetFullNodeAPI

also some other minor bug fixes
This commit is contained in:
ognots 2020-01-10 21:13:11 -05:00
parent d8d8ce7526
commit 3953227702

View File

@ -2,25 +2,20 @@ package main
import ( import (
"context" "context"
"net/http"
"os" "os"
"time" "time"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/lib/jsonrpc" lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo"
cid "github.com/ipfs/go-cid" cid "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
manet "github.com/multiformats/go-multiaddr-net"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
) )
type CidWindow [][]cid.Cid type CidWindow [][]cid.Cid
var log = logging.Logger("lotus-seed") var log = logging.Logger("lotus-health")
func main() { func main() {
logging.SetLogLevel("*", "INFO") logging.SetLogLevel("*", "INFO")
@ -36,6 +31,13 @@ func main() {
Usage: "Tools for monitoring lotus daemon health", Usage: "Tools for monitoring lotus daemon health",
Version: build.UserVersion, Version: build.UserVersion,
Commands: local, Commands: local,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
EnvVars: []string{"LOTUS_PATH"},
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
},
},
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -47,11 +49,6 @@ func main() {
var watchHeadCmd = &cli.Command{ var watchHeadCmd = &cli.Command{
Name: "watch-head", Name: "watch-head",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Value: "~/.lotus",
Usage: "lotus repo path",
},
&cli.IntFlag{ &cli.IntFlag{
Name: "threshold", Name: "threshold",
Value: 3, Value: 3,
@ -59,7 +56,7 @@ var watchHeadCmd = &cli.Command{
}, },
&cli.IntFlag{ &cli.IntFlag{
Name: "interval", Name: "interval",
Value: 45, Value: build.BlockDelay,
Usage: "interval in seconds between chain head checks", Usage: "interval in seconds between chain head checks",
}, },
&cli.StringFlag{ &cli.StringFlag{
@ -69,19 +66,18 @@ var watchHeadCmd = &cli.Command{
}, },
}, },
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
repo := c.String("repo")
threshold := c.Int("threshold") threshold := c.Int("threshold")
interval := time.Duration(c.Int("interval")) interval := time.Duration(c.Int("interval")) * time.Second
name := c.String("systemd-unit") name := c.String("systemd-unit")
var headCheckWindow CidWindow var headCheckWindow CidWindow
ctx := context.Background()
api, closer, err := GetFullNodeAPI(repo) api, closer, err := lcli.GetFullNodeAPI(c)
if err != nil { if err != nil {
return err return err
} }
defer closer() defer closer()
ctx := lcli.ReqContext(c)
if err := WaitForSyncComplete(ctx, api); err != nil { if err := WaitForSyncComplete(ctx, api); err != nil {
log.Fatal(err) log.Fatal(err)
@ -96,7 +92,7 @@ var watchHeadCmd = &cli.Command{
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
time.Sleep(interval * time.Second) time.Sleep(interval)
} }
}() }()
@ -193,44 +189,6 @@ func appendCIDsToWindow(w CidWindow, c []cid.Cid, t int) CidWindow {
return append(w, c) return append(w, c)
} }
/*
* initialize and return lotus api
*/
func getAPI(path string) (string, http.Header, error) {
r, err := repo.NewFS(path)
if err != nil {
return "", nil, err
}
ma, err := r.APIEndpoint()
if err != nil {
return "", nil, xerrors.Errorf("failed to get api endpoint: %w", err)
}
_, addr, err := manet.DialArgs(ma)
if err != nil {
return "", nil, err
}
var headers http.Header
token, err := r.APIToken()
if err != nil {
log.Warn("Couldn't load CLI token, capabilities may be limited: %w", err)
} else {
headers = http.Header{}
headers.Add("Authorization", "Bearer "+string(token))
}
return "ws://" + addr + "/rpc/v0", headers, nil
}
func GetFullNodeAPI(repo string) (api.FullNode, jsonrpc.ClientCloser, error) {
addr, headers, err := getAPI(repo)
if err != nil {
return nil, nil, err
}
return client.NewFullNodeRPC(addr, headers)
}
/* /*
* wait for node to sync * wait for node to sync
*/ */