Foobar

    :param aws_config:
544253
def match_instances_and_roles(aws_config):
    
    printInfo('Matching EC2 instances and IAM roles...')
    ec2_config = aws_config['services']['ec2']
    iam_config = aws_config['services']['iam']
    role_instances = {}
    for r in ec2_config['regions']:
        for v in ec2_config['regions'][r]['vpcs']:
            if 'instances' in ec2_config['regions'][r]['vpcs'][v]:
                for i in ec2_config['regions'][r]['vpcs'][v]['instances']:
                    instance_profile = ec2_config['regions'][r]['vpcs'][v]['instances'][i]['IamInstanceProfile']
                    instance_profile_id = instance_profile['Id'] if instance_profile else None
                    if instance_profile_id:
                        manage_dictionary(role_instances, instance_profile_id, [])
                        role_instances[instance_profile_id].append(i)
    for role_id in iam_config['roles']:
        iam_config['roles'][role_id]['instances_count'] = 0
        for instance_profile_id in iam_config['roles'][role_id]['instance_profiles']:
            if instance_profile_id in role_instances:
                iam_config['roles'][role_id]['instance_profiles'][instance_profile_id]['instances'] = role_instances[instance_profile_id]
                iam_config['roles'][role_id]['instances_count'] += len(role_instances[instance_profile_id])
667816
def aws_xray_trace_config(name=None):
    

    def _trace_config_ctx_factory(trace_request_ctx):
        return SimpleNamespace(
            name=name,
            trace_request_ctx=trace_request_ctx
        )

    trace_config = aiohttp.TraceConfig(trace_config_ctx_factory=_trace_config_ctx_factory)
    trace_config.on_request_start.append(begin_subsegment)
    trace_config.on_request_end.append(end_subsegment)
    trace_config.on_request_exception.append(end_subsegment_with_exception)
    return trace_config
752786
def preprocessing(aws_config, ip_ranges = [], ip_ranges_name_key = None):
    

    map_all_sgs(aws_config)
    map_all_subnets(aws_config)
    set_emr_vpc_ids(aws_config)
    #parse_elb_policies(aws_config)

    # Various data processing calls
    add_security_group_name_to_ec2_grants(aws_config['services']['ec2'], aws_config['aws_account_id'])
    process_cloudtrail_trails(aws_config['services']['cloudtrail'])
    add_cidr_display_name(aws_config, ip_ranges, ip_ranges_name_key)
    merge_route53_and_route53domains(aws_config)
    match_instances_and_roles(aws_config)
    match_iam_policies_and_buckets(aws_config)

    # Preprocessing dictated by metadata
    process_metadata_callbacks(aws_config)
278927
def _default_options(p, blacklist=""):
    
    provider = p.add_argument_group(
        "provider", "AWS account information, defaults per the aws cli")

    if 'region' not in blacklist:
        provider.add_argument(
            "-r", "--region", action='append', default=[],
            dest='regions', metavar='REGION',
            help="AWS Region to target.  Can be used multiple times")
    provider.add_argument(
        "--profile",
        help="AWS Account Config File Profile to utilize")
    provider.add_argument("--assume", default=None, dest="assume_role",
                          help="Role to assume")
    provider.add_argument("--external-id", default=None, dest="external_id",
                          help="External Id to provide when assuming a role")

    config = p.add_argument_group(
        "config", "Policy config file(s) and policy selectors")
    # -c is deprecated.  Supported for legacy reasons
    config.add_argument("-c", "--config", help=argparse.SUPPRESS)
    config.add_argument("configs", nargs='*',
                        help="Policy configuration file(s)")
    config.add_argument("-p", "--policies", default=None, dest='policy_filter',
                        help="Only use named/matched policies")
    config.add_argument("-t", "--resource", default=None, dest='resource_type',
                        help="Only use policies with the given resource type")

    output = p.add_argument_group("output", "Output control")
    output.add_argument("-v", "--verbose", action="count", help="Verbose logging")
    if 'quiet' not in blacklist:
        output.add_argument("-q", "--quiet", action="count",
                            help="Less logging (repeatable, -qqq for no output)")
    else:
        output.add_argument("-q", "--quiet", action="count", help=argparse.SUPPRESS)
    output.add_argument("--debug", default=False, help=argparse.SUPPRESS,
                        action="store_true")

    if 'vars' not in blacklist:
        # p.add_argument('--vars', default=None,
        #               help='Vars file to substitute into policy')
        p.set_defaults(vars=None)

    if 'log-group' not in blacklist:
        p.add_argument(
            "-l", "--log-group", default=None,
            help="Cloudwatch Log Group to send policy logs")
    else:
        p.add_argument("--log-group", default=None, help=argparse.SUPPRESS)

    if 'output-dir' not in blacklist:
        p.add_argument("-s", "--output-dir", required=True,
                       help="[REQUIRED] Directory or S3 URL For policy output")

    if 'cache' not in blacklist:
        p.add_argument(
            "-f", "--cache", default="~/.cache/cloud-custodian.cache",
            help="Cache file (default %(default)s)")
        p.add_argument(
            "--cache-period", default=15, type=int,
            help="Cache validity in minutes (default %(default)i)")
    else:
        p.add_argument("--cache", default=None, help=argparse.SUPPRESS)
