Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new cli command for status service #822

Merged
merged 17 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 {
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
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()
}