clean up repo if miner init fail and use log

This commit is contained in:
wanghui 2019-11-15 19:54:12 +08:00
parent 52a05c3d0e
commit b28ce76e8a

View File

@ -1,17 +1,9 @@
package main
import (
"os"
"context"
"crypto/rand"
"fmt"
"os"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
@ -20,6 +12,11 @@ import (
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"golang.org/x/xerrors"
"gopkg.in/urfave/cli.v2"
)
var initCmd = &cli.Command{
@ -105,29 +102,34 @@ var initCmd = &cli.Command{
return err
}
if err := storageMinerInit(ctx, cctx, api, r); err != nil {
fmt.Printf("ERROR: failed to initialize lotus-storage-miner: %s\n", err)
fmt.Println("Cleaning up after attempt...")
if err := os.RemoveAll(repoPath); err != nil {
fmt.Println("ERROR: failed to clean up failed storage repo: ", err)
lr, err := r.Lock(repo.StorageMiner)
if err != nil {
return err
}
return fmt.Errorf("storage-miner init failed")
if err := storageMinerInit(ctx, cctx, api, lr); err != nil {
log.Errorf("Failed to initialize lotus-storage-miner: %s", err)
log.Infof("Cleaning up %s after attempt...", lr.Path())
if err := lr.Close(); err != nil {
log.Errorf("Failed to close storage repo: %s", err)
}
if err := os.RemoveAll(lr.Path()); err != nil {
log.Errorf("Failed to clean up failed storage repo: %s", err)
}
return xerrors.Errorf("Storage-miner init failed")
}
// TODO: Point to setting storage price, maybe do it interactively or something
log.Info("Storage miner successfully created, you can now start it with 'lotus-storage-miner run'")
if err := lr.Close(); err != nil {
log.Errorf("Failed to close storage repo: %s", err)
}
return nil
},
}
func storageMinerInit(ctx context.Context, cctx *cli.Context, api api.FullNode, r repo.Repo) error {
lr, err := r.Lock(repo.StorageMiner)
if err != nil {
return err
}
defer lr.Close()
func storageMinerInit(ctx context.Context, cctx *cli.Context, api api.FullNode, lr repo.LockedRepo) error {
log.Info("Initializing libp2p identity")
p2pSk, err := makeHostKey(lr)