p2psim: Use P2PSIM_NETWORK environment variable

Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
Lewis Marshall 2017-06-11 21:13:58 +01:00
parent e32197f998
commit 8e17ee44f0
2 changed files with 54 additions and 45 deletions

View file

@ -26,6 +26,7 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -39,7 +40,10 @@ import (
"gopkg.in/urfave/cli.v1" "gopkg.in/urfave/cli.v1"
) )
var client *simulations.Client var (
client *simulations.Client
networkID string
)
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
@ -109,18 +113,32 @@ func main() {
Name: "node", Name: "node",
Usage: "manage simulation nodes", Usage: "manage simulation nodes",
Action: listNodes, Action: listNodes,
Flags: []cli.Flag{
cli.StringFlag{
Name: "network",
Usage: "simulation network",
},
},
Before: func(ctx *cli.Context) error {
networkID = ctx.GlobalString("network")
if networkID == "" {
networkID = os.Getenv("P2PSIM_NETWORK")
}
if networkID == "" {
return errors.New("missing network, set with --network or P2PSIM_NETWORK")
}
return nil
},
Subcommands: []cli.Command{ Subcommands: []cli.Command{
{ {
Name: "list", Name: "list",
ArgsUsage: "<network>", Usage: "list nodes",
Usage: "list nodes", Action: listNodes,
Action: listNodes,
}, },
{ {
Name: "create", Name: "create",
ArgsUsage: "<network>", Usage: "create a node",
Usage: "create a node", Action: createNode,
Action: createNode,
Flags: []cli.Flag{ Flags: []cli.Flag{
cli.StringFlag{ cli.StringFlag{
Name: "config", Name: "config",
@ -131,37 +149,37 @@ func main() {
}, },
{ {
Name: "show", Name: "show",
ArgsUsage: "<network> <node>", ArgsUsage: "<node>",
Usage: "show node information", Usage: "show node information",
Action: showNode, Action: showNode,
}, },
{ {
Name: "start", Name: "start",
ArgsUsage: "<network> <node>", ArgsUsage: "<node>",
Usage: "start a node", Usage: "start a node",
Action: startNode, Action: startNode,
}, },
{ {
Name: "stop", Name: "stop",
ArgsUsage: "<network> <node>", ArgsUsage: "<node>",
Usage: "stop a node", Usage: "stop a node",
Action: stopNode, Action: stopNode,
}, },
{ {
Name: "connect", Name: "connect",
ArgsUsage: "<network> <node> <peer>", ArgsUsage: "<node> <peer>",
Usage: "connect a node to a peer node", Usage: "connect a node to a peer node",
Action: connectNode, Action: connectNode,
}, },
{ {
Name: "disconnect", Name: "disconnect",
ArgsUsage: "<network> <node> <peer>", ArgsUsage: "<node> <peer>",
Usage: "disconnect a node from a peer node", Usage: "disconnect a node from a peer node",
Action: disconnectNode, Action: disconnectNode,
}, },
{ {
Name: "rpc", Name: "rpc",
ArgsUsage: "<network> <node> <method> [<args>]", ArgsUsage: "<node> <method> [<args>]",
Usage: "call a node RPC method", Usage: "call a node RPC method",
Action: rpcNode, Action: rpcNode,
Flags: []cli.Flag{ Flags: []cli.Flag{
@ -280,11 +298,9 @@ func loadSnapshot(ctx *cli.Context) error {
} }
func listNodes(ctx *cli.Context) error { func listNodes(ctx *cli.Context) error {
args := ctx.Args() if len(ctx.Args()) != 0 {
if len(args) != 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0]
nodes, err := client.GetNodes(networkID) nodes, err := client.GetNodes(networkID)
if err != nil { if err != nil {
return err return err
@ -307,11 +323,9 @@ func protocolList(node *p2p.NodeInfo) []string {
} }
func createNode(ctx *cli.Context) error { func createNode(ctx *cli.Context) error {
args := ctx.Args() if len(ctx.Args()) != 0 {
if len(args) != 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0]
config := &adapters.NodeConfig{} config := &adapters.NodeConfig{}
if err := json.Unmarshal([]byte(ctx.String("config")), config); err != nil { if err := json.Unmarshal([]byte(ctx.String("config")), config); err != nil {
return err return err
@ -326,11 +340,10 @@ func createNode(ctx *cli.Context) error {
func showNode(ctx *cli.Context) error { func showNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) != 2 { if len(args) != 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1]
node, err := client.GetNode(networkID, nodeName) node, err := client.GetNode(networkID, nodeName)
if err != nil { if err != nil {
return err return err
@ -352,11 +365,10 @@ func showNode(ctx *cli.Context) error {
func startNode(ctx *cli.Context) error { func startNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) != 2 { if len(args) != 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1]
if err := client.StartNode(networkID, nodeName); err != nil { if err := client.StartNode(networkID, nodeName); err != nil {
return err return err
} }
@ -366,11 +378,10 @@ func startNode(ctx *cli.Context) error {
func stopNode(ctx *cli.Context) error { func stopNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) != 2 { if len(args) != 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1]
if err := client.StopNode(networkID, nodeName); err != nil { if err := client.StopNode(networkID, nodeName); err != nil {
return err return err
} }
@ -380,12 +391,11 @@ func stopNode(ctx *cli.Context) error {
func connectNode(ctx *cli.Context) error { func connectNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) != 3 { if len(args) != 2 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1] peerName := args[1]
peerName := args[2]
if err := client.ConnectNode(networkID, nodeName, peerName); err != nil { if err := client.ConnectNode(networkID, nodeName, peerName); err != nil {
return err return err
} }
@ -395,12 +405,11 @@ func connectNode(ctx *cli.Context) error {
func disconnectNode(ctx *cli.Context) error { func disconnectNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) != 3 { if len(args) != 2 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1] peerName := args[1]
peerName := args[2]
if err := client.DisconnectNode(networkID, nodeName, peerName); err != nil { if err := client.DisconnectNode(networkID, nodeName, peerName); err != nil {
return err return err
} }
@ -410,12 +419,11 @@ func disconnectNode(ctx *cli.Context) error {
func rpcNode(ctx *cli.Context) error { func rpcNode(ctx *cli.Context) error {
args := ctx.Args() args := ctx.Args()
if len(args) < 3 { if len(args) < 2 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name) return cli.ShowCommandHelp(ctx, ctx.Command.Name)
} }
networkID := args[0] nodeName := args[0]
nodeName := args[1] method := args[1]
method := args[2]
rpcClient, err := client.RPCClient(context.Background(), networkID, nodeName) rpcClient, err := client.RPCClient(context.Background(), networkID, nodeName)
if err != nil { if err != nil {
return err return err

View file

@ -13,14 +13,15 @@ main() {
p2psim network create --config '{"id": "example", "default_service": "ping-pong"}' p2psim network create --config '{"id": "example", "default_service": "ping-pong"}'
info "creating 10 nodes" info "creating 10 nodes"
export P2PSIM_NETWORK="example"
for i in $(seq 1 10); do for i in $(seq 1 10); do
p2psim node create "example" p2psim node create
p2psim node start "example" "$(node_name $i)" p2psim node start "$(node_name $i)"
done done
info "connecting node01 to all other nodes" info "connecting node01 to all other nodes"
for i in $(seq 2 10); do for i in $(seq 2 10); do
p2psim node connect "example" "node01" "$(node_name $i)" p2psim node connect "node01" "$(node_name $i)"
done done
info "done" info "done"