<s> from __future__ import unicode_literals <EOL> import logging <EOL> from celery import shared_task <EOL> from django . contrib . auth import get_user_model <EOL> from django . db import transaction <EOL> from nodeconductor . core import utils as core_utils <EOL> from nodeconductor . core . tasks import retry_if_false , throttle , StateTransitionTask , ErrorMessageTask , Task <EOL> from nodeconductor . core . models import SshPublicKey <EOL> from nodeconductor . iaas . backend import CloudBackendError <EOL> from nodeconductor . structure import ( SupportedServices , ServiceBackendError , <EOL> ServiceBackendNotImplemented , models ) <EOL> from nodeconductor . structure . utils import deserialize_ssh_key , deserialize_user , GeoIpException <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def push_ssh_public_keys ( service_project_links ) : <EOL> link_objects = models . ServiceProjectLink . from_string ( service_project_links ) <EOL> for link in link_objects : <EOL> str_link = link . to_string ( ) <EOL> ssh_keys = SshPublicKey . objects . filter ( user__groups__projectrole__project = link . project ) <EOL> if not ssh_keys . exists ( ) : <EOL> logger . debug ( '<STR_LIT>' , str_link ) <EOL> continue <EOL> for key in ssh_keys : <EOL> push_ssh_public_key . delay ( key . uuid . hex , str_link ) <EOL> @ shared_task ( name = '<STR_LIT>' , max_retries = <NUM_LIT> , default_retry_delay = <NUM_LIT:30> ) <EOL> @ retry_if_false <EOL> def push_ssh_public_key ( ssh_public_key_uuid , service_project_link_str ) : <EOL> try : <EOL> public_key = SshPublicKey . objects . get ( uuid = ssh_public_key_uuid ) <EOL> except SshPublicKey . DoesNotExist : <EOL> logger . warning ( '<STR_LIT>' , ssh_public_key_uuid ) <EOL> return True <EOL> try : <EOL> service_project_link = next ( models . ServiceProjectLink . from_string ( service_project_link_str ) ) <EOL> except StopIteration : <EOL> logger . warning ( '<STR_LIT>' , service_project_link_str ) <EOL> return True <EOL> backend = service_project_link . get_backend ( ) <EOL> try : <EOL> backend . add_ssh_key ( public_key , service_project_link ) <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> public_key . uuid , service_project_link_str ) <EOL> except ServiceBackendNotImplemented : <EOL> pass <EOL> except ( ServiceBackendError , CloudBackendError ) : <EOL> logger . warning ( <EOL> '<STR_LIT>' , <EOL> public_key . uuid , service_project_link_str , <EOL> exc_info = <NUM_LIT:1> ) <EOL> return True <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def remove_ssh_public_key ( key_data , service_project_link_str ) : <EOL> public_key = deserialize_ssh_key ( key_data ) <EOL> try : <EOL> service_project_link = next ( models . ServiceProjectLink . from_string ( service_project_link_str ) ) <EOL> except StopIteration : <EOL> logger . warning ( '<STR_LIT>' , service_project_link_str ) <EOL> return True <EOL> try : <EOL> backend = service_project_link . get_backend ( ) <EOL> backend . remove_ssh_key ( public_key , service_project_link ) <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> public_key . uuid , service_project_link_str ) <EOL> except ServiceBackendNotImplemented : <EOL> pass <EOL> except ( ServiceBackendError , CloudBackendError ) : <EOL> logger . warning ( <EOL> '<STR_LIT>' , <EOL> public_key . uuid , service_project_link_str , <EOL> exc_info = <NUM_LIT:1> ) <EOL> @ shared_task ( name = '<STR_LIT>' , max_retries = <NUM_LIT> , default_retry_delay = <NUM_LIT:30> ) <EOL> @ retry_if_false <EOL> def add_user ( user_uuid , service_project_link_str ) : <EOL> try : <EOL> user = get_user_model ( ) . objects . get ( uuid = user_uuid ) <EOL> except get_user_model ( ) . DoesNotExist : <EOL> logger . warning ( '<STR_LIT>' , user_uuid ) <EOL> return True <EOL> try : <EOL> service_project_link = next ( models . ServiceProjectLink . from_string ( service_project_link_str ) ) <EOL> except StopIteration : <EOL> logger . warning ( '<STR_LIT>' , service_project_link_str ) <EOL> return True <EOL> backend = service_project_link . get_backend ( ) <EOL> try : <EOL> backend . add_user ( user , service_project_link ) <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> user . uuid , service_project_link_str ) <EOL> except ServiceBackendNotImplemented : <EOL> pass <EOL> except ( ServiceBackendError , CloudBackendError ) : <EOL> logger . warning ( <EOL> '<STR_LIT>' , <EOL> user . uuid , service_project_link_str , <EOL> exc_info = <NUM_LIT:1> ) <EOL> return True <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def remove_user ( user_data , service_project_link_str ) : <EOL> user = deserialize_user ( user_data ) <EOL> try : <EOL> service_project_link = next ( models . ServiceProjectLink . from_string ( service_project_link_str ) ) <EOL> except StopIteration : <EOL> logger . warning ( '<STR_LIT>' , service_project_link_str ) <EOL> return True <EOL> try : <EOL> backend = service_project_link . get_backend ( ) <EOL> backend . remove_user ( user , service_project_link ) <EOL> logger . info ( <EOL> '<STR_LIT>' , <EOL> user . uuid , service_project_link_str ) <EOL> except ServiceBackendNotImplemented : <EOL> pass <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def detect_vm_coordinates_batch ( virtual_machines ) : <EOL> for vm in models . Resource . from_string ( virtual_machines ) : <EOL> detect_vm_coordinates . delay ( vm . to_string ( ) ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def detect_vm_coordinates ( vm_str ) : <EOL> try : <EOL> vm = next ( models . Resource . from_string ( vm_str ) ) <EOL> except StopIteration : <EOL> logger . warning ( '<STR_LIT>' , vm_str ) <EOL> return <EOL> try : <EOL> coordinates = vm . detect_coordinates ( ) <EOL> except GeoIpException as e : <EOL> logger . warning ( '<STR_LIT>' , vm_str , e ) <EOL> return <EOL> if coordinates : <EOL> vm . latitude = coordinates . latitude <EOL> vm . longitude = coordinates . longitude <EOL> vm . save ( update_fields = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class ConnectSharedSettingsTask ( Task ) : <EOL> def execute ( self , service_settings ) : <EOL> logger . debug ( '<STR_LIT>' % service_settings . name ) <EOL> if not service_settings . shared : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> service_model = SupportedServices . get_service_models ( ) [ service_settings . type ] [ '<STR_LIT>' ] <EOL> with transaction . atomic ( ) : <EOL> for customer in models . Customer . objects . all ( ) : <EOL> defaults = { '<STR_LIT:name>' : service_settings . name , '<STR_LIT>' : True } <EOL> service , _ = service_model . objects . get_or_create ( <EOL> customer = customer , settings = service_settings , defaults = defaults ) <EOL> service_project_link_model = service . projects . through <EOL> for project in service . customer . projects . all ( ) : <EOL> service_project_link_model . objects . get_or_create ( project = project , service = service ) <EOL> logger . info ( '<STR_LIT>' % service_settings . name ) <EOL> @ shared_task ( name = '<STR_LIT>' ) <EOL> def pull_service_settings ( ) : <EOL> for service_settings in models . ServiceSettings . objects . filter ( state = models . ServiceSettings . States . OK ) : <EOL> serialized = core_utils . serialize_instance ( service_settings ) <EOL> sync_service_settings . delay ( serialized ) <EOL> for service_settings in models . ServiceSettings . objects . filter ( state = models . ServiceSettings . States . ERRED ) : <EOL> serialized = core_utils . serialize_instance ( service_settings ) <EOL> sync_service_settings . apply_async ( <EOL> args = ( serialized , ) , <EOL> link = StateTransitionTask ( ) . si ( serialized , state_transition = '<STR_LIT>' ) , <EOL> link_error = ErrorMessageTask ( ) . s ( serialized ) , <EOL> ) <EOL> @ shared_task <EOL> @ throttle ( concurrency = <NUM_LIT:2> , key = '<STR_LIT>' ) <EOL> def sync_service_settings ( serialized_service_settings ) : <EOL> service_settings = core_utils . deserialize_instance ( serialized_service_settings ) <EOL> backend = service_settings . get_backend ( ) <EOL> backend . sync ( ) </s>
