forked from streamingfast/firehose-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_node_bootstrapper_tarball.go
98 lines (81 loc) · 2.1 KB
/
reader_node_bootstrapper_tarball.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package firecore
import (
"archive/tar"
"context"
"fmt"
"io"
"os"
"path/filepath"
"time"
"github.com/streamingfast/dstore"
"go.uber.org/zap"
)
func NewTarballReaderNodeBootstrapper(
url string,
dataDir string,
logger *zap.Logger,
) *TarballNodeBootstrapper {
return &TarballNodeBootstrapper{
url: url,
dataDir: dataDir,
logger: logger,
}
}
type TarballNodeBootstrapper struct {
url string
dataDir string
logger *zap.Logger
}
func (b *TarballNodeBootstrapper) isBootstrapped() bool {
return isBootstrapped(b.dataDir, b.logger)
}
func (b *TarballNodeBootstrapper) Bootstrap() error {
if b.isBootstrapped() {
return nil
}
b.logger.Info("bootstrapping native node chain data from pre-built archive", zap.String("bootstrap_data_url", b.url))
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
reader, _, _, err := dstore.OpenObject(ctx, b.url, dstore.Compression("zstd"))
if err != nil {
return fmt.Errorf("cannot get snapshot from gstore: %w", err)
}
defer reader.Close()
b.createChainData(reader)
return nil
}
func (b *TarballNodeBootstrapper) createChainData(reader io.Reader) error {
err := os.MkdirAll(b.dataDir, os.ModePerm)
if err != nil {
return fmt.Errorf("unable to create blocks log file: %w", err)
}
b.logger.Info("extracting bootstrapping data into node data directory", zap.String("data_dir", b.dataDir))
tr := tar.NewReader(reader)
for {
header, err := tr.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
path := filepath.Join(b.dataDir, header.Name)
b.logger.Debug("about to write content of entry", zap.String("name", header.Name), zap.String("path", path), zap.Bool("is_dir", header.FileInfo().IsDir()))
if header.FileInfo().IsDir() {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return fmt.Errorf("unable to create directory: %w", err)
}
continue
}
file, err := os.Create(path)
if err != nil {
return fmt.Errorf("unable to create file: %w", err)
}
if _, err := io.Copy(file, tr); err != nil {
file.Close()
return err
}
file.Close()
}
}