Skip to content

Commit

Permalink
fix rchain restart node
Browse files Browse the repository at this point in the history
  • Loading branch information
n8wb committed Jun 21, 2019
1 parent 155be49 commit 676fde6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 32 deletions.
1 change: 0 additions & 1 deletion deploy/postBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ func handleExtraPublicKeys(tn *testnet.TestNet) error {

func handlePostBuild(tn *testnet.TestNet) error {
return handleExtraPublicKeys(tn)
//return nil
}
9 changes: 4 additions & 5 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ import (
"strings"
)

var conf *util.Config

func init() {
conf = util.GetConfig()
}
var conf = util.GetConfig()

// Kill kills a single node by index on a server
func Kill(client ssh.Client, node int) error {
Expand Down Expand Up @@ -237,6 +233,9 @@ func StartServices(tn *testnet.TestNet, servs []services.Service) error {
ip = ""
}
err = service.Prepare(client, tn)
if err != nil {
return util.LogError(err)
}
_, err = client.KeepTryRun(serviceDockerRunCmd(net, ip,
fmt.Sprintf("%s%d", conf.ServicePrefix, i),
service.GetEnv(),
Expand Down
54 changes: 54 additions & 0 deletions protocols/helpers/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2019 whiteblock Inc.
This file is a part of the genesis.
Genesis is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Genesis is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package helpers

import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/whiteblock/genesis/testnet"
"github.com/whiteblock/genesis/util"
"strings"
)

// Helper functions to hint at common things that one may want to set

const (
alternativeCommandRegexKey = "__alternative_commands"
)

// SetAlternativeCmdExprs allows you to have your protocol support restart and related
// functionality if the blockchain main process looks diferent from the actual process.
func SetAlternativeCmdExprs(tn *testnet.TestNet, alts ...string) {
tn.BuildState.Set(alternativeCommandRegexKey, alts)
}

// GetCommandExprs get the command expressions to match on to find the main
// blockchain process
func GetCommandExprs(tn *testnet.TestNet, node string) ([]string, error) {
var cmd util.Command
ok := tn.BuildState.GetP(node, &cmd)
if !ok {
log.WithFields(log.Fields{"node": node}).Error("node not found")
return nil, fmt.Errorf("node not found")
}
out := []string{strings.Split(cmd.Cmdline, " ")[0]}
var alts []string
tn.BuildState.GetP(alternativeCommandRegexKey, &alts)
return append(out, alts...), nil
}
1 change: 1 addition & 0 deletions protocols/rchain/rchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func build(tn *testnet.TestNet) error {
}
buildState.Set("bootnode", enode)
buildState.Set("rConf", *rConf)
helpers.SetAlternativeCmdExprs(tn, "/docker-java-home/bin/java")

err = helpers.CreateConfigs(tn, "/datadir/rnode.conf", func(node ssh.Node) ([]byte, error) {
if node.GetAbsoluteNumber() == 0 {
Expand Down
58 changes: 32 additions & 26 deletions rest/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
log "github.com/sirupsen/logrus"
"github.com/whiteblock/genesis/db"
"github.com/whiteblock/genesis/manager"
"github.com/whiteblock/genesis/protocols/helpers"
"github.com/whiteblock/genesis/ssh"
"github.com/whiteblock/genesis/state"
"github.com/whiteblock/genesis/status"
"github.com/whiteblock/genesis/testnet"
Expand Down Expand Up @@ -152,6 +154,23 @@ func delNodes(w http.ResponseWriter, r *http.Request) {
go manager.DelNodes(num, testnetID)
}

func getNodePids(tn *testnet.TestNet, n ssh.Node, node string) ([]string, error) {
cmdsToTry, err := helpers.GetCommandExprs(tn, node)
if err != nil {
return nil, util.LogError(err)
}
log.WithFields(log.Fields{"toTry": cmdsToTry}).Info("got the commands to try")
out := []string{}
for _, cmd := range cmdsToTry {
pid, err := tn.Clients[n.GetServerID()].DockerExec(n, fmt.Sprintf(
"ps aux | grep '%s' | grep -v grep | grep -v nibbler | awk '{print $2}'", cmd))
if err == nil {
out = append(out, strings.Split(pid, "\n")...)
}
}
return out, nil
}

func restartNode(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
testnetID := params["id"]
Expand All @@ -177,34 +196,29 @@ func restartNode(w http.ResponseWriter, r *http.Request) {
http.Error(w, util.LogError(err).Error(), 500)
return
}
cmdgexCmd := fmt.Sprintf("ps aux | grep '%s' | grep -v grep| awk '{print $2}'", strings.Split(cmd.Cmdline, " ")[0])
node, err := db.GetNodeByLocalID(tn.Nodes, cmd.Node)
if err != nil {
http.Error(w, util.LogError(err).Error(), 500)
return
}
procsRaw, err := client.DockerExec(node, cmdgexCmd)

procs, err := getNodePids(tn, &tn.Nodes[cmd.Node], nodeNum)
if err != nil {
http.Error(w, util.LogError(err).Error(), 500)
return
}
procs := strings.Split(procsRaw, "\n")
log.WithFields(log.Fields{"procs": procs}).Debug("got the possible process ids")

for _, pid := range procs {
if pid == "" {
continue
}
_, err = client.DockerExec(node, fmt.Sprintf("kill -INT %s", pid))
_, err = client.DockerExec(&tn.Nodes[cmd.Node], fmt.Sprintf("kill -INT %s", pid))
if err != nil {
http.Error(w, util.LogError(err).Error(), 500)
return
}
}

node := &tn.Nodes[cmd.Node]
killedSuccessfully := false
for i := uint(0); i < conf.KillRetries; i++ {
_, err = client.DockerExec(node, fmt.Sprintf("ps aux | grep '%s' | grep -v grep", strings.Split(cmd.Cmdline, " ")[0]))
_, err = client.DockerExec(node,
fmt.Sprintf("ps aux | grep '%s' | grep -v grep", strings.Split(cmd.Cmdline, " ")[0]))
if err != nil {
killedSuccessfully = true
break
Expand Down Expand Up @@ -252,26 +266,18 @@ func signalNode(w http.ResponseWriter, r *http.Request) {
return
}
n := &tn.Nodes[nodeNum]
var cmd util.Command
ok := tn.BuildState.GetP(node, &cmd)
if !ok {
log.WithFields(log.Fields{"node": node}).Error("node not found")
http.Error(w, fmt.Sprintf("Node %s not found", node), 404)
return
}

cmdgexCmd := fmt.Sprintf("ps aux | grep '%s' | grep -v grep| grep -v nibbler | awk '{print $2}'| tail -n 1",
strings.Split(cmd.Cmdline, " ")[0])
pid, err := tn.Clients[n.Server].DockerExec(n, cmdgexCmd)
procs, err := getNodePids(tn, tn.Nodes[nodeNum], node)
if err != nil {
http.Error(w, util.LogError(err).Error(), 500)
return
}
log.WithFields(log.Fields{"procs": procs}).Debug("got the possible process ids")

_, err = tn.Clients[n.Server].DockerExec(n, fmt.Sprintf("kill -%s %s", signal, pid))
if err != nil {
http.Error(w, util.LogError(err).Error(), 500)
return
for _, pid := range procs {
if pid == "" {
continue
}
_, err = tn.Clients[n.GetServerID()].DockerExec(n, fmt.Sprintf("kill -%s %s", signal, pid))
}
w.Write([]byte(fmt.Sprintf("Sent signal %s to node %s", signal, node)))
}
Expand Down

0 comments on commit 676fde6

Please sign in to comment.