Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default node start params #603

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions cmd/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,6 @@ func newStartCommand() *cobra.Command {
false,
"true to assign dynamic ports",
)
if err := cmd.MarkPersistentFlagRequired("avalanchego-path"); err != nil {
panic(err)
}
return cmd
}

Expand Down
3 changes: 0 additions & 3 deletions local/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"regexp"
"strings"
"sync"
"syscall"
"time"

"github.com/ava-labs/avalanche-network-runner/api"
Expand Down Expand Up @@ -843,7 +842,6 @@ func (ln *localNetwork) pauseNode(ctx context.Context, nodeName string) error {
if exitCode := node.process.Stop(ctx); exitCode != 0 {
return fmt.Errorf("node %q exited with exit code: %d", nodeName, exitCode)
}
syscall.Sync()
node.paused = true
return nil
}
Expand Down Expand Up @@ -964,7 +962,6 @@ func (ln *localNetwork) restartNode(
if err := ln.removeNode(ctx, nodeName); err != nil {
return err
}
syscall.Sync()
}

if _, err := ln.addNode(nodeConfig); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions local/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"

"github.com/ava-labs/avalanche-network-runner/api"
"github.com/ava-labs/avalanche-network-runner/network"
Expand Down Expand Up @@ -156,7 +155,6 @@ func (ln *localNetwork) SaveSnapshot(ctx context.Context, snapshotName string) (
if err := ln.stop(ctx); err != nil {
return "", err
}
syscall.Sync()
// create main snapshot dirs
snapshotDBDir := filepath.Join(snapshotDir, defaultDBSubdir)
if err := os.MkdirAll(snapshotDBDir, os.ModePerm); err != nil {
Expand Down
42 changes: 32 additions & 10 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,13 @@ func (s *server) Start(_ context.Context, req *rpcpb.StartRequest) (*rpcpb.Start
return nil, ErrNotEnoughNodesForStart
}

if err := utils.CheckExecPath(req.GetExecPath()); err != nil {
execPath := applyDefaultExecPath(req.GetExecPath())
pluginDir := applyDefaultPluginDir(req.GetPluginDir())

if err := utils.CheckExecPath(execPath); err != nil {
return nil, err
}

pluginDir := req.GetPluginDir()
chainSpecs := []network.BlockchainSpec{}
if len(req.GetBlockchainSpecs()) > 0 {
s.log.Info("plugin-dir:", zap.String("plugin-dir", pluginDir))
Expand All @@ -293,7 +295,6 @@ func (s *server) Start(_ context.Context, req *rpcpb.StartRequest) (*rpcpb.Start
}

var (
execPath = req.GetExecPath()
numNodes = req.GetNumNodes()
trackSubnets = req.GetWhitelistedSubnets()
rootDataDir = req.GetRootDataDir()
Expand Down Expand Up @@ -1007,14 +1008,15 @@ func (s *server) AddNode(_ context.Context, req *rpcpb.AddNodeRequest) (*rpcpb.A
}
}

if req.GetPluginDir() != "" {
nodeFlags[config.PluginDirKey] = req.GetPluginDir()
pluginDir := applyDefaultPluginDir(req.GetPluginDir())
if pluginDir != "" {
nodeFlags[config.PluginDirKey] = pluginDir
}

nodeConfig := node.Config{
Name: req.Name,
Flags: nodeFlags,
BinaryPath: req.GetExecPath(),
BinaryPath: applyDefaultExecPath(req.GetExecPath()),
RedirectStdout: s.cfg.RedirectNodesOutput,
RedirectStderr: s.cfg.RedirectNodesOutput,
ChainConfigFiles: req.ChainConfigs,
Expand Down Expand Up @@ -1083,8 +1085,8 @@ func (s *server) RestartNode(ctx context.Context, req *rpcpb.RestartNodeRequest)
if err := s.network.nw.RestartNode(
ctx,
req.Name,
req.GetExecPath(),
req.GetPluginDir(),
applyDefaultExecPath(req.GetExecPath()),
applyDefaultPluginDir(req.GetPluginDir()),
req.GetWhitelistedSubnets(),
req.GetChainConfigs(),
req.GetUpgradeConfigs(),
Expand Down Expand Up @@ -1281,8 +1283,8 @@ func (s *server) LoadSnapshot(_ context.Context, req *rpcpb.LoadSnapshotRequest)
s.log.Info("starting", zap.Int32("pid", pid), zap.String("root-data-dir", rootDataDir))

s.network, err = newLocalNetwork(localNetworkOptions{
execPath: req.GetExecPath(),
pluginDir: req.GetPluginDir(),
execPath: applyDefaultExecPath(req.GetExecPath()),
pluginDir: applyDefaultPluginDir(req.GetPluginDir()),
rootDataDir: rootDataDir,
chainConfigs: req.ChainConfigs,
upgradeConfigs: req.UpgradeConfigs,
Expand Down Expand Up @@ -1663,3 +1665,23 @@ func readFileOrString(conf string) []byte {
}
return confBytes
}

// if [userGivenExecPath] is non empty, returns it
// otherwise if env var utils.DefaultExecPathEnvVar is
// defined, returns its contents
func applyDefaultExecPath(userGivenExecPath string) string {
if userGivenExecPath != "" {
return userGivenExecPath
}
return os.Getenv(constants.DefaultExecPathEnvVar)
}

// if [userGivenPluginDir] is non empty, returns it
// otherwise if env var utils.DefaultPluginDirEnvVar is
// defined, returns its contents
func applyDefaultPluginDir(userGivenPluginDir string) string {
if userGivenPluginDir != "" {
return userGivenPluginDir
}
return os.Getenv(constants.DefaultPluginDirEnvVar)
}
4 changes: 2 additions & 2 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ var _ = ginkgo.Describe("[Start/Remove/Restart/Add/Stop]", func() {
})

ginkgo.It("can start", func() {
ginkgo.By("start request with invalid exec path should fail", func() {
ginkgo.By("start request with empty exec path should fail", func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
_, err := cli.Start(ctx, "")
cancel()
gomega.Ω(err.Error()).Should(gomega.ContainSubstring(utils.ErrInvalidExecPath.Error()))
gomega.Ω(err.Error()).Should(gomega.ContainSubstring(utils.ErrEmptyExecPath.Error()))
})

ginkgo.By("start request with invalid exec path should fail", func() {
Expand Down
10 changes: 6 additions & 4 deletions utils/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package constants
import "path/filepath"

const (
LogNameMain = "main"
LogNameControl = "control"
LogNameTest = "test"
RootDirPrefix = "network-runner-root-data"
LogNameMain = "main"
LogNameControl = "control"
LogNameTest = "test"
RootDirPrefix = "network-runner-root-data"
DefaultExecPathEnvVar = "AVALANCHEGO_EXEC_PATH"
DefaultPluginDirEnvVar = "AVALANCHEGO_PLUGIN_PATH"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func NetworkIDFromGenesis(genesis []byte) (uint32, error) {
}

var (
ErrInvalidExecPath = errors.New("avalanche exec is invalid")
ErrEmptyExecPath = errors.New("avalanche exec is not defined")
ErrNotExists = errors.New("avalanche exec not exists")
ErrNotExistsPlugin = errors.New("plugin exec not exists")
)

func CheckExecPath(exec string) error {
if exec == "" {
return ErrInvalidExecPath
return ErrEmptyExecPath
}
_, err := os.Stat(exec)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestCheckExecPath(t *testing.T) {
},
{
execPath: "",
expectedErr: ErrInvalidExecPath,
expectedErr: ErrEmptyExecPath,
},
{
execPath: "invalid",
Expand Down
Loading