Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud roster fixes #40201

Merged
merged 6 commits into from Mar 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 26 additions & 17 deletions salt/roster/cloud.py
Expand Up @@ -50,27 +50,34 @@ def targets(tgt, tgt_type='glob', **kwargs): # pylint: disable=W0613
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)
not_actioned = info.get('Not Actioned/Not Running')
if not_actioned and tgt in not_actioned:
return {}

indexed_minion = cache_data.get(tgt, None)

if indexed_minion:
provider = indexed_minion.get('provider', None)
driver = indexed_minion.get('driver', None)
profile = indexed_minion.get('profile', None)
else:
provider = next(iter(info))
driver = next(iter(info[provider]))
profile = None

vm_ = {
'provider': provider,
'profile': profile,
}

full_info = info.get(provider, {}).get(driver, {}).get(tgt, {}).get(tgt, {})
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 = []
Expand All @@ -85,34 +92,36 @@ def targets(tgt, tgt_type='glob', **kwargs): # pylint: disable=W0613
))
preferred_ip = extract_ipv4(roster_order, ip_list)

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

cloud_opts = salt.config.cloud_config('/etc/salt/cloud')
ssh_username = salt.utils.cloud.ssh_usernames({}, cloud_opts)
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
ret[tgt]['user'] = ssh_username
elif isinstance(ssh_username, list):
ret['tgt']['user'] = ssh_username[0]
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
ret[tgt]['password'] = password

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

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

return ret

Expand Down