<s> import logging <EOL> from celery import shared_task <EOL> from nodeconductor . core . tasks import save_error_message , transition , throttle <EOL> from nodeconductor . openstack . models import Instance , FloatingIP , Tenant <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def provision ( instance_uuid , ** kwargs ) : <EOL> instance = Instance . objects . get ( uuid = instance_uuid ) <EOL> spl = instance . service_project_link <EOL> tenant = spl . tenant <EOL> if tenant is None : <EOL> from nodeconductor . openstack import executors <EOL> tenant = spl . create_tenant ( ) <EOL> executors . TenantCreateExecutor . execute ( tenant , async = False ) <EOL> tenant . refresh_from_db ( ) <EOL> if tenant . state != Tenant . States . OK : <EOL> instance . set_erred ( ) <EOL> instance . error_message = '<STR_LIT>' % ( tenant , tenant . pk ) <EOL> instance . save ( ) <EOL> return <EOL> provision_instance . apply_async ( <EOL> args = ( instance_uuid , ) , <EOL> kwargs = kwargs , <EOL> link = set_online . si ( instance_uuid ) , <EOL> link_error = set_erred . si ( instance_uuid ) <EOL> ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def destroy ( instance_uuid , force = False ) : <EOL> if force : <EOL> instance = Instance . objects . get ( uuid = instance_uuid ) <EOL> FloatingIP . objects . filter ( <EOL> service_project_link = instance . service_project_link , <EOL> address = instance . external_ips , <EOL> ) . update ( status = '<STR_LIT>' ) <EOL> instance . delete ( ) <EOL> backend = instance . get_backend ( ) <EOL> backend . cleanup_instance ( <EOL> backend_id = instance . backend_id , <EOL> external_ips = instance . external_ips , <EOL> internal_ips = instance . internal_ips , <EOL> system_volume_id = instance . system_volume_id , <EOL> data_volume_id = instance . data_volume_id ) <EOL> return <EOL> destroy_instance . apply_async ( <EOL> args = ( instance_uuid , ) , <EOL> link = delete . si ( instance_uuid ) , <EOL> link_error = set_erred . si ( instance_uuid ) ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def start ( instance_uuid ) : <EOL> start_instance . apply_async ( <EOL> args = ( instance_uuid , ) , <EOL> link = set_online . si ( instance_uuid ) , <EOL> link_error = set_erred . si ( instance_uuid ) ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def stop ( instance_uuid ) : <EOL> stop_instance . apply_async ( <EOL> args = ( instance_uuid , ) , <EOL> link = set_offline . si ( instance_uuid ) , <EOL> link_error = set_erred . si ( instance_uuid ) ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def restart ( instance_uuid ) : <EOL> restart_instance . apply_async ( <EOL> args = ( instance_uuid , ) , <EOL> link = set_online . si ( instance_uuid ) , <EOL> link_error = set_erred . si ( instance_uuid ) ) <EOL> @ shared_task ( is_heavy_task = True ) <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> @ save_error_message <EOL> def provision_instance ( instance_uuid , transition_entity = None , ** kwargs ) : <EOL> instance = transition_entity <EOL> with throttle ( key = instance . service_project_link . service . settings . backend_url ) : <EOL> backend = instance . get_backend ( ) <EOL> backend . provision_instance ( instance , ** kwargs ) <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> @ save_error_message <EOL> def start_instance ( instance_uuid , transition_entity = None ) : <EOL> instance = transition_entity <EOL> backend = instance . get_backend ( ) <EOL> backend . start_instance ( instance ) <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> @ save_error_message <EOL> def stop_instance ( instance_uuid , transition_entity = None ) : <EOL> instance = transition_entity <EOL> backend = instance . get_backend ( ) <EOL> backend . stop_instance ( instance ) <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> @ save_error_message <EOL> def restart_instance ( instance_uuid , transition_entity = None ) : <EOL> instance = transition_entity <EOL> backend = instance . get_backend ( ) <EOL> backend . restart_instance ( instance ) <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> @ save_error_message <EOL> def destroy_instance ( instance_uuid , transition_entity = None ) : <EOL> instance = transition_entity <EOL> backend = instance . get_backend ( ) <EOL> backend . delete_instance ( instance ) <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> def set_online ( instance_uuid , transition_entity = None ) : <EOL> pass <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> def set_offline ( instance_uuid , transition_entity = None ) : <EOL> pass <EOL> @ shared_task <EOL> @ transition ( Instance , '<STR_LIT>' ) <EOL> def set_erred ( instance_uuid , transition_entity = None ) : <EOL> pass <EOL> @ shared_task <EOL> def delete ( instance_uuid ) : <EOL> Instance . objects . get ( uuid = instance_uuid ) . delete ( ) </s>
