Skip to content

Commit

Permalink
Start stop relayer (#1631)
Browse files Browse the repository at this point in the history
* add network options with cluster + teleporter deploy cmd

* fix e2e v1

* point to correct branch

* increase time of cli setup from source for e2e

* force 10 min cli setup from source if e2e

* lint

* add basic relayer commands

* partial work

* awm relayer host selection working

* nits

* add cchain teleporter info to cluster

* nits

* nits

* partial work

* nits

* almost

* relayer working

* nits

* all good except receipt

* add -y flag to node destroy

* nit

* add clusters to key list network options

* add flag to key list to have a shortest way to specify chains to show
info about

* add tx hash for failed msg at destination

* partial work

* bump golang

* add trace to teleporter msg is receipt status is wrong

* enable first creating a subnet then creating a blockchain

* improve add subnet validators speed

* nit

* seems pretty well

* seems pretty weel

* add proposer vm setup simil teleporter e2e

* just create subnet/blockchain on one subnet deploy

* working!

* nit

* nit

* nit

* nit

* subnet join to not fail if blockchain was not deployed yet

* local stop almost working

* nit

* start accessing node related functions from teleportercmd

* basic remote start/stop without check for process status

* add node lib

* disable cli setup from source

* nit

* nit

* lint

* address PR comments

* addres PR comments
  • Loading branch information
felipemadero authored Apr 4, 2024
1 parent 15d0746 commit 690e734
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 57 deletions.
48 changes: 5 additions & 43 deletions cmd/nodecmd/wiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/constants"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/node"
"github.com/ava-labs/avalanche-cli/pkg/ssh"
"github.com/ava-labs/avalanche-cli/pkg/subnet"
"github.com/ava-labs/avalanche-cli/pkg/teleporter"
Expand Down Expand Up @@ -268,7 +269,7 @@ func wiz(cmd *cobra.Command, args []string) error {
var awmRelayerHost *models.Host
if sc.TeleporterReady && sc.RunRelayer && isEVMGenesis {
// get or set AWM Relayer host and configure/stop service
awmRelayerHost, err = getAWMRelayerHost(clusterName)
awmRelayerHost, err = node.GetAWMRelayerHost(app, clusterName)
if err != nil {
return err
}
Expand Down Expand Up @@ -488,31 +489,10 @@ func updateProposerVMs(
return teleporter.SetProposerVM(app, network, "C", "")
}

func getHostWithCloudID(clusterName string, cloudID string) (*models.Host, error) {
hosts, err := ansible.GetInventoryFromAnsibleInventoryFile(app.GetAnsibleInventoryDirPath(clusterName))
if err != nil {
return nil, err
}
monitoringInventoryFile := app.GetMonitoringInventoryDir(clusterName)
if utils.FileExists(monitoringInventoryFile) {
monitoringHosts, err := ansible.GetInventoryFromAnsibleInventoryFile(monitoringInventoryFile)
if err != nil {
return nil, err
}
hosts = append(hosts, monitoringHosts...)
}
for _, host := range hosts {
if host.GetCloudID() == cloudID {
return host, nil
}
}
return nil, nil
}

func setAWMRelayerHost(host *models.Host) error {
cloudID := host.GetCloudID()
ux.Logger.PrintToUser("")
ux.Logger.PrintToUser("configuring AWM Relayer on host %s", cloudID)
ux.Logger.PrintToUser("configuring AWM RElayer on host %s", cloudID)
nodeConfig, err := app.LoadClusterNodeConfig(cloudID)
if err != nil {
return err
Expand Down Expand Up @@ -541,24 +521,6 @@ func updateAWMRelayerHostConfig(host *models.Host, subnetName string, clusterNam
return ssh.RunSSHStartAWMRelayerService(host)
}

func getAWMRelayerHost(clusterName string) (*models.Host, error) {
clusterConfig, err := app.GetClusterConfig(clusterName)
if err != nil {
return nil, err
}
relayerCloudID := ""
for _, cloudID := range clusterConfig.GetCloudIDs() {
nodeConfig, err := app.LoadClusterNodeConfig(cloudID)
if err != nil {
return nil, err
}
if nodeConfig.IsAWMRelayer {
relayerCloudID = nodeConfig.NodeID
}
}
return getHostWithCloudID(clusterName, relayerCloudID)
}

func chooseAWMRelayerHost(clusterName string) (*models.Host, error) {
// first look up for separate monitoring host
monitoringInventoryFile := app.GetMonitoringInventoryDir(clusterName)
Expand All @@ -577,11 +539,11 @@ func chooseAWMRelayerHost(clusterName string) (*models.Host, error) {
return nil, err
}
if len(clusterConfig.APINodes) > 0 {
return getHostWithCloudID(clusterName, clusterConfig.APINodes[0])
return node.GetHostWithCloudID(app, clusterName, clusterConfig.APINodes[0])
}
// finally go for other hosts
if len(clusterConfig.Nodes) > 0 {
return getHostWithCloudID(clusterName, clusterConfig.Nodes[0])
return node.GetHostWithCloudID(app, clusterName, clusterConfig.Nodes[0])
}
return nil, fmt.Errorf("no hosts found on cluster")
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/teleportercmd/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ and configuring an AWM relayer on localhost.`,
}
cmd.AddCommand(newPrepareRelayerServiceCmd())
cmd.AddCommand(newAddSubnetToRelayerServiceCmd())
cmd.AddCommand(newStopRelayerCmd())
cmd.AddCommand(newStartRelayerCmd())
return cmd
}
76 changes: 76 additions & 0 deletions cmd/teleportercmd/startRelayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package teleportercmd

import (
"fmt"

"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/node"
"github.com/ava-labs/avalanche-cli/pkg/ssh"
"github.com/ava-labs/avalanche-cli/pkg/teleporter"
"github.com/ava-labs/avalanche-cli/pkg/ux"

"github.com/spf13/cobra"
)

var startRelayerNetworkOptions = []networkoptions.NetworkOption{networkoptions.Local, networkoptions.Cluster}

// avalanche teleporter relayer start
func newStartRelayerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: "starts AWM relayer",
Long: `Starts AWM relayer on the specified network (Currently only for local network).`,
SilenceUsage: true,
RunE: startRelayer,
Args: cobra.ExactArgs(0),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, startRelayerNetworkOptions)
return cmd
}

func startRelayer(_ *cobra.Command, _ []string) error {
network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
globalNetworkFlags,
false,
startRelayerNetworkOptions,
"",
)
if err != nil {
return err
}
switch {
case network.Kind == models.Local:
if relayerIsUp, _, _, err := teleporter.RelayerIsUp(
app.GetAWMRelayerRunPath(),
); err != nil {
return err
} else if relayerIsUp {
return fmt.Errorf("local AWM relayer is already running")
}
if err := teleporter.DeployRelayer(
app.GetAWMRelayerBinDir(),
app.GetAWMRelayerConfigPath(),
app.GetAWMRelayerLogPath(),
app.GetAWMRelayerRunPath(),
app.GetAWMRelayerStorageDir(),
); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("Local AWM Relayer successfully started")
ux.Logger.PrintToUser("Logs can be found at %s", app.GetAWMRelayerLogPath())
case network.ClusterName != "":
host, err := node.GetAWMRelayerHost(app, network.ClusterName)
if err != nil {
return err
}
if err := ssh.RunSSHStartAWMRelayerService(host); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("Remote AWM Relayer on %s successfully started", host.GetCloudID())
}
return nil
}
74 changes: 74 additions & 0 deletions cmd/teleportercmd/stopRelayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package teleportercmd

import (
"fmt"

"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/node"
"github.com/ava-labs/avalanche-cli/pkg/ssh"
"github.com/ava-labs/avalanche-cli/pkg/teleporter"
"github.com/ava-labs/avalanche-cli/pkg/ux"

"github.com/spf13/cobra"
)

var stopRelayerNetworkOptions = []networkoptions.NetworkOption{networkoptions.Local, networkoptions.Cluster}

// avalanche teleporter relayer stop
func newStopRelayerCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "stop",
Short: "stops AWM relayer",
Long: `Stops AWM relayer on the specified network (Currently only for local network, cluster).`,
SilenceUsage: true,
RunE: stopRelayer,
Args: cobra.ExactArgs(0),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, stopRelayerNetworkOptions)
return cmd
}

func stopRelayer(_ *cobra.Command, _ []string) error {
network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
globalNetworkFlags,
false,
stopRelayerNetworkOptions,
"",
)
if err != nil {
return err
}
switch {
case network.Kind == models.Local:
b, _, _, err := teleporter.RelayerIsUp(
app.GetAWMRelayerRunPath(),
)
if err != nil {
return err
}
if !b {
return fmt.Errorf("there is no CLI-managed local AWM relayer running")
}
if err := teleporter.RelayerCleanup(
app.GetAWMRelayerRunPath(),
app.GetAWMRelayerStorageDir(),
); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("Local AWM Relayer successfully stopped")
case network.ClusterName != "":
host, err := node.GetAWMRelayerHost(app, network.ClusterName)
if err != nil {
return err
}
if err := ssh.RunSSHStopAWMRelayerService(host); err != nil {
return err
}
ux.Logger.GreenCheckmarkToUser("Remote AWM Relayer on %s successfully stopped", host.GetCloudID())
}
return nil
}
47 changes: 47 additions & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package node

import (
"github.com/ava-labs/avalanche-cli/pkg/ansible"
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/utils"
)

func GetHostWithCloudID(app *application.Avalanche, clusterName string, cloudID string) (*models.Host, error) {
hosts, err := ansible.GetInventoryFromAnsibleInventoryFile(app.GetAnsibleInventoryDirPath(clusterName))
if err != nil {
return nil, err
}
monitoringInventoryFile := app.GetMonitoringInventoryDir(clusterName)
if utils.FileExists(monitoringInventoryFile) {
monitoringHosts, err := ansible.GetInventoryFromAnsibleInventoryFile(monitoringInventoryFile)
if err != nil {
return nil, err
}
hosts = append(hosts, monitoringHosts...)
}
for _, host := range hosts {
if host.GetCloudID() == cloudID {
return host, nil
}
}
return nil, nil
}

func GetAWMRelayerHost(app *application.Avalanche, clusterName string) (*models.Host, error) {
clusterConfig, err := app.GetClusterConfig(clusterName)
if err != nil {
return nil, err
}
relayerCloudID := ""
for _, cloudID := range clusterConfig.GetCloudIDs() {
if nodeConfig, err := app.LoadClusterNodeConfig(cloudID); err != nil {
return nil, err
} else if nodeConfig.IsAWMRelayer {
relayerCloudID = nodeConfig.NodeID
}
}
return GetHostWithCloudID(app, clusterName, relayerCloudID)
}
Loading

0 comments on commit 690e734

Please sign in to comment.