Skip to content

Commit

Permalink
more local cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
arturrez committed Oct 9, 2024
1 parent 71e313b commit 884043e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
3 changes: 3 additions & 0 deletions cmd/nodecmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ func getClusterNodes(clusterName string) ([]string, error) {
if err != nil {
return nil, err
}
if clustersConfig.Clusters[clusterName].Local {
return []string{fmt.Sprintf("local: %s", clusterName)}, nil
}
clusterNodes := clustersConfig.Clusters[clusterName].Nodes
if len(clusterNodes) == 0 {
return nil, fmt.Errorf("no nodes found in cluster %s", clusterName)
Expand Down
2 changes: 2 additions & 0 deletions cmd/nodecmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func list(_ *cobra.Command, _ []string) error {
}
if clusterConf.External {

Check failure on line 62 in cmd/nodecmd/list.go

View workflow job for this annotation

GitHub Actions / Lint

ifElseChain: rewrite if-else to switch statement (gocritic)
ux.Logger.PrintToUser("cluster %q (%s) EXTERNAL", clusterName, clusterConf.Network.Kind.String())
} else if clusterConf.Local {
ux.Logger.PrintToUser("cluster %q (%s) LOCAL", clusterName, clusterConf.Network.Kind.String())
} else {
ux.Logger.PrintToUser("Cluster %q (%s)", clusterName, clusterConf.Network.Kind.String())
}
Expand Down
34 changes: 18 additions & 16 deletions cmd/nodecmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,8 @@ func localStartNode(_ *cobra.Command, args []string) error {
rootDir := app.GetLocalDir(clusterName)
logDir := filepath.Join(rootDir, "logs")
pluginDir := app.GetPluginsDir()

cli, err := binutils.NewGRPCClient()
if err != nil {
return err
}

ctx, cancel := utils.GetANRContext()
defer cancel()

if localClusterDataExists(clusterName) {
ux.Logger.GreenCheckmarkToUser("Local cluster %s found. Booting up...", clusterName)
} else {
Expand Down Expand Up @@ -243,16 +236,13 @@ func localStartNode(_ *cobra.Command, args []string) error {
}

sd := subnet.NewLocalDeployer(app, avalancheGoVersion, avalanchegoBinaryPath, "")

if err := sd.StartServer(); err != nil {
return err
}

_, avalancheGoBinPath, err := sd.SetupLocalEnv()
if err != nil {
return err
}

// make sure rootDir exists
if err := os.MkdirAll(rootDir, 0o700); err != nil {
return fmt.Errorf("could not create root directory %s: %w", rootDir, err)
Expand Down Expand Up @@ -292,12 +282,19 @@ func localStartNode(_ *cobra.Command, args []string) error {
if bootstrapIPs != nil {
anrOpts = append(anrOpts, client.WithBootstrapNodeIPPortPairs(bootstrapIPs))
}

ux.Logger.PrintToUser("Booting Network. Wait until healthy...")
spinSession := ux.NewUserSpinner()
spinner := spinSession.SpinToUser("Booting Network. Wait until healthy...")
cli, err := binutils.NewGRPCClient()
if err != nil {
ux.SpinFailWithError(spinner, "", err)
return err
}

if _, err := cli.Start(ctx, avalancheGoBinPath, anrOpts...); err != nil {
ux.SpinFailWithError(spinner, "", err)
return fmt.Errorf("failed to start local avalanchego: %w", err)
}
ux.SpinComplete(spinner)
// save snapshot after successful start
if _, err := cli.SaveSnapshot(
ctx,
Expand All @@ -311,6 +308,10 @@ func localStartNode(_ *cobra.Command, args []string) error {
return err
}
}
cli, err := binutils.NewGRPCClient()
if err != nil {
return err
}

loadSnapshotOpts := []client.OpOption{
client.WithRootDataDir(rootDir),
Expand Down Expand Up @@ -338,9 +339,10 @@ func localStartNode(_ *cobra.Command, args []string) error {
}

func localStopNode(_ *cobra.Command, args []string) error {
// todo: support only one local node and detect what cluster to stop
clusterName := args[0]
if ok, err := checkClusterIsLocal(clusterName); err != nil || !ok {
return fmt.Errorf("failed to check if cluster %q is local: %w", clusterName, err)
return fmt.Errorf("local node %q is not found", clusterName)
}
cli, err := binutils.NewGRPCClient(
binutils.WithAvoidRPCVersionCheck(true),
Expand Down Expand Up @@ -373,20 +375,20 @@ func localStopNode(_ *cobra.Command, args []string) error {
if _, err = cli.Stop(ctx); err != nil {
return fmt.Errorf("failed to stop avalanchego: %w", err)
}

ux.Logger.GreenCheckmarkToUser("avalanchego stopped. State saved for %s", clusterName)
return nil
}

func localDestroyNode(_ *cobra.Command, args []string) error {
clusterName := args[0]
if ok, err := checkClusterIsLocal(clusterName); err != nil || !ok {
return fmt.Errorf("failed to check if cluster %q is local: %w", clusterName, err)
return fmt.Errorf("local cluster %q not found", clusterName)
}

if err := cleanupLocalNode(clusterName); err != nil {
return fmt.Errorf("failed to cleanup local node: %w", err)
}
ux.Logger.PrintToUser("Local node cleaned up.")
ux.Logger.GreenCheckmarkToUser("Local node %s cleaned up.", clusterName)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.22.8

require (
github.com/ava-labs/apm v1.0.0
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241005224128-cc3c07bb1344
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241003175856-6b1bcdf33e7a
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3
github.com/ava-labs/awm-relayer v1.4.1-0.20241003162124-807fd305670f
github.com/ava-labs/coreth v0.13.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/ava-labs/apm v1.0.0 h1:6FwozH67hEkbWVsOXNZGexBy5KLpNeYucN9zcFUHv+Q=
github.com/ava-labs/apm v1.0.0/go.mod h1:TJL7pTlZNvQatsQPsLUtDHApEwVZ/qS7iSNtRFU83mc=
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241005224128-cc3c07bb1344 h1:wD/rBr+QKztcKtRtBNqPjzMhwcxnVcuJ3GT62DdgS2Q=
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241005224128-cc3c07bb1344/go.mod h1:l4QzFnujbyyyeq6oBQ4F6sw9TrTQCjD2V4vUd7ZBCCo=
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241003175856-6b1bcdf33e7a h1:xd2RUTW9w34+V6yI7zWfByHZ8y5CeXhvHK+nAJ9FCbI=
github.com/ava-labs/avalanche-network-runner v1.8.4-0.20241003175856-6b1bcdf33e7a/go.mod h1:l4QzFnujbyyyeq6oBQ4F6sw9TrTQCjD2V4vUd7ZBCCo=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3 h1:JfVooBCdMzpeGUT9/phJNl2GHflkGehlMJokXeWKa2A=
github.com/ava-labs/avalanchego v1.12.0-initial-poc.3/go.mod h1:qSHmog3wMVjo/ruIAQo0ppXAilyni07NIu5K88RyhWE=
github.com/ava-labs/awm-relayer v1.4.1-0.20241003162124-807fd305670f h1:YUQF1wQJeEcTMC5W/OrwgSFTFMS4zeCM8O02rLeEDow=
Expand Down
7 changes: 5 additions & 2 deletions pkg/models/clusters_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ func (cc *ClusterConfig) GetValidatorHosts(hosts []*Host) []*Host {
}

func (cc *ClusterConfig) IsAPIHost(hostCloudID string) bool {
return slices.Contains(cc.APINodes, hostCloudID)
return cc.Local || slices.Contains(cc.APINodes, hostCloudID)
}

func (cc *ClusterConfig) IsAvalancheGoHost(hostCloudID string) bool {
return slices.Contains(cc.Nodes, hostCloudID)
return cc.Local || slices.Contains(cc.Nodes, hostCloudID)
}

func (cc *ClusterConfig) GetCloudIDs() []string {
if cc.Local {
return nil
}
r := cc.Nodes
if cc.MonitoringInstance != "" {
r = append(r, cc.MonitoringInstance)
Expand Down

0 comments on commit 884043e

Please sign in to comment.