#NEWLINE# def stop_instance( self , instance ) : #NEWLINE# #INDENT# nova = self . nova_client #NEWLINE# logger . debug ( 'About to stop instance %s' , backend_instance_state . uuid ) #NEWLINE# try : #NEWLINE# #INDENT# backend_instance = nova . servers . find ( id = instance . backend_id ) #NEWLINE# backend_instance_state = self . _get_instance_state ( backend_instance ) #NEWLINE# if ( backend_instance_state == models . Instance . States . OFFLINE ) : #NEWLINE# #INDENT# logger . warning ( 'Instance %s is already stopped' , instance . uuid ) #NEWLINE# return #NEWLINE# #UNINDENT# nova . servers . stop ( instance . backend_id ) #NEWLINE# if ( not self . _wait_for_instance_status ( instance . backend_id , nova , 'SHUTOFF' ) ) : #NEWLINE# #INDENT# logger . error ( 'Failed to stop instance %s' , instance . uuid ) #NEWLINE# raise OpenStackBackendError ( ( 'Timed out waiting for instance %s to stop' % instance . uuid ) ) #NEWLINE# #UNINDENT# #UNINDENT# except nova_exceptions . ClientException as e : #NEWLINE# #INDENT# logger . exception ( 'Failed to stop instance %s' , instance . uuid ) #NEWLINE# six . reraise ( OpenStackBackendError , e ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# instance . start_time = None #NEWLINE# instance . save ( update_fields = [ 'start_time' ] ) #NEWLINE# logger . info ( 'Successfully stopped instance %s' , instance . uuid )