-
Notifications
You must be signed in to change notification settings - Fork 397
/
snapshot.go
61 lines (53 loc) · 1.34 KB
/
snapshot.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storagenodedbtest
import (
"archive/zip"
"bytes"
"context"
_ "embed"
"io"
"os"
"path/filepath"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/storagenode/storagenodedb"
)
//go:generate go run ./gensnapshot testdata/snapshot.zip
//go:embed testdata/snapshot.zip
var snapshotZip []byte
// OpenNew opens a new storage node database pre-populated with a newly
// initialized and migrated database snapshot.
func OpenNew(ctx context.Context, log *zap.Logger, config storagenodedb.Config) (*storagenodedb.DB, error) {
db, err := storagenodedb.OpenNew(ctx, log, config)
if err != nil {
return nil, err
}
if err := deploySnapshot(db.DBDirectory()); err != nil {
return nil, err
}
return db, nil
}
func deploySnapshot(storageDir string) error {
zipReader, err := zip.NewReader(bytes.NewReader(snapshotZip), int64(len(snapshotZip)))
if err != nil {
return errs.Wrap(err)
}
for _, f := range zipReader.File {
rc, err := f.Open()
if err != nil {
return errs.Wrap(err)
}
data, err := io.ReadAll(rc)
if err != nil {
return errs.Wrap(err)
}
if err := os.WriteFile(filepath.Join(storageDir, f.Name), data, 0644); err != nil {
return errs.Wrap(err)
}
if err := rc.Close(); err != nil {
return errs.Wrap(err)
}
}
return nil
}