Skip to content

Commit 897e125

Browse files
committed
Move WMI service functions to cim package
1 parent e8f5d13 commit 897e125

File tree

3 files changed

+115
-84
lines changed

3 files changed

+115
-84
lines changed

pkg/cim/system.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ import (
1010
"github.com/microsoft/wmi/server2019/root/cimv2"
1111
)
1212

13+
var (
14+
BIOSSelectorList = []string{"SerialNumber"}
15+
ServiceSelectorList = []string{"DisplayName", "State", "StartMode"}
16+
)
17+
18+
type ServiceInterface interface {
19+
GetPropertyName() (string, error)
20+
GetPropertyDisplayName() (string, error)
21+
GetPropertyState() (string, error)
22+
GetPropertyStartMode() (string, error)
23+
GetDependents() ([]ServiceInterface, error)
24+
StartService() (result uint32, err error)
25+
StopService() (result uint32, err error)
26+
Refresh() error
27+
}
28+
1329
// QueryBIOSElement retrieves the BIOS element.
1430
//
1531
// The equivalent WMI query is:
@@ -33,6 +49,11 @@ func QueryBIOSElement(selectorList []string) (*cimv2.CIM_BIOSElement, error) {
3349
return bios, err
3450
}
3551

52+
// GetBIOSSerialNumber returns the BIOS serial number.
53+
func GetBIOSSerialNumber(bios *cimv2.CIM_BIOSElement) (string, error) {
54+
return bios.GetPropertySerialNumber()
55+
}
56+
3657
// QueryServiceByName retrieves a specific service by its name.
3758
//
3859
// The equivalent WMI query is:
@@ -55,3 +76,60 @@ func QueryServiceByName(name string, selectorList []string) (*cimv2.Win32_Servic
5576

5677
return service, err
5778
}
79+
80+
// GetServiceName returns the name of a service.
81+
func GetServiceName(service ServiceInterface) (string, error) {
82+
return service.GetPropertyName()
83+
}
84+
85+
// GetServiceDisplayName returns the display name of a service.
86+
func GetServiceDisplayName(service ServiceInterface) (string, error) {
87+
return service.GetPropertyDisplayName()
88+
}
89+
90+
// GetServiceState returns the state of a service.
91+
func GetServiceState(service ServiceInterface) (string, error) {
92+
return service.GetPropertyState()
93+
}
94+
95+
// GetServiceStartMode returns the start mode of a service.
96+
func GetServiceStartMode(service ServiceInterface) (string, error) {
97+
return service.GetPropertyStartMode()
98+
}
99+
100+
// Win32Service wraps the WMI class Win32_Service (mainly for testing)
101+
type Win32Service struct {
102+
*cimv2.Win32_Service
103+
}
104+
105+
func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
106+
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
107+
if err != nil {
108+
return nil, err
109+
}
110+
111+
var result []ServiceInterface
112+
for _, coll := range collection {
113+
service, err := cimv2.NewWin32_ServiceEx1(coll)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
result = append(result, &Win32Service{
119+
service,
120+
})
121+
}
122+
return result, nil
123+
}
124+
125+
type Win32ServiceFactory struct {
126+
}
127+
128+
func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
129+
service, err := QueryServiceByName(name, ServiceSelectorList)
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
return &Win32Service{Win32_Service: service}, nil
135+
}

pkg/os/system/api.go

Lines changed: 20 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/kubernetes-csi/csi-proxy/pkg/cim"
88
"github.com/kubernetes-csi/csi-proxy/pkg/server/system/impl"
9-
"github.com/microsoft/wmi/server2019/root/cimv2"
109
"github.com/pkg/errors"
1110
"k8s.io/klog/v2"
1211
)
@@ -27,8 +26,8 @@ type ServiceInfo struct {
2726
Status uint32 `json:"Status"`
2827
}
2928

30-
type stateCheckFunc func(ServiceInterface, string) (bool, string, error)
31-
type stateTransitionFunc func(ServiceInterface) error
29+
type stateCheckFunc func(cim.ServiceInterface, string) (bool, string, error)
30+
type stateTransitionFunc func(cim.ServiceInterface) error
3231

3332
const (
3433
// startServiceErrorCodeAccepted indicates the request is accepted
@@ -83,24 +82,13 @@ func serviceState(status string) uint32 {
8382
return stateMappings[status]
8483
}
8584

86-
type ServiceInterface interface {
87-
GetPropertyName() (string, error)
88-
GetPropertyDisplayName() (string, error)
89-
GetPropertyState() (string, error)
90-
GetPropertyStartMode() (string, error)
91-
GetDependents() ([]ServiceInterface, error)
92-
StartService() (result uint32, err error)
93-
StopService() (result uint32, err error)
94-
Refresh() error
95-
}
96-
9785
type ServiceManager interface {
98-
WaitUntilServiceState(ServiceInterface, stateTransitionFunc, stateCheckFunc, time.Duration, time.Duration) (string, error)
86+
WaitUntilServiceState(cim.ServiceInterface, stateTransitionFunc, stateCheckFunc, time.Duration, time.Duration) (string, error)
9987
GetDependentsForService(string) ([]string, error)
10088
}
10189

10290
type ServiceFactory interface {
103-
GetService(name string) (ServiceInterface, error)
91+
GetService(name string) (cim.ServiceInterface, error)
10492
}
10593

10694
type APIImplementor struct {
@@ -109,7 +97,7 @@ type APIImplementor struct {
10997
}
11098

11199
func New() APIImplementor {
112-
serviceFactory := Win32ServiceFactory{}
100+
serviceFactory := cim.Win32ServiceFactory{}
113101
return APIImplementor{
114102
serviceFactory: serviceFactory,
115103
serviceManager: ServiceManagerImpl{
@@ -119,36 +107,36 @@ func New() APIImplementor {
119107
}
120108

121109
func (APIImplementor) GetBIOSSerialNumber() (string, error) {
122-
bios, err := cim.QueryBIOSElement([]string{"SerialNumber"})
110+
bios, err := cim.QueryBIOSElement(cim.BIOSSelectorList)
123111
if err != nil {
124112
return "", fmt.Errorf("failed to get BIOS element: %w", err)
125113
}
126114

127-
sn, err := bios.GetPropertySerialNumber()
115+
sn, err := cim.GetBIOSSerialNumber(bios)
128116
if err != nil {
129117
return "", fmt.Errorf("failed to get BIOS serial number property: %w", err)
130118
}
131119

132120
return sn, nil
133121
}
134122

135-
func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
136-
service, err := cim.QueryServiceByName(name, []string{"DisplayName", "State", "StartMode"})
123+
func (impl APIImplementor) GetService(name string) (*ServiceInfo, error) {
124+
service, err := impl.serviceFactory.GetService(name)
137125
if err != nil {
138126
return nil, fmt.Errorf("failed to get service %s: %w", name, err)
139127
}
140128

141-
displayName, err := service.GetPropertyDisplayName()
129+
displayName, err := cim.GetServiceDisplayName(service)
142130
if err != nil {
143131
return nil, fmt.Errorf("failed to get displayName property of service %s: %w", name, err)
144132
}
145133

146-
state, err := service.GetPropertyState()
134+
state, err := cim.GetServiceState(service)
147135
if err != nil {
148136
return nil, fmt.Errorf("failed to get state property of service %s: %w", name, err)
149137
}
150138

151-
startMode, err := service.GetPropertyStartMode()
139+
startMode, err := cim.GetServiceStartMode(service)
152140
if err != nil {
153141
return nil, fmt.Errorf("failed to get startMode property of service %s: %w", name, err)
154142
}
@@ -161,14 +149,14 @@ func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
161149
}
162150

163151
func (impl APIImplementor) StartService(name string) error {
164-
startService := func(service ServiceInterface) error {
152+
startService := func(service cim.ServiceInterface) error {
165153
retVal, err := service.StartService()
166154
if err != nil || (retVal != startServiceErrorCodeAccepted && retVal != startServiceErrorCodeAlreadyRunning) {
167155
return fmt.Errorf("error starting service name %s. return value: %d, error: %v", name, retVal, err)
168156
}
169157
return nil
170158
}
171-
serviceRunningCheck := func(service ServiceInterface, state string) (bool, string, error) {
159+
serviceRunningCheck := func(service cim.ServiceInterface, state string) (bool, string, error) {
172160
err := service.Refresh()
173161
if err != nil {
174162
return false, "", err
@@ -202,7 +190,7 @@ func (impl APIImplementor) StartService(name string) error {
202190

203191
func (impl APIImplementor) stopSingleService(name string) (bool, error) {
204192
var dependentRunning bool
205-
stopService := func(service ServiceInterface) error {
193+
stopService := func(service cim.ServiceInterface) error {
206194
retVal, err := service.StopService()
207195
if err != nil || (retVal != stopServiceErrorCodeAccepted && retVal != stopServiceErrorCodeStopPending) {
208196
if retVal == stopServiceErrorCodeDependentRunning {
@@ -213,7 +201,7 @@ func (impl APIImplementor) stopSingleService(name string) (bool, error) {
213201
}
214202
return nil
215203
}
216-
serviceStoppedCheck := func(service ServiceInterface, state string) (bool, string, error) {
204+
serviceStoppedCheck := func(service cim.ServiceInterface, state string) (bool, string, error) {
217205
err := service.Refresh()
218206
if err != nil {
219207
return false, "", err
@@ -266,47 +254,11 @@ func (impl APIImplementor) StopService(name string, force bool) error {
266254
return nil
267255
}
268256

269-
type Win32Service struct {
270-
*cimv2.Win32_Service
271-
}
272-
273-
func (s *Win32Service) GetDependents() ([]ServiceInterface, error) {
274-
collection, err := s.GetAssociated("Win32_DependentService", "Win32_Service", "Dependent", "Antecedent")
275-
if err != nil {
276-
return nil, err
277-
}
278-
279-
var result []ServiceInterface
280-
for _, coll := range collection {
281-
service, err := cimv2.NewWin32_ServiceEx1(coll)
282-
if err != nil {
283-
return nil, err
284-
}
285-
286-
result = append(result, &Win32Service{
287-
service,
288-
})
289-
}
290-
return result, nil
291-
}
292-
293-
type Win32ServiceFactory struct {
294-
}
295-
296-
func (impl Win32ServiceFactory) GetService(name string) (ServiceInterface, error) {
297-
service, err := cim.QueryServiceByName(name, nil)
298-
if err != nil {
299-
return nil, err
300-
}
301-
302-
return &Win32Service{Win32_Service: service}, nil
303-
}
304-
305257
type ServiceManagerImpl struct {
306258
serviceFactory ServiceFactory
307259
}
308260

309-
func (impl ServiceManagerImpl) WaitUntilServiceState(service ServiceInterface, stateTransition stateTransitionFunc, stateCheck stateCheckFunc, interval time.Duration, timeout time.Duration) (string, error) {
261+
func (impl ServiceManagerImpl) WaitUntilServiceState(service cim.ServiceInterface, stateTransition stateTransitionFunc, stateCheck stateCheckFunc, interval time.Duration, timeout time.Duration) (string, error) {
310262
done, state, err := stateCheck(service, "")
311263
if err != nil {
312264
return state, err
@@ -346,7 +298,7 @@ func (impl ServiceManagerImpl) WaitUntilServiceState(service ServiceInterface, s
346298

347299
func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, error) {
348300
var serviceNames []string
349-
var servicesToCheck []ServiceInterface
301+
var servicesToCheck []cim.ServiceInterface
350302
servicesByName := map[string]string{}
351303

352304
service, err := impl.serviceFactory.GetService(name)
@@ -360,12 +312,12 @@ func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, e
360312
service = servicesToCheck[i]
361313
i += 1
362314

363-
serviceName, err := service.GetPropertyName()
315+
serviceName, err := cim.GetServiceName(service)
364316
if err != nil {
365317
return serviceNames, err
366318
}
367319

368-
currentState, err := service.GetPropertyState()
320+
currentState, err := cim.GetServiceState(service)
369321
if err != nil {
370322
return serviceNames, err
371323
}

0 commit comments

Comments
 (0)