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

adding new cli cmd #8806

Draft
wants to merge 5 commits into
base: add_status
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions api/external/ingest/request/action.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ message Delete {
message MultipleNodeDeleteRequest {
repeated string node_ids = 1;
}

3,562 changes: 1,853 additions & 1,709 deletions api/interservice/deployment/automate_deployment.pb.go

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions api/interservice/deployment/automate_deployment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ service Deployment {
rpc GetCLIExecutable(GetCLIExecutableRequest) returns (stream GetCLIExecutableResponse);
rpc BootstrapBundle (BootstrapBundleRequest) returns (stream BootstrapBundleResponse);
rpc ControlIndexUpgradeStatus(google.protobuf.Empty) returns (ControlIndexUpgradeStatusResponse);
rpc GetReindexStatus (GetReindexStatusRequest) returns (GetReindexStatusResponse);
}

message GetReindexStatusResponse {
string status_json = 1;
}

message GetReindexStatusRequest {
int32 request_id = 1;
}

message ControlIndexUpgradeStatusResponse {
Expand Down
35 changes: 35 additions & 0 deletions api/interservice/ingest/chef.pb.client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

310 changes: 228 additions & 82 deletions api/interservice/ingest/chef.pb.go

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions api/interservice/ingest/chef.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion api/interservice/ingest/chef.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ message Version {
message VersionRequest {
}

message GetReindexStatusResponse {
string status_json = 1;
}

message GetReindexStatusRequest {
int32 request_id = 1;
}

service ChefIngesterService {
rpc ProcessChefRun (api.ingest.request.Run) returns (api.ingest.response.ProcessChefRunResponse) {
option (google.api.http) = {
Expand Down Expand Up @@ -58,4 +66,8 @@ service ChefIngesterService {
rpc GetVersion (VersionRequest) returns (Version) {
option (google.api.http).get = "/api/v0/ingest/version";
};
}
rpc GetReindexStatus (GetReindexStatusRequest) returns (GetReindexStatusResponse) {
option (google.api.http).get = "/api/v0/ingest/reindex/status";
};

}
39 changes: 39 additions & 0 deletions api/interservice/ingest/chef.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions components/automate-cli/cmd/chef-automate/reindex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"

api "github.com/chef/automate/api/interservice/deployment"
"github.com/chef/automate/components/automate-cli/pkg/status"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/components/automate-deployment/pkg/client"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

// reindexCmd is the parent command
var reindexCmd = &cobra.Command{
Use: "reindex COMMAND",
Short: "Manage OpenSearch reindexing",
}

// reindexStatusCmd retrieves reindexing status
var reindexStatusCmd = &cobra.Command{
Use: "status",
Short: "Retrieve the current reindexing status",
RunE: runReindexStatusCmd,
PersistentPreRunE: preReindexCmd,
}

// ReindexDSClient defines the required RPC methods for deployment-service
type ReindexDSClient interface {
GetReindexStatus(ctx context.Context, in *api.GetReindexStatusRequest, opts ...grpc.CallOption) (*api.GetReindexStatusResponse, error)
Close() error
}

// ReindexFlow manages the CLI flow for reindexing
type ReindexFlow struct {
DsClient ReindexDSClient
Writer *cli.Writer
}

// runReindexStatusCmd connects to deployment-service and fetches reindex status
func runReindexStatusCmd(cmd *cobra.Command, args []string) error {
rf, err := NewReindexFlow(writer)
if err != nil {
return err
}
return rf.GetReindexStatus()
}

// NewReindexFlow establishes a connection to deployment-service
func NewReindexFlow(writer *cli.Writer) (*ReindexFlow, error) {
connection, err := client.Connection(client.DefaultClientTimeout)
if err != nil {
return nil, err
}
return &ReindexFlow{DsClient: connection, Writer: writer}, nil
}

// GetReindexStatus fetches and prints the reindexing status
func (rf *ReindexFlow) GetReindexStatus() error {
defer rf.DsClient.Close()

req := &api.GetReindexStatusRequest{RequestId: 1} // Adjust as needed

resp, err := rf.DsClient.GetReindexStatus(context.Background(), req)
if err != nil {
return status.Wrap(err, status.DeploymentServiceCallError, "Failed to get reindex status")
}

// Parse and print the response
var statusData map[string]interface{}
err = json.Unmarshal([]byte(resp.StatusJson), &statusData)
if err != nil {
return fmt.Errorf("failed to parse JSON response: %w", err)
}

fmt.Println("Reindex Status:")
printReindexStatus(statusData)

return nil
}

// printReindexStatus formats and prints the reindex status
func printReindexStatus(statusData map[string]interface{}) {
overallStatus, _ := statusData["overall_status"].(string)
fmt.Printf("Overall Status: %s\n", overallStatus)

indexes, ok := statusData["indexes"].([]interface{})
if !ok {
fmt.Println("No index details available.")
return
}

fmt.Println("Index Details:")
for _, index := range indexes {
if indexMap, ok := index.(map[string]interface{}); ok {
fmt.Printf("- Index: %s, Stage: %s\n", indexMap["index"], indexMap["stage"])
}
}
}

// preReindexCmd performs pre-run validation before executing the command
func preReindexCmd(cmd *cobra.Command, args []string) error {
err := commandPrePersistent(cmd)
if err != nil {
return status.Wrap(err, status.CommandExecutionError, "unable to set command parent settings")
}

// If running in an A2HA setup, execute on a single node and exit
if isA2HARBFileExist() {
output, err := RunCmdOnSingleAutomateNode(cmd, args)
if err != nil {
return err
}
writer.Print(output)
os.Exit(0) // Prevents further execution in HA mode
}

return nil
}

func init() {
reindexCmd.AddCommand(reindexStatusCmd)
RootCmd.AddCommand(reindexCmd)
}
Loading