Perform a function against a cloud provider
776834
def do_function(self, prov, func, kwargs):
        '''
        
        '''
        matches = self.lookup_providers(prov)
        if len(matches) > 1:
            raise SaltCloudSystemExit(
                'More than one results matched \'{0}\'. Please specify '
                'one of: {1}'.format(
                    prov,
                    ', '.join([
                        '{0}:{1}'.format(alias, driver) for
                        (alias, driver) in matches
                    ])
                )
            )

        alias, driver = matches.pop()
        fun = '{0}.{1}'.format(driver, func)
        if fun not in self.clouds:
            raise SaltCloudSystemExit(
                'The \'{0}\' cloud provider alias, for the \'{1}\' driver, does '
                'not define the function \'{2}\''.format(alias, driver, func)
            )

        log.debug(
            'Trying to execute \'%s\' with the following kwargs: %s',
            fun, kwargs
        )

        with salt.utils.context.func_globals_inject(
            self.clouds[fun],
            __active_provider_name__=':'.join([alias, driver])
        ):
            if kwargs:
                return {
                    alias: {
                        driver: self.clouds[fun](
                            call='function', kwargs=kwargs
                        )
                    }
                }
            return {
                alias: {
                    driver: self.clouds[fun](call='function')
                }
            }
444726
def _execute_request(self, request):
        
        with GoogleCloudProvider.__gce_lock:
            return request.execute(http=self._auth_http)
1304871
def main():
  
  setup_logging(logging.INFO)

  # Parse arguments
  parser = argparse.ArgumentParser(
    description="A utility to interact with AWS using Cloudera Manager.")
  parser.add_argument('-H', '--hostname', action='store', dest='hostname',
                      required=True,
                      help='The hostname of the Cloudera Manager server.')
  parser.add_argument('-p', action='store', dest='port', type=int,
                      help='The port of the Cloudera Manager server. Defaults '
                           'to 7180 (http) or 7183 (https).')
  parser.add_argument('-u', '--username', action='store', dest='username',
                      help='Login name.')
  parser.add_argument('--password', action='store', dest='password',
                      help='Login password.')
  parser.add_argument('--api-version', action='store', dest='api_version',
                      type=int,
                      default=MINIMUM_SUPPORTED_API_VERSION,
                      help='API version to be used. Defaults to {0}.'.format(
                        MINIMUM_SUPPORTED_API_VERSION))
  parser.add_argument('--tls', action='store_const', dest='use_tls',
                      const=True, default=False,
                      help='Whether to use tls (https).')
  parser.add_argument('-c', '--show-categories', action='store_true',
                      default=False, dest='show_categories',
                      help='Prints a list of supported external account '
                           'category names. For example, "AWS" is a supported '
                           'external account category name.')
  parser.add_argument('-t', '--show-types', action='store',
                      dest='category_name',
                      help='Prints a list of supported external account type '
                           'names for the given CATEGORY_NAME. For example, '
                           '"AWS_ACCESS_KEY_AUTH" is a supported external '
                           'account type name for external account category '
                           '"AWS".')
  parser.add_argument('-n', '--show-credentials', action='store',
                      dest='type_name',
                      help='Prints a list of available credential names for '
                           'the given TYPE_NAME.')
  parser.add_argument('--prune', action='store', dest='credential_name',
                      help='Runs S3Guard prune command on external account '
                           'associated with the given CREDENTIAL_NAME.')
  parser.add_argument('--version', action='version', version='%(prog)s 1.0')
  args = parser.parse_args()

  # Use the default port if required.
  if not args.port:
    if args.use_tls:
      args.port = DEFAULT_HTTPS_PORT
    else:
      args.port = DEFAULT_HTTP_PORT

  validate_api_compatibility(args)
  get_login_credentials(args)
  initialize_api(args)

  # Perform the AWS operation based on the input arguments.
  if args.show_categories:
    list_supported_categories()
  elif args.category_name:
    list_supported_types(args.category_name)
  elif args.type_name:
    list_credentials_by_name(args.type_name)
  elif args.credential_name:
    call_s3guard_prune(args.credential_name)
  else:
    print ("ERROR: No arguments given to perform any AWS operation.")
    parser.print_help()
    sys.exit(1)
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)
289697
function buildCloudwatchEvent(options, functionType) {
  validateCloudWatchEvent(functionType, options.eventSources);

  let eventName = options.name + utils.capitalizeFirst(functionType);
  let event = {
    Resources: {}
  };

  event.Resources[eventName + 'Permission'] = {
    Type: 'AWS::Lambda::Permission',
    Properties: {
      FunctionName: cf.getAtt(options.name, 'Arn'),
      Action: 'lambda:InvokeFunction',
      Principal: 'events.amazonaws.com',
      SourceArn: cf.getAtt(eventName + 'Rule', 'Arn')
    }
  };

  event.Resources[eventName + 'Rule'] = {
    Type: 'AWS::Events::Rule',
    Properties: {
      RoleArn: cf.if('HasDispatchSnsArn', cf.getAtt('LambdaCfnDispatchRole', 'Arn'), cf.getAtt('LambdaCfnRole', 'Arn')),
      State: 'ENABLED',
      Targets: [
        {
          Arn: cf.getAtt(options.name, 'Arn'),
          Id: options.name
        }
      ]
    }
  };

  if (functionType === 'cloudwatchEvent') {
    event.Resources[eventName + 'Rule'].Properties.EventPattern = options.eventSources.cloudwatchEvent.eventPattern;
  } else {
    event.Resources[eventName + 'Rule'].Properties.ScheduleExpression = options.eventSources.schedule.expression;
  }

  return event;
}
30904
def get_function(session_factory, name, handler, runtime, role,
                 log_groups,
                 project, account_name, account_id,
                 sentry_dsn,
                 pattern="Traceback"):
    
    # Lazy import to avoid runtime dependency
    from c7n.mu import (
        LambdaFunction, PythonPackageArchive, CloudWatchLogSubscription)

    config = dict(
        name=name,
        handler=handler,
        runtime=runtime,
        memory_size=512,
        timeout=15,
        role=role,
        description='Custodian Sentry Relay',
        events=[
            CloudWatchLogSubscription(
                session_factory, log_groups, pattern)])

    archive = PythonPackageArchive('c7n_sentry')
    archive.add_contents(
        'config.json', json.dumps({
            'project': project,
            'account_name': account_name,
            'account_id': account_id,
            'sentry_dsn': sentry_dsn,
        }))
    archive.add_contents(
        'handler.py',
        'from c7n_sentry.c7nsentry import process_log_event'
    )
    archive.close()

    return LambdaFunction(config, archive)
