Skip to content

Commit

Permalink
Add new cli command for status service (#822)
Browse files Browse the repository at this point in the history
* add new command to gain access to status service
* add test for status cmd
* fix lint issues
  • Loading branch information
emmanuelm41 committed Oct 6, 2021
1 parent 8a3dece commit 5a9f689
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 11 deletions.
11 changes: 11 additions & 0 deletions cmd/drand-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,11 @@ var upToFlag = &cli.IntFlag{
Value: 0,
}

var jsonFlag = &cli.BoolFlag{
Name: "json",
Usage: "Set the output as json format",
}

var appCommands = []*cli.Command{
{
Name: "start",
Expand Down Expand Up @@ -359,6 +364,12 @@ var appCommands = []*cli.Command{
Flags: toArray(controlFlag),
Action: pingpongCmd,
},
{
Name: "status",
Usage: "get the status of many modules of running the daemon\n",
Flags: toArray(controlFlag, jsonFlag),
Action: statusCmd,
},
{
Name: "reset",
Usage: "Resets the local distributed information (share, group file and random beacons). It KEEPS the private/public key pair.",
Expand Down
43 changes: 32 additions & 11 deletions cmd/drand-cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,8 @@ func TestStartWithoutGroup(t *testing.T) {
}

func testStartedDrandFunctional(t *testing.T, ctrlPort, rootPath, address string, group *key.Group, fileStore key.Store) {
fmt.Println(" + running PING command with ", ctrlPort)
var err error
for i := 0; i < 3; i++ {
ping := []string{"drand", "util", "ping", "--control", ctrlPort}
err = CLI().Run(ping)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
require.NoError(t, err)
testPing(t, ctrlPort)
testStatus(t, ctrlPort)

require.NoError(t, toml.NewEncoder(os.Stdout).Encode(group))

Expand Down Expand Up @@ -343,6 +334,36 @@ func testStartedDrandFunctional(t *testing.T, ctrlPort, rootPath, address string
require.Error(t, err)
}

func testPing(t *testing.T, ctrlPort string) {
var err error

fmt.Println(" + running PING command with ", ctrlPort)
for i := 0; i < 3; i++ {
ping := []string{"drand", "util", "ping", "--control", ctrlPort}
err = CLI().Run(ping)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
require.NoError(t, err)
}

func testStatus(t *testing.T, ctrlPort string) {
var err error

fmt.Println(" + running STATUS command with ", ctrlPort)
for i := 0; i < 3; i++ {
status := []string{"drand", "util", "status", "--control", ctrlPort}
err = CLI().Run(status)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
require.NoError(t, err)
}

func TestClientTLS(t *testing.T) {
tmpPath := path.Join(os.TempDir(), "drand")
os.Mkdir(tmpPath, 0740)
Expand Down
24 changes: 24 additions & 0 deletions cmd/drand-cli/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,30 @@ func pingpongCmd(c *cli.Context) error {
return nil
}

func statusCmd(c *cli.Context) error {
client, err := controlClient(c)
if err != nil {
return err
}
resp, err := client.Status()
if err != nil {
return fmt.Errorf("drand: can't get the status of the daemon ... %s", err)
}

if c.IsSet(jsonFlag.Name) {
str, err := json.Marshal(resp)
if err != nil {
return fmt.Errorf("cannot marshal the response ... %s", err)
}
fmt.Fprintf(output, "%s \n", string(str))
} else {
fmt.Fprintf(output, "drand daemon is alive on port %s and its status is: \n", controlPort(c))
fmt.Fprintf(output, "%s \n", core.StatusResponseToString(resp))
}

return nil
}

func showGroupCmd(c *cli.Context) error {
client, err := controlClient(c)
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions core/drand_status.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package core

import (
"fmt"
"strings"

"github.com/drand/drand/protobuf/drand"
)

const UnknownDesc = "Unknown"
const NotStartedDesc = "Not started"
const InProgressDesc = "In progress"
Expand All @@ -25,3 +32,62 @@ const (
BeaconNotInited BeaconStatus = iota
BeaconInited
)

func GetDkgStatusDescription(value DkgStatus) string {
switch value {
case DkgReady:
return "Done"
case DkgInProgress:
return InProgressDesc
case DkgNotStarted:
return NotStartedDesc
default:
return UnknownDesc
}
}

func GetReshareStatusDescription(value ReshareStatus) string {
switch value {
case ReshareInProgress:
return InProgressDesc
case ReshareNotInProgress:
return NotStartedDesc
default:
return UnknownDesc
}
}

func GetBeaconDescription(value BeaconStatus) string {
switch value {
case BeaconNotInited:
return "Not inited"
case BeaconInited:
return "Inited"
default:
return UnknownDesc
}
}

func StatusResponseToString(status *drand.StatusResponse) string {
dkgStatus := GetDkgStatusDescription(DkgStatus(status.Dkg.Status))
reshareStatus := GetReshareStatusDescription(ReshareStatus(status.Reshare.Status))
beaconStatus := GetBeaconDescription(BeaconStatus(status.Beacon.Status))

output := new(strings.Builder)
fmt.Fprintf(output, "* Dkg \n")
fmt.Fprintf(output, " - Status: %s \n", dkgStatus)
fmt.Fprintf(output, "* Reshare \n")
fmt.Fprintf(output, " - Status: %s \n", reshareStatus)
fmt.Fprintf(output, "* ChainStore \n")
fmt.Fprintf(output, " - IsEmpty: %t \n", status.ChainStore.IsEmpty)
fmt.Fprintf(output, " - Length: %d \n", status.ChainStore.Length)
fmt.Fprintf(output, " - LastRound: %d \n", status.ChainStore.LastRound)
fmt.Fprintf(output, "* Beacons \n")
fmt.Fprintf(output, " - Status: %s \n", beaconStatus)
fmt.Fprintf(output, " - Stopped: %t \n", status.Beacon.IsStopped)
fmt.Fprintf(output, " - Started: %t \n", status.Beacon.IsStarted)
fmt.Fprintf(output, " - Serving: %t \n", status.Beacon.IsServing)
fmt.Fprintf(output, " - Running: %t \n", status.Beacon.IsRunning)

return output.String()
}

0 comments on commit 5a9f689

Please sign in to comment.