Skip to content

Commit

Permalink
Merge pull request #6 from vechain/pedro/docker_implementation
Browse files Browse the repository at this point in the history
Docker Implementation
  • Loading branch information
otherview authored Jun 14, 2024
2 parents c0bfbb8 + 676c250 commit ff89704
Show file tree
Hide file tree
Showing 24 changed files with 674 additions and 170 deletions.
19 changes: 17 additions & 2 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ networkHub is a versatile framework designed to streamline the process of launch
### **Launch Pre-configured Network via command line**:
```bash
# Setup the preset network
> go run cmd/main.go cmd preset threeMasterNodesNetwork /Users/pedro/go/src/github.com/vechain/thor/bin/thor
> go run cmd/main.go cmd preset local threeMasterNodesNetwork /Users/pedro/go/src/github.com/vechain/thor/bin/thor
...
2024/06/07 17:31:43 INFO preset network config was successful... networkId=localthreeMaster

Expand All @@ -18,6 +18,21 @@ networkHub is a versatile framework designed to streamline the process of launch
2024/06/07 17:31:57 INFO Starting network... ID=localthreeMaster
...
```
### **Launch Pre-configured Network via command line using Docker**:
```bash
# Setup the preset network
> go run cmd/main.go cmd preset docker threeMasterNodesNetwork vechain/thor:latest
...
2024/06/13 17:15:18 INFO Configuring network...
2024/06/13 17:15:18 INFO Network saved to file filepath=/Users/pedro/go/src/github.com/vechain/networkhub/networks_db.json
2024/06/13 17:15:18 INFO preset network config was successful... networkId=dockerthreeMaster
# Start preset network
> go run cmd/main.go cmd start dockerthreeMaster
2024/06/13 17:16:05 INFO Network created networkName=dockerthreeMaster-network
2024/06/13 17:16:06 INFO network started successfully...
...
```


### **Launch Pre-configured Network via API server**:
Expand All @@ -32,7 +47,7 @@ networkHub is a versatile framework designed to streamline the process of launch
# Start a local pre-setup configured network
# Must specify the path to thor locally
curl -X POST http://localhost:8080/preset/threeMasterNodesNetwork \
-d '{"artifactPath":"/Users/pedro/go/src/github.com/vechain/thor/bin/thor"}'
-d '{"artifactPath":"/Users/pedro/go/src/github.com/vechain/thor/bin/thor", "environment":"local"}'
# Response
{"networkId": "localthreeMasterNodes"}
Expand Down
97 changes: 0 additions & 97 deletions .github/README.old.md

This file was deleted.

1 change: 1 addition & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1cd6cde4277dd30deb5a533446e516649fb9fbce:environments/local/local_test.go:generic-api-key:96
10 changes: 5 additions & 5 deletions cmd/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package cmd
import (
"fmt"

"github.com/vechain/networkhub/hub"
"github.com/vechain/networkhub/preset"

"github.com/spf13/cobra"
"github.com/vechain/networkhub/entrypoint/api"
"github.com/vechain/networkhub/environments/docker"
"github.com/vechain/networkhub/environments/local"

"github.com/spf13/cobra"
"github.com/vechain/networkhub/hub"
"github.com/vechain/networkhub/preset"
)

