Add lowpkg.bin_pkg_info for dpkg

This commit is contained in:
Erik Johnson 2015-06-29 16:41:06 -05:00
parent 050cf7b4ca
commit 52ef17fa99

View file

@ -6,9 +6,11 @@ from __future__ import absolute_import
# Import python libs
import logging
import os
# Import salt libs
import salt.utils
from salt.exceptions import CommandExecutionError, SaltInvocationError
log = logging.getLogger(__name__)
@ -23,6 +25,82 @@ def __virtual__():
return __virtualname__ if __grains__['os_family'] == 'Debian' else False
def bin_pkg_info(path, saltenv='base'):
'''
.. versionadded:: Beryllium
Parses RPM metadata and returns a dictionary of information about the
package (name, version, etc.).
path
Path to the file. Can either be an absolute path to a file on the
minion, or a salt fileserver URL (e.g. ``salt://path/to/file.rpm``).
If a salt fileserver URL is passed, the file will be cached to the
minion so that it can be examined.
saltenv : base
Salt fileserver envrionment from which to retrieve the package. Ignored
if ``path`` is a local file path on the minion.
CLI Example:
.. code-block:: bash
salt '*' lowpkg.bin_pkg_info /root/foo-1.2.3-1ubuntu1_all.deb
salt '*' lowpkg.bin_pkg_info salt://foo-1.2.3-1ubuntu1_all.deb
'''
# If the path is a valid protocol, pull it down using cp.cache_file
if __salt__['config.valid_fileproto'](path):
newpath = __salt__['cp.cache_file'](path, saltenv)
if not newpath:
raise CommandExecutionError(
'Unable to retrieve {0} from saltenv \'{1}\''
.format(path, saltenv)
)
path = newpath
else:
if not os.path.exists(path):
raise CommandExecutionError(
'{0} does not exist on minion'.format(path)
)
elif not os.path.isabs(path):
raise SaltInvocationError(
'{0} does not exist on minion'.format(path)
)
cmd = ['dpkg', '-I', path]
result = __salt__['cmd.run_all'](cmd, output_loglevel='trace')
if result['retcode'] != 0:
msg = 'Unable to get info for ' + path
if result['stderr']:
msg += ': ' + result['stderr']
raise CommandExecutionError(msg)
ret = {}
for line in result['stdout'].splitlines():
line = line.strip()
if line.startswith('Package:'):
ret['name'] = line.split()[-1]
elif line.startswith('Version:'):
ret['version'] = line.split()[-1]
elif line.startswith('Architecture:'):
ret['arch'] = line.split()[-1]
missing = [x for x in ('name', 'version', 'arch') if x not in ret]
if missing:
raise CommandExecutionError(
'Unable to get {0} for {1}'.format(', '.join(missing), path)
)
if __grains__.get('cpuarch', '') == 'x86_64':
osarch = __grains__.get('osarch', '')
arch = ret['arch']
if arch != 'all' and osarch == 'amd64' and osarch != arch:
ret['name'] += ':{0}'.format(arch)
return ret
def unpurge(*packages):
'''
Change package selection for each package specified to 'install'