pkgrepo.managed: properly handle comments for debian

Resolves #39778.
This commit is contained in:
Erik Johnson 2017-04-06 15:24:38 -05:00
parent 74366c57a4
commit 191610482d
3 changed files with 45 additions and 7 deletions

View file

@ -38,6 +38,7 @@ import salt.config
import salt.syspaths
from salt.modules.cmdmod import _parse_env
import salt.utils
import salt.utils.pkg.deb
import salt.utils.systemd
from salt.exceptions import (
CommandExecutionError, MinionError, SaltInvocationError
@ -1858,8 +1859,9 @@ def mod_repo(repo, saltenv='base', **kwargs):
comments
Sometimes you want to supply additional information, but not as
enabled configuration. Anything supplied for this list will be saved
in the repo configuration with a comment marker (#) in front.
enabled configuration. All comments provided here will be joined
into a single string and appended to the repo configuration with a
comment marker (#) before it.
.. versionadded:: 2015.8.9
@ -2089,13 +2091,17 @@ def mod_repo(repo, saltenv='base', **kwargs):
if mod_source:
break
if 'comments' in kwargs:
kwargs['comments'] = \
salt.utils.pkg.deb.combine_comments(kwargs['comments'])
if not mod_source:
mod_source = sourceslist.SourceEntry(repo)
if 'comments' in kwargs:
mod_source.comment = " ".join(str(c) for c in kwargs['comments'])
mod_source.comment = kwargs['comments']
sources.list.append(mod_source)
elif 'comments' in kwargs:
mod_source.comment = " ".join(str(c) for c in kwargs['comments'])
mod_source.comment = kwargs['comments']
for key in kwargs:
if key in _MODIFY_OK and hasattr(mod_source, key):

View file

@ -95,6 +95,7 @@ from salt.exceptions import CommandExecutionError, SaltInvocationError
from salt.modules.aptpkg import _strip_uri
from salt.state import STATE_INTERNAL_KEYWORDS as _STATE_INTERNAL_KEYWORDS
import salt.utils
import salt.utils.pkg.deb
def __virtual__():
@ -386,13 +387,16 @@ def managed(name, ppa=None, **kwargs):
# split the line and sort everything after the URL
sanitizedsplit = sanitizedkwargs[kwarg].split()
sanitizedsplit[3:] = sorted(sanitizedsplit[3:])
reposplit = pre[kwarg].split()
reposplit, _, pre_comments = \
[x.strip() for x in pre[kwarg].partition('#')]
reposplit = reposplit.split()
reposplit[3:] = sorted(reposplit[3:])
if sanitizedsplit != reposplit:
break
if 'comments' in kwargs:
_line = pre[kwarg].split('#')
if str(kwargs['comments']) not in _line:
post_comments = \
salt.utils.pkg.deb.combine_comments(kwargs['comments'])
if pre_comments != post_comments:
break
else:
if os_family in ('redhat', 'suse') \

28
salt/utils/pkg/deb.py Normal file
View file

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
'''
Common functions for working with RPM packages
'''
# Import python libs
from __future__ import absolute_import
# Import 3rd-party libs
from salt.ext import six
from salt.ext.six.moves import range # pylint: disable=redefined-builtin
def combine_comments(comments):
'''
Given a list of comments, or a comment submitted as a string, return a
single line of text containing all of the comments.
'''
if isinstance(comments, list):
for idx in range(len(comments)):
if not isinstance(comments[idx], six.string_types):
comments[idx] = str(comments[idx])
else:
if not isinstance(comments, six.string_types):
comments = [str(comments)]
else:
comments = [comments]
return ' '.join(comments).strip()