def listen_for_allocation_threshold_met(sender, instance, created, **kwargs): """ This listener expects: EventType - 'allocation_source_threshold_met' EventEntityID - '<allocation_source.source_id>' EventPayload - { "allocation_source_id": "37623", "threshold":20 # The '20%' threshold was hit for this allocation. } The method should fire off emails to the users who should be informed of the new threshold value. """ #FIXME+TODO: next version: Fire and respond to the `clear_allocation_threshold_met` for a given allocation_source_id (This event should be generated any time you `.save()` and update the `compute_allowed` for an AllocationSource event = instance if event.name != 'allocation_source_threshold_met': return None payload = event.payload allocation_source_id = payload['allocation_source_id'] threshold = payload['threshold'] actual_value = payload['actual_value'] if not settings.ENFORCING: return None source = AllocationSource.objects.filter( source_id=allocation_source_id).first() if not source: return None users = AtmosphereUser.for_allocation_source(source.source_id) for user in users: send_usage_email_to(user, source, threshold, actual_value)
def listen_for_allocation_threshold_met(sender, instance, created, **kwargs): """ This listener expects: EventType - 'allocation_source_threshold_met' EventEntityID - '<allocation_source.name>' EventPayload - { "allocation_source_name": "37623", "threshold":20 # The '20%' threshold was hit for this allocation. "usage_percentage":22 # The actual perecntage used } The method should fire off emails to the users who should be informed of the new threshold value. """ # FIXME+TODO: next version: Fire and respond to the # `clear_allocation_threshold_met` for a given allocation_source_name # (This event should be generated any time you `.save()` and update the # `compute_allowed` for an AllocationSource event = instance if event.name != 'allocation_source_threshold_met': return None payload = event.payload allocation_source_name = payload['allocation_source_name'] threshold = payload['threshold'] usage_percentage = payload['usage_percentage'] source = AllocationSource.objects.filter(name=allocation_source_name).last() ##CHANGED # source = AllocationSource.objects.filter(name=allocation_source_name).last() if not source: return None users = AtmosphereUser.for_allocation_source(source.name) for user in users: send_usage_email_to( user, source, threshold, usage_percentage=usage_percentage )
def ansible_deployment( instance_ip, username, instance_id, playbooks_dir, limit_playbooks=[], limit_hosts={}, extra_vars={}, raise_exception=True, debug=False, **runner_opts): """ Use service.ansible to deploy to an instance. """ if not check_ansible(): return [] # Expecting to be path-relative to the playbook path, so use basename if type(limit_playbooks) == str: limit_playbooks = limit_playbooks.split(",") if type(limit_playbooks) != list: raise Exception("Invalid 'limit_playbooks' argument (%s). Expected List" % limit_playbooks) limit_playbooks = [os.path.basename(filepath) for filepath in limit_playbooks] logger = create_instance_logger( deploy_logger, instance_ip, username, instance_id) hostname = build_host_name(instance_id, instance_ip) configure_ansible(debug=debug) if debug: runner_opts['verbosity'] = 4 if not limit_hosts: if hostname: limit_hosts = hostname else: limit_hosts = instance_ip host_file = settings.ANSIBLE_HOST_FILE identity = Identity.find_instance(instance_id) if identity: time_zone = identity.provider.timezone extra_vars.update({ "TIMEZONE": time_zone, }) shared_users = list(AtmosphereUser.users_for_instance(instance_id).values_list('username', flat=True)) if not shared_users: shared_users = [username] if username not in shared_users: shared_users.append(username) extra_vars.update({ "SHARED_USERS": shared_users, }) extra_vars.update({ "ATMOUSERNAME": username, }) pbs = execute_playbooks( playbooks_dir, host_file, extra_vars, limit_hosts, logger=logger, limit_playbooks=limit_playbooks, **runner_opts) if raise_exception: raise_playbook_errors(pbs, instance_id, instance_ip, hostname) return pbs
def ansible_deployment(instance_ip, username, instance_id, playbooks_dir, limit_playbooks=[], limit_hosts={}, extra_vars={}, raise_exception=True, debug=False, **runner_opts): """ Use service.ansible to deploy to an instance. """ if not check_ansible(): return [] # Expecting to be path-relative to the playbook path, so use basename if type(limit_playbooks) == str: limit_playbooks = limit_playbooks.split(",") if type(limit_playbooks) != list: raise Exception( "Invalid 'limit_playbooks' argument (%s). Expected List" % limit_playbooks) limit_playbooks = [ os.path.basename(filepath) for filepath in limit_playbooks ] logger = create_instance_logger(deploy_logger, instance_ip, username, instance_id) hostname = build_host_name(instance_id, instance_ip) configure_ansible(debug=debug) if debug: runner_opts['verbosity'] = 4 if not limit_hosts: if hostname: limit_hosts = hostname else: limit_hosts = instance_ip host_file = settings.ANSIBLE_HOST_FILE identity = Identity.find_instance(instance_id) if identity: time_zone = identity.provider.timezone extra_vars.update({ "TIMEZONE": time_zone, }) shared_users = list( AtmosphereUser.users_for_instance(instance_id).values_list('username', flat=True)) if not shared_users: shared_users = [username] if username not in shared_users: shared_users.append(username) extra_vars.update({ "SHARED_USERS": shared_users, }) extra_vars.update({ "ATMOUSERNAME": username, }) pbs = execute_playbooks(playbooks_dir, host_file, extra_vars, limit_hosts, logger=logger, limit_playbooks=limit_playbooks, **runner_opts) if raise_exception: raise_playbook_errors(pbs, instance_id, instance_ip, hostname) return pbs