@@ -69,16 +69,12 @@ def configure(**kwargs):
6969 try :
7070 if ctx .instance .runtime_properties .get ("use_external_resource" ):
7171 # lookup the default domain by name
72+ resource_id = ctx .instance .runtime_properties ["resource_id" ]
7273 try :
73- dom = conn .lookupByName (
74- ctx .instance .runtime_properties ["resource_id" ])
75- except Exception as e :
76- dom = None
77- ctx .logger .info ("Non critical error: {}" .format (str (e )))
78-
79- if dom is None :
74+ dom = conn .lookupByName (resource_id )
75+ except libvirt .libvirtError as e :
8076 raise cfy_exc .NonRecoverableError (
81- 'Failed to find the domain'
77+ 'Failed to find the domain: {}' . format ( repr ( e ))
8278 )
8379
8480 # save settings
@@ -94,14 +90,13 @@ def configure(**kwargs):
9490 if domain_file :
9591 domain_template = ctx .get_resource (domain_file )
9692
97- if not domain_file and not domain_template :
93+ if not ( domain_file or domain_template ) :
9894 resource_dir = resource_filename (__name__ , 'templates' )
9995 domain_file = '{}/domain.xml' .format (resource_dir )
10096 ctx .logger .info ("Will be used internal: %s" % domain_file )
10197
10298 if not domain_template :
103- domain_desc = open (domain_file )
104- with domain_desc :
99+ with open (domain_file ) as domain_desc :
105100 domain_template = domain_desc .read ()
106101
107102 template_engine = Template (domain_template )
@@ -181,8 +176,8 @@ def reboot(**kwargs):
181176 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
182177
183178 if not resource_id :
184- ctx . logger . info ( "No servers for reboot" )
185- return
179+ # not uninstall workflow, raise exception
180+ raise cfy_exc . NonRecoverableError ( "No servers for reboot" )
186181
187182 libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
188183 conn = libvirt .open (libvirt_auth )
@@ -194,13 +189,9 @@ def reboot(**kwargs):
194189 try :
195190 try :
196191 dom = conn .lookupByName (resource_id )
197- except Exception as e :
198- dom = None
199- ctx .logger .info ("Non critical error: {}" .format (str (e )))
200-
201- if dom is None :
192+ except libvirt .libvirtError as e :
202193 raise cfy_exc .NonRecoverableError (
203- 'Failed to find the domain'
194+ 'Failed to find the domain: {}' . format ( repr ( e ))
204195 )
205196
206197 if dom .reboot () < 0 :
@@ -218,8 +209,8 @@ def update(**kwargs):
218209 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
219210
220211 if not resource_id :
221- ctx . logger . info ( "No servers for update" )
222- return
212+ # not uninstall workflow, raise exception
213+ raise cfy_exc . NonRecoverableError ( "No servers for update" )
223214
224215 libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
225216 conn = libvirt .open (libvirt_auth )
@@ -231,13 +222,9 @@ def update(**kwargs):
231222 try :
232223 try :
233224 dom = conn .lookupByName (resource_id )
234- except Exception as e :
235- dom = None
236- ctx .logger .info ("Non critical error: {}" .format (str (e )))
237-
238- if dom is None :
225+ except libvirt .libvirtError as e :
239226 raise cfy_exc .NonRecoverableError (
240- 'Failed to find the domain'
227+ 'Failed to find the domain: {}' . format ( repr ( e ))
241228 )
242229
243230 # change memory values
@@ -284,8 +271,8 @@ def start(**kwargs):
284271 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
285272
286273 if not resource_id :
287- ctx . logger . info ( "No servers for start" )
288- return
274+ # not uninstall workflow, raise exception
275+ raise cfy_exc . NonRecoverableError ( "No servers for start" )
289276
290277 libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
291278 conn = libvirt .open (libvirt_auth )
@@ -300,13 +287,9 @@ def start(**kwargs):
300287 try :
301288 try :
302289 dom = conn .lookupByName (resource_id )
303- except Exception as e :
304- dom = None
305- ctx .logger .info ("Non critical error: {}" .format (str (e )))
306-
307- if dom is None :
290+ except libvirt .libvirtError as e :
308291 raise cfy_exc .NonRecoverableError (
309- 'Failed to find the domain'
292+ 'Failed to find the domain: {}' . format ( repr ( e ))
310293 )
311294
312295 for i in xrange (10 ):
@@ -345,7 +328,8 @@ def stop(**kwargs):
345328 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
346329
347330 if not resource_id :
348- ctx .logger .info ("No servers for delete" )
331+ # not raise exception on 'uninstall' workflow
332+ ctx .logger .info ("No servers for stop" )
349333 return
350334
351335 if ctx .instance .runtime_properties .get ('use_external_resource' ):
@@ -362,13 +346,9 @@ def stop(**kwargs):
362346 try :
363347 try :
364348 dom = conn .lookupByName (resource_id )
365- except Exception as e :
366- dom = None
367- ctx .logger .info ("Non critical error: {}" .format (str (e )))
368-
369- if dom is None :
349+ except libvirt .libvirtError as e :
370350 raise cfy_exc .NonRecoverableError (
371- 'Failed to find the domain'
351+ 'Failed to find the domain: {}' . format ( repr ( e ))
372352 )
373353
374354 # reset ip on stop
@@ -398,8 +378,8 @@ def resume(**kwargs):
398378 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
399379
400380 if not resource_id :
401- ctx . logger . info ( "No servers for resume" )
402- return
381+ # not uninstall workflow, raise exception
382+ raise cfy_exc . NonRecoverableError ( "No servers for resume" )
403383
404384 libvirt_auth , _ = common .get_libvirt_params (** kwargs )
405385 conn = libvirt .open (libvirt_auth )
@@ -411,13 +391,9 @@ def resume(**kwargs):
411391 try :
412392 try :
413393 dom = conn .lookupByName (resource_id )
414- except Exception as e :
415- dom = None
416- ctx .logger .info ("Non critical error: {}" .format (str (e )))
417-
418- if dom is None :
394+ except libvirt .libvirtError as e :
419395 raise cfy_exc .NonRecoverableError (
420- 'Failed to find the domain'
396+ 'Failed to find the domain: {}' . format ( repr ( e ))
421397 )
422398
423399 state , _ = dom .state ()
@@ -444,8 +420,8 @@ def suspend(**kwargs):
444420 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
445421
446422 if not resource_id :
447- ctx . logger . info ( "No servers for suspend" )
448- return
423+ # not uninstall workflow, raise exception
424+ raise cfy_exc . NonRecoverableError ( "No servers for suspend" )
449425
450426 libvirt_auth , _ = common .get_libvirt_params (** kwargs )
451427 conn = libvirt .open (libvirt_auth )
@@ -457,13 +433,9 @@ def suspend(**kwargs):
457433 try :
458434 try :
459435 dom = conn .lookupByName (resource_id )
460- except Exception as e :
461- dom = None
462- ctx .logger .info ("Non critical error: {}" .format (str (e )))
463-
464- if dom is None :
436+ except libvirt .libvirtError as e :
465437 raise cfy_exc .NonRecoverableError (
466- 'Failed to find the domain'
438+ 'Failed to find the domain: {}' . format ( repr ( e ))
467439 )
468440
469441 state , _ = dom .state ()
@@ -540,6 +512,7 @@ def delete(**kwargs):
540512 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
541513
542514 if not resource_id :
515+ # not raise exception on 'uninstall' workflow
543516 ctx .logger .info ("No servers for delete" )
544517 return
545518
@@ -557,13 +530,9 @@ def delete(**kwargs):
557530 try :
558531 try :
559532 dom = conn .lookupByName (resource_id )
560- except Exception as e :
561- dom = None
562- ctx .logger .info ("Non critical error: {}" .format (str (e )))
563-
564- if dom is None :
533+ except libvirt .libvirtError as e :
565534 raise cfy_exc .NonRecoverableError (
566- 'Failed to find the domain'
535+ 'Failed to find the domain: {}' . format ( repr ( e ))
567536 )
568537
569538 _delete_force (dom )
@@ -608,8 +577,8 @@ def snapshot_create(**kwargs):
608577 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
609578
610579 if not resource_id :
611- ctx . logger . info ( "No servers for backup." )
612- return
580+ # not uninstall workflow, raise exception
581+ raise cfy_exc . NonRecoverableError ( "No servers for backup." )
613582
614583 snapshot_name = common .get_backupname (kwargs )
615584
@@ -623,13 +592,9 @@ def snapshot_create(**kwargs):
623592 try :
624593 try :
625594 dom = conn .lookupByName (resource_id )
626- except Exception as e :
627- dom = None
628- ctx .logger .info ("Non critical error: {}" .format (str (e )))
629-
630- if dom is None :
595+ except libvirt .libvirtError as e :
631596 raise cfy_exc .NonRecoverableError (
632- 'Failed to find the domain'
597+ 'Failed to find the domain: {}' . format ( repr ( e ))
633598 )
634599
635600 if kwargs .get ("snapshot_incremental" ):
@@ -646,9 +611,8 @@ def snapshot_create(**kwargs):
646611 ctx .logger .info ("Will be used internal: %s" % backup_file )
647612
648613 if not backup_template :
649- domain_desc = open (backup_file )
650- with domain_desc :
651- backup_template = domain_desc .read ()
614+ with open (backup_file ) as backup_desc :
615+ backup_template = backup_desc .read ()
652616
653617 template_engine = Template (backup_template )
654618 if not template_params :
@@ -712,8 +676,8 @@ def snapshot_delete(**kwargs):
712676 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
713677
714678 if not resource_id :
715- ctx . logger . info ( "No servers for remove_backup." )
716- return
679+ # not uninstall workflow, raise exception
680+ raise cfy_exc . NonRecoverableError ( "No servers for remove_backup." )
717681
718682 snapshot_name = common .get_backupname (kwargs )
719683
@@ -727,13 +691,9 @@ def snapshot_delete(**kwargs):
727691 try :
728692 try :
729693 dom = conn .lookupByName (resource_id )
730- except Exception as e :
731- dom = None
732- ctx .logger .info ("Non critical error: {}" .format (str (e )))
733-
734- if dom is None :
694+ except libvirt .libvirtError as e :
735695 raise cfy_exc .NonRecoverableError (
736- 'Failed to find the domain'
696+ 'Failed to find the domain: {}' . format ( repr ( e ))
737697 )
738698
739699 if kwargs .get ("snapshot_incremental" ):
@@ -799,8 +759,8 @@ def snapshot_apply(**kwargs):
799759 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
800760
801761 if not resource_id :
802- ctx . logger . info ( "No servers for restore." )
803- return
762+ # not uninstall workflow, raise exception
763+ raise cfy_exc . NonRecoverableError ( "No servers for restore." )
804764
805765 snapshot_name = common .get_backupname (kwargs )
806766
@@ -814,13 +774,9 @@ def snapshot_apply(**kwargs):
814774 try :
815775 try :
816776 dom = conn .lookupByName (resource_id )
817- except Exception as e :
818- dom = None
819- ctx .logger .info ("Non critical error: {}" .format (str (e )))
820-
821- if dom is None :
777+ except libvirt .libvirtError as e :
822778 raise cfy_exc .NonRecoverableError (
823- 'Failed to find the domain'
779+ 'Failed to find the domain: {}' . format ( repr ( e ))
824780 )
825781
826782 if kwargs .get ("snapshot_incremental" ):
@@ -855,8 +811,8 @@ def perfomance(**kwargs):
855811 resource_id = ctx .instance .runtime_properties .get ('resource_id' )
856812
857813 if not resource_id :
858- ctx . logger . info ( "No servers for statistics." )
859- return
814+ # not uninstall workflow, raise exception
815+ raise cfy_exc . NonRecoverableError ( "No servers for statistics." )
860816
861817 libvirt_auth , template_params = common .get_libvirt_params (** kwargs )
862818 conn = libvirt .open (libvirt_auth )
@@ -868,13 +824,9 @@ def perfomance(**kwargs):
868824 try :
869825 try :
870826 dom = conn .lookupByName (resource_id )
871- except Exception as e :
872- dom = None
873- ctx .logger .info ("Non critical error: {}" .format (str (e )))
874-
875- if dom is None :
827+ except libvirt .libvirtError as e :
876828 raise cfy_exc .NonRecoverableError (
877- 'Failed to find the domain'
829+ 'Failed to find the domain: {}' . format ( repr ( e ))
878830 )
879831
880832 statistics = ctx .instance .runtime_properties .get ('stat' , {})
0 commit comments