Merge pull request #21147 from terminalmage/rename-lxc-cmd-funcs

Rename new lxc.cmd_run*, lxc.cmd_retcode functions
This commit is contained in:
Erik Johnson 2015-02-28 00:11:12 -06:00
commit 5cc7b61d8a
2 changed files with 121 additions and 123 deletions

View File

@ -420,15 +420,15 @@ New functions have been added to mimic the behavior of the functions in the
equivalents:
======================================= ====================================================== ===========================================================
======================================= ====================================================== ===================================================
Description :mod:`cmd <salt.modules.cmdmod>` module :mod:`lxc <salt.modules.lxc>` module
======================================= ====================================================== ===========================================================
Run a command and get all output :mod:`cmd.run <salt.modules.cmdmod.run>` :mod:`lxc.cmd_run <salt.modules.lxc.cmd_run>`
Run a command and get just stdout :mod:`cmd.run_stdout <salt.modules.cmdmod.run_stdout>` :mod:`lxc.cmd_run_stdout <salt.modules.lxc.cmd_run_stdout>`
Run a command and get just stderr :mod:`cmd.run_stderr <salt.modules.cmdmod.run_stderr>` :mod:`lxc.cmd_run_stderr <salt.modules.lxc.cmd_run_stderr>`
Run a command and get just the retcode :mod:`cmd.retcode <salt.modules.cmdmod.retcode>` :mod:`lxc.cmd_retcode <salt.modules.lxc.cmd_retcode>`
Run a command and get all information :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` :mod:`lxc.cmd_run_all <salt.modules.lxc.cmd_run_all>`
======================================= ====================================================== ===========================================================
======================================= ====================================================== ===================================================
Run a command and get all output :mod:`cmd.run <salt.modules.cmdmod.run>` :mod:`lxc.run <salt.modules.lxc.run>`
Run a command and get just stdout :mod:`cmd.run_stdout <salt.modules.cmdmod.run_stdout>` :mod:`lxc.run_stdout <salt.modules.lxc.run_stdout>`
Run a command and get just stderr :mod:`cmd.run_stderr <salt.modules.cmdmod.run_stderr>` :mod:`lxc.run_stderr <salt.modules.lxc.run_stderr>`
Run a command and get just the retcode :mod:`cmd.retcode <salt.modules.cmdmod.retcode>` :mod:`lxc.retcode <salt.modules.lxc.retcode>`
Run a command and get all information :mod:`cmd.run_all <salt.modules.cmdmod.run_all>` :mod:`lxc.run_all <salt.modules.lxc.run_all>`
======================================= ====================================================== ===================================================
2014.7.x and Earlier

View File

@ -1230,9 +1230,9 @@ def init(name,
if old_chunks != chunks:
to_reboot = True
if remove_seed_marker:
cmd_run(name,
'rm -f \'{0}\''.format(SEED_MARKER),
python_shell=False)
run(name,
'rm -f \'{0}\''.format(SEED_MARKER),
python_shell=False)
# last time to be sure any of our property is correctly applied
cfg = _LXCConfig(name=name, network_profile=network_profile,
@ -1271,9 +1271,9 @@ def init(name,
gids = [gid,
'/lxc.initial_pass',
'/.lxc.{0}.initial_pass'.format(name)]
if not any(cmd_retcode(name,
'test -e "{0}"'.format(x),
ignore_retcode=True) == 0
if not any(retcode(name,
'test -e "{0}"'.format(x),
ignore_retcode=True) == 0
for x in gids):
# think to touch the default user generated by default templates
# which has a really unsecure passwords...
@ -1281,10 +1281,10 @@ def init(name,
for default_user in ['ubuntu']:
if (
default_user not in users
and cmd_retcode(name,
'id {0}'.format(default_user),
python_shell=False,
ignore_retcode=True) == 0
and retcode(name,
'id {0}'.format(default_user),
python_shell=False,
ignore_retcode=True) == 0
):
users.append(default_user)
for user in users:
@ -1305,10 +1305,10 @@ def init(name,
log.debug(msg)
if ret.get('result', True):
changes.append({'password': 'Password(s) updated'})
if cmd_retcode(name,
('sh -c \'touch "{0}"; test -e "{0}"\''
.format(gid)),
ignore_retcode=True) != 0:
if retcode(name,
('sh -c \'touch "{0}"; test -e "{0}"\''
.format(gid)),
ignore_retcode=True) != 0:
ret['comment'] = 'Failed to set password marker'
changes[-1]['password'] += '. ' + ret['comment'] + '.'
ret['result'] = False
@ -1320,9 +1320,9 @@ def init(name,
gids = [gid,
'/lxc.initial_dns',
'/lxc.{0}.initial_dns'.format(name)]
if not any(cmd_retcode(name,
'test -e "{0}"'.format(x),
ignore_retcode=True) == 0
if not any(retcode(name,
'test -e "{0}"'.format(x),
ignore_retcode=True) == 0
for x in gids):
try:
set_dns(name,
@ -1333,10 +1333,10 @@ def init(name,
ret['result'] = False
else:
changes.append({'dns': 'DNS updated'})
if cmd_retcode(name,
('sh -c \'touch "{0}"; test -e "{0}"\''
.format(gid)),
ignore_retcode=True) != 0:
if retcode(name,
('sh -c \'touch "{0}"; test -e "{0}"\''
.format(gid)),
ignore_retcode=True) != 0:
ret['comment'] = 'Failed to set DNS marker'
changes[-1]['dns'] += '. ' + ret['comment'] + '.'
ret['result'] = False
@ -1345,9 +1345,9 @@ def init(name,
gid = '/.lxc.initial_seed'
gids = [gid, '/lxc.initial_seed']
if (
any(cmd_retcode(name,
'test -e {0}'.format(x),
ignore_retcode=True) == 0
any(retcode(name,
'test -e {0}'.format(x),
ignore_retcode=True) == 0
for x in gids)
or not ret.get('result', True)
):
@ -2327,20 +2327,20 @@ def info(name):
free = limit - usage
ret['memory_limit'] = limit
ret['memory_free'] = free
size = cmd_run_stdout(name, 'df /', python_shell=False)
size = run_stdout(name, 'df /', python_shell=False)
# The size is the 2nd column of the last line
ret['size'] = size.splitlines()[-1].split()[1]
# First try iproute2
ip_cmd = cmd_run_all(name, 'ip link show', python_shell=False)
ip_cmd = run_all(name, 'ip link show', python_shell=False)
if ip_cmd['retcode'] == 0:
ip_data = ip_cmd['stdout']
ip_cmd = cmd_run_all(name, 'ip addr show', python_shell=False)
ip_cmd = run_all(name, 'ip addr show', python_shell=False)
ip_data += '\n' + ip_cmd['stdout']
ip_data = salt.utils.network._interfaces_ip(ip_data)
else:
# That didn't work, try ifconfig
ip_cmd = cmd_run_all(name, 'ifconfig', python_shell=False)
ip_cmd = run_all(name, 'ifconfig', python_shell=False)
if ip_cmd['retcode'] == 0:
ip_data = \
salt.utils.network._interfaces_ifconfig(
@ -2429,11 +2429,11 @@ def set_password(name, users, password, encrypted=True):
failed_users = []
for user in users:
result = cmd_retcode(name,
'chpasswd{0}'.format(' -e' if encrypted else ''),
stdin=':'.join((user, password)),
python_shell=False,
output_loglevel='quiet')
result = retcode(name,
'chpasswd{0}'.format(' -e' if encrypted else ''),
stdin=':'.join((user, password)),
python_shell=False,
output_loglevel='quiet')
if result != 0:
failed_users.append(user)
if failed_users:
@ -2579,10 +2579,10 @@ def set_dns(name, dnsservers=None, searchdomains=None):
dns = ['nameserver {0}'.format(x) for x in dnsservers]
dns.extend(['search {0}'.format(x) for x in searchdomains])
dns = '\n'.join(dns) + '\n'
result = cmd_run_all(name,
'tee /etc/resolv.conf',
stdin=dns,
python_shell=False)
result = run_all(name,
'tee /etc/resolv.conf',
stdin=dns,
python_shell=False)
if result['retcode'] != 0:
error = ('Unable to write to /etc/resolv.conf in container \'{0}\''
.format(name))
@ -2594,17 +2594,17 @@ def set_dns(name, dnsservers=None, searchdomains=None):
def _need_install(name):
ret = 0
has_minion = cmd_retcode(name, "command -v salt-minion")
has_minion = retcode(name, "command -v salt-minion")
# we assume that installing is when no minion is running
# but testing the executable presence is not enougth for custom
# installs where the bootstrap can do much more than installing
# the bare salt binaries.
if has_minion:
processes = cmd_run_stdout(name, "ps aux")
processes = run_stdout(name, "ps aux")
if 'salt-minion' not in processes:
ret = 1
else:
cmd_retcode(name, "salt-call --local service.stop salt-minion")
retcode(name, "salt-call --local service.stop salt-minion")
else:
ret = 1
return ret
@ -2707,7 +2707,7 @@ def bootstrap(name,
needs_install = _need_install(name)
else:
needs_install = True
seeded = cmd_retcode(name, 'test -e \'{0}\''.format(SEED_MARKER)) == 0
seeded = retcode(name, 'test -e \'{0}\''.format(SEED_MARKER)) == 0
tmp = tempfile.mkdtemp()
if seeded and not unconditional_install:
ret = True
@ -2720,9 +2720,9 @@ def bootstrap(name,
if install:
rstr = __salt__['test.rand_str']()
configdir = '/tmp/.c_{0}'.format(rstr)
cmd_run(name,
'install -m 0700 -d {0}'.format(configdir),
python_shell=False)
run(name,
'install -m 0700 -d {0}'.format(configdir),
python_shell=False)
bs_ = __salt__['config.gather_bootstrap_script'](
bootstrap=bootstrap_url)
dest_dir = os.path.join('/tmp', rstr)
@ -2730,7 +2730,7 @@ def bootstrap(name,
'mkdir -p {0}'.format(dest_dir),
'chmod 700 {0}'.format(dest_dir),
]:
if cmd_run_stdout(name, cmd):
if run_stdout(name, cmd):
log.error(
('tmpdir {0} creation'
' failed ({1}').format(dest_dir, cmd))
@ -2753,7 +2753,7 @@ def bootstrap(name,
# out of the output in case of unexpected problem
log.info('Running {0} in LXC container \'{1}\''
.format(cmd, name))
ret = cmd_retcode(name, cmd, output_loglevel='info',
ret = retcode(name, cmd, output_loglevel='info',
use_vt=True) == 0
else:
ret = False
@ -2763,9 +2763,9 @@ def bootstrap(name,
cp(name, cfg_files['config'], '/etc/salt/minion')
cp(name, cfg_files['privkey'], os.path.join(pki_dir, 'minion.pem'))
cp(name, cfg_files['pubkey'], os.path.join(pki_dir, 'minion.pub'))
cmd_run(name,
'salt-call --local service.enable salt-minion',
python_shell=False)
run(name,
'salt-call --local service.enable salt-minion',
python_shell=False)
ret = True
shutil.rmtree(tmp)
if orig_state == 'stopped':
@ -2774,7 +2774,7 @@ def bootstrap(name,
freeze(name)
# mark seeded upon successful install
if ret:
cmd_run(name,
run(name,
'touch \'{0}\''.format(SEED_MARKER),
python_shell=False)
return ret
@ -2795,7 +2795,7 @@ def attachable(name):
return __context__['lxc.attachable']
except KeyError:
_ensure_exists(name)
# Can't use cmd_run() here because it uses attachable() and would
# Can't use run() here because it uses attachable() and would
# endlessly recurse, resulting in a traceback
cmd = 'lxc-attach --clear-env -n {0} -- /usr/bin/env'.format(name)
result = __salt__['cmd.retcode'](cmd, python_shell=False) == 0
@ -2815,7 +2815,7 @@ def _run(name,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
Common logic for lxc.cmd_run functions
Common logic for lxc.run functions
'''
_ensure_exists(name)
valid_output = ('stdout', 'stderr', 'retcode', 'all')
@ -2937,12 +2937,13 @@ def run_cmd(name,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. deprecated:: 2015.2.0
Use :mod:`lxc.cmd_run <salt.modules.lxc.cmd_run>` instead
Use :mod:`lxc.run <salt.modules.lxc.run>` instead
'''
salt.utils.warn_until(
'Boron',
'lxc.run_cmd has been deprecated, please use one of the lxc.cmd_run* '
'functions. See the documentation for more information.'
'lxc.run_cmd has been deprecated, please use one of the lxc.run '
'functions (or lxc.retcode). See the documentation for more '
'information.'
)
if stdout and stderr:
output = 'all'
@ -2965,16 +2966,16 @@ def run_cmd(name,
keep_env=keep_env)
def cmd_run(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
def run(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2015.2.0
@ -2991,8 +2992,8 @@ def cmd_run(name,
The same error will be displayed in stderr if the command being run
does not exist. If no output is returned using this function, try using
:mod:`lxc.cmd_run_stderr <salt.modules.lxc.cmd_run_stderr>` or
:mod:`lxc.cmd_run_all <salt.modules.lxc.cmd_run_all>`.
:mod:`lxc.run_stderr <salt.modules.lxc.run_stderr>` or
:mod:`lxc.run_all <salt.modules.lxc.run_all>`.
name
Name of the container in which to run the command
@ -3025,7 +3026,7 @@ def cmd_run(name,
.. code-block:: bash
salt myminion lxc.cmd_run mycontainer 'ifconfig -a'
salt myminion lxc.run mycontainer 'ifconfig -a'
'''
return _run(name,
cmd,
@ -3040,16 +3041,16 @@ def cmd_run(name,
keep_env=keep_env)
def cmd_run_stdout(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
def run_stdout(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2015.2.0
@ -3066,8 +3067,8 @@ def cmd_run_stdout(name,
The same error will be displayed in stderr if the command being run
does not exist. If no output is returned using this function, try using
:mod:`lxc.cmd_run_stderr <salt.modules.lxc.cmd_run_stderr>` or
:mod:`lxc.cmd_run_all <salt.modules.lxc.cmd_run_all>`.
:mod:`lxc.run_stderr <salt.modules.lxc.run_stderr>` or
:mod:`lxc.run_all <salt.modules.lxc.run_all>`.
name
Name of the container in which to run the command
@ -3100,7 +3101,7 @@ def cmd_run_stdout(name,
.. code-block:: bash
salt myminion lxc.cmd_run_stdout mycontainer 'ifconfig -a'
salt myminion lxc.run_stdout mycontainer 'ifconfig -a'
'''
return _run(name,
cmd,
@ -3115,16 +3116,16 @@ def cmd_run_stdout(name,
keep_env=keep_env)
def cmd_run_stderr(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
def run_stderr(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2015.2.0
@ -3173,7 +3174,7 @@ def cmd_run_stderr(name,
.. code-block:: bash
salt myminion lxc.cmd_run_stderr mycontainer 'ip addr show'
salt myminion lxc.run_stderr mycontainer 'ip addr show'
'''
return _run(name,
cmd,
@ -3188,7 +3189,7 @@ def cmd_run_stderr(name,
keep_env=keep_env)
def cmd_retcode(name,
def retcode(name,
cmd,
no_start=False,
preserve_state=True,
@ -3214,8 +3215,8 @@ def cmd_retcode(name,
The same error will be displayed in stderr if the command being run
does not exist. If the retcode is nonzero and not what was expected,
try using :mod:`lxc.cmd_run_stderr <salt.modules.lxc.cmd_run_stderr>`
or :mod:`lxc.cmd_run_all <salt.modules.lxc.cmd_run_all>`.
try using :mod:`lxc.run_stderr <salt.modules.lxc.run_stderr>`
or :mod:`lxc.run_all <salt.modules.lxc.run_all>`.
name
Name of the container in which to run the command
@ -3248,7 +3249,7 @@ def cmd_retcode(name,
.. code-block:: bash
salt myminion lxc.cmd_retcode mycontainer 'ip addr show'
salt myminion lxc.retcode mycontainer 'ip addr show'
'''
return _run(name,
cmd,
@ -3263,16 +3264,16 @@ def cmd_retcode(name,
keep_env=keep_env)
def cmd_run_all(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
def run_all(name,
cmd,
no_start=False,
preserve_state=True,
stdin=None,
python_shell=True,
output_loglevel='debug',
use_vt=False,
ignore_retcode=False,
keep_env='http_proxy,https_proxy,no_proxy'):
'''
.. versionadded:: 2015.2.0
@ -3321,7 +3322,7 @@ def cmd_run_all(name,
.. code-block:: bash
salt myminion lxc.cmd_run_all mycontainer 'ip addr show'
salt myminion lxc.run_all mycontainer 'ip addr show'
'''
return _run(name,
cmd,
@ -3340,9 +3341,7 @@ def _get_md5(name, path):
'''
Get the MD5 checksum of a file from a container
'''
output = cmd_run_stdout(name,
'md5sum "{0}"'.format(path),
ignore_retcode=True)
output = run_stdout(name, 'md5sum "{0}"'.format(path), ignore_retcode=True)
try:
return output.split()[0]
except IndexError:
@ -3408,7 +3407,7 @@ def cp(name, source, dest, makedirs=False):
# Destination file sanity checks
if not os.path.isabs(dest):
raise SaltInvocationError('Destination path must be absolute')
if cmd_retcode(name,
if retcode(name,
'test -d \'{0}\''.format(dest),
ignore_retcode=True) == 0:
# Destination is a directory, full path to dest file will include the
@ -3419,12 +3418,11 @@ def cp(name, source, dest, makedirs=False):
# dir is a directory, and then (if makedirs=True) attempt to create the
# parent directory.
dest_dir, dest_name = os.path.split(dest)
if cmd_retcode(name,
if retcode(name,
'test -d \'{0}\''.format(dest_dir),
ignore_retcode=True) != 0:
if makedirs:
result = cmd_run_all(name,
'mkdir -p \'{0}\''.format(dest_dir))
result = run_all(name, 'mkdir -p \'{0}\''.format(dest_dir))
if result['retcode'] != 0:
error = ('Unable to create destination directory {0} in '
'container \'{1}\''.format(dest_dir, name))
@ -3441,7 +3439,7 @@ def cp(name, source, dest, makedirs=False):
source_md5 = __salt__['file.get_sum'](source, 'md5')
if source_md5 != _get_md5(name, dest):
# Using cat here instead of opening the file, reading it into memory,
# and passing it as stdin to cmd_run(). This will keep down memory
# and passing it as stdin to run(). This will keep down memory
# usage for the minion and make the operation run quicker.
__salt__['cmd.run_stdout'](
'cat "{0}" | lxc-attach --clear-env --set-var {1} -n {2} -- '