-
Notifications
You must be signed in to change notification settings - Fork 55
/
plugin.go
97 lines (85 loc) · 2.57 KB
/
plugin.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
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package plugins
import (
"fmt"
"os/user"
"path/filepath"
"strings"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/binutils"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-network-runner/utils"
)
func SanitizePath(path string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
homeDir := usr.HomeDir
if path == "~" {
// In case of "~", which won't be caught by the "else if"
path = homeDir
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir, path[2:])
}
return path, nil
}
// Downloads the subnet's VM (if necessary) and copies it into the plugin directory
func CreatePlugin(app *application.Avalanche, subnetName string, pluginDir string) (string, error) {
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return "", fmt.Errorf("failed to load sidecar: %w", err)
}
var vmSourcePath string
var vmDestPath string
if sc.ImportedFromAPM {
vmSourcePath = binutils.SetupAPMBin(app, sc.ImportedVMID)
vmDestPath = filepath.Join(pluginDir, sc.ImportedVMID)
} else {
// Not imported
chainVMID, err := utils.VMID(subnetName)
if err != nil {
return "", fmt.Errorf("failed to create VM ID from %s: %w", subnetName, err)
}
switch sc.VM {
case models.SubnetEvm:
_, vmSourcePath, err = binutils.SetupSubnetEVM(app, sc.VMVersion)
if err != nil {
return "", fmt.Errorf("failed to install subnet-evm: %w", err)
}
case models.CustomVM:
vmSourcePath = binutils.SetupCustomBin(app, subnetName)
default:
return "", fmt.Errorf("unknown vm: %s", sc.VM)
}
vmDestPath = filepath.Join(pluginDir, chainVMID.String())
}
return vmDestPath, binutils.CopyFile(vmSourcePath, vmDestPath)
}
// Downloads the target VM (if necessary) and copies it into the plugin directory
func CreatePluginFromVersion(
app *application.Avalanche,
subnetName string,
vm models.VMType,
version string,
vmid string,
pluginDir string,
) (string, error) {
var vmSourcePath string
var vmDestPath string
var err error
switch vm {
case models.SubnetEvm:
_, vmSourcePath, err = binutils.SetupSubnetEVM(app, version)
if err != nil {
return "", fmt.Errorf("failed to install subnet-evm: %w", err)
}
case models.CustomVM:
vmSourcePath = binutils.SetupCustomBin(app, subnetName)
default:
return "", fmt.Errorf("unknown vm: %s", vm)
}
vmDestPath = filepath.Join(pluginDir, vmid)
return vmDestPath, binutils.CopyFile(vmSourcePath, vmDestPath)
}