Skip to content

Commit

Permalink
Improvements on Status command (#901)
Browse files Browse the repository at this point in the history
* add method to list running beacon ids
* expand status features to support fetching all ids and listing them
  • Loading branch information
emmanuelm41 committed Jan 24, 2022
1 parent 437aa1f commit 1d8f563
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 297 deletions.
9 changes: 7 additions & 2 deletions cmd/drand-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,15 @@ var beaconIDFlag = &cli.StringFlag{
Usage: "Indicates the id for the randomness generation process which will be started",
Value: "",
}
var listIdsFlag = &cli.BoolFlag{
Name: "list-ids",
Usage: "Indicates if it only have to list the running beacon ids instead of the statuses",
Value: false,
}

var allBeaconsFlag = &cli.BoolFlag{
Name: "all",
Usage: "Indicates if we have to reset all beacons chains",
Usage: "Indicates if we have to interact with all beacons chains",
Value: false,
}

Expand Down Expand Up @@ -421,7 +426,7 @@ var appCommands = []*cli.Command{
{
Name: "status",
Usage: "Get the status of many modules of running the daemon\n",
Flags: toArray(controlFlag, jsonFlag, beaconIDFlag),
Flags: toArray(controlFlag, jsonFlag, beaconIDFlag, allBeaconsFlag, listIdsFlag),
Action: statusCmd,
},
{
Expand Down
13 changes: 13 additions & 0 deletions cmd/drand-cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ func getSBFolderStructure() string {
return tmp
}

func TestDrandListSchemes(t *testing.T) {
n := 5
instances, tempPath := launchDrandInstances(t, n)
defer os.RemoveAll(tempPath)

for _, instance := range instances {
remote := []string{"drand", "util", "list-schemes", "--control", instance.ctrlPort}

err := CLI().Run(remote)
require.NoError(t, err)
}
}

func TestDrandReloadBeacon(t *testing.T) {
sch := scheme.GetSchemeFromEnv()
beaconID := common.GetBeaconIDFromEnv()
Expand Down
61 changes: 53 additions & 8 deletions cmd/drand-cli/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type shareArgs struct {
conf *core.Config
}

type beaconIDsStatuses struct {
Beacons map[string]*control.StatusResponse `json:"beacons"`
}

func (s *shareArgs) loadSecret(c *cli.Context) error {
secret := os.Getenv("DRAND_SHARE_SECRET")
if c.IsSet(secretFlag.Name) {
Expand Down Expand Up @@ -413,27 +417,68 @@ func pingpongCmd(c *cli.Context) error {
return nil
}

//nolint:gocyclo
func statusCmd(c *cli.Context) error {
client, err := controlClient(c)
if err != nil {
return err
}

beaconID := getBeaconID(c)
resp, err := client.Status(beaconID)
if err != nil {
return fmt.Errorf("drand: can't get the status of the daemon ... %s", err)
listIds := c.IsSet(listIdsFlag.Name)
allIds := c.IsSet(allBeaconsFlag.Name)
beaconID := c.String(beaconIDFlag.Name)

if beaconID != "" && (allIds || listIds) {
return fmt.Errorf("drand: can't use --%s with --%s or --%s flags at the same time",
beaconIDFlag.Name, allBeaconsFlag.Name, listIdsFlag.Name)
}

beaconIDsList := &control.ListBeaconIDsResponse{}
if allIds || listIds {
beaconIDsList, err = client.ListBeaconIDs()
if err != nil {
return fmt.Errorf("drand: can't get the list of running beacon ids on the daemon ... %s", err)
}
} else {
beaconIDsList.Ids = append(beaconIDsList.Ids, getBeaconID(c))
}

if listIds {
if c.IsSet(jsonFlag.Name) {
str, err := json.Marshal(beaconIDsList)
if err != nil {
return fmt.Errorf("cannot marshal the response ... %s", err)
}
fmt.Fprintf(output, "%s \n", string(str))
return nil
}

fmt.Fprintf(output, "running beacon ids on the node: [%s]\n", strings.Join(beaconIDsList.Ids, ", "))
return nil
}

statuses := beaconIDsStatuses{Beacons: make(map[string]*control.StatusResponse)}
for _, id := range beaconIDsList.Ids {
resp, err := client.Status(id)
if err != nil {
return fmt.Errorf("drand: can't get the status of the network with id [%s]... %s", id, err)
}

if c.IsSet(jsonFlag.Name) {
statuses.Beacons[id] = resp
continue
}

fmt.Fprintf(output, "the status of network with id [%s] is: \n", id)
fmt.Fprintf(output, "%s \n", core.StatusResponseToString(resp))
}

if c.IsSet(jsonFlag.Name) {
str, err := json.Marshal(resp)
str, err := json.Marshal(statuses)
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
Expand Down
4 changes: 4 additions & 0 deletions core/drand_beacon_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,10 @@ func (bp *BeaconProcess) ListSchemes(c context.Context, in *drand.ListSchemesReq
return &drand.ListSchemesResponse{Ids: scheme.ListSchemes(), Metadata: metadata}, nil
}

func (bp *BeaconProcess) ListBeaconIDs(c context.Context, in *drand.ListSchemesRequest) (*drand.ListSchemesResponse, error) {
return nil, fmt.Errorf("method not implemented")
}

func extractGroup(i *drand.GroupInfo) (*key.Group, error) {
var g = new(key.Group)
switch x := i.Location.(type) {
Expand Down
14 changes: 14 additions & 0 deletions core/drand_daemon_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,20 @@ func (dd *DrandDaemon) StartFollowChain(in *drand.StartFollowRequest, stream dra
return bp.StartFollowChain(in, stream)
}

func (dd *DrandDaemon) ListBeaconIDs(ctx context.Context, in *drand.ListBeaconIDsRequest) (*drand.ListBeaconIDsResponse, error) {
metadata := common.NewMetadata(dd.version.ToProto())

dd.state.Lock()
defer dd.state.Unlock()

ids := make([]string, 0)
for id := range dd.beaconProcesses {
ids = append(ids, id)
}

return &drand.ListBeaconIDsResponse{Ids: ids, Metadata: metadata}, nil
}

// /////////

// Stop simply stops all drand operations.
Expand Down
10 changes: 10 additions & 0 deletions net/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ func (c *ControlClient) ReloadBeacon(beaconID string) (*control.ReloadBeaconResp
return resp, err
}

// ListBeaconIDs
func (c *ControlClient) ListBeaconIDs() (*control.ListBeaconIDsResponse, error) {
metadata := protoCommon.Metadata{
NodeVersion: c.version.ToProto(),
}

resp, err := c.client.ListBeaconIDs(ctx.Background(), &control.ListBeaconIDsRequest{Metadata: &metadata})
return resp, err
}

// Status gets the current daemon status
func (c *ControlClient) Status(beaconID string) (*control.StatusResponse, error) {
metadata := protoCommon.Metadata{
Expand Down
Loading

0 comments on commit 1d8f563

Please sign in to comment.