Skip to content

Commit

Permalink
feat: add script to run testnet + run node + config node
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyletang committed Jan 3, 2019
1 parent 221749d commit 74e33a4
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
71 changes: 70 additions & 1 deletion cmd/blockmania/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"chainspace.io/blockmania/internal/fsutil"
"chainspace.io/blockmania/internal/log"
"chainspace.io/blockmania/node"
)

func runCommand(args []string) int {
Expand All @@ -14,13 +19,17 @@ func runCommand(args []string) int {
nodeID uint64
configRoot string
runtimeRoot string
consoleLog string
fileLog string
)

cmd := flag.NewFlagSet("run", flag.ContinueOnError)
cmd.StringVar(&networkName, "network-name", "default", "Name of the network to be generated")
cmd.StringVar(&configRoot, "config-root", fsutil.DefaultRootDir(), "Path to the Blockmania root directory")
cmd.StringVar(&runtimeRoot, "runtime-root", fsutil.DefaultRootDir(), "Path to the runtime root directory")
cmd.StringVar(&runtimeRoot, "runtime-root", "", "Path to the runtime root directory")
cmd.Uint64Var(&nodeID, "node-id", 0, "ID of the blockmania node to be started")
cmd.StringVar(&consoleLog, "console-log", "", "Level of the log to the console")
cmd.StringVar(&fileLog, "file-log", "", "Level of the log to write to file")

cmd.Usage = func() {
fmt.Fprintf(cmd.Output(), "%v\n\n", helpRun())
Expand All @@ -35,6 +44,66 @@ func runCommand(args []string) int {
return 1
}

// load the configuration
cfg, err := node.LoadConfiguration(networkName, configRoot, runtimeRoot, nodeID)
if err != nil {
fmt.Fprintf(cmd.Output(), "%v", err)
return 1
}

// setup loging
if len(consoleLog) > 0 {
switch consoleLog {
case "debug":
log.ToConsole(log.DebugLevel)
case "error":
log.ToConsole(log.ErrorLevel)
case "fatal":
log.ToConsole(log.FatalLevel)
case "info":
log.ToConsole(log.InfoLevel)
default:
fmt.Fprintf(cmd.Output(), "Unknown console-log level `%v`\n", consoleLog)
return 1
}
} else {
log.ToConsole(cfg.Node.Logging.ConsoleLevel)
}

if len(fileLog) > 0 {
switch fileLog {
case "debug":
cfg.Node.Logging.FileLevel = log.DebugLevel
case "error":
cfg.Node.Logging.FileLevel = log.ErrorLevel
case "fatal":
cfg.Node.Logging.FileLevel = log.FatalLevel
case "info":
cfg.Node.Logging.FileLevel = log.InfoLevel
default:
fmt.Fprintf(cmd.Output(), "Unknown file-log level `%v`\n", consoleLog)
return 1
}
}

// init/start the node
var s *node.Server
s, err = node.Run(cfg)
if err != nil {
fmt.Fprintf(cmd.Output(), "Could not start node-%v, %v", nodeID, err)
return 1
}

defer func() {
if s != nil {
s.Shutdown()
}
}()

c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGKILL)
<-c

return 0
}

Expand Down
55 changes: 55 additions & 0 deletions node/load_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package node

import (
"fmt"
"os"
"path/filepath"
"strconv"

"chainspace.io/blockmania/config"
)

func LoadConfiguration(networkName, configRoot, runtimeRoot string, nodeID uint64) (*Config, error) {
_, err := os.Stat(configRoot)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf(
"Could not find the blockmania root directory `%v`", configRoot)
}
return nil, fmt.Errorf("Unable to access the blockmania root directory `%v`, %v", configRoot, err)
}

netPath := filepath.Join(configRoot, networkName)
netCfg, err := config.LoadNetwork(filepath.Join(netPath, "network.yaml"))
if err != nil {
return nil, fmt.Errorf("Could not load network.yaml, %v", err)
}

nodeDir := "node-" + strconv.FormatUint(nodeID, 10)
nodePath := filepath.Join(netPath, nodeDir)
nodeCfg, err := config.LoadNode(filepath.Join(nodePath, "node.yaml"))
if err != nil {
return nil, fmt.Errorf("Could not load node.yaml, %v", err)
}

keys, err := config.LoadKeys(filepath.Join(nodePath, "keys.yaml"))
if err != nil {
return nil, fmt.Errorf("Could not load keys.yaml, %v", err)
}

root := configRoot
if len(runtimeRoot) != 0 {
root = os.ExpandEnv(runtimeRoot)
}

cfg := &Config{
Directory: filepath.Join(root, networkName, nodeDir),
Keys: keys,
Network: netCfg,
NetworkName: networkName,
NodeID: nodeID,
Node: nodeCfg,
}

return cfg, nil
}
36 changes: 36 additions & 0 deletions scripts/run-testnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

if (( $# != 1 )); then
echo "missing nodes count";
exit 1;
fi

# session / network name
session=testnet-blockmania-${1}

# destroy session if exists
tmux kill-session -t ${session}
pkill blockmania
fuser 8080/tcp

# remove existing conf
rm -rf ~/.blockmania/${session}

# inializes the network
blockmania init -network-name ${session} -node-count ${1}

echo ">> Setting up tmux session ${session}"
tmux new-session -d -s ${session}

tot="${1}";
for i in $(seq 1 $tot)
do
tmux send-keys "blockmania run -network-name ${session} -node-id ${i} -file-log info" "C-l" "C-m"
if (( ${i} != $tot )); then
tmux split-window
tmux select-layout tiled
fi

done

tmux attach-session -t $session

0 comments on commit 74e33a4

Please sign in to comment.