152062
def do_get_queue_config(self, line):
        

        def f(p, args):
            try:
                source, queue = args
            except:
                print("argument error")
                return
            o = p.get_config(source)
            for q in o.resources.queue:
                if q.resource_id != queue:
                    continue
                print(q.resource_id)
                conf = q.properties
                for k in self._queue_settings:
                    try:
                        v = getattr(conf, k)
                    except AttributeError:
                        continue
                    print('%s %s' % (k, v))

        self._request(line, f)
1234480
def extract_aws_metadata(wrapped, instance, args, kwargs, return_value):
    
    response = return_value
    LOGGER.debug(
        'Extracting AWS metadata',
        args=args,
        kwargs=kwargs,
    )
    if 'operation_name' in kwargs:
        operation_name = kwargs['operation_name']
    else:
        operation_name = args[0]

    # Most of the time the actual keyword arguments to the client call are
    # passed in as a positial argument after the operation name.
    if len(kwargs) == 0 and len(args) == 2:
        kwargs = args[1]

    region_name = instance._client_config.region_name

    response_metadata = response.get('ResponseMetadata')

    metadata = {
        'aws': {
            'operation': operation_name,
            'region': region_name,
        }
    }

    if 'TableName' in kwargs:
        metadata['aws']['table_name'] = kwargs['TableName']
    if 'QueueUrl' in kwargs:
        metadata['aws']['queue_url'] = kwargs['QueueUrl']

    if response_metadata is not None:
        metadata['http'] = {
            'response': {
                'status': response_metadata['HTTPStatusCode'],
            },
        }
        metadata['aws']['request_id'] = response_metadata['RequestId']

    return metadata
279118
def awsRetry(f):
    
    @wraps(f)
    def wrapper(*args, **kwargs):
        for attempt in retry(delays=truncExpBackoff(),
                             timeout=300,
                             predicate=awsRetryPredicate):
            with attempt:
                return f(*args, **kwargs)
    return wrapper
536322
def deploy_lambda(collector):
    """"""
    amazon = collector.configuration['amazon']
    aws_syncr = collector.configuration['aws_syncr']
    find_lambda_function(aws_syncr, collector.configuration).deploy(aws_syncr, amazon)
1059506
def handle(self, *args, **options):  # NoQA
        

        # Load the settings
        self.require_settings(args, options)

        # Load your AWS credentials from ~/.aws/credentials
        self.load_credentials()

        try:
            # Tail the available logs
            all_logs = self.zappa.fetch_logs(self.lambda_name)
            self.print_logs(all_logs)

            # Keep polling, and print any new logs.
            while True:
                all_logs_again = self.zappa.fetch_logs(self.lambda_name)
                new_logs = []
                for log in all_logs_again:
                    if log not in all_logs:
                        new_logs.append(log)

                self.print_logs(new_logs)
                all_logs = all_logs + new_logs
        except KeyboardInterrupt:
            # Die gracefully
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)

        return
591210
def configure_args(self, arguments):
        

        module, key, config_path = self.check_file_paths(arguments.module,
                                                         arguments.key,
                                                         arguments.config)
        log_dir = self.check_directory_paths(arguments.log_dir)

        if arguments.repository_url is None:
            url = default_config['repository']['url']
        else:
            url = arguments.repository_url

        args_config = dict(aws=dict(bucket=arguments.bucket),
                           logging=dict(dir=arguments.log_dir,
                                        prefix=arguments.log_prefix),
                           workers=arguments.workers,
                           repository=dict(enabled=arguments.repository,
                                           url=url,
                                           manifest=arguments.repository_manifest,
                                           gpg_verify=arguments.gpg_verify))

        if arguments.server is not None:

            jump_host = None
            if arguments.jump_server is not None:
                if arguments.jump_port is not None:
                    jump_port = int(arguments.jump_port)
                else:
                    jump_port = None
                jump_host = dict(zip(jump_host_allowed_keys,
                                     [arguments.jump_server,
                                      jump_port,
                                      arguments.jump_username,
                                      arguments.jump_password,
                                      arguments.jump_key]))

            if arguments.port is not None:
                port = int(arguments.port)
            else:
                port = None
            host = dict(zip(host_allowed_keys,
                            [arguments.server, port, arguments.username,
                             arguments.password, module, key,
                             arguments.filename, jump_host]))
            args_config['hosts'] = []
            args_config['hosts'].append(host)

        if config_path is not None:
            try:
                config = self.load_config(config_path)
                self.validate_config(config)
                args_config.update(config)
            except YAMLError as ex:
                logger.warn('Invalid yaml Format: {0}'.format(ex))
                raise
            except InvalidConfigurationError as ex:
                logger.warn(ex)
                raise

        return args_config
