Renamed yum.py to yumcmd.py to avoid namespace conflict with the real yum module. Updated list_pkgs to handle any number of packages to be passed in for filtering purposes. Added clean_metadata function to keep options similar to the actual yum cli tool. More enhancements to available_version, now will return the version regardless of the package being an update to existing package or the version of a currently not installed package.

This commit is contained in:
Eric Poelke 2011-11-22 17:15:45 -08:00
parent c9a8348d25
commit 9f2cd50a04
5 changed files with 70 additions and 32 deletions

View file

@ -1265,7 +1265,7 @@ regardless of what the actual module is named.
The package manager modules are the best example of using the \fB__virtual__\fP
function:
\fI\%https://github.com/thatch45/salt/blob/v0.9.3/salt/modules/pacman.py\fP
\fI\%https://github.com/thatch45/salt/blob/v0.9.3/salt/modules/yum.py\fP
\fI\%https://github.com/thatch45/salt/blob/v0.9.3/salt/modules/yumpkg.py\fP
\fI\%https://github.com/thatch45/salt/blob/v0.9.3/salt/modules/apt.py\fP
.SS Documentation
.sp
@ -1432,7 +1432,7 @@ def ping():
.IP \(bu 2
\fI\%salt.modules.virt\fP
.IP \(bu 2
\fI\%salt.modules.yum\fP
\fI\%salt.modules.yumpkg\fP
.UNINDENT
.SS \fBsalt.modules.apache\fP
.sp
@ -3714,12 +3714,12 @@ Return detailed information about the vms on this hyper in a dict:
CLI Example:
salt \(aq*\(aq virt.vm_info
.UNINDENT
.SS \fBsalt.modules.yum\fP
.SS \fBsalt.modules.yumpkg\fP
.sp
Support for YUM
.INDENT 0.0
.TP
.B salt.modules.yum.available_version(name)
.B salt.modules.yumpkg.available_version(name)
The available version of the package in the repository
.sp
CLI Example:
@ -3727,7 +3727,7 @@ salt \(aq*\(aq pkg.available_version <package name>
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.install(pkg, refresh=False)
.B salt.modules.yumpkg.install(pkg, refresh=False)
Install the passed package, add refresh=True to clean out the yum database
before executing
.sp
@ -3745,7 +3745,7 @@ salt \(aq*\(aq pkg.install <package name>
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.list_pkgs()
.B salt.modules.yumpkg.list_pkgs()
List the packages currently installed in a dict:
{\(aq<package_name>\(aq: \(aq<version>\(aq}
.sp
@ -3754,7 +3754,7 @@ salt \(aq*\(aq pkg.list_pkgs
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.purge(pkg)
.B salt.modules.yumpkg.purge(pkg)
Yum does not have a purge, this function calls remove
.sp
Return a list containing the removed packages:
@ -3764,7 +3764,7 @@ salt \(aq*\(aq pkg.purge <package name>
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.refresh_db()
.B salt.modules.yumpkg.refresh_db()
Since yum refreshes the database automatically, this runs a yum clean,
so that the next yum operation will have a clean database
.sp
@ -3773,7 +3773,7 @@ salt \(aq*\(aq pkg.refresh_db
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.remove(pkg)
.B salt.modules.yumpkg.remove(pkg)
Remove a single package with yum remove
.sp
Return a list containing the removed packages:
@ -3783,7 +3783,7 @@ salt \(aq*\(aq pkg.remove <package name>
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.upgrade()
.B salt.modules.yumpkg.upgrade()
Run a full system upgrade, a yum upgrade
.sp
Return a dict containing the new package names and versions:
@ -3800,7 +3800,7 @@ salt \(aq*\(aq pkg.upgrade
.UNINDENT
.INDENT 0.0
.TP
.B salt.modules.yum.version(name)
.B salt.modules.yumpkg.version(name)
Returns a version if the package is installed, else returns an empty string
.sp
CLI Example:

View file

@ -1,6 +0,0 @@
================
salt.modules.yum
================
.. automodule:: salt.modules.yum
:members:

View file

@ -0,0 +1,6 @@
===================
salt.modules.yumpkg
===================
.. automodule:: salt.modules.yumpkg
:members:

View file

@ -142,7 +142,7 @@ regardless of what the actual module is named.
The package manager modules are the best example of using the ``__virtual__``
function:
:blob:`salt/modules/pacman.py`
:blob:`salt/modules/yum.py`
:blob:`salt/modules/yumpkg.py`
:blob:`salt/modules/apt.py`
Documentation

View file

@ -37,17 +37,32 @@ def available_version(name):
yb = yum.YumBase()
# look for available packages only, if package is already installed with
# latest version it will not show up here.
pl = yb.doPackageLists('available')
exactmatch, matched, unmatched = yum.packages.parsePackages(pl.available,
[name])
# latest version it will not show up here. If we want to use wildcards
# here we can, but for now its exactmatch only.
for pkg in exactmatch:
# ignore packages that do not match base arch
if pkg.arch == getBaseArch():
return '-'.join([pkg.version, pkg.release])
versions_list = []
for pkgtype in ['available', 'updates']:
pl = yb.doPackageLists(pkgtype)
exactmatch, matched, unmatched = yum.packages.parsePackages(pl, [name])
# build a list of available packages from either available or updates
# this will result in a double match for a package that is already
# installed. Maybe we should just return the value if we get a hit
# on available, and only iterate though updates if we don't..
for pkg in exactmatch:
if pkg.arch == getBaseArch():
versions_list.append('-'.join([pkg.version, pkg.release]))
if len(versions_list) == 0:
# if versions_list is empty return empty string. It may make sense
# to also check if a package is installed and on latest version
# already and return a message saying 'up to date' or something along
# those lines.
return ''
# remove the duplicate items from the list and return the first one
return list(set(versions_list))[0]
return ''
def version(name):
@ -103,9 +118,19 @@ def refresh_db():
import yum
yb = yum.YumBase()
yb.cleanMetadata()
return true
return True
def clean_metadata():
'''
Cleans local yum metadata.
CLI Example::
salt '*' pkg.clean_metadata
'''
return refresh_db()
def install(pkg, refresh=False):
'''
@ -121,13 +146,24 @@ def install(pkg, refresh=False):
salt '*' pkg.install <package name>
'''
old = list_pkgs()
cmd = 'yum -y install ' + pkg
# WIP only commiting this so I can keep working on it at home later...
import yum
if refresh:
refresh_db()
__salt__['cmd.retcode'](cmd)
new = list_pkgs()
yb = yum.YumBase()
try:
yb.install(name=pkg)
yb.resolveDeps()
except yum.Errors.InstallError, e:
return False
pkgs = {}
for npkg in new:
if npkg in old:
if old[npkg] == new[npkg]:
@ -188,7 +224,9 @@ def remove(pkg):
salt '*' pkg.remove <package name>
'''
# import subprocess as sp
old = list_pkgs()
# sp.Popen(['yum', '-y', 'remove', pkg])
cmd = 'yum -y remove ' + pkg
__salt__['cmd.retcode'](cmd)
new = list_pkgs()