Skip to content

Commit

Permalink
Merge pull request saltstack#41390 from rallytime/merge-nitrogen
Browse files Browse the repository at this point in the history
[nitrogen] Merge forward from 2016.11 to nitrogen
  • Loading branch information
Nicole Thomas authored May 24, 2017
2 parents 78078cb + 537fd76 commit 460af48
Show file tree
Hide file tree
Showing 17 changed files with 804 additions and 51 deletions.
66 changes: 57 additions & 9 deletions salt/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,13 +1602,10 @@ def _gather_buffer_space():


# ----- Salt Proxy Minion Configuration Defaults ----------------------------------->
# Note DEFAULT_MINION_OPTS
# is loaded first, then if we are setting up a proxy, the config is overwritten with
# these settings.
# These are merged with DEFAULT_MINION_OPTS since many of them also apply here.
DEFAULT_PROXY_MINION_OPTS = {
'conf_file': os.path.join(salt.syspaths.CONFIG_DIR, 'proxy'),
'log_file': os.path.join(salt.syspaths.LOGS_DIR, 'proxy'),
'sign_pub_messages': False,
'add_proxymodule_to_opts': False,
'proxy_merge_grains_in_module': True,
'append_minionid_config_dirs': ['cachedir', 'pidfile', 'default_include'],
Expand All @@ -1621,9 +1618,11 @@ def _gather_buffer_space():
'proxy_always_alive': True,

'proxy_keep_alive': True, # by default will try to keep alive the connection
'proxy_keep_alive_interval': 1 # frequency of the proxy keepalive in minutes
'proxy_keep_alive_interval': 1, # frequency of the proxy keepalive in minutes
'pki_dir': os.path.join(salt.syspaths.CONFIG_DIR, 'pki', 'proxy'),
'cachedir': os.path.join(salt.syspaths.CACHE_DIR, 'proxy'),
'sock_dir': os.path.join(salt.syspaths.SOCK_DIR, 'proxy'),
}