1052559
function buildLambda(options) {
  // crawl the module path to make sure the Lambda handler path is
  // set correctly: <functionDir>/function.fn
  let handlerPath = (module.parent.parent.parent.filename).split(path.sep).slice(-2).shift();
  let fn = {
    Resources: {}
  };

  // all function parameters available as environment variables
  fn.Resources[options.name] = {
    Type: 'AWS::Lambda::Function',
    Properties: {
      Code: {
        S3Bucket: cf.ref('CodeS3Bucket'),
        S3Key: cf.join([cf.ref('CodeS3Prefix'), cf.ref('GitSha'), '.zip'])
      },
      Role: cf.if('HasDispatchSnsArn', cf.getAtt('LambdaCfnDispatchRole', 'Arn'), cf.getAtt('LambdaCfnRole', 'Arn')),
      Description: cf.stackName,
      Environment: {
        Variables: {}
      },
      Handler: handlerPath + '/function.fn'
    }
  };

  fn.Resources[options.name].Properties.Timeout = setLambdaTimeout(options.timeout);
  fn.Resources[options.name].Properties.MemorySize = setLambdaMemorySize(options.memorySize);
  fn.Resources[options.name].Properties.Runtime = setLambdaRuntine(options.runtime);

  return fn;
}
747491
def choose_args(metadata, config):
    
    return dict(
        connect_args=choose_connect_args(metadata, config),
        echo=config.echo,
        max_overflow=config.max_overflow,
        pool_size=config.pool_size,
        pool_timeout=config.pool_timeout,
    )
71593
def apply_config(self, config):
        
        self.haproxy_config_path = config["config_file"]

        global_stanza = Stanza("global")
        global_stanza.add_lines(config.get("global", []))
        global_stanza.add_lines([
            "stats socket %s mode 600 level admin" % config["socket_file"],
            "stats timeout 2m"
        ])

        defaults_stanza = Stanza("defaults")
        defaults_stanza.add_lines(config.get("defaults", []))

        proxy_stanzas = [
            ProxyStanza(
                name, proxy["port"], proxy["upstreams"],
                proxy.get("options", []),
                proxy.get("bind_address")
            )
            for name, proxy in six.iteritems(config.get("proxies", {}))
        ]

        stats_stanza = None
        if "stats" in config:
            stats_stanza = StatsStanza(
                config["stats"]["port"], config["stats"].get("uri", "/")
            )
            for timeout in ("client", "connect", "server"):
                if timeout in config["stats"].get("timeouts", {}):
                    stats_stanza.add_line(
                        "timeout %s %d" % (
                            timeout,
                            config["stats"]["timeouts"][timeout]
                        )
                    )

        self.config_file = HAProxyConfig(
            global_stanza, defaults_stanza,
            proxy_stanzas=proxy_stanzas, stats_stanza=stats_stanza,
            meta_clusters=config.get("meta_clusters", {}),
            bind_address=config.get("bind_address")
        )

        self.control = HAProxyControl(
            config["config_file"], config["socket_file"], config["pid_file"],
        )
930332
def main(stack_name, template, mustache_variables):
    ''

    template_data = _parse_template(template, mustache_variables)
    params = {
        'StackName': stack_name,
        'TemplateBody': template_data
    }

    try:
        if _stack_exists(stack_name):
            print('Updating {}'.format(stack_name))
            stack_result = cf.update_stack(
                **params,
                Capabilities=['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'])
            waiter = cf.get_waiter('stack_update_complete')
            waiter.wait(StackName=stack_name)
        else:
            print('Creating {}'.format(stack_name))
            stack_result = cf.create_stack(
                **params,
                Capabilities=['CAPABILITY_IAM', 'CAPABILITY_NAMED_IAM'])
            try:
                waiter = cf.get_waiter('stack_create_complete')
                print("...waiting for stack to be ready...")
                waiter.wait(StackName=stack_name)
            except Exception as ex:
                print(ex)
                print("""
                There was an error creating your stack. Please go to CloudFormation in your 
                AWS console, click on the stack you created, resolve any errors, delete the stack
                and try again.

                You are seeing this error because your stack failed to create, when stacks fail
                to create they are put into a terminal ROLLBACK_COMPLETE state and the stack cannot
                be recovered because they have no previous state to roll back to.
                """)
                exit(1)

    except botocore.exceptions.ClientError as ex:
        error_message = ex.response['Error']['Message']
        if error_message == 'No updates are to be performed.':
            print("No changes")
        else:
            raise
    else:
        print(json.dumps(
            cf.describe_stacks(StackName=stack_result['StackId']),
            indent=2,
            default=json_serial
        ))