var apiCmd = &cobra.Command{
Expand All @@ -20,6 +19,7 @@ var apiCmd = &cobra.Command{

envManager := hub.NewNetworkHub()
envManager.RegisterEnvironment("local", local.NewLocalEnv)
envManager.RegisterEnvironment("docker", docker.NewDockerEnv)

presets := preset.NewPresetNetworks()
presets.Register("threeMasterNodesNetwork", preset.LocalThreeMasterNodesNetwork)
Expand Down
22 changes: 11 additions & 11 deletions cmd/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package cmd

import (
"fmt"
"io/ioutil"
"log/slog"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/spf13/cobra"
"github.com/vechain/networkhub/environments/docker"
"github.com/vechain/networkhub/environments/local"
"github.com/vechain/networkhub/hub"
"github.com/vechain/networkhub/preset"

"github.com/spf13/cobra"

cmdentrypoint "github.com/vechain/networkhub/entrypoint/cmd"
)

func setup() *cmdentrypoint.Cmd {
envManager := hub.NewNetworkHub()
envManager.RegisterEnvironment("local", local.NewLocalEnv)
envManager.RegisterEnvironment("docker", docker.NewDockerEnv)

presets := preset.NewPresetNetworks()
presets.Register("threeMasterNodesNetwork", preset.LocalThreeMasterNodesNetwork)
Expand Down Expand Up @@ -87,7 +87,7 @@ var configureCmd = &cobra.Command{
cmdManager := setup()

// Read from the specified file
data, err := ioutil.ReadFile(args[0])
data, err := os.ReadFile(args[0])
if err != nil {
fmt.Printf("Error reading config file: %v\n", err)
os.Exit(1)
Expand All @@ -104,19 +104,20 @@ var configureCmd = &cobra.Command{
},
}

// TODO add a preset list
var presetCmd = &cobra.Command{
Use: "preset [preset-name] [preset-thor-path]",
Use: "preset [environment] [preset-name] [preset-thor-path]",
Short: "Configures a preset network",
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(3),
Run: func(cmd *cobra.Command, args []string) {
cmdManager := setup()

presetNetwork := args[0]
presetArtifactPath := args[1]
presetEnv := args[0]
presetNetwork := args[1]
presetArtifactPath := args[2]

slog.Info("Configuring network...")

networkID, err := cmdManager.Preset(presetNetwork, presetArtifactPath)
networkID, err := cmdManager.Preset(presetNetwork, presetEnv, presetArtifactPath)
if err != nil {
slog.Error("unable to config preset network", "err", err)
return
Expand All @@ -126,7 +127,6 @@ var presetCmd = &cobra.Command{
}

func init() {

cmdCmd.AddCommand(startCmd, configureCmd, presetCmd)
rootCmd.AddCommand(cmdCmd)
}
4 changes: 2 additions & 2 deletions entrypoint/api/http_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Server struct {
presets *preset.Networks
}

func New(envMgr *hub.NetworkHub, presets *preset.Networks) *Server {
func New(networkHub *hub.NetworkHub, presets *preset.Networks) *Server {
return &Server{
networkHub: envMgr,
networkHub: networkHub,
presets: presets,
}
}
Expand Down
2 changes: 1 addition & 1 deletion entrypoint/api/http_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestStartStopHandler(t *testing.T) {
{
name: "Load existing preset network",
target: "/preset/noop-network",
payload: "{ \"artifactPath\": \"noop/dir\" }",
payload: "{ \"artifactPath\": \"noop/dir\", \"environment\":\"noop\" }",
method: http.MethodPost,
wantStatus: http.StatusOK,
wantBody: "{\"networkId\": \"noop\"}",
Expand Down
5 changes: 3 additions & 2 deletions entrypoint/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"encoding/json"
"fmt"

"github.com/vechain/networkhub/hub"
"github.com/vechain/networkhub/network"
"github.com/vechain/networkhub/preset"
Expand Down Expand Up @@ -51,8 +52,8 @@ func (c *Cmd) LoadExistingNetworks() error {
return nil
}

func (c *Cmd) Preset(presetNetwork string, presetConfig string) (string, error) {
netCfg, err := c.presets.Load(presetNetwork, &preset.APIConfigPayload{ArtifactPath: presetConfig})
func (c *Cmd) Preset(presetNetwork string, environment, artifactPath string) (string, error) {
netCfg, err := c.presets.Load(presetNetwork, &preset.APIConfigPayload{Environment: environment, ArtifactPath: artifactPath})
if err != nil {
return "", fmt.Errorf("unable to load network preset: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions entrypoint/cmd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/vechain/networkhub/network"
"io/ioutil"
"log/slog"
"os"

"github.com/vechain/networkhub/network"
)

type StorageJson struct {
Expand All @@ -33,7 +33,7 @@ func (s *Storage) Store(networkID string, net *network.Network) error {
}

// Write the updated data back to file
err = ioutil.WriteFile(s.path, data, 0644)
err = os.WriteFile(s.path, data, 0644)
if err != nil {
return err
}
Expand All @@ -49,7 +49,7 @@ func (s *Storage) LoadExistingNetworks() (map[string]*network.Network, error) {
// Check if file exists
if _, err := os.Stat(s.path); err == nil {
// File exists, load the current data
fileData, err := ioutil.ReadFile(s.path)
fileData, err := os.ReadFile(s.path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ff89704

Please sign in to comment.