Skip to content

Commit ea4f2b1

Browse files
committed
set snapshot removal and list to not depend on bootstrapped network
1 parent cd7b4f1 commit ea4f2b1

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

local/network.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ var (
135135
deprecatedFlagsSupportBytes []byte
136136
deprecatedFlagsSupport []deprecatedFlagEsp
137137
// snapshots directory
138-
defaultSnapshotsDir string
138+
DefaultSnapshotsDir string
139139
)
140140

141141
// populate default network config from embedded default directory
@@ -149,14 +149,14 @@ func init() {
149149
if err != nil {
150150
panic(err)
151151
}
152-
defaultSnapshotsDir = filepath.Join(usr.HomeDir, snapshotsRelPath)
152+
DefaultSnapshotsDir = filepath.Join(usr.HomeDir, snapshotsRelPath)
153153
}
154154

155155
// NewNetwork returns a new network that uses the given log.
156156
// Files (e.g. logs, databases) default to being written at directory [rootDir].
157157
// If there isn't a directory at [dir] one will be created.
158158
// If len([dir]) == 0, files will be written underneath a new temporary directory.
159-
// Snapshots are saved to snapshotsDir, defaults to defaultSnapshotsDir if not given
159+
// Snapshots are saved to snapshotsDir, defaults to DefaultSnapshotsDir if not given
160160
func NewNetwork(
161161
log logging.Logger,
162162
networkConfig network.Config,
@@ -214,7 +214,7 @@ func newNetwork(
214214
}
215215
}
216216
if snapshotsDir == "" {
217-
snapshotsDir = defaultSnapshotsDir
217+
snapshotsDir = DefaultSnapshotsDir
218218
}
219219
// create the snapshots dir if not present
220220
err = os.MkdirAll(snapshotsDir, os.ModePerm)

local/snapshot.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,16 @@ func (ln *localNetwork) loadSnapshot(
334334

335335
// Remove network snapshot
336336
func (ln *localNetwork) RemoveSnapshot(snapshotName string) error {
337-
snapshotDir := filepath.Join(ln.snapshotsDir, snapshotPrefix+snapshotName)
337+
return RemoveSnapshot(ln.snapshotsDir, snapshotName)
338+
}
339+
340+
// Get network snapshots
341+
func (ln *localNetwork) GetSnapshotNames() ([]string, error) {
342+
return GetSnapshotNames(ln.snapshotsDir)
343+
}
344+
345+
func RemoveSnapshot(snapshotsDir string, snapshotName string) error {
346+
snapshotDir := filepath.Join(snapshotsDir, snapshotPrefix+snapshotName)
338347
_, err := os.Stat(snapshotDir)
339348
if err != nil {
340349
if errors.Is(err, os.ErrNotExist) {
@@ -349,17 +358,16 @@ func (ln *localNetwork) RemoveSnapshot(snapshotName string) error {
349358
return nil
350359
}
351360

352-
// Get network snapshots
353-
func (ln *localNetwork) GetSnapshotNames() ([]string, error) {
354-
_, err := os.Stat(ln.snapshotsDir)
361+
func GetSnapshotNames(snapshotsDir string) ([]string, error) {
362+
_, err := os.Stat(snapshotsDir)
355363
if err != nil {
356364
if errors.Is(err, os.ErrNotExist) {
357-
return nil, fmt.Errorf("snapshots dir %q does not exists", ln.snapshotsDir)
365+
return nil, fmt.Errorf("snapshots dir %q does not exists", snapshotsDir)
358366
} else {
359-
return nil, fmt.Errorf("failure accessing snapshots dir %q: %w", ln.snapshotsDir, err)
367+
return nil, fmt.Errorf("failure accessing snapshots dir %q: %w", snapshotsDir, err)
360368
}
361369
}
362-
matches, err := filepath.Glob(filepath.Join(ln.snapshotsDir, snapshotPrefix+"*"))
370+
matches, err := filepath.Glob(filepath.Join(snapshotsDir, snapshotPrefix+"*"))
363371
if err != nil {
364372
return nil, err
365373
}

server/server.go

+7-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"go.uber.org/multierr"
2323

24+
"github.com/ava-labs/avalanche-network-runner/local"
2425
"github.com/ava-labs/avalanche-network-runner/network"
2526
"github.com/ava-labs/avalanche-network-runner/network/node"
2627
"github.com/ava-labs/avalanche-network-runner/rpcpb"
@@ -133,6 +134,10 @@ func New(cfg Config, log logging.Logger) (Server, error) {
133134
return nil, err
134135
}
135136

137+
if cfg.SnapshotsDir == "" {
138+
cfg.SnapshotsDir = local.DefaultSnapshotsDir
139+
}
140+
136141
s := &server{
137142
cfg: cfg,
138143
log: log,
@@ -1412,11 +1417,7 @@ func (s *server) RemoveSnapshot(_ context.Context, req *rpcpb.RemoveSnapshotRequ
14121417

14131418
s.log.Info("RemoveSnapshot", zap.String("snapshot-name", req.SnapshotName))
14141419

1415-
if s.network == nil {
1416-
return nil, ErrNotBootstrapped
1417-
}
1418-
1419-
if err := s.network.nw.RemoveSnapshot(req.SnapshotName); err != nil {
1420+
if err := local.RemoveSnapshot(s.cfg.SnapshotsDir, req.SnapshotName); err != nil {
14201421
s.log.Warn("snapshot remove failed to complete", zap.Error(err))
14211422
return nil, err
14221423
}
@@ -1429,11 +1430,7 @@ func (s *server) GetSnapshotNames(context.Context, *rpcpb.GetSnapshotNamesReques
14291430

14301431
s.log.Info("GetSnapshotNames")
14311432

1432-
if s.network == nil {
1433-
return nil, ErrNotBootstrapped
1434-
}
1435-
1436-
snapshotNames, err := s.network.nw.GetSnapshotNames()
1433+
snapshotNames, err := local.GetSnapshotNames(s.cfg.SnapshotsDir)
14371434
if err != nil {
14381435
return nil, err
14391436
}

0 commit comments

Comments
 (0)