<commit_before># -*- coding: utf-8 -*-
'''
Use the cloud cache on the master to derive IPv4 addresses based on minion ID.

This roster requires that the minion in question was created using at least the
2015.5.0 version of Salt Cloud. Starting with the 2015.5.0 release, Salt Cloud
maintains an index of minions that it creates and deletes. This index tracks the
provider and profile configuration used to provision the minion, including
authentication information. So long as this configuration remains current, it can
be used by Salt SSH to log into any minion in the index.

To connect as a user other than root, modify the cloud configuration file
usually located at /etc/salt/cloud. For example, add the following:

.. code-block:: yaml

    ssh_username: my_user
    sudo: True
'''

# Import python libs
from __future__ import absolute_import
import os.path

# Import 3rd-party libs
import msgpack

# Import Salt libs
import salt.loader
import salt.utils
import salt.utils.cloud
import salt.utils.validate.net
import salt.config
from salt import syspaths
from salt.ext.six import string_types


def targets(tgt, tgt_type='glob', **kwargs):  # pylint: disable=W0613
    '''
    Return the targets from the flat yaml file, checks opts for location but
    defaults to /etc/salt/roster
    '''
    ret = {}

    cache = os.path.join(syspaths.CACHE_DIR, 'cloud', 'index.p')

    if not os.path.exists(cache):
        return {}

    with salt.utils.fopen(cache, 'r') as fh_:
        cache_data = msgpack.load(fh_)

    indexed_minion = cache_data.get(tgt, None)

    if indexed_minion is None:
        return {}

    client = salt.cloud.CloudClient(
            os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
            )
    info = client.action('show_instance', names=[tgt])
    if not info:
        return {}

    provider = indexed_minion.get('provider', None)
    profile = indexed_minion.get('profile', None)
    driver = indexed_minion.get('driver', None)
    vm_ = {
        'provider': provider,
        'profile': profile,
    }

    full_info = info.get(provider, {}).get(driver, {}).get(tgt, {})
    public_ips = full_info.get('public_ips', [])
    private_ips = full_info.get('private_ips', [])
    ip_list = []
    for item in (public_ips, private_ips):
        if isinstance(item, list):
            ip_list = ip_list + item
        elif isinstance(item, string_types):
            ip_list.append(item)

    roster_order = __opts__.get('roster_order', (
        'public', 'private', 'local'
    ))
    preferred_ip = extract_ipv4(roster_order, ip_list)

    ret[tgt] = {
        'host': preferred_ip,
    }

    cloud_opts = salt.config.cloud_config(
        os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
    )
    ssh_username = salt.utils.cloud.ssh_usernames({}, cloud_opts)
    if isinstance(ssh_username, string_types):
        ret[tgt]['user'] = ssh_username
    elif isinstance(ssh_username, list):
        ret[tgt]['user'] = ssh_username[0]

    password = salt.config.get_cloud_config_value(
        'password', vm_, cloud_opts, search_global=False, default=None
    )
    if password:
        ret[tgt]['password'] = password
<commit_msg>cloud roster: Check provider and profile configs for ssh_username<commit_after># -*- coding: utf-8 -*-
'''
Use the cloud cache on the master to derive IPv4 addresses based on minion ID.

This roster requires that the minion in question was created using at least the
2015.5.0 version of Salt Cloud. Starting with the 2015.5.0 release, Salt Cloud
maintains an index of minions that it creates and deletes. This index tracks the
provider and profile configuration used to provision the minion, including
authentication information. So long as this configuration remains current, it can
be used by Salt SSH to log into any minion in the index.

To connect as a user other than root, modify the cloud configuration file
usually located at /etc/salt/cloud. For example, add the following:

.. code-block:: yaml

    ssh_username: my_user
    sudo: True
'''

# Import python libs
from __future__ import absolute_import
import os.path

# Import 3rd-party libs
import msgpack

# Import Salt libs
import salt.loader
import salt.utils
import salt.utils.cloud
import salt.utils.validate.net
import salt.config
from salt import syspaths
from salt.ext.six import string_types


def targets(tgt, tgt_type='glob', **kwargs):  # pylint: disable=W0613
    '''
    Return the targets from the flat yaml file, checks opts for location but
    defaults to /etc/salt/roster
    '''
    ret = {}

    cache = os.path.join(syspaths.CACHE_DIR, 'cloud', 'index.p')

    if not os.path.exists(cache):
        return {}

    with salt.utils.fopen(cache, 'r') as fh_:
        cache_data = msgpack.load(fh_)

    indexed_minion = cache_data.get(tgt, None)

    if indexed_minion is None:
        return {}

    client = salt.cloud.CloudClient(
            os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
            )
    info = client.action('show_instance', names=[tgt])
    if not info:
        return {}

    provider = indexed_minion.get('provider', None)
    profile = indexed_minion.get('profile', None)
    driver = indexed_minion.get('driver', None)
    vm_ = {
        'provider': provider,
        'profile': profile,
    }

    full_info = info.get(provider, {}).get(driver, {}).get(tgt, {})
    public_ips = full_info.get('public_ips', [])
    private_ips = full_info.get('private_ips', [])
    ip_list = []
    for item in (public_ips, private_ips):
        if isinstance(item, list):
            ip_list = ip_list + item
        elif isinstance(item, string_types):
            ip_list.append(item)

    roster_order = __opts__.get('roster_order', (
        'public', 'private', 'local'
    ))
    preferred_ip = extract_ipv4(roster_order, ip_list)

    ret[tgt] = {
        'host': preferred_ip,
    }

    cloud_opts = salt.config.cloud_config(
        os.path.join(os.path.dirname(__opts__['conf_file']), 'cloud')
    )
    ssh_username = salt.utils.cloud.ssh_usernames(vm_, cloud_opts)
    if isinstance(ssh_username, string_types):
        ret[tgt]['user'] = ssh_username
    elif isinstance(ssh_username, list):
        ret[tgt]['user'] = ssh_username[0]

    password = salt.config.get_cloud_config_value(
        'password', vm_, cloud_opts, search_global=False, default=None
    )
    if password:
        ret[tgt]['password'] = password

{'fraction_non_alphanumeric': 0.10883403198261139, 'fraction_numerical': 0.006520726595249185, 'mean_word_length': 3.2242622950819673, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '194367', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1949, 'n_words': 586}