forked from RedHatInsights/rhc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
108 lines (102 loc) · 3.06 KB
/
status.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"time"
"github.com/briandowns/spinner"
systemd "github.com/coreos/go-systemd/v22/dbus"
)
// rhsmStatus tries to print status provided by RHSM D-Bus API. If we provide
// output in machine-readable format, then we only set files in SystemStatus
// structure and content of this structure will be printed later
func rhsmStatus(systemStatus *SystemStatus) error {
uuid, err := getConsumerUUID()
if err != nil {
return fmt.Errorf("unable to get consumer UUID: %s", err)
}
if uuid == "" {
systemStatus.returnCode += 1
if uiSettings.isMachineReadable {
systemStatus.RHSMConnected = false
} else {
fmt.Printf("%v Not connected to Red Hat Subscription Management\n", uiSettings.iconInfo)
}
} else {
if uiSettings.isMachineReadable {
systemStatus.RHSMConnected = true
} else {
fmt.Printf("%v Connected to Red Hat Subscription Management\n", uiSettings.iconOK)
}
}
return nil
}
// insightStatus tries to print status of insights client
func insightStatus(systemStatus *SystemStatus) {
var s *spinner.Spinner
if uiSettings.isRich {
s = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Suffix = " Checking Red Hat Insights..."
s.Start()
}
isRegistered, err := insightsIsRegistered()
if uiSettings.isRich {
s.Stop()
}
if isRegistered {
if uiSettings.isMachineReadable {
systemStatus.InsightsConnected = true
} else {
fmt.Print(uiSettings.iconOK + " Connected to Red Hat Insights\n")
}
} else {
systemStatus.returnCode += 1
if err == nil {
if uiSettings.isMachineReadable {
systemStatus.InsightsConnected = false
} else {
fmt.Print(uiSettings.iconInfo + " Not connected to Red Hat Insights\n")
}
} else {
if uiSettings.isMachineReadable {
systemStatus.InsightsConnected = false
systemStatus.InsightsError = err.Error()
} else {
fmt.Printf(uiSettings.iconError+" Cannot detect Red Hat Insights status: %v\n", err)
}
}
}
}
// serviceStatus tries to print status of yggdrasil.service or rhcd.service
func serviceStatus(systemStatus *SystemStatus) error {
ctx := context.Background()
conn, err := systemd.NewSystemConnectionContext(ctx)
if err != nil {
systemStatus.YggdrasilRunning = false
systemStatus.YggdrasilError = err.Error()
return fmt.Errorf("unable to connect to systemd: %s", err)
}
defer conn.Close()
unitName := ServiceName + ".service"
properties, err := conn.GetUnitPropertiesContext(ctx, unitName)
if err != nil {
systemStatus.YggdrasilRunning = false
systemStatus.YggdrasilError = err.Error()
return fmt.Errorf("unable to get properties of %s: %s", unitName, err)
}
activeState := properties["ActiveState"]
if activeState.(string) == "active" {
if uiSettings.isMachineReadable {
systemStatus.YggdrasilRunning = true
} else {
fmt.Printf(uiSettings.iconOK+" The %v service is active\n", ServiceName)
}
} else {
systemStatus.returnCode += 1
if uiSettings.isMachineReadable {
systemStatus.YggdrasilRunning = false
} else {
fmt.Printf(uiSettings.iconInfo+" The %v service is inactive\n", ServiceName)
}
}
return nil
}