diff --git a/griddriver/main.go b/griddriver/main.go index 4823047b0..ddd0ffc3f 100644 --- a/griddriver/main.go +++ b/griddriver/main.go @@ -450,6 +450,20 @@ func main() { return generateWgPrivKey() }, }, + { + Name: "generate-wg-public-key", + Usage: "Generates wireguard public key", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "key", + Usage: "wireguard private key", + Required: true, + }, + }, + Action: func(c *cli.Context) error { + return generateWgPublicKey(c) + }, + }, { Name: "rmb", Usage: "Make RMB call", @@ -491,6 +505,57 @@ func main() { }, Action: rmbDecorator(rmbCall), }, + { + Name: "batch-create-contract", + Description: "makes a batch create contract call to the tfchain", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "mnemonics", + Value: "", + Usage: "user mnemonics", + Required: true, + }, + cli.StringFlag{ + Name: "substrate", + Value: "wss://tfchain.grid.tf/ws", + Usage: "substrate URL", + }, + cli.StringFlag{ + Name: "contracts-data", + Usage: "json encoding of list of substrate BatchCreateContractData objects", + Required: true, + }, + cli.StringFlag{ + Name: "contracts-body", + Usage: "deployment body string", + Required: true, + }, + }, + Action: substrateDecorator(batchAllCreateContract), + }, + { + Name: "batch-cancel-contract", + Description: "makes a batch cancel contract call to the tfchain", + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "mnemonics", + Value: "", + Usage: "user mnemonics", + Required: true, + }, + cli.StringFlag{ + Name: "substrate", + Value: "wss://tfchain.grid.tf/ws", + Usage: "substrate URL", + }, + cli.StringFlag{ + Name: "contract-ids", + Usage: "json encoding of list of the contract ids to delete", + Required: true, + }, + }, + Action: substrateDecorator(batchCancelContract), + }, }, } diff --git a/griddriver/network.go b/griddriver/network.go index 4db35a79b..50edbc98b 100644 --- a/griddriver/network.go +++ b/griddriver/network.go @@ -74,15 +74,15 @@ func deployVM() cli.ActionFunc { } func buildNetwork(name, solutionType string, nodes []uint32) workloads.ZNet { - return workloads.ZNet{ - Name: name, - Nodes: nodes, - IPRange: gridtypes.NewIPNet(net.IPNet{ - IP: net.IPv4(10, 20, 0, 0), - Mask: net.CIDRMask(16, 32), - }), - SolutionType: solutionType, - } + return workloads.ZNet{ + Name: name, + Nodes: nodes, + IPRange: gridtypes.NewIPNet(net.IPNet{ + IP: net.IPv4(10, 20, 0, 0), + Mask: net.CIDRMask(16, 32), + }), + SolutionType: solutionType, + } } func generateWgPrivKey() error { @@ -95,3 +95,15 @@ func generateWgPrivKey() error { return nil } + +func generateWgPublicKey(ctx *cli.Context) error { + keyStr := ctx.String("key") + key, err := wgtypes.ParseKey(keyStr) + if err != nil { + return fmt.Errorf("failed to parse private key: %w", err) + } + + fmt.Printf("%s", key.PublicKey().String()) + return nil + +} diff --git a/griddriver/rmb.go b/griddriver/rmb.go index 69aa5f386..93becd4ae 100644 --- a/griddriver/rmb.go +++ b/griddriver/rmb.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math/rand" "time" substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go" @@ -33,7 +34,7 @@ func rmbDecorator(action func(c *cli.Context, client *peer.RpcClient) (interface mnemonics, subManager, peer.WithRelay(relay_url), - peer.WithSession("tfgrid-vclient"), + peer.WithSession(fmt.Sprintf("tfgrid-vclient-%d", rand.Int63())), ) if err != nil { return fmt.Errorf("failed to create peer client: %w", err) @@ -144,7 +145,7 @@ func nodeTakenPorts(c *cli.Context, client *peer.RpcClient) (interface{}, error) dst := uint32(c.Uint("dst")) var takenPorts []uint16 - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() if err := client.Call(ctx, dst, "zos.network.list_wg_ports", nil, &takenPorts); err != nil { @@ -172,6 +173,6 @@ func getNodePublicConfig(c *cli.Context, client *peer.RpcClient) (interface{}, e if err != nil { return nil, fmt.Errorf("failed to marshal public configuration: %w", err) } - fmt.Println(string(json)) + return string(json), nil } diff --git a/griddriver/substrate.go b/griddriver/substrate.go index d39fb5aa7..62ab48b4d 100644 --- a/griddriver/substrate.go +++ b/griddriver/substrate.go @@ -2,6 +2,7 @@ package main import ( "encoding/hex" + "encoding/json" "fmt" "github.com/pkg/errors" @@ -149,3 +150,45 @@ func signDeployment(ctx *cli.Context, sub *substrate.Substrate, identity substra sig := hex.EncodeToString(signatureBytes) return sig, nil } + +func batchAllCreateContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) { + body := ctx.String("contracts-body") + data := []byte(ctx.String("contracts-data")) + + contractData := []substrate.BatchCreateContractData{} + if err := json.Unmarshal(data, &contractData); err != nil { + return nil, fmt.Errorf("failed to decode contract data: %w", err) + } + + for id := range contractData { + if contractData[id].Name == "" { + contractData[id].Body = body + } + } + + contractIds, err := sub.BatchAllCreateContract(identity, contractData) + if err != nil { + return nil, fmt.Errorf("failed to create contracts: %w", err) + } + + ret, err := json.Marshal(contractIds) + if err != nil { + return nil, fmt.Errorf("failed to encode contract ids: %w", err) + } + + return string(ret), nil +} + +func batchCancelContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) { + data := []byte(ctx.String("contract-ids")) + contractIDs := []uint64{} + if err := json.Unmarshal(data, &contractIDs); err != nil { + return nil, fmt.Errorf("failed to decode contract ids: %w", err) + } + + if err := sub.BatchCancelContract(identity, contractIDs); err != nil { + return nil, fmt.Errorf("failed to cancel contracts: %w", err) + } + + return nil, nil +}