Skip to content

Commit eb3fc1a

Browse files
committed
feat: implement GCP Go provider interface and CLI registration
1 parent 1f07f1b commit eb3fc1a

4 files changed

Lines changed: 89 additions & 16 deletions

File tree

cmd/sifgen/main.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context" // Add context import
45
"fmt"
56
"io/ioutil"
67
"log"
@@ -56,22 +57,27 @@ func networkCmd() *cobra.Command {
5657
}
5758
}
5859

60+
61+
5962
func networkCreateCmd() *cobra.Command {
60-
return &cobra.Command{
63+
cmd := &cobra.Command{
6164
Use: "create [chain-id] [validator-count] [output-dir] [seed-ip-address] [output-file]",
6265
Short: "Create a new network.",
63-
Args: cobra.MinimumNArgs(4),
66+
Args: cobra.MinimumNArgs(5), // Changed to 5 args due to new provider flag
6467
Run: func(cmd *cobra.Command, args []string) {
6568
bondAmount, _ := cmd.Flags().GetString("bond-amount")
6669
mintAmount, _ := cmd.Flags().GetString("mint-amount")
6770
keyringBackend, _ := cmd.Flags().GetString("keyring-backend")
71+
cloudProvider, _ := cmd.Flags().GetString("cloud-provider") // Get cloud-provider flag
6872

6973
count, _ := strconv.Atoi(args[1])
70-
network := sifgen.NewSifgen(&args[0]).NewNetwork(keyringBackend)
74+
sifgenInstance := sifgen.NewSifgen(&args[0])
75+
network := sifgenInstance.NewNetwork(keyringBackend, cloudProvider) // Pass cloudProvider
7176
network.BondAmount = bondAmount
7277
network.MintAmount = mintAmount
7378

74-
summary, err := network.Build(count, args[2], args[3])
79+
ctx := context.Background() // Create a new context
80+
summary, err := network.Build(ctx, count, args[2], args[3]) // Pass context to Build
7581
if err != nil {
7682
log.Fatal(err)
7783
}
@@ -81,6 +87,8 @@ func networkCreateCmd() *cobra.Command {
8187
}
8288
},
8389
}
90+
cmd.Flags().String("cloud-provider", "", "Cloud provider to use (e.g., 'gcp')") // Define the flag
91+
return cmd
8492
}
8593

8694
func networkResetCmd() *cobra.Command {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
package types
3+
4+
import "context"
5+
6+
type Node struct {
7+
ID string
8+
Name string
9+
IP string
10+
// Add other relevant node properties
11+
}
12+
13+
// Provider defines the interface for cloud provider interactions.
14+
type Provider interface {
15+
GetNodes(ctx context.Context) ([]Node, error)
16+
CreateNode(ctx context.Context, name string) (*Node, error)
17+
DeleteNode(ctx context.Context, id string) error
18+
}

tools/sifgen/network/network.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package network
22

33
import (
4+
"context" // Add context import
45
"crypto/sha256"
56
"fmt"
67
"io/ioutil"
@@ -9,6 +10,7 @@ import (
910
"strings"
1011

1112
"github.com/Sifchain/sifnode/tools/sifgen/common"
13+
sifgentypes "github.com/serfersac/sifnode/tools/sifgen/common/types" // Import the common types
1214
"github.com/Sifchain/sifnode/tools/sifgen/genesis"
1315
"github.com/Sifchain/sifnode/tools/sifgen/network/types"
1416
"github.com/Sifchain/sifnode/tools/sifgen/utils"
@@ -18,11 +20,23 @@ import (
1820
"gopkg.in/yaml.v3"
1921
)
2022

23+
24+
2125
type Network struct {
2226
ChainID string
2327
CLI utils.CLI
2428
BondAmount string
2529
MintAmount string
30+
Provider sifgentypes.Provider // Add Provider field
31+
}
32+
33+
// NewNetwork creates a new Network instance.
34+
func NewNetwork(chainID string, provider sifgentypes.Provider) *Network {
35+
return &Network{
36+
ChainID: chainID,
37+
CLI: utils.NewCLI(chainID, keyring.BackendTest),
38+
Provider: provider,
39+
}
2640
}
2741

2842
func Reset(chainID, networkDir string) error {
@@ -54,14 +68,9 @@ func Reset(chainID, networkDir string) error {
5468
return nil
5569
}
5670

57-
func NewNetwork(chainID string) *Network {
58-
return &Network{
59-
ChainID: chainID,
60-
CLI: utils.NewCLI(chainID, keyring.BackendTest),
61-
}
62-
}
6371

64-
func (n *Network) Build(count int, outputDir, seedIPv4Addr string) (*string, error) {
72+
73+
func (n *Network) Build(ctx context.Context, count int, outputDir, seedIPv4Addr string) (*string, error) { // Added ctx
6574
if err := n.CLI.Reset([]string{outputDir}); err != nil {
6675
return nil, err
6776
}
@@ -76,7 +85,21 @@ func (n *Network) Build(count int, outputDir, seedIPv4Addr string) (*string, err
7685
}
7786

7887
gentxDir := fmt.Sprintf("%s/%s", outputDir, GentxsDir)
79-
validators := n.initValidators(count, outputDir, seedIPv4Addr)
88+
89+
var validators []*Validator
90+
for i := 0; i < count; i++ {
91+
nodeName := fmt.Sprintf("sifnode-validator-%d", i)
92+
cloudNode, err := n.Provider.CreateNode(ctx, nodeName) // Use n.Provider
93+
if err != nil {
94+
return nil, fmt.Errorf("failed to create cloud node %s: %w", nodeName, err)
95+
}
96+
97+
validator := NewValidator(outputDir, n.ChainID, i == 0, cloudNode.IP) // Assuming first node is seed
98+
validator.Moniker = cloudNode.Name
99+
validator.IPv4Address = cloudNode.IP
100+
// Set other validator fields from cloudNode if available
101+
validators = append(validators, validator)
102+
}
80103

81104
for _, validator := range validators {
82105
appDirs := []string{validator.NodeHomeDir}

tools/sifgen/sifgen.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package sifgen
22

33
import (
4+
"context"
45
"fmt"
56
"io/ioutil"
67
"log"
78

89
"github.com/MakeNowJust/heredoc"
9-
10+
"github.com/serfersac/sifnode/gcp" // Import the GCP provider
11+
sifgentypes "github.com/serfersac/sifnode/tools/sifgen/common/types" // Import the common types
1012
"github.com/Sifchain/sifnode/tools/sifgen/key"
1113
"github.com/Sifchain/sifnode/tools/sifgen/network"
1214
"github.com/Sifchain/sifnode/tools/sifgen/node"
@@ -15,12 +17,27 @@ import (
1517

1618
type Sifgen struct {
1719
chainID *string
20+
providers map[string]sifgentypes.Provider
1821
}
1922

2023
func NewSifgen(chainID *string) Sifgen {
21-
return Sifgen{
24+
s := Sifgen{
2225
chainID: chainID,
26+
providers: make(map[string]sifgentypes.Provider),
27+
}
28+
// Register GCP provider
29+
ctx := context.Background() // Use a background context for initial provider setup
30+
gcpProvider, err := gcp.NewGCPProvider(ctx, "sifchain-devnet-307421", "asia-east1-a") // TODO: Make projectID and zone configurable
31+
if err != nil {
32+
log.Fatalf("Failed to create GCP provider: %v", err)
2333
}
34+
s.RegisterProvider("gcp", gcpProvider)
35+
return s
36+
}
37+
38+
// RegisterProvider registers a new cloud provider.
39+
func (s *Sifgen) RegisterProvider(name string, provider sifgentypes.Provider) {
40+
s.providers[name] = provider
2441
}
2542

2643
func (s Sifgen) NewNetwork(keyringBackend string) *network.Network {
@@ -30,9 +47,16 @@ func (s Sifgen) NewNetwork(keyringBackend string) *network.Network {
3047
}
3148
}
3249

33-
func (s Sifgen) NetworkCreate(count int, outputDir, startingIPAddress string, outputFile string) {
50+
func (s Sifgen) NetworkCreate(ctx context.Context, count int, outputDir, startingIPAddress, providerName, outputFile string) {
51+
provider, ok := s.providers[providerName]
52+
if !ok {
53+
log.Fatalf("Provider '%s' not found", providerName)
54+
}
55+
3456
net := network.NewNetwork(*s.chainID)
35-
summary, err := net.Build(count, outputDir, startingIPAddress)
57+
// Assuming net.Build can take a provider to create nodes
58+
// This part needs further integration with the existing network.Build logic
59+
summary, err := net.Build(ctx, count, outputDir, startingIPAddress, provider)
3660
if err != nil {
3761
log.Fatal(err)
3862
return

0 commit comments

Comments
 (0)