yum module

add state=latest and clean up the output cases
added some fixmes on verification
reviewable/pr18780/r1
Seth Vidal 13 years ago
parent 93824ff54e
commit ec6cd6e360

78
yum

@ -63,6 +63,8 @@ def pkg_to_dict(po):
return d return d
def list_stuff(my, stuff): def list_stuff(my, stuff):
# FIXME - there are poitential tracebacks that could occur here
# need some more catching for them so we can see what happened
if stuff == 'installed': if stuff == 'installed':
return [ pkg_to_dict(po) for po in my.rpmdb ] return [ pkg_to_dict(po) for po in my.rpmdb ]
elif stuff == 'updates': elif stuff == 'updates':
@ -113,6 +115,7 @@ def run_yum(command):
def ensure(my, state, pkgspec): def ensure(my, state, pkgspec):
yumconf = my.conf.config_file_path yumconf = my.conf.config_file_path
res = {}
if state == 'installed': if state == 'installed':
pkg = None pkg = None
# check if pkgspec is installed # check if pkgspec is installed
@ -126,13 +129,16 @@ def ensure(my, state, pkgspec):
pkgs = e +m pkgs = e +m
if pkgs: if pkgs:
return { 'changed':False, 'failed':False, 'results':'', 'errors':'' } return { 'changed':False }
# if not - try to install it # if not - try to install it
my.close() my.close()
del my del my
cmd = 'yum -c %s -d1 -y install %s' % (yumconf, pkgspec) cmd = 'yum -c %s -d1 -y install %s' % (yumconf, pkgspec)
rc, out, err = run_yum(cmd) rc, out, err = run_yum(cmd)
# FIXME - if we did an install - go and check the rpmdb to see if it actually installed
# look for the pkg in rpmdb
# look for the pkg via obsoletes
if rc: if rc:
changed = False changed = False
failed = True failed = True
@ -140,10 +146,14 @@ def ensure(my, state, pkgspec):
changed = True changed = True
failed = False failed = False
return {'changed': changed, res['changed'] = changed
'failed': failed,
'results':out, if failed:
'errors': err } res['failed'] = failed
res['msg'] = err
res['results'] = out
return res
if state == 'removed': if state == 'removed':
# check if pkgspec is installed # check if pkgspec is installed
@ -162,12 +172,41 @@ def ensure(my, state, pkgspec):
pkgs = e +m pkgs = e +m
if not pkgs: if not pkgs:
return { 'changed':False, 'failed':False, 'results':'', 'errors':'' } return { 'changed':False }
my.close() my.close()
del my del my
cmd = 'yum -c %s -d1 -y remove %s' % (yumconf, pkgspec) cmd = 'yum -c %s -d1 -y remove %s' % (yumconf, pkgspec)
rc, out, err = run_yum(cmd) rc, out, err = run_yum(cmd)
# FIXME if we ran the remove - check to make sure it actually removed :(
# look for the pkg in the rpmdb
if rc:
changed = False
failed = True
else:
changed = True
failed = False
res['changed'] = changed
if failed:
res['failed'] = failed
res['msg'] = err
res['results'] = out
return res
if state == 'latest':
if not [ pkg_to_dict(po) for
po in my.doPackageLists(pkgnarrow='updates', patterns=[pkgspec]).updates ]:
# there nothing in updates matching this.
return { 'changed':False,}
# we have something in updates
cmd = 'yum -c %s -d1 -y update %s' % (yumconf, pkgspec)
rc, out, err = run_yum(cmd)
# FIXME if it is - update it and check to see if it applied
# check to see if there is no longer an update available for the pkgspec
if rc: if rc:
changed = False changed = False
failed = True failed = True
@ -175,15 +214,15 @@ def ensure(my, state, pkgspec):
changed = True changed = True
failed = False failed = False
return {'changed': changed,
'failed': failed, res['changed'] = changed
'results':out,
'errors': err } if failed:
#if state == 'latest': res['failed'] = failed
# check to see if this pkg is in an update res['msg'] = err
# if it is - update it and check to see if it applied res['results'] = out
# if it is not - then return
# return { 'changed':False, 'failed':False, 'results':'', 'errors':'' } return res
return {'changed': False, return {'changed': False,
'failed': True, 'failed': True,
@ -214,7 +253,11 @@ def main():
args = " ".join(sys.argv[1:]) args = " ".join(sys.argv[1:])
items = shlex.split(args) items = shlex.split(args)
# if nothing else changes - it fails # if nothing else changes - it fails
results = { 'changed':False, 'failed':True, 'results':'', 'errors':args } results = { 'changed':False,
'failed':True,
'results':'',
'errors':'',
'msg':args }
params = {} params = {}
for x in items: for x in items:
(k, v) = x.split("=", 1) (k, v) = x.split("=", 1)
@ -228,6 +271,9 @@ def main():
my = yum_base(conf_file=params['conf_file'], cachedir=True) my = yum_base(conf_file=params['conf_file'], cachedir=True)
results = list_stuff(my, params['list']) results = list_stuff(my, params['list'])
elif 'state' in params: elif 'state' in params:
if 'pkg' not in params:
results['msg'] = "No pkg specified"
else:
my = yum_base(conf_file=params['conf_file'], cachedir=True) my = yum_base(conf_file=params['conf_file'], cachedir=True)
state = params['state'] state = params['state']
pkgspec = params['pkg'] pkgspec = params['pkg']

Loading…
Cancel
Save