daemon cmd: Support imports straight from http
This commit is contained in:
parent
a3145bae07
commit
f809529699
@ -3,11 +3,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
@ -101,11 +104,11 @@ var DaemonCmd = &cli.Command{
|
|||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "import-chain",
|
Name: "import-chain",
|
||||||
Usage: "on first run, load chain from given file and validate",
|
Usage: "on first run, load chain from given file or url and validate",
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "import-snapshot",
|
Name: "import-snapshot",
|
||||||
Usage: "import chain state from a given chain export file",
|
Usage: "import chain state from a given chain export file or url",
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "halt-after-import",
|
Name: "halt-after-import",
|
||||||
@ -207,11 +210,6 @@ var DaemonCmd = &cli.Command{
|
|||||||
issnapshot = true
|
issnapshot = true
|
||||||
}
|
}
|
||||||
|
|
||||||
chainfile, err := homedir.Expand(chainfile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ImportChain(r, chainfile, issnapshot); err != nil {
|
if err := ImportChain(r, chainfile, issnapshot); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -327,16 +325,41 @@ func importKey(ctx context.Context, api api.FullNode, f string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ImportChain(r repo.Repo, fname string, snapshot bool) error {
|
func ImportChain(r repo.Repo, fname string, snapshot bool) (err error) {
|
||||||
fi, err := os.Open(fname)
|
var rd io.Reader
|
||||||
if err != nil {
|
var l int64
|
||||||
return err
|
if strings.HasPrefix(fname, "http://") || strings.HasPrefix(fname, "https://") {
|
||||||
}
|
resp, err := http.Get(fname) //nolint:gosec
|
||||||
defer fi.Close() //nolint:errcheck
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close() //nolint:errcheck
|
||||||
|
|
||||||
st, err := os.Stat(fname)
|
if resp.StatusCode != http.StatusOK {
|
||||||
if err != nil {
|
return xerrors.Errorf("non-200 response: %d", resp.StatusCode)
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
rd = resp.Body
|
||||||
|
l = resp.ContentLength
|
||||||
|
} else {
|
||||||
|
fname, err = homedir.Expand(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := os.Open(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fi.Close() //nolint:errcheck
|
||||||
|
|
||||||
|
st, err := os.Stat(fname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rd = fi
|
||||||
|
l = st.Size()
|
||||||
}
|
}
|
||||||
|
|
||||||
lr, err := r.Lock(repo.FullNode)
|
lr, err := r.Lock(repo.FullNode)
|
||||||
@ -359,12 +382,15 @@ func ImportChain(r repo.Repo, fname string, snapshot bool) error {
|
|||||||
|
|
||||||
cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
|
cst := store.NewChainStore(bs, mds, vm.Syscalls(ffiwrapper.ProofVerifier))
|
||||||
|
|
||||||
log.Info("importing chain from file...")
|
log.Infof("importing chain from %s...", fname)
|
||||||
|
|
||||||
bar := pb.New64(st.Size())
|
bufr := bufio.NewReaderSize(rd, 1<<20)
|
||||||
br := bar.NewProxyReader(fi)
|
|
||||||
|
bar := pb.New64(l)
|
||||||
|
br := bar.NewProxyReader(bufr)
|
||||||
bar.ShowTimeLeft = true
|
bar.ShowTimeLeft = true
|
||||||
bar.ShowPercent = true
|
bar.ShowPercent = true
|
||||||
|
bar.ShowSpeed = true
|
||||||
bar.Units = pb.U_BYTES
|
bar.Units = pb.U_BYTES
|
||||||
|
|
||||||
bar.Start()
|
bar.Start()
|
||||||
|
Loading…
Reference in New Issue
Block a user