-
Notifications
You must be signed in to change notification settings - Fork 669
/
vm_getter.go
99 lines (82 loc) · 2.46 KB
/
vm_getter.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
99
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package registry
import (
"errors"
"fmt"
"path/filepath"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/filesystem"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/rpcchainvm"
)
var _ VMGetter = &vmGetter{}
// VMGetter defines functionality to get the plugins on the node.
type VMGetter interface {
// Get fetches the VMs that are registered and the VMs that are not
// registered but available to be installed on the node.
Get() (
registeredVMs map[ids.ID]vms.Factory,
unregisteredVMs map[ids.ID]vms.Factory,
err error,
)
}
// VMGetterConfig defines settings for VMGetter
type VMGetterConfig struct {
FileReader filesystem.Reader
Manager vms.Manager
PluginDirectory string
}
type vmGetter struct {
config VMGetterConfig
}
// NewVMGetter returns a new instance of a VMGetter
func NewVMGetter(config VMGetterConfig) VMGetter {
return &vmGetter{
config: config,
}
}
func (getter *vmGetter) Get() (map[ids.ID]vms.Factory, map[ids.ID]vms.Factory, error) {
files, err := getter.config.FileReader.ReadDir(getter.config.PluginDirectory)
if err != nil {
return nil, nil, err
}
registeredVMs := make(map[ids.ID]vms.Factory)
unregisteredVMs := make(map[ids.ID]vms.Factory)
for _, file := range files {
if file.IsDir() {
continue
}
nameWithExtension := file.Name()
// Strip any extension from the file. This is to support windows .exe
// files.
name := nameWithExtension[:len(nameWithExtension)-len(filepath.Ext(nameWithExtension))]
// Skip hidden files.
if len(name) == 0 {
continue
}
vmID, err := getter.config.Manager.Lookup(name)
if err != nil {
// there is no alias with plugin name, try to use full vmID.
vmID, err = ids.FromString(name)
if err != nil {
return nil, nil, fmt.Errorf("invalid vmID %s", name)
}
}
registeredFactory, err := getter.config.Manager.GetFactory(vmID)
if err == nil {
// If we already have the VM registered, we shouldn't attempt to
// register it again.
registeredVMs[vmID] = registeredFactory
continue
}
// If the error isn't "not found", then we should report the error.
if !errors.Is(err, vms.ErrNotFound) {
return nil, nil, err
}
unregisteredVMs[vmID] = rpcchainvm.NewFactory(
filepath.Join(getter.config.PluginDirectory, file.Name()),
)
}
return registeredVMs, unregisteredVMs, nil
}