Skip to content

Commit

Permalink
fix: Detect insights-client status in non-legacy mode
Browse files Browse the repository at this point in the history
* Card ID: CCT-525

With `legacy_upload=True` mode, `insights-client --status` returns code
0 if the system is registered and 1 otherwise. In non-legacy mode, 0 is
always returned.

Even though the `/etc/insights-client/.registered` file is not a public
API, it is reliable and we should be able to use it to detect the
registration status.
  • Loading branch information
m-horky authored and jirihnidek committed Sep 19, 2024
1 parent 17654f3 commit 5023890
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
42 changes: 16 additions & 26 deletions insights.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package main

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)

func registerInsights() error {
Expand All @@ -21,29 +18,22 @@ func unregisterInsights() error {
}

func insightsIsRegistered() (bool, error) {
var errBuffer bytes.Buffer
cmd := exec.Command("/usr/bin/insights-client", "--status")
cmd.Stderr = &errBuffer

err := cmd.Run()

// While `insights-client --status` properly checks for registration status by
// asking Inventory, its two modes (legacy v. non-legacy API) behave
// differently (they return different texts with different exit codes) and
// we can't rely on the output or exit codes.
// The `.registered` file is always present on a registered system.
err := exec.Command("/usr/bin/insights-client", "--status").Run()
if err != nil {
// When the error is ExitError, then we know that insights-client only returned
// some error code not equal to zero. We do not care about error number.
var exitError *exec.ExitError
if errors.As(err, &exitError) {
// When stderr is not empty, then we should return this as error
// to be able to print this error in rhc output
stdErr := errBuffer.String()
if len(stdErr) == 0 {
return false, nil
} else {
return false, fmt.Errorf("%s", strings.TrimSpace(stdErr))
}
} else {
return false, err
}
return false, err
}

return cmd.ProcessState.Success(), err
_, err = os.Stat("/etc/insights-client/.registered")
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
2 changes: 1 addition & 1 deletion status.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func insightStatus(systemStatus *SystemStatus) {
systemStatus.InsightsConnected = false
systemStatus.InsightsError = err.Error()
} else {
fmt.Printf(uiSettings.iconError+" Cannot execute insights-client: %v\n", err)
fmt.Printf(uiSettings.iconError+" Cannot detect Red Hat Insights status: %v\n", err)
}
}
}
Expand Down

0 comments on commit 5023890

Please sign in to comment.