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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements on Status command #901

Merged
merged 5 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
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
46 changes: 36 additions & 10 deletions cmd/drand-cli/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,21 +419,47 @@ func statusCmd(c *cli.Context) error {
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)
}

if c.IsSet(jsonFlag.Name) {
str, err := json.Marshal(resp)
ids := make([]string, 0)
if allIds || listIds {
resp, err := client.ListBeaconIDs()
if err != nil {
return fmt.Errorf("cannot marshal the response ... %s", err)
return fmt.Errorf("drand: can't get the list of running beacon ids on the daemon ... %s", err)
}
fmt.Fprintf(output, "%s \n", string(str))
ids = resp.Ids
} 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))
ids = append(ids, getBeaconID(c))
}

if listIds {
fmt.Fprintf(output, "running beacon ids on the node: [%s]\n", strings.Join(ids, ", "))
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

for _, id := range 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) {
str, err := json.Marshal(resp)
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("cannot marshal the response ... %s", err)
}
fmt.Fprintf(output, "%s \n", string(str))
} else {
fmt.Fprintf(output, "the status of network with id [%s] is: \n", id)
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