789609
def get_default_config_help(self):
        
        config = super(cloudwatchHandler, self).get_default_config_help()

        config.update({
            'region': 'AWS region',
            'metric': 'Diamond metric name',
            'namespace': 'CloudWatch metric namespace',
            'name': 'CloudWatch metric name',
            'unit': 'CloudWatch metric unit',
            'collector': 'Diamond collector name',
            'collect_by_instance': 'Collect metrics for instances separately',
            'collect_without_dimension': 'Collect metrics without dimension'
        })

        return config
777624
def load_config(app_name, *args, **kwargs):
    
    configure_logging()

    # compatible with Python 2 and 3.
    prefix = kwargs.get('prefix', 'etc')
    verbose = kwargs.get('verbose', False)
    location = kwargs.get('location', None)
    passphrase = kwargs.get('passphrase',
        os.getenv("%s_SETTINGS_CRYPT_KEY" % app_name.upper(),
            os.getenv("SETTINGS_CRYPT_KEY", None)))
    confnames = args

    if not location:
        location = os.getenv("%s_SETTINGS_LOCATION" % app_name.upper(), None)
    if not location:
        location = os.getenv("SETTINGS_LOCATION", None)
        if location:
            location = "%s/%s" % (location, app_name)

    config = {}
    for confname in confnames:
        content = None
        if location and location.startswith('s3://'):
            try:
                import boto
                _, bucket_name, prefix = urlparse(location)[:3]
                try:
                    conn = boto.connect_s3()
                    bucket = conn.get_bucket(bucket_name)
                    key_name = '%s/%s' % (prefix, confname)
                    key = bucket.get_key(key_name)
                    content = key.get_contents_as_string()
                    if verbose:
                        LOGGER.info("config loaded from 's3://%s/%s'",
                            bucket_name, key_name)
                except (boto.exception.NoAuthHandlerFound,
                        boto.exception.S3ResponseError) as _:
                    pass
            except ImportError:
                pass

        # We cannot find a deployutils S3 bucket. Let's look on the filesystem.
        if not content:
            confpath = locate_config(
                confname, app_name, location=location,
                prefix=prefix, verbose=verbose)
            if confpath:
                with open(confpath, 'rb') as conffile:
                    content = conffile.read()

        if content:
            if passphrase:
                content = crypt.decrypt(content, passphrase)
            if hasattr(content, 'decode'):
                content = content.decode('utf-8')
            for line in content.split('\n'):
                if not line.startswith('#'):
                    look = re.match(r'(\w+)\s*=\s*(.*)', line)
                    if look:
                        try:
                            # We used to parse the file line by line.
                            # Once Django 1.5 introduced ALLOWED_HOSTS
                            # (a tuple that definitely belongs to the site.conf
                            # set), we had no choice other than resort
                            # to eval(value, {}, {}).
                            # We are not resorting to import conf module yet
                            # but that might be necessary once we use
                            # dictionary configs for some of the apps...
                            # TODO: consider using something like ConfigObj
                            # for this:
                            # http://www.voidspace.org.uk/python/configobj.html
                            #pylint:disable=eval-used
                            config.update({look.group(1).upper():
                                eval(look.group(2), {}, {})})
                        except Exception:
                            raise

    return config
690995
def get_lambdas(awsclient, config, add_arn=False):
    
    if 'lambda' in config:
        client_lambda = awsclient.get_client('lambda')
        lambda_entries = config['lambda'].get('entries', [])
        lmbdas = []
        for lambda_entry in lambda_entries:
            lmbda = {
                'name': lambda_entry.get('name', None),
                'alias': lambda_entry.get('alias', None),
                'swagger_ref': lambda_entry.get('swaggerRef', None)
            }
            if add_arn:
                _sleep()
                response_lambda = client_lambda.get_function(
                    FunctionName=lmbda['name'])
                lmbda['arn'] = response_lambda['Configuration']['FunctionArn']
            lmbdas.append(lmbda)
        return lmbdas
    else:
        return []