# ----- Salt Cloud Configuration Defaults ----------------------------------->
DEFAULT_CLOUD_OPTS = {
'verify_env': True,
Expand Down Expand Up @@ -2113,9 +2112,6 @@ def minion_config(path,
if defaults is None:
defaults = DEFAULT_MINION_OPTS.copy()

if path is not None and path.endswith('proxy'):
defaults.update(DEFAULT_PROXY_MINION_OPTS)

if not os.environ.get(env_var, None):
# No valid setting was given using the configuration variable.
# Lets see is SALT_CONFIG_DIR is of any use
Expand Down Expand Up @@ -2145,6 +2141,58 @@ def minion_config(path,
return opts


def proxy_config(path,
env_var='SALT_PROXY_CONFIG',
defaults=None,
cache_minion_id=False,
ignore_config_errors=True,
minion_id=None):
'''
Reads in the proxy minion configuration file and sets up special options
This is useful for Minion-side operations, such as the
:py:class:`~salt.client.Caller` class, and manually running the loader
interface.
.. code-block:: python
import salt.config
proxy_opts = salt.config.proxy_config('/etc/salt/proxy')
'''
if defaults is None:
defaults = DEFAULT_MINION_OPTS.copy()

defaults.update(DEFAULT_PROXY_MINION_OPTS)

if not os.environ.get(env_var, None):
# No valid setting was given using the configuration variable.
# Lets see is SALT_CONFIG_DIR is of any use
salt_config_dir = os.environ.get('SALT_CONFIG_DIR', None)
if salt_config_dir:
env_config_file_path = os.path.join(salt_config_dir, 'proxy')
if salt_config_dir and os.path.isfile(env_config_file_path):
# We can get a configuration file using SALT_CONFIG_DIR, let's
# update the environment with this information
os.environ[env_var] = env_config_file_path

overrides = load_config(path, env_var, DEFAULT_PROXY_MINION_OPTS['conf_file'])
default_include = overrides.get('default_include',
defaults['default_include'])
include = overrides.get('include', [])

overrides.update(include_config(default_include, path, verbose=False,
exit_on_config_errors=not ignore_config_errors))
overrides.update(include_config(include, path, verbose=True,
exit_on_config_errors=not ignore_config_errors))

opts = apply_minion_config(overrides, defaults,
cache_minion_id=cache_minion_id,
minion_id=minion_id)
apply_sdb(opts)
_validate_opts(opts)
return opts


def syndic_config(master_config_path,
minion_config_path,
master_env_var='SALT_MASTER_CONFIG',
Expand Down
46 changes: 20 additions & 26 deletions salt/modules/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import re
import shlex
import stat
import subprocess
import tarfile
import tempfile
import zipfile
try:
from shlex import quote as _quote # pylint: disable=E0611
Expand Down Expand Up @@ -164,7 +164,10 @@ def _list_tar(name, cached, decompress_cmd, failhard=False):
files = []
links = []
try:
with contextlib.closing(tarfile.open(cached)) as tar_archive:
open_kwargs = {'name': cached} \
if not isinstance(cached, subprocess.Popen) \
else {'fileobj': cached.stdout, 'mode': 'r|'}
with contextlib.closing(tarfile.open(**open_kwargs)) as tar_archive:
for member in tar_archive.getmembers():
if member.issym():
links.append(member.name)
Expand All @@ -175,7 +178,15 @@ def _list_tar(name, cached, decompress_cmd, failhard=False):
return dirs, files, links

except tarfile.ReadError:
if not failhard:
if failhard:
if isinstance(cached, subprocess.Popen):
stderr = cached.communicate()[1]
if cached.returncode != 0:
raise CommandExecutionError(
'Failed to decompress {0}'.format(name),
info={'error': stderr}
)
else:
if not salt.utils.which('tar'):
raise CommandExecutionError('\'tar\' command not available')
if decompress_cmd is not None:
Expand All @@ -194,29 +205,12 @@ def _list_tar(name, cached, decompress_cmd, failhard=False):
decompress_cmd = 'xz --decompress --stdout'

if decompress_cmd:
fd, decompressed = tempfile.mkstemp()
os.close(fd)
try:
cmd = '{0} {1} > {2}'.format(decompress_cmd,
_quote(cached),
_quote(decompressed))
result = __salt__['cmd.run_all'](cmd, python_shell=True)
if result['retcode'] != 0:
raise CommandExecutionError(
'Failed to decompress {0}'.format(name),
info={'error': result['stderr']}
)
return _list_tar(name, decompressed, None, True)
finally:
try:
os.remove(decompressed)
except OSError as exc:
if exc.errno != errno.ENOENT:
log.warning(
'Failed to remove intermediate '
'decompressed archive %s: %s',
decompressed, exc.__str__()
)
decompressed = subprocess.Popen(
'{0} {1}'.format(decompress_cmd, _quote(cached)),
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return _list_tar(name, decompressed, None, True)

raise CommandExecutionError(
'Unable to list contents of {0}. If this is an XZ-compressed tar '
Expand Down
94 changes: 94 additions & 0 deletions salt/modules/dummyproxy_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
'''
Package support for the dummy proxy used by the test suite
'''
from __future__ import absolute_import

# Import python libs
import logging
import salt.utils


log = logging.getLogger(__name__)

# Define the module's virtual name
__virtualname__ = 'pkg'


def __virtual__():
'''
Only work on systems that are a proxy minion
'''
try:
if salt.utils.is_proxy() and __opts__['proxy']['proxytype'] == 'dummy':
return __virtualname__
except KeyError:
return (False, 'The dummyproxy_package execution module failed to load. Check the proxy key in pillar or /etc/salt/proxy.')

return (False, 'The dummyproxy_package execution module failed to load: only works on a dummy proxy minion.')


def list_pkgs(versions_as_list=False, **kwargs):
return __proxy__['dummy.package_list']()


def install(name=None, refresh=False, fromrepo=None,
pkgs=None, sources=None, **kwargs):
return __proxy__['dummy.package_install'](name, **kwargs)


def remove(name=None, pkgs=None, **kwargs):
return __proxy__['dummy.package_remove'](name)


def version(*names, **kwargs):
'''
Returns a string representing the package version or an empty string if not
installed. If more than one package name is specified, a dict of
name/version pairs is returned.
CLI Example:
.. code-block:: bash
salt '*' pkg.version <package name>
salt '*' pkg.version <package1> <package2> <package3> ...
'''
if len(names) == 1:
vers = __proxy__['dummy.package_status'](names[0])
return vers[names[0]]
else:
results = {}
for n in names:
vers = __proxy__['dummy.package_status'](n)
results.update(vers)
return results


def upgrade(name=None, pkgs=None, refresh=True, skip_verify=True,
normalize=True, **kwargs):
old = __proxy__['dummy.package_list']()
new = __proxy__['dummy.uptodate']()
pkg_installed = __proxy__['dummy.upgrade']()
ret = salt.utils.compare_dicts(old, pkg_installed)
return ret


def installed(name,
version=None,
refresh=False,
fromrepo=None,
skip_verify=False,
pkgs=None,
sources=None,
**kwargs):

p = __proxy__['dummy.package_status'](name)
if version is None:
if 'ret' in p:
return str(p['ret'])
else:
return True
else:
if p is not None:
return version == str(p)
Loading

0 comments on commit 460af48

Please sign in to comment.