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

MorpheusCLI - Add Deploy Command #1330

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
100 changes: 100 additions & 0 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package cmd

import (
"context"
"encoding/json"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/ava-labs/avalanchego/tests/fixture/tmpnet"
"github.com/spf13/cobra"

"github.com/ava-labs/hypersdk/examples/morpheusvm/consts"
"github.com/ava-labs/hypersdk/examples/morpheusvm/rpc"
"github.com/ava-labs/hypersdk/examples/morpheusvm/tests/workload"
"github.com/ava-labs/hypersdk/tests/fixture"
"github.com/ava-labs/hypersdk/utils"
)

const (
owner = "morpheus-cli"
devNetPort = "9560"
)

var deployCmd = &cobra.Command{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead add this via the root handler, so that it can be shared across VMs

root, err := cli.New(controller)
?

Use: "deploy",
Short: "Quickly deploy an instance of MorpheusVM",
RunE: func(cmd *cobra.Command, args []string) error {
genesis, _, err := workload.New(minBlockGap)
if err != nil {
return err
}
genBytes, err := json.Marshal(genesis)
if err != nil {
return err
}
nodes := tmpnet.NewNodesOrPanic(numOfNodes)
// Set static port for DevNet
nodes[0].Flags["http-port"] = devNetPort
subnet := fixture.NewHyperVMSubnet(
consts.Name,
consts.ID,
genBytes,
nodes...,
)

timeOut := 2 * time.Minute

ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()

network := fixture.NewTmpnetNetwork(owner, nodes, subnet)
if err := tmpnet.BootstrapNewNetwork(
ctx,
os.Stdout,
network,
"",
avalancheGoPath,
avalancheGoPluginDir,
); err != nil {
utils.Outf(err.Error())
return err
}

utils.Outf("\nBootstrapped Network")
var rpcURL strings.Builder
if _, err := rpcURL.WriteString("http://127.0.0.1:"); err != nil {
return err
}
if _, err := rpcURL.WriteString(devNetPort); err != nil {
return err
}
if _, err := rpcURL.WriteString("/ext/bc/"); err != nil {
return err
}
if _, err := rpcURL.WriteString(subnet.Chains[0].ChainID.String()); err != nil {
return err
}
if _, err := rpcURL.WriteString(rpc.JSONRPCEndpoint); err != nil {
return err
}
utils.Outf("\nRPC URL is: %v\n", rpcURL.String())

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

<-signals
if err := network.Stop(context.Background()); err != nil {
panic(err)
}
utils.Outf("\nClosed network\n")

return nil
},
}
23 changes: 23 additions & 0 deletions examples/morpheusvm/cmd/morpheus-cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ var (
prometheusFile string
prometheusData string
startPrometheus bool
avalancheGoPath string
avalancheGoPluginDir string
numOfNodes int

rootCmd = &cobra.Command{
Use: "morpheus-cli",
Expand Down Expand Up @@ -183,6 +186,26 @@ func init() {
prometheusCmd.AddCommand(
generatePrometheusCmd,
)

// deploy
deployCmd.PersistentFlags().StringVar(
&avalancheGoPath,
"avalancheGo-path",
"/tmp/hypersdk/avalanchego-d729e5c7ef9f008c3e89cd7131148ad3acda2e34/avalanchego",
"location of avalancheGo binary",
)
deployCmd.PersistentFlags().StringVar(
&avalancheGoPluginDir,
"avalancheGo-plugin-dir",
"/tmp/hypersdk/avalanchego-d729e5c7ef9f008c3e89cd7131148ad3acda2e34/plugins",
"location of avalancheGo plugin binaries",
)
deployCmd.PersistentFlags().IntVar(
&numOfNodes,
"num-of-nodes",
1,
"number of nodes to deploy with",
)
}

func Execute() error {
Expand Down
Loading