|
| 1 | +package playground |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/unpackdev/fdb/pkg/rbac" |
| 12 | + "github.com/unpackdev/fdb/pkg/types" |
| 13 | + "github.com/unpackdev/fdb/playground/suite" |
| 14 | + "github.com/urfave/cli/v2" |
| 15 | + "go.uber.org/zap" |
| 16 | +) |
| 17 | + |
| 18 | +// Config holds the configuration for the playground environment |
| 19 | +type Config struct { |
| 20 | + BasePort int |
| 21 | + NodeCount int |
| 22 | + LogLevel zap.AtomicLevel |
| 23 | +} |
| 24 | + |
| 25 | +// Run initializes and runs the playground environment based on the provided CLI context and config |
| 26 | +func Run(cliCtx *cli.Context, config Config) error { |
| 27 | + // Create a cancelable context |
| 28 | + ctx, cancel := context.WithCancel(context.Background()) |
| 29 | + defer cancel() |
| 30 | + |
| 31 | + // Use configuration from config parameter, overridden by CLI flags if provided |
| 32 | + basePort := config.BasePort |
| 33 | + if basePort == 0 { |
| 34 | + basePort = 9000 |
| 35 | + } |
| 36 | + if cliCtx.IsSet("base-port") { |
| 37 | + basePort = cliCtx.Int("base-port") |
| 38 | + } |
| 39 | + |
| 40 | + nodeCount := config.NodeCount |
| 41 | + if nodeCount == 0 { |
| 42 | + nodeCount = 5 |
| 43 | + } |
| 44 | + if cliCtx.IsSet("node-count") { |
| 45 | + nodeCount = cliCtx.Int("node-count") |
| 46 | + } |
| 47 | + |
| 48 | + logLevel := zap.InfoLevel |
| 49 | + if config.LogLevel.Level() != zap.InfoLevel { |
| 50 | + logLevel = config.LogLevel.Level() |
| 51 | + } |
| 52 | + if cliCtx.IsSet("debug") && cliCtx.Bool("debug") { |
| 53 | + logLevel = zap.DebugLevel |
| 54 | + } |
| 55 | + |
| 56 | + // Define the node roles |
| 57 | + roles := make([]types.Role, nodeCount) |
| 58 | + for i := range roles { |
| 59 | + roles[i] = rbac.RoleNode |
| 60 | + } |
| 61 | + |
| 62 | + // Initialize the nodes |
| 63 | + fmt.Printf("Initializing %d nodes with base port %d...\n", nodeCount, basePort) |
| 64 | + nodes, err := suite.InitializeNodes( |
| 65 | + ctx, |
| 66 | + logLevel, |
| 67 | + types.Ed25519SignerType, |
| 68 | + roles, |
| 69 | + basePort, |
| 70 | + types.TCPTransportType, |
| 71 | + ) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("failed to initialize nodes: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + // Ensure we clean up nodes when exiting |
| 77 | + defer func() { |
| 78 | + fmt.Println("Shutting down nodes...") |
| 79 | + if err := suite.ShutdownTestNodes(nodes); err != nil { |
| 80 | + fmt.Printf("Error shutting down nodes: %v\n", err) |
| 81 | + } |
| 82 | + }() |
| 83 | + |
| 84 | + // Print node information |
| 85 | + fmt.Println("Playground nodes initialized:") |
| 86 | + for i, node := range nodes { |
| 87 | + fmt.Printf("Node %d: PeerID=%s, Role=%s\n", i, node.PeerID().String(), node.Role()) |
| 88 | + } |
| 89 | + |
| 90 | + // Wait for nodes to connect to each other |
| 91 | + fmt.Println("Waiting for nodes to connect to each other...") |
| 92 | + for i, node := range nodes { |
| 93 | + // Minus one because own peer needs to be excluded |
| 94 | + if err := node.WaitForPeersConnected(len(roles)-1, 10*time.Second); err != nil { |
| 95 | + fmt.Printf("Warning: Node %d didn't connect to all peers in time: %v\n", i, err) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Setup a signal handler for graceful shutdown |
| 100 | + sigCh := make(chan os.Signal, 1) |
| 101 | + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 102 | + |
| 103 | + fmt.Println("Playground environment is ready!") |
| 104 | + fmt.Println("Press Ctrl+C to exit...") |
| 105 | + |
| 106 | + // Wait for interrupt signal |
| 107 | + <-sigCh |
| 108 | + fmt.Println("Received interrupt signal, shutting down playground...") |
| 109 | + return nil |
| 110 | +} |
0 commit comments