815226
def api_handler(input_data, cloud, api, url_params=None, batch_size=None, **kwargs):
    
    url_params = url_params or {}
    input_data = standardize_input_data(input_data)

    cloud = cloud or config.cloud
    host = "%s.indico.domains" % cloud if cloud else config.host

    # LOCAL DEPLOYMENTS
    if not (host.endswith('indico.domains') or host.endswith('indico.io')):
        url_protocol = "http"
    else:
        url_protocol = config.url_protocol

    headers = dict(JSON_HEADERS)
    headers["X-ApiKey"] = url_params.get("api_key") or config.api_key
    url = create_url(url_protocol, host, api, dict(kwargs, **url_params))
    return collect_api_results(input_data, url, headers, api, batch_size, kwargs)
1147932
function executeApiCall(callback, needConnection) {
        needConnection = typeof needConnection !== 'undefined' ? needConnection : true;
        authenticate(function (error, result) {
            if (error || !result) {
                callback(error, result);
                return;
            }
            if (needConnection) {
                isConnected(function (error, result) {
                    if (error || !result) {
                        console.error("[Deluge] WebUI not connected to a daemon");
                        return;
                    }
                    callback(error, result);
                });
            } else {
                callback(error, result);
            }
        });
    }
820549
function listen(appDirectory, sessionStore, preSessionInject, postSessionInject, sendToLogFunction) {

    let builder = new ConfigBuilder(new ConfigApi());
    let configStore = builder.build(appDirectory);
    let amorphicOptions = AmorphicContext.amorphicOptions;

    if (typeof sendToLogFunction === 'function') {
        AmorphicContext.appContext.sendToLog = sendToLogFunction;
        SupertypeSession.logger.setLogger(sendToLogFunction);
    }

    buildStartUpParams(configStore);

    let sanitizedAmorphicOptions = Object.assign({}, amorphicOptions);
    delete sanitizedAmorphicOptions.sessionSecret;

    logMessage('Starting Amorphic with options: ' + JSON.stringify(sanitizedAmorphicOptions));

    let sessionConfig = {
        secret: amorphicOptions.sessionSecret,
        cookie: {
            maxAge: amorphicOptions.sessionExpiration
        },
        resave: false,
        saveUninitialized: true,
        rolling: true
    };

    if (sessionStore) {
        sessionConfig['store'] = sessionStore;
    }
    // Initialize applications
    let appList = amorphicOptions.appList;
    let appStartList = amorphicOptions.appStartList;
    let promises = [];

    for (let appKey in appList) {
        if (appStartList.indexOf(appKey) >= 0) {
            promises.push(startApplication(appKey, appDirectory, appList, configStore, sessionStore));
        }
    }

    return Bluebird.all(promises)
        .then(createServer.bind(this, preSessionInject, postSessionInject, appList, appStartList, appDirectory, sessionConfig))
        .then(function logStart() {
            logMessage('Amorphic has been started');
        })
        .catch(function error(e) {
            logMessage(e.message + ' ' + e.stack);
        });
}
1084764
def show_instance(vm_name, call=None):
    '''
    
    '''
    if call != 'action':
        raise SaltCloudSystemExit(
            'The show_instance action must be called with -a or --action.'
        )
    conn = get_conn()
    node = _expand_node(conn.ex_get_node(vm_name))
    __utils__['cloud.cache_node'](node, __active_provider_name__, __opts__)
    return node
814822
def query_instance(vm_=None, call=None):
    '''
    
    '''
    if call == 'function':
        # Technically this function may be called other ways too, but it
        # definitely cannot be called with --function.
        raise SaltCloudSystemExit(
            'The query_instance action must be called with -a or --action.'
        )

    instance_id = vm_['instance_id']
    location = vm_.get('location', get_location(vm_))
    __utils__['cloud.fire_event'](
        'event',
        'querying instance',
        'salt/cloud/{0}/querying'.format(vm_['name']),
        args={'instance_id': instance_id},
        sock_dir=__opts__['sock_dir'],
        transport=__opts__['transport']
    )

    log.debug('The new VM instance_id is %s', instance_id)

    params = {'Action': 'DescribeInstances',
              'InstanceId.1': instance_id}

    provider = get_provider(vm_)

    attempts = 0
    while attempts < aws.AWS_MAX_RETRIES:
        data, requesturl = aws.query(params,                # pylint: disable=unbalanced-tuple-unpacking
                                     location=location,
                                     provider=provider,
                                     opts=__opts__,
                                     return_url=True,
                                     sigver='4')
        log.debug('The query returned: %s', data)

        if isinstance(data, dict) and 'error' in data:
            log.warning(
                'There was an error in the query. %s attempts '
                'remaining: %s', attempts, data['error']
            )
        elif isinstance(data, list) and not data:
            log.warning(
                'Query returned an empty list. %s attempts '
                'remaining.', attempts
            )
        else:
            break

        aws.sleep_exponential_backoff(attempts)
        attempts += 1
        continue
    else:
        raise SaltCloudSystemExit(
            'An error occurred while creating VM: {0}'.format(data['error'])
        )

    def __query_ip_address(params, url):  # pylint: disable=W0613
        data = aws.query(params,
                         location=location,
                         provider=provider,
                         opts=__opts__,
                         sigver='4')
        if not data:
            log.error(
                'There was an error while querying EC2. Empty response'
            )
            # Trigger a failure in the wait for IP function
            return False

        if isinstance(data, dict) and 'error' in data:
            log.warning('There was an error in the query. %s', data['error'])
            # Trigger a failure in the wait for IP function
            return False

        log.debug('Returned query data: %s', data)

        if ssh_interface(vm_) == 'public_ips':
            if 'ipAddress' in data[0]['instancesSet']['item']:
                return data
            else:
                log.error(
                    'Public IP not detected.'
                )

        if ssh_interface(vm_) == 'private_ips':
            if 'privateIpAddress' in data[0]['instancesSet']['item']:
                return data
            else:
                log.error(
                    'Private IP not detected.'
                )

    try:
        data = salt.utils.cloud.wait_for_ip(
            __query_ip_address,
            update_args=(params, requesturl),
            timeout=config.get_cloud_config_value(
                'wait_for_ip_timeout', vm_, __opts__, default=10 * 60),
            interval=config.get_cloud_config_value(
                'wait_for_ip_interval', vm_, __opts__, default=10),
            interval_multiplier=config.get_cloud_config_value(
                'wait_for_ip_interval_multiplier', vm_, __opts__, default=1),
        )
    except (SaltCloudExecutionTimeout, SaltCloudExecutionFailure) as exc:
        try:
            # It might be already up, let's destroy it!
            destroy(vm_['name'])
        except SaltCloudSystemExit:
            pass
        finally:
            raise SaltCloudSystemExit(six.text_type(exc))

    if 'reactor' in vm_ and vm_['reactor'] is True:
        __utils__['cloud.fire_event'](
            'event',
            'instance queried',
            'salt/cloud/{0}/query_reactor'.format(vm_['name']),
            args={'data': data},
            sock_dir=__opts__['sock_dir'],
            transport=__opts__['transport']
        )

    return data
1034011
def action(func=None,
           cloudmap=None,
           instances=None,
           provider=None,
           instance=None,
           opts=None,
           **kwargs):
    '''
    
    '''
    info = {}
    client = _get_client()
    if isinstance(opts, dict):
        client.opts.update(opts)
    try:
        info = client.action(
            func,
            cloudmap,
            instances,
            provider,
            instance,
            salt.utils.args.clean_kwargs(**kwargs)
        )
    except SaltCloudConfigError as err:
        log.error(err)
    return info
1019888
def _query(action=None,
           command=None,
           args=None,
           method='GET',
           header_dict=None,
           data=None,
           url='https://api.linode.com/'):
    '''
    
    '''
    global LASTCALL
    vm_ = get_configured_provider()

    ratelimit_sleep = config.get_cloud_config_value(
        'ratelimit_sleep', vm_, __opts__, search_global=False, default=0,
    )
    apikey = config.get_cloud_config_value(
        'apikey', vm_, __opts__, search_global=False
    )

    if not isinstance(args, dict):
        args = {}

    if 'api_key' not in args.keys():
        args['api_key'] = apikey

    if action and 'api_action' not in args.keys():
        args['api_action'] = '{0}.{1}'.format(action, command)

    if header_dict is None:
        header_dict = {}

    if method != 'POST':
        header_dict['Accept'] = 'application/json'

    decode = True
    if method == 'DELETE':
        decode = False

    now = int(time.mktime(datetime.datetime.now().timetuple()))
    if LASTCALL >= now:
        time.sleep(ratelimit_sleep)

    result = __utils__['http.query'](
        url,
        method,
        params=args,
        data=data,
        header_dict=header_dict,
        decode=decode,
        decode_type='json',
        text=True,
        status=True,
        hide_fields=['api_key', 'rootPass'],
        opts=__opts__,
    )

    if 'ERRORARRAY' in result['dict']:
        if result['dict']['ERRORARRAY']:
            error_list = []

            for error in result['dict']['ERRORARRAY']:
                msg = error['ERRORMESSAGE']

                if msg == "Authentication failed":
                    raise SaltCloudSystemExit(
                        'Linode API Key is expired or invalid'
                    )
                else:
                    error_list.append(msg)
            raise SaltCloudException(
                'Linode API reported error(s): {}'.format(", ".join(error_list))
            )

    LASTCALL = int(time.mktime(datetime.datetime.now().timetuple()))
    log.debug('Linode Response Status Code: %s', result['status'])

    return result['dict']
324963
def run_func(func, *args, **kwargs):
    """"""
    ray.init()

    func = ray.remote(func)

    # NOTE: kwargs not allowed for now
    result = ray.get(func.remote(*args))

    # Inspect the stack to get calling example
    caller = inspect.stack()[1][3]
    print("%s: %s" % (caller, str(result)))

    return result
828883
def handle(self, *args, **options):
        

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

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

        # Invoke it!
        command = {"command": ' '.join(options['environment'][1:])}

        response = self.zappa.invoke_lambda_function(
            self.lambda_name, json.dumps(command), invocation_type='RequestResponse')

        if 'LogResult' in response:
            print(base64.b64decode(response['LogResult']))
        else:
            print(response)
            import pdb
            pdb.set_trace()

        return
1026113
def avail_images(call=None):
    '''
    
    '''
    if call == 'action':
        raise SaltCloudException(
            'The avail_images function must be called with -f or --function.'
        )

    ret = {}

    vm_ = get_configured_provider()
    manager = packet.Manager(auth_token=vm_['token'])

    ret = {}

    for os_system in manager.list_operating_systems():
        ret[os_system.name] = os_system.__dict__

    return ret
603487
def show_instance(name, call=None):
    '''
    
    '''
    if call != 'action':
        raise SaltCloudSystemExit(
            'The show_instance action must be called with -a or --action.'
        )

    nodes = list_nodes_full()
    # Find under which cloud service the name is listed, if any
    if name not in nodes:
        return {}
    __utils__['cloud.cache_node'](nodes[name], __active_provider_name__, __opts__)
    return nodes[name]