From da6d692e4e0559259bb860f639772abdf0bf3cf3 Mon Sep 17 00:00:00 2001 From: Mathieu Le Marec - Pasquet Date: Mon, 8 Sep 2014 14:33:56 +0200 Subject: [PATCH] Fix interactivity in aptpkg --- salt/modules/aptpkg.py | 36 ++++++++++++++++++++++++------------ salt/modules/cmdmod.py | 15 +++++++++++---- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/salt/modules/aptpkg.py b/salt/modules/aptpkg.py index 0429e80297..ebf913ce32 100644 --- a/salt/modules/aptpkg.py +++ b/salt/modules/aptpkg.py @@ -20,6 +20,7 @@ import json import yaml # Import salt libs +from salt.modules.cmdmod import _parse_env import salt.utils from salt._compat import string_types from salt.exceptions import ( @@ -51,6 +52,12 @@ LP_PVT_SRC_FORMAT = 'deb https://{0}private-ppa.launchpad.net/{1}/{2}/ubuntu' \ _MODIFY_OK = frozenset(['uri', 'comps', 'architectures', 'disabled', 'file', 'dist']) +DPKG_ENV_VARS = { + 'APT_LISTBUGS_FRONTEND': 'none', + 'APT_LISTCHANGES_FRONTEND': 'none', + 'DEBIAN_FRONTEND': 'noninteractive', + 'UCF_FORCE_CONFFOLD': '1', +} # Define the module's virtual name __virtualname__ = 'pkg' @@ -72,14 +79,8 @@ def __init__(): non-interactive. ''' if __virtual__(): - env_vars = { - 'APT_LISTBUGS_FRONTEND': 'none', - 'APT_LISTCHANGES_FRONTEND': 'none', - 'DEBIAN_FRONTEND': 'noninteractive', - 'UCF_FORCE_CONFFOLD': '1', - } # Export these puppies so they persist - os.environ.update(env_vars) + os.environ.update(DPKG_ENV_VARS) def _get_ppa_info_from_launchpad(owner_name, ppa_name): @@ -484,7 +485,9 @@ def install(name=None, if refreshdb: refresh_db() - __salt__['cmd.run'](cmd, env=kwargs.get('env'), python_shell=False) + env = _parse_env(kwargs.get('env')) + env.update(copy.deepcopy(DPKG_ENV_VARS)) + __salt__['cmd.run'](cmd, python_shell=False, env=env) __context__.pop('pkg.list_pkgs', None) new = list_pkgs() return salt.utils.compare_dicts(old, new) @@ -618,10 +621,19 @@ def upgrade(refresh=True, dist_upgrade=True): else: cmd = ['apt-get', '-q', '-y', '-o', 'DPkg::Options::=--force-confold', '-o', 'DPkg::Options::=--force-confdef', 'upgrade'] - __salt__['cmd.run'](cmd, python_shell=False, output_loglevel='trace', env={'DEBIAN_FRONTEND': 'noninteractive'}) - __context__.pop('pkg.list_pkgs', None) - new = list_pkgs() - return salt.utils.compare_dicts(old, new) + call = __salt__['cmd.run_all'](cmd, python_shell=False, output_loglevel='trace', + env=copy.deepcopy(DPKG_ENV_VARS)) + if call['retcode'] != 0: + ret['result'] = False + if 'stderr' in call: + ret['comment'] += call['stderr'] + if 'stdout' in call: + ret['comment'] += call['stdout'] + else: + __context__.pop('pkg.list_pkgs', None) + new = list_pkgs() + ret['changes'] = salt.utils.compare_dicts(old, new) + return ret def hold(name=None, pkgs=None, sources=None, **kwargs): # pylint: disable=W0613 diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index 7d38c67a90..2b214f2afb 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -141,6 +141,16 @@ def _check_loglevel(level='info', quiet=False): return LOG_LEVELS[level] +def _parse_env(env): + if not env: + env = {} + if isinstance(env, list): + env = salt.utils.repack_dictlist(env) + if not isinstance(env, dict): + env = {} + return env + + def _run(cmd, cwd=None, stdin=None, @@ -221,10 +231,7 @@ def _run(cmd, ret = {} - if not env: - env = {} - if isinstance(env, list): - env = salt.utils.repack_dictlist(env) + env = _parse_env(env) for bad_env_key in (x for x, y in env.iteritems() if y is None): log.error('Environment variable {0!r} passed without a value. '