6
6
7
7
"github.com/kubernetes-csi/csi-proxy/pkg/cim"
8
8
"github.com/kubernetes-csi/csi-proxy/pkg/server/system/impl"
9
- "github.com/microsoft/wmi/server2019/root/cimv2"
10
9
"github.com/pkg/errors"
11
10
"k8s.io/klog/v2"
12
11
)
@@ -27,8 +26,8 @@ type ServiceInfo struct {
27
26
Status uint32 `json:"Status"`
28
27
}
29
28
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
32
31
33
32
const (
34
33
// startServiceErrorCodeAccepted indicates the request is accepted
@@ -83,24 +82,13 @@ func serviceState(status string) uint32 {
83
82
return stateMappings [status ]
84
83
}
85
84
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
-
97
85
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 )
99
87
GetDependentsForService (string ) ([]string , error )
100
88
}
101
89
102
90
type ServiceFactory interface {
103
- GetService (name string ) (ServiceInterface , error )
91
+ GetService (name string ) (cim. ServiceInterface , error )
104
92
}
105
93
106
94
type APIImplementor struct {
@@ -109,7 +97,7 @@ type APIImplementor struct {
109
97
}
110
98
111
99
func New () APIImplementor {
112
- serviceFactory := Win32ServiceFactory {}
100
+ serviceFactory := cim. Win32ServiceFactory {}
113
101
return APIImplementor {
114
102
serviceFactory : serviceFactory ,
115
103
serviceManager : ServiceManagerImpl {
@@ -119,36 +107,36 @@ func New() APIImplementor {
119
107
}
120
108
121
109
func (APIImplementor ) GetBIOSSerialNumber () (string , error ) {
122
- bios , err := cim .QueryBIOSElement ([] string { "SerialNumber" } )
110
+ bios , err := cim .QueryBIOSElement (cim . BIOSSelectorList )
123
111
if err != nil {
124
112
return "" , fmt .Errorf ("failed to get BIOS element: %w" , err )
125
113
}
126
114
127
- sn , err := bios . GetPropertySerialNumber ( )
115
+ sn , err := cim . GetBIOSSerialNumber ( bios )
128
116
if err != nil {
129
117
return "" , fmt .Errorf ("failed to get BIOS serial number property: %w" , err )
130
118
}
131
119
132
120
return sn , nil
133
121
}
134
122
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 )
137
125
if err != nil {
138
126
return nil , fmt .Errorf ("failed to get service %s: %w" , name , err )
139
127
}
140
128
141
- displayName , err := service . GetPropertyDisplayName ( )
129
+ displayName , err := cim . GetServiceDisplayName ( service )
142
130
if err != nil {
143
131
return nil , fmt .Errorf ("failed to get displayName property of service %s: %w" , name , err )
144
132
}
145
133
146
- state , err := service . GetPropertyState ( )
134
+ state , err := cim . GetServiceState ( service )
147
135
if err != nil {
148
136
return nil , fmt .Errorf ("failed to get state property of service %s: %w" , name , err )
149
137
}
150
138
151
- startMode , err := service . GetPropertyStartMode ( )
139
+ startMode , err := cim . GetServiceStartMode ( service )
152
140
if err != nil {
153
141
return nil , fmt .Errorf ("failed to get startMode property of service %s: %w" , name , err )
154
142
}
@@ -161,14 +149,14 @@ func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
161
149
}
162
150
163
151
func (impl APIImplementor ) StartService (name string ) error {
164
- startService := func (service ServiceInterface ) error {
152
+ startService := func (service cim. ServiceInterface ) error {
165
153
retVal , err := service .StartService ()
166
154
if err != nil || (retVal != startServiceErrorCodeAccepted && retVal != startServiceErrorCodeAlreadyRunning ) {
167
155
return fmt .Errorf ("error starting service name %s. return value: %d, error: %v" , name , retVal , err )
168
156
}
169
157
return nil
170
158
}
171
- serviceRunningCheck := func (service ServiceInterface , state string ) (bool , string , error ) {
159
+ serviceRunningCheck := func (service cim. ServiceInterface , state string ) (bool , string , error ) {
172
160
err := service .Refresh ()
173
161
if err != nil {
174
162
return false , "" , err
@@ -202,7 +190,7 @@ func (impl APIImplementor) StartService(name string) error {
202
190
203
191
func (impl APIImplementor ) stopSingleService (name string ) (bool , error ) {
204
192
var dependentRunning bool
205
- stopService := func (service ServiceInterface ) error {
193
+ stopService := func (service cim. ServiceInterface ) error {
206
194
retVal , err := service .StopService ()
207
195
if err != nil || (retVal != stopServiceErrorCodeAccepted && retVal != stopServiceErrorCodeStopPending ) {
208
196
if retVal == stopServiceErrorCodeDependentRunning {
@@ -213,7 +201,7 @@ func (impl APIImplementor) stopSingleService(name string) (bool, error) {
213
201
}
214
202
return nil
215
203
}
216
- serviceStoppedCheck := func (service ServiceInterface , state string ) (bool , string , error ) {
204
+ serviceStoppedCheck := func (service cim. ServiceInterface , state string ) (bool , string , error ) {
217
205
err := service .Refresh ()
218
206
if err != nil {
219
207
return false , "" , err
@@ -266,47 +254,11 @@ func (impl APIImplementor) StopService(name string, force bool) error {
266
254
return nil
267
255
}
268
256
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
-
305
257
type ServiceManagerImpl struct {
306
258
serviceFactory ServiceFactory
307
259
}
308
260
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 ) {
310
262
done , state , err := stateCheck (service , "" )
311
263
if err != nil {
312
264
return state , err
@@ -346,7 +298,7 @@ func (impl ServiceManagerImpl) WaitUntilServiceState(service ServiceInterface, s
346
298
347
299
func (impl ServiceManagerImpl ) GetDependentsForService (name string ) ([]string , error ) {
348
300
var serviceNames []string
349
- var servicesToCheck []ServiceInterface
301
+ var servicesToCheck []cim. ServiceInterface
350
302
servicesByName := map [string ]string {}
351
303
352
304
service , err := impl .serviceFactory .GetService (name )
@@ -360,12 +312,12 @@ func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, e
360
312
service = servicesToCheck [i ]
361
313
i += 1
362
314
363
- serviceName , err := service . GetPropertyName ( )
315
+ serviceName , err := cim . GetServiceName ( service )
364
316
if err != nil {
365
317
return serviceNames , err
366
318
}
367
319
368
- currentState , err := service . GetPropertyState ( )
320
+ currentState , err := cim . GetServiceState ( service )
369
321
if err != nil {
370
322
return serviceNames , err
371
323
}
0 commit comments