2012-03-09 23:33:58 +00:00
|
|
|
#!/usr/bin/python -tt
|
|
|
|
# (c) 2012, Red Hat, Inc
|
|
|
|
# Written by Seth Vidal <skvidal at fedoraproject.org>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import yum
|
|
|
|
import datetime
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
def yum_base(conf_file=None, cachedir=False):
|
|
|
|
my = yum.YumBase()
|
|
|
|
my.preconf.debuglevel=0
|
|
|
|
if conf_file and os.path.exists(conf_file):
|
|
|
|
my.preconf.fn = conf_file
|
|
|
|
if cachedir:
|
2012-03-27 16:06:21 +00:00
|
|
|
if hasattr(my, 'setCacheDir'):
|
|
|
|
my.setCacheDir()
|
|
|
|
else:
|
|
|
|
cachedir = yum.misc.getCacheDir()
|
|
|
|
my.repos.setCacheDir(cachedir)
|
|
|
|
my.conf.cache = 0
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
return my
|
|
|
|
|
|
|
|
def pkg_to_dict(po):
|
|
|
|
d = {
|
|
|
|
'name':po.name,
|
|
|
|
'arch':po.arch,
|
|
|
|
'epoch':po.epoch,
|
|
|
|
'release':po.release,
|
|
|
|
'version':po.version,
|
|
|
|
}
|
2012-04-27 21:38:46 +00:00
|
|
|
|
2012-03-09 23:33:58 +00:00
|
|
|
if type(po) == yum.rpmsack.RPMInstalledPackage:
|
2012-03-10 03:06:44 +00:00
|
|
|
d['yumstate'] = 'installed'
|
2012-04-27 21:38:46 +00:00
|
|
|
d['repo'] = 'installed'
|
2012-03-09 23:33:58 +00:00
|
|
|
else:
|
2012-03-10 03:06:44 +00:00
|
|
|
d['yumstate'] = 'available'
|
2012-04-27 21:38:46 +00:00
|
|
|
d['repo'] = po.repoid
|
|
|
|
|
|
|
|
if hasattr(po, 'ui_from_repo'):
|
|
|
|
d['repo'] = po.ui_from_repo
|
|
|
|
|
|
|
|
if hasattr(po, 'ui_nevra'):
|
|
|
|
d['_nevra'] = po.ui_nevra
|
|
|
|
else:
|
|
|
|
d['_nevra'] = '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
|
|
|
|
|
|
|
|
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
def list_stuff(my, stuff):
|
2012-04-13 10:19:36 +00:00
|
|
|
# FIXME - there are potential tracebacks that could occur here
|
2012-03-12 20:31:13 +00:00
|
|
|
# need some more catching for them so we can see what happened
|
2012-03-09 23:33:58 +00:00
|
|
|
if stuff == 'installed':
|
|
|
|
return [ pkg_to_dict(po) for po in my.rpmdb ]
|
|
|
|
elif stuff == 'updates':
|
|
|
|
return [ pkg_to_dict(po) for
|
|
|
|
po in my.doPackageLists(pkgnarrow='updates').updates ]
|
|
|
|
elif stuff == 'available':
|
|
|
|
return [ pkg_to_dict(po) for po in my.pkgSack ]
|
|
|
|
elif stuff == 'repos':
|
|
|
|
r = []
|
|
|
|
for repo in my.repos.repos.values():
|
|
|
|
t = {}
|
|
|
|
s = 'disabled'
|
|
|
|
if repo.enabled:
|
|
|
|
s = 'enabled'
|
2012-03-10 07:19:57 +00:00
|
|
|
t[repo.id] = s
|
2012-03-09 23:33:58 +00:00
|
|
|
r.append(t)
|
|
|
|
|
|
|
|
return r
|
|
|
|
else:
|
|
|
|
e,m,u = my.rpmdb.matchPackageNames([stuff])
|
|
|
|
p = e + m
|
|
|
|
e,m,u = my.pkgSack.matchPackageNames([stuff])
|
|
|
|
p = p + e + m
|
|
|
|
return [ pkg_to_dict(po) for po in p ]
|
|
|
|
|
|
|
|
def run_yum(command):
|
|
|
|
try:
|
|
|
|
cmd = subprocess.Popen(command, shell=True,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = cmd.communicate()
|
|
|
|
except (OSError, IOError), e:
|
|
|
|
rc = 1
|
|
|
|
err = str(e)
|
|
|
|
out = ''
|
|
|
|
except:
|
|
|
|
rc = 1
|
|
|
|
err = traceback.format_exc()
|
|
|
|
out = ''
|
|
|
|
|
|
|
|
if out is None:
|
|
|
|
out = ''
|
|
|
|
if err is None:
|
|
|
|
err = ''
|
|
|
|
else:
|
|
|
|
rc = cmd.returncode
|
|
|
|
|
|
|
|
return rc, out, err
|
|
|
|
|
|
|
|
def ensure(my, state, pkgspec):
|
|
|
|
yumconf = my.conf.config_file_path
|
2012-03-12 20:31:13 +00:00
|
|
|
res = {}
|
2012-03-09 23:33:58 +00:00
|
|
|
if state == 'installed':
|
|
|
|
# check if pkgspec is installed
|
|
|
|
if re.search('[></=]',pkgspec):
|
|
|
|
try:
|
|
|
|
pkgs = my.returnInstalledPackagesByDep(pkgspec)
|
|
|
|
except yum.Errors.YumBaseError:
|
|
|
|
pkgs = None
|
|
|
|
else:
|
|
|
|
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
|
|
|
|
pkgs = e +m
|
|
|
|
|
|
|
|
if pkgs:
|
2012-03-12 20:31:13 +00:00
|
|
|
return { 'changed':False }
|
2012-03-09 23:33:58 +00:00
|
|
|
|
2012-03-14 04:38:38 +00:00
|
|
|
# if not see if it is available
|
|
|
|
if re.search('[></=]',pkgspec):
|
|
|
|
try:
|
|
|
|
pkgs = my.returnPackagesByDep(pkgspec)
|
|
|
|
except yum.Errors.YumBaseError:
|
|
|
|
pkgs = None
|
|
|
|
else:
|
|
|
|
e,m,u = my.pkgSack.matchPackageNames([pkgspec])
|
|
|
|
pkgs = e +m
|
|
|
|
|
|
|
|
if not pkgs:
|
|
|
|
msg = "No Package matching %s available" % pkgspec
|
|
|
|
return { 'changed':False, 'failed': True, 'msg':msg }
|
|
|
|
|
2012-03-09 23:33:58 +00:00
|
|
|
my.close()
|
|
|
|
del my
|
2012-03-14 04:38:38 +00:00
|
|
|
cmd = "yum -c %s -d1 -y install '%s'" % (yumconf, pkgspec)
|
2012-03-09 23:33:58 +00:00
|
|
|
rc, out, err = run_yum(cmd)
|
2012-03-12 20:31:13 +00:00
|
|
|
# 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
|
2012-03-09 23:33:58 +00:00
|
|
|
if rc:
|
|
|
|
changed = False
|
|
|
|
failed = True
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
failed = False
|
|
|
|
|
2012-03-12 20:31:13 +00:00
|
|
|
res['changed'] = changed
|
2012-03-09 23:33:58 +00:00
|
|
|
|
2012-03-12 20:31:13 +00:00
|
|
|
if failed:
|
|
|
|
res['failed'] = failed
|
|
|
|
res['msg'] = err
|
|
|
|
res['results'] = out
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2012-03-09 23:33:58 +00:00
|
|
|
if state == 'removed':
|
|
|
|
# check if pkgspec is installed
|
|
|
|
# if not return {changed: False, failed=False }
|
|
|
|
# if so try to remove it:
|
|
|
|
# if successfull {changed:True, failed=False, results=stdout, errors=stderr }
|
|
|
|
# if fail {'changed':False, failed='True', results=stdout, errors=stderr }
|
|
|
|
yumconf = my.conf.config_file_path
|
|
|
|
if re.search('[></=]',pkgspec):
|
|
|
|
try:
|
|
|
|
pkgs = my.returnInstalledPackagesByDep(pkgspec)
|
|
|
|
except yum.Errors.YumBaseError:
|
|
|
|
pkgs = None
|
|
|
|
else:
|
|
|
|
e,m,u = my.rpmdb.matchPackageNames([pkgspec])
|
|
|
|
pkgs = e +m
|
|
|
|
|
|
|
|
if not pkgs:
|
2012-03-12 20:31:13 +00:00
|
|
|
return { 'changed':False }
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
my.close()
|
|
|
|
del my
|
2012-03-14 04:38:38 +00:00
|
|
|
cmd = "yum -c %s -d1 -y remove '%s'" % (yumconf, pkgspec)
|
2012-03-09 23:33:58 +00:00
|
|
|
rc, out, err = run_yum(cmd)
|
2012-03-12 20:31:13 +00:00
|
|
|
# FIXME if we ran the remove - check to make sure it actually removed :(
|
|
|
|
# look for the pkg in the rpmdb
|
2012-03-09 23:33:58 +00:00
|
|
|
if rc:
|
|
|
|
changed = False
|
|
|
|
failed = True
|
|
|
|
else:
|
|
|
|
changed = True
|
|
|
|
failed = False
|
2012-03-12 20:31:13 +00:00
|
|
|
|
|
|
|
res['changed'] = changed
|
|
|
|
|
|
|
|
if failed:
|
|
|
|
res['failed'] = failed
|
|
|
|
res['msg'] = err
|
|
|
|
res['results'] = out
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
if state == 'latest':
|
2012-03-27 17:58:49 +00:00
|
|
|
updates = my.doPackageLists(pkgnarrow='updates', patterns=[pkgspec]).updates
|
2012-04-27 21:38:46 +00:00
|
|
|
# sucks but this is for rhel5 - won't matter for rhel6 or fedora or whatnot
|
|
|
|
e,m,u = yum.parsePackages(updates, [pkgspec], casematch=True)
|
|
|
|
updates = e + m
|
2012-03-27 17:58:49 +00:00
|
|
|
avail = my.doPackageLists(pkgnarrow='available', patterns=[pkgspec]).available
|
|
|
|
if not updates and not avail:
|
2012-03-27 18:15:48 +00:00
|
|
|
if not my.doPackageLists(pkgnarrow='installed', patterns=[pkgspec]).installed:
|
|
|
|
msg = "No Package matching '%s' found available, installed or updated" % pkgspec
|
|
|
|
return { 'changed':False, 'failed':True, 'msg': msg }
|
2012-03-12 20:31:13 +00:00
|
|
|
return { 'changed':False,}
|
|
|
|
|
2012-03-27 17:58:49 +00:00
|
|
|
# we have something in updates or available
|
|
|
|
if not updates:
|
|
|
|
cmd = "yum -c %s -d1 -y install '%s'" % (yumconf, pkgspec)
|
|
|
|
else:
|
|
|
|
cmd = "yum -c %s -d1 -y update '%s'" % (yumconf, pkgspec)
|
2012-03-12 20:31:13 +00:00
|
|
|
rc, out, err = run_yum(cmd)
|
2012-03-27 18:15:48 +00:00
|
|
|
|
2012-03-12 20:31:13 +00:00
|
|
|
# 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:
|
|
|
|
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
|
2012-07-26 00:14:15 +00:00
|
|
|
|
|
|
|
# should be caught by AnsibleModule argument_spec
|
|
|
|
return dict(changed=False, failed=True, results='', errors='unexpected state')
|
|
|
|
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update(args):
|
|
|
|
#generic update routine
|
|
|
|
pass
|
|
|
|
|
|
|
|
def remove_only(pkgspec):
|
|
|
|
# remove this pkg and only this pkg - fail if it will require more to remove
|
|
|
|
pass
|
|
|
|
|
|
|
|
def main():
|
2012-03-09 23:50:34 +00:00
|
|
|
# state=installed pkg=pkgspec
|
|
|
|
# state=removed pkg=pkgspec
|
2012-04-01 06:35:19 +00:00
|
|
|
# state=latest pkg=pkgspec
|
2012-03-09 23:33:58 +00:00
|
|
|
#
|
2012-04-01 06:35:19 +00:00
|
|
|
# informational commands:
|
|
|
|
# list=installed
|
|
|
|
# list=updates
|
|
|
|
# list=available
|
|
|
|
# list=repos
|
|
|
|
# list=pkgspec
|
2012-03-09 23:33:58 +00:00
|
|
|
|
2012-07-25 14:15:41 +00:00
|
|
|
module = AnsibleModule(
|
2012-07-26 00:14:15 +00:00
|
|
|
argument_spec = dict(
|
2012-07-28 13:22:13 +00:00
|
|
|
pkg=dict(aliases=['name','package']),
|
2012-07-26 00:14:15 +00:00
|
|
|
# removed==absent, installed==present, these are accepted as aliases
|
|
|
|
state=dict(default='installed', choices=['absent','present','installed','removed','latest']),
|
|
|
|
list=dict(choices=['installed','updates','available','repos','pkgspec']),
|
|
|
|
)
|
2012-07-25 14:15:41 +00:00
|
|
|
)
|
2012-03-10 07:19:57 +00:00
|
|
|
|
2012-07-25 14:15:41 +00:00
|
|
|
params = module.params
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
if 'conf_file' not in params:
|
|
|
|
params['conf_file'] = None
|
2012-07-26 00:14:15 +00:00
|
|
|
|
|
|
|
if 'list' in params and 'pkg' in params:
|
|
|
|
module.fail_json(msg="expected 'list=' or 'name=', but not both")
|
2012-03-09 23:33:58 +00:00
|
|
|
|
|
|
|
if 'list' in params:
|
2012-04-02 16:33:46 +00:00
|
|
|
try:
|
|
|
|
my = yum_base(conf_file=params['conf_file'], cachedir=True)
|
|
|
|
results = dict(results=list_stuff(my, params['list']))
|
|
|
|
except Exception, e:
|
2012-07-25 14:15:41 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2012-07-26 00:14:15 +00:00
|
|
|
module.exit_json(**results)
|
2012-04-02 16:33:46 +00:00
|
|
|
|
2012-04-12 00:47:38 +00:00
|
|
|
else:
|
2012-07-26 00:14:15 +00:00
|
|
|
pkg = params['pkg']
|
2012-04-12 00:44:15 +00:00
|
|
|
if 'pkg' is None:
|
2012-07-26 00:14:15 +00:00
|
|
|
module.fail_json(msg="expected 'list=' or 'name='")
|
2012-03-12 20:31:13 +00:00
|
|
|
else:
|
2012-04-02 16:33:46 +00:00
|
|
|
try:
|
|
|
|
my = yum_base(conf_file=params['conf_file'], cachedir=True)
|
2012-07-26 00:14:15 +00:00
|
|
|
state = params['state']
|
2012-04-12 00:44:15 +00:00
|
|
|
results = ensure(my, state, pkg)
|
2012-04-02 16:33:46 +00:00
|
|
|
except Exception, e:
|
2012-07-25 14:15:41 +00:00
|
|
|
module.fail_json(msg=str(e))
|
2012-07-26 00:14:15 +00:00
|
|
|
module.exit_json(**results)
|
2012-03-14 04:38:38 +00:00
|
|
|
|
2012-07-25 14:15:41 +00:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|
|
|
|
|