Fix state.sls_id in salt-ssh

Additionally, update the other funcs to pull the SLS opts from the new
salt.uilts.state.get_sls_opts().
This commit is contained in:
Erik Johnson 2017-12-18 16:09:30 -06:00
parent 15296e59a5
commit a88f1cb80e
No known key found for this signature in database
GPG Key ID: 5E5583C437808F3F

View File

@ -7,7 +7,6 @@ from __future__ import absolute_import, print_function, unicode_literals
# Import python libs # Import python libs
import os import os
import time import time
import copy
import json import json
import logging import logging
@ -101,8 +100,9 @@ def sls(mods, saltenv='base', test=None, exclude=None, **kwargs):
st_kwargs = __salt__.kwargs st_kwargs = __salt__.kwargs
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -133,37 +133,37 @@ def sls(mods, saltenv='base', test=None, exclude=None, **kwargs):
chunks, chunks,
_merge_extra_filerefs( _merge_extra_filerefs(
kwargs.get('extra_filerefs', ''), kwargs.get('extra_filerefs', ''),
__opts__.get('extra_filerefs', '') opts.get('extra_filerefs', '')
) )
) )
roster = salt.roster.Roster(__opts__, __opts__.get('roster', 'flat')) roster = salt.roster.Roster(opts, opts.get('roster', 'flat'))
roster_grains = roster.opts['grains'] roster_grains = roster.opts['grains']
# Create the tar containing the state pkg and relevant files. # Create the tar containing the state pkg and relevant files.
trans_tar = salt.client.ssh.state.prep_trans_tar( trans_tar = salt.client.ssh.state.prep_trans_tar(
__opts__, opts,
__context__['fileclient'], __context__['fileclient'],
chunks, chunks,
file_refs, file_refs,
__pillar__, __pillar__,
st_kwargs['id_'], st_kwargs['id_'],
roster_grains) roster_grains)
trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__['hash_type']) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts['hash_type'])
cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format( cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format(
__opts__['thin_dir'], opts['thin_dir'],
test, test,
trans_tar_sum, trans_tar_sum,
__opts__['hash_type']) opts['hash_type'])
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
__opts__, opts,
cmd, cmd,
fsclient=__context__['fileclient'], fsclient=__context__['fileclient'],
minion_opts=__salt__.minion_opts, minion_opts=__salt__.minion_opts,
**st_kwargs) **st_kwargs)
single.shell.send( single.shell.send(
trans_tar, trans_tar,
'{0}/salt_state.tgz'.format(__opts__['thin_dir'])) '{0}/salt_state.tgz'.format(opts['thin_dir']))
stdout, stderr, _ = single.cmd_block() stdout, stderr, _ = single.cmd_block()
# Clean up our tar # Clean up our tar
@ -245,32 +245,6 @@ def _check_queue(queue, kwargs):
return conflict return conflict
def _get_opts(**kwargs):
'''
Return a copy of the opts for use, optionally load a local config on top
'''
opts = copy.deepcopy(__opts__)
if 'localconfig' in kwargs:
return salt.config.minion_config(kwargs['localconfig'], defaults=opts)
if 'saltenv' in kwargs:
saltenv = kwargs['saltenv']
if saltenv is not None and not isinstance(saltenv, six.string_types):
opts['environment'] = str(kwargs['saltenv'])
else:
opts['environment'] = kwargs['saltenv']
if 'pillarenv' in kwargs:
pillarenv = kwargs['pillarenv']
if pillarenv is not None and not isinstance(pillarenv, six.string_types):
opts['pillarenv'] = str(kwargs['pillarenv'])
else:
opts['pillarenv'] = kwargs['pillarenv']
return opts
def _get_initial_pillar(opts): def _get_initial_pillar(opts):
return __pillar__ if __opts__['__cli'] == 'salt-call' \ return __pillar__ if __opts__['__cli'] == 'salt-call' \
and opts['pillarenv'] == __opts__['pillarenv'] \ and opts['pillarenv'] == __opts__['pillarenv'] \
@ -382,8 +356,9 @@ def high(data, **kwargs):
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
st_kwargs = __salt__.kwargs st_kwargs = __salt__.kwargs
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -392,36 +367,36 @@ def high(data, **kwargs):
chunks, chunks,
_merge_extra_filerefs( _merge_extra_filerefs(
kwargs.get('extra_filerefs', ''), kwargs.get('extra_filerefs', ''),
__opts__.get('extra_filerefs', '') opts.get('extra_filerefs', '')
) )
) )
roster = salt.roster.Roster(__opts__, __opts__.get('roster', 'flat')) roster = salt.roster.Roster(opts, opts.get('roster', 'flat'))
roster_grains = roster.opts['grains'] roster_grains = roster.opts['grains']
# Create the tar containing the state pkg and relevant files. # Create the tar containing the state pkg and relevant files.
trans_tar = salt.client.ssh.state.prep_trans_tar( trans_tar = salt.client.ssh.state.prep_trans_tar(
__opts__, opts,
__context__['fileclient'], __context__['fileclient'],
chunks, chunks,
file_refs, file_refs,
__pillar__, __pillar__,
st_kwargs['id_'], st_kwargs['id_'],
roster_grains) roster_grains)
trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__['hash_type']) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts['hash_type'])
cmd = 'state.pkg {0}/salt_state.tgz pkg_sum={1} hash_type={2}'.format( cmd = 'state.pkg {0}/salt_state.tgz pkg_sum={1} hash_type={2}'.format(
__opts__['thin_dir'], opts['thin_dir'],
trans_tar_sum, trans_tar_sum,
__opts__['hash_type']) opts['hash_type'])
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
__opts__, opts,
cmd, cmd,
fsclient=__context__['fileclient'], fsclient=__context__['fileclient'],
minion_opts=__salt__.minion_opts, minion_opts=__salt__.minion_opts,
**st_kwargs) **st_kwargs)
single.shell.send( single.shell.send(
trans_tar, trans_tar,
'{0}/salt_state.tgz'.format(__opts__['thin_dir'])) '{0}/salt_state.tgz'.format(opts['thin_dir']))
stdout, stderr, _ = single.cmd_block() stdout, stderr, _ = single.cmd_block()
# Clean up our tar # Clean up our tar
@ -618,9 +593,9 @@ def highstate(test=None, **kwargs):
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
st_kwargs = __salt__.kwargs st_kwargs = __salt__.kwargs
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -629,7 +604,7 @@ def highstate(test=None, **kwargs):
chunks, chunks,
_merge_extra_filerefs( _merge_extra_filerefs(
kwargs.get('extra_filerefs', ''), kwargs.get('extra_filerefs', ''),
__opts__.get('extra_filerefs', '') opts.get('extra_filerefs', '')
) )
) )
# Check for errors # Check for errors
@ -638,33 +613,33 @@ def highstate(test=None, **kwargs):
__context__['retcode'] = 1 __context__['retcode'] = 1
return chunks return chunks
roster = salt.roster.Roster(__opts__, __opts__.get('roster', 'flat')) roster = salt.roster.Roster(opts, opts.get('roster', 'flat'))
roster_grains = roster.opts['grains'] roster_grains = roster.opts['grains']
# Create the tar containing the state pkg and relevant files. # Create the tar containing the state pkg and relevant files.
trans_tar = salt.client.ssh.state.prep_trans_tar( trans_tar = salt.client.ssh.state.prep_trans_tar(
__opts__, opts,
__context__['fileclient'], __context__['fileclient'],
chunks, chunks,
file_refs, file_refs,
__pillar__, __pillar__,
st_kwargs['id_'], st_kwargs['id_'],
roster_grains) roster_grains)
trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__['hash_type']) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts['hash_type'])
cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format( cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format(
__opts__['thin_dir'], opts['thin_dir'],
test, test,
trans_tar_sum, trans_tar_sum,
__opts__['hash_type']) opts['hash_type'])
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
__opts__, opts,
cmd, cmd,
fsclient=__context__['fileclient'], fsclient=__context__['fileclient'],
minion_opts=__salt__.minion_opts, minion_opts=__salt__.minion_opts,
**st_kwargs) **st_kwargs)
single.shell.send( single.shell.send(
trans_tar, trans_tar,
'{0}/salt_state.tgz'.format(__opts__['thin_dir'])) '{0}/salt_state.tgz'.format(opts['thin_dir']))
stdout, stderr, _ = single.cmd_block() stdout, stderr, _ = single.cmd_block()
# Clean up our tar # Clean up our tar
@ -699,12 +674,13 @@ def top(topfn, test=None, **kwargs):
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
st_kwargs = __salt__.kwargs st_kwargs = __salt__.kwargs
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
if salt.utils.args.test_mode(test=test, **kwargs): if salt.utils.args.test_mode(test=test, **kwargs):
__opts__['test'] = True opts['test'] = True
else: else:
__opts__['test'] = __opts__.get('test', None) opts['test'] = __opts__.get('test', None)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -714,37 +690,37 @@ def top(topfn, test=None, **kwargs):
chunks, chunks,
_merge_extra_filerefs( _merge_extra_filerefs(
kwargs.get('extra_filerefs', ''), kwargs.get('extra_filerefs', ''),
__opts__.get('extra_filerefs', '') opts.get('extra_filerefs', '')
) )
) )
roster = salt.roster.Roster(__opts__, __opts__.get('roster', 'flat')) roster = salt.roster.Roster(opts, opts.get('roster', 'flat'))
roster_grains = roster.opts['grains'] roster_grains = roster.opts['grains']
# Create the tar containing the state pkg and relevant files. # Create the tar containing the state pkg and relevant files.
trans_tar = salt.client.ssh.state.prep_trans_tar( trans_tar = salt.client.ssh.state.prep_trans_tar(
__opts__, opts,
__context__['fileclient'], __context__['fileclient'],
chunks, chunks,
file_refs, file_refs,
__pillar__, __pillar__,
st_kwargs['id_'], st_kwargs['id_'],
roster_grains) roster_grains)
trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__['hash_type']) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts['hash_type'])
cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format( cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format(
__opts__['thin_dir'], opts['thin_dir'],
test, test,
trans_tar_sum, trans_tar_sum,
__opts__['hash_type']) opts['hash_type'])
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
__opts__, opts,
cmd, cmd,
fsclient=__context__['fileclient'], fsclient=__context__['fileclient'],
minion_opts=__salt__.minion_opts, minion_opts=__salt__.minion_opts,
**st_kwargs) **st_kwargs)
single.shell.send( single.shell.send(
trans_tar, trans_tar,
'{0}/salt_state.tgz'.format(__opts__['thin_dir'])) '{0}/salt_state.tgz'.format(opts['thin_dir']))
stdout, stderr, _ = single.cmd_block() stdout, stderr, _ = single.cmd_block()
# Clean up our tar # Clean up our tar
@ -764,7 +740,7 @@ def top(topfn, test=None, **kwargs):
return stdout return stdout
def show_highstate(): def show_highstate(**kwargs):
''' '''
Retrieve the highstate data from the salt master and display it Retrieve the highstate data from the salt master and display it
@ -775,15 +751,16 @@ def show_highstate():
salt '*' state.show_highstate salt '*' state.show_highstate
''' '''
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
return st_.compile_highstate() return st_.compile_highstate()
def show_lowstate(): def show_lowstate(**kwargs):
''' '''
List out the low data that will be applied to this minion List out the low data that will be applied to this minion
@ -794,8 +771,9 @@ def show_lowstate():
salt '*' state.show_lowstate salt '*' state.show_lowstate
''' '''
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -838,13 +816,13 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs):
if conflict is not None: if conflict is not None:
return conflict return conflict
orig_test = __opts__.get('test', None) orig_test = __opts__.get('test', None)
opts = _get_opts(**kwargs) opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
opts['test'] = _get_test_value(test, **kwargs) opts['test'] = _get_test_value(test, **kwargs)
# Since this is running a specific ID within a specific SLS file, fall back # Since this is running a specific ID within a specific SLS file, fall back
# to the 'base' saltenv if none is configured and none was passed. # to the 'base' saltenv if none is configured and none was passed.
if opts['environment'] is None: if opts['saltenv'] is None:
opts['environment'] = 'base' opts['saltenv'] = 'base'
try: try:
st_ = salt.state.HighState(opts, st_ = salt.state.HighState(opts,
@ -864,7 +842,7 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs):
split_mods = mods.split(',') split_mods = mods.split(',')
st_.push_active() st_.push_active()
try: try:
high_, errors = st_.render_highstate({opts['environment']: split_mods}) high_, errors = st_.render_highstate({opts['saltenv']: split_mods})
finally: finally:
st_.pop_active() st_.pop_active()
errors += st_.state.verify_high(high_) errors += st_.state.verify_high(high_)
@ -884,13 +862,10 @@ def sls_id(id_, mods, test=None, queue=False, **kwargs):
ret.update(st_.state.call_chunk(chunk, {}, chunks)) ret.update(st_.state.call_chunk(chunk, {}, chunks))
_set_retcode(ret, highstate=highstate) _set_retcode(ret, highstate=highstate)
# Work around Windows multiprocessing bug, set __opts__['test'] back to
# value from before this function was run.
__opts__['test'] = orig_test
if not ret: if not ret:
raise SaltInvocationError( raise SaltInvocationError(
'No matches for ID \'{0}\' found in SLS \'{1}\' within saltenv ' 'No matches for ID \'{0}\' found in SLS \'{1}\' within saltenv '
'\'{2}\''.format(id_, mods, opts['environment']) '\'{2}\''.format(id_, mods, opts['saltenv'])
) )
return ret return ret
@ -908,13 +883,13 @@ def show_sls(mods, saltenv='base', test=None, **kwargs):
''' '''
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = copy.copy(__opts__) opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
if salt.utils.args.test_mode(test=test, **kwargs): if salt.utils.args.test_mode(test=test, **kwargs):
opts['test'] = True opts['test'] = True
else: else:
opts['test'] = __opts__.get('test', None) opts['test'] = __opts__.get('test', None)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -951,13 +926,13 @@ def show_low_sls(mods, saltenv='base', test=None, **kwargs):
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = copy.copy(__opts__) opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
if salt.utils.args.test_mode(test=test, **kwargs): if salt.utils.args.test_mode(test=test, **kwargs):
opts['test'] = True opts['test'] = True
else: else:
opts['test'] = __opts__.get('test', None) opts['test'] = __opts__.get('test', None)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -979,7 +954,7 @@ def show_low_sls(mods, saltenv='base', test=None, **kwargs):
return ret return ret
def show_top(): def show_top(**kwargs):
''' '''
Return the top data that the minion will use for a highstate Return the top data that the minion will use for a highstate
@ -990,8 +965,9 @@ def show_top():
salt '*' state.show_top salt '*' state.show_top
''' '''
__opts__['grains'] = __grains__ __opts__['grains'] = __grains__
opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
st_ = salt.client.ssh.state.SSHHighState( st_ = salt.client.ssh.state.SSHHighState(
__opts__, opts,
__pillar__, __pillar__,
__salt__, __salt__,
__context__['fileclient']) __context__['fileclient'])
@ -1038,7 +1014,7 @@ def single(fun, name, test=None, **kwargs):
'__id__': name, '__id__': name,
'name': name}) 'name': name})
opts = copy.deepcopy(__opts__) opts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
# Set test mode # Set test mode
if salt.utils.args.test_mode(test=test, **kwargs): if salt.utils.args.test_mode(test=test, **kwargs):
@ -1050,7 +1026,7 @@ def single(fun, name, test=None, **kwargs):
__pillar__.update(kwargs.get('pillar', {})) __pillar__.update(kwargs.get('pillar', {}))
# Create the State environment # Create the State environment
st_ = salt.client.ssh.state.SSHState(__opts__, __pillar__) st_ = salt.client.ssh.state.SSHState(opts, __pillar__)
# Verify the low chunk # Verify the low chunk
err = st_.verify_data(kwargs) err = st_.verify_data(kwargs)
@ -1067,16 +1043,16 @@ def single(fun, name, test=None, **kwargs):
chunks, chunks,
_merge_extra_filerefs( _merge_extra_filerefs(
kwargs.get('extra_filerefs', ''), kwargs.get('extra_filerefs', ''),
__opts__.get('extra_filerefs', '') opts.get('extra_filerefs', '')
) )
) )
roster = salt.roster.Roster(__opts__, __opts__.get('roster', 'flat')) roster = salt.roster.Roster(opts, opts.get('roster', 'flat'))
roster_grains = roster.opts['grains'] roster_grains = roster.opts['grains']
# Create the tar containing the state pkg and relevant files. # Create the tar containing the state pkg and relevant files.
trans_tar = salt.client.ssh.state.prep_trans_tar( trans_tar = salt.client.ssh.state.prep_trans_tar(
__opts__, opts,
__context__['fileclient'], __context__['fileclient'],
chunks, chunks,
file_refs, file_refs,
@ -1085,18 +1061,18 @@ def single(fun, name, test=None, **kwargs):
roster_grains) roster_grains)
# Create a hash so we can verify the tar on the target system # Create a hash so we can verify the tar on the target system
trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, __opts__['hash_type']) trans_tar_sum = salt.utils.hashutils.get_hash(trans_tar, opts['hash_type'])
# We use state.pkg to execute the "state package" # We use state.pkg to execute the "state package"
cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format( cmd = 'state.pkg {0}/salt_state.tgz test={1} pkg_sum={2} hash_type={3}'.format(
__opts__['thin_dir'], opts['thin_dir'],
test, test,
trans_tar_sum, trans_tar_sum,
__opts__['hash_type']) opts['hash_type'])
# Create a salt-ssh Single object to actually do the ssh work # Create a salt-ssh Single object to actually do the ssh work
single = salt.client.ssh.Single( single = salt.client.ssh.Single(
__opts__, opts,
cmd, cmd,
fsclient=__context__['fileclient'], fsclient=__context__['fileclient'],
minion_opts=__salt__.minion_opts, minion_opts=__salt__.minion_opts,
@ -1105,7 +1081,7 @@ def single(fun, name, test=None, **kwargs):
# Copy the tar down # Copy the tar down
single.shell.send( single.shell.send(
trans_tar, trans_tar,
'{0}/salt_state.tgz'.format(__opts__['thin_dir'])) '{0}/salt_state.tgz'.format(opts['thin_dir']))
# Run the state.pkg command on the target # Run the state.pkg command on the target
stdout, stderr, _ = single.cmd_block() stdout, stderr, _ = single.cmd_block()