Skip to content

Commit

Permalink
node: Add config rewriting
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <[email protected]>
  • Loading branch information
aluzzardi committed Dec 6, 2018
1 parent f59f044 commit 5472a22
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
45 changes: 45 additions & 0 deletions node/init.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package node

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"

"github.com/blocklayerhq/chainkit/project"
"github.com/blocklayerhq/chainkit/ui"
Expand Down Expand Up @@ -41,3 +47,42 @@ func initialize(ctx context.Context, p *project.Project) error {

return nil
}

// updateConfig updates the config file for the node before starting.
func updateConfig(file string, vars map[string]string) error {
config, err := ioutil.ReadFile(file)
if err != nil {
return err
}

output := bytes.NewBufferString("")
scanner := bufio.NewScanner(bytes.NewReader(config))
for scanner.Scan() {
line := scanner.Text()
// Scan vars to replace in the current line
for k, v := range vars {
if strings.HasPrefix(line+" = ", k) {
line = fmt.Sprintf("%s = %s", k, v)
}
}
if _, err := fmt.Fprintln(output, line); err != nil {
return err
}
}

if err := scanner.Err(); err != nil {
return err
}

f, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()

if _, err := io.Copy(f, output); err != nil {
return err
}

return nil
}
35 changes: 32 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (n *Node) Stop() {
// starting.
func (n *Node) Start(ctx context.Context, chainID string) error {
ui.Info("Starting %s", n.p.Name)

n.parentCtx, n.cancelCtx = context.WithCancel(ctx)

n.doneCh = make(chan struct{})
defer close(n.doneCh)

// Initialize if needed.
if err := initialize(n.parentCtx, n.p); err != nil {
return errors.Wrap(err, "initialization failed")
if err := n.init(ctx); err != nil {
return err
}

if err := n.discovery.Start(n.parentCtx); err != nil {
Expand Down Expand Up @@ -123,6 +123,33 @@ func (n *Node) Start(ctx context.Context, chainID string) error {
return g.Wait()
}

// init initializes the server if needed and updates the runtime config.
func (n *Node) init(ctx context.Context) error {
moniker, err := os.Hostname()
if err != nil {
return errors.Wrap(err, "unable to determine hostname")
}

// Initialize if needed.
if err := initialize(ctx, n.p); err != nil {
return errors.Wrap(err, "initialization failed")
}

return updateConfig(
n.p.ConfigFile(),
map[string]string{
// Set custom moniker. Needed to join nodes together.
"moniker": fmt.Sprintf("%q", moniker),
// Needed to join local/private networks.
"addr_book_strict": "false",
// Needed to enable dial_seeds
"unsafe": "true",
// Info logs are just too verbose.
"log_level": fmt.Sprintf("%q", "*:error"),
},
)
}

func (n *Node) createNetwork(ctx context.Context) (string, error) {
f, err := ioutil.TempFile(os.TempDir(), "chainkit-image")
if err != nil {
Expand Down Expand Up @@ -192,6 +219,8 @@ func (n *Node) announce(ctx context.Context, chainID string, peer *discovery.Pee
}

func (n *Node) discoverPeers(ctx context.Context, chainID string) error {
ui.Info("Discovering peer nodes...")

seenNodes := make(map[string]struct{})

for {
Expand Down
5 changes: 5 additions & 0 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func (p *Project) ConfigDir() string {
return path.Join(p.StateDir(), "config")
}

// ConfigFile returns the path of the configuration file.
func (p *Project) ConfigFile() string {
return path.Join(p.ConfigDir(), "config.toml")
}

// GenesisPath returns the genesis path for the project.
func (p *Project) GenesisPath() string {
return path.Join(p.ConfigDir(), "genesis.json")
Expand Down

0 comments on commit 5472a22

Please sign in to comment.