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)
1211
@@ -26,8 +25,8 @@ type ServiceInfo struct {
2625 Status uint32 `json:"Status"`
2726}
2827
29- type stateCheckFunc func () (bool , string , ServiceInterface , error )
30- type stateTransitionFunc func (ServiceInterface ) error
28+ type stateCheckFunc func () (bool , string , cim. ServiceInterface , error )
29+ type stateTransitionFunc func (cim. ServiceInterface ) error
3130
3231const (
3332 // startServiceErrorCodeAccepted indicates the request is accepted
@@ -82,23 +81,13 @@ func serviceState(status string) uint32 {
8281 return stateMappings [status ]
8382}
8483
85- type ServiceInterface interface {
86- GetPropertyName () (string , error )
87- GetPropertyDisplayName () (string , error )
88- GetPropertyState () (string , error )
89- GetPropertyStartMode () (string , error )
90- GetDependents () ([]ServiceInterface , error )
91- StartService () (result uint32 , err error )
92- StopService () (result uint32 , err error )
93- }
94-
9584type ServiceManager interface {
9685 WaitUntilServiceState (stateTransition stateTransitionFunc , stateCheck stateCheckFunc , interval time.Duration , timeout time.Duration ) (string , error )
9786 GetDependentsForService (name string ) ([]string , error )
9887}
9988
10089type ServiceFactory interface {
101- GetService (name string ) (ServiceInterface , error )
90+ GetService (name string ) (cim. ServiceInterface , error )
10291}
10392
10493type APIImplementor struct {
@@ -107,7 +96,7 @@ type APIImplementor struct {
10796}
10897
10998func New () APIImplementor {
110- serviceFactory := Win32ServiceFactory {}
99+ serviceFactory := cim. Win32ServiceFactory {}
111100 return APIImplementor {
112101 serviceFactory : serviceFactory ,
113102 serviceManager : ServiceManagerImpl {
@@ -117,36 +106,36 @@ func New() APIImplementor {
117106}
118107
119108func (APIImplementor ) GetBIOSSerialNumber () (string , error ) {
120- bios , err := cim .QueryBIOSElement ([] string { "SerialNumber" } )
109+ bios , err := cim .QueryBIOSElement (cim . BIOSSelectorList )
121110 if err != nil {
122111 return "" , fmt .Errorf ("failed to get BIOS element: %w" , err )
123112 }
124113
125- sn , err := bios . GetPropertySerialNumber ( )
114+ sn , err := cim . GetBIOSSerialNumber ( bios )
126115 if err != nil {
127116 return "" , fmt .Errorf ("failed to get BIOS serial number property: %w" , err )
128117 }
129118
130119 return sn , nil
131120}
132121
133- func (APIImplementor ) GetService (name string ) (* ServiceInfo , error ) {
134- service , err := cim . QueryServiceByName (name , [] string { "DisplayName" , "State" , "StartMode" } )
122+ func (impl APIImplementor ) GetService (name string ) (* ServiceInfo , error ) {
123+ service , err := impl . serviceFactory . GetService (name )
135124 if err != nil {
136125 return nil , fmt .Errorf ("failed to get service %s: %w" , name , err )
137126 }
138127
139- displayName , err := service . GetPropertyDisplayName ( )
128+ displayName , err := cim . GetServiceDisplayName ( service )
140129 if err != nil {
141130 return nil , fmt .Errorf ("failed to get displayName property of service %s: %w" , name , err )
142131 }
143132
144- state , err := service . GetPropertyState ( )
133+ state , err := cim . GetServiceState ( service )
145134 if err != nil {
146135 return nil , fmt .Errorf ("failed to get state property of service %s: %w" , name , err )
147136 }
148137
149- startMode , err := service . GetPropertyStartMode ( )
138+ startMode , err := cim . GetServiceStartMode ( service )
150139 if err != nil {
151140 return nil , fmt .Errorf ("failed to get startMode property of service %s: %w" , name , err )
152141 }
@@ -159,20 +148,20 @@ func (APIImplementor) GetService(name string) (*ServiceInfo, error) {
159148}
160149
161150func (impl APIImplementor ) StartService (name string ) error {
162- startService := func (service ServiceInterface ) error {
151+ startService := func (service cim. ServiceInterface ) error {
163152 retVal , err := service .StartService ()
164153 if err != nil || (retVal != startServiceErrorCodeAccepted && retVal != startServiceErrorCodeAlreadyRunning ) {
165154 return fmt .Errorf ("error starting service name %s. return value: %d, error: %v" , name , retVal , err )
166155 }
167156 return nil
168157 }
169- serviceRunningCheck := func () (bool , string , ServiceInterface , error ) {
158+ serviceRunningCheck := func () (bool , string , cim. ServiceInterface , error ) {
170159 service , err := impl .serviceFactory .GetService (name )
171160 if err != nil {
172161 return false , "" , nil , err
173162 }
174163
175- state , err := service . GetPropertyState ( )
164+ state , err := cim . GetServiceState ( service )
176165 if err != nil {
177166 return false , state , service , err
178167 }
@@ -194,7 +183,7 @@ func (impl APIImplementor) StartService(name string) error {
194183
195184func (impl APIImplementor ) stopSingleService (name string ) (bool , error ) {
196185 var dependentRunning bool
197- stopService := func (service ServiceInterface ) error {
186+ stopService := func (service cim. ServiceInterface ) error {
198187 retVal , err := service .StopService ()
199188 if err != nil || (retVal != stopServiceErrorCodeAccepted && retVal != stopServiceErrorCodeStopPending ) {
200189 if retVal == stopServiceErrorCodeDependentRunning {
@@ -205,13 +194,13 @@ func (impl APIImplementor) stopSingleService(name string) (bool, error) {
205194 }
206195 return nil
207196 }
208- serviceStoppedCheck := func () (bool , string , ServiceInterface , error ) {
197+ serviceStoppedCheck := func () (bool , string , cim. ServiceInterface , error ) {
209198 service , err := impl .serviceFactory .GetService (name )
210199 if err != nil {
211200 return false , "" , nil , err
212201 }
213202
214- state , err := service . GetPropertyState ( )
203+ state , err := cim . GetServiceState ( service )
215204 if err != nil {
216205 return false , state , service , err
217206 }
@@ -252,42 +241,6 @@ func (impl APIImplementor) StopService(name string, force bool) error {
252241 return nil
253242}
254243
255- type Win32Service struct {
256- * cimv2.Win32_Service
257- }
258-
259- func (s * Win32Service ) GetDependents () ([]ServiceInterface , error ) {
260- collection , err := s .GetAssociated ("Win32_DependentService" , "Win32_Service" , "Dependent" , "Antecedent" )
261- if err != nil {
262- return nil , err
263- }
264-
265- var result []ServiceInterface
266- for _ , coll := range collection {
267- service , err := cimv2 .NewWin32_ServiceEx1 (coll )
268- if err != nil {
269- return nil , err
270- }
271-
272- result = append (result , & Win32Service {
273- service ,
274- })
275- }
276- return result , nil
277- }
278-
279- type Win32ServiceFactory struct {
280- }
281-
282- func (impl Win32ServiceFactory ) GetService (name string ) (ServiceInterface , error ) {
283- service , err := cim .QueryServiceByName (name , nil )
284- if err != nil {
285- return nil , err
286- }
287-
288- return & Win32Service {Win32_Service : service }, nil
289- }
290-
291244type ServiceManagerImpl struct {
292245 serviceFactory ServiceFactory
293246}
@@ -330,7 +283,7 @@ func (impl ServiceManagerImpl) WaitUntilServiceState(stateTransition stateTransi
330283
331284func (impl ServiceManagerImpl ) GetDependentsForService (name string ) ([]string , error ) {
332285 var serviceNames []string
333- var servicesToCheck []ServiceInterface
286+ var servicesToCheck []cim. ServiceInterface
334287 servicesByName := map [string ]string {}
335288
336289 service , err := impl .serviceFactory .GetService (name )
@@ -344,12 +297,12 @@ func (impl ServiceManagerImpl) GetDependentsForService(name string) ([]string, e
344297 service = servicesToCheck [i ]
345298 i += 1
346299
347- serviceName , err := service . GetPropertyName ( )
300+ serviceName , err := cim . GetServiceName ( service )
348301 if err != nil {
349302 return serviceNames , err
350303 }
351304
352- currentState , err := service . GetPropertyState ( )
305+ currentState , err := cim . GetServiceState ( service )
353306 if err != nil {
354307 return serviceNames , err
355308 }
0 commit comments