-
Notifications
You must be signed in to change notification settings - Fork 18
/
insights.go
55 lines (48 loc) · 1.51 KB
/
insights.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"errors"
"io/fs"
"os"
"os/exec"
)
func registerInsights() error {
cmd := exec.Command("/usr/bin/insights-client", "--register")
return cmd.Run()
}
func unregisterInsights() error {
cmd := exec.Command("/usr/bin/insights-client", "--unregister")
return cmd.Run()
}
// insightsIsRegistered checks whether insights-client reports its
// status as registered or not. If the system is registered, `true` is
// returned, otherwise `false` is returned, and `error` is filled with
// an error value.
func insightsIsRegistered() (bool, error) {
// 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 {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
// If .unregistered exists, insights-client is confident
// it is not registered. We can suppress the error,
// we don't care why it returned non-zero exit code.
_, err := os.Stat("/etc/insights-client/.unregistered")
if err == nil {
return false, nil
}
}
return false, err
}
_, err = os.Stat("/etc/insights-client/.registered")
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}