first cut at yum module with enable/disable repo

This commit is contained in:
Seth Vidal 2012-10-24 00:21:24 -04:00
parent 9626111d5b
commit d89d07550f

View file

@ -49,10 +49,27 @@ options:
required: false required: false
choices: [ "present", "latest", "absent" ] choices: [ "present", "latest", "absent" ]
default: "present" default: "present"
enablerepo:
description:
- repoid of repositories to enable for the install/update operation
- these repos will not persist beyond the transaction
- multiple repos separated with a ,
required: false
default: null
aliases: []
disablerepo:
description:
- repoid of repositories to disable for the install/update operation
- these repos will not persist beyond the transaction
- multiple repos separated with a ,
required: false
default: null
aliases: []
examples: examples:
- code: "yum: name=httpd state=latest" - code: yum name=httpd state=latest
- code: "yum: name=httpd state=removed" - code: yum name=httpd state=removed
- code: "yum: name=httpd state=installed" - code: yum name=httpd enablerepo=testing state=installed
notes: [] notes: []
# informational: requirements for nodes # informational: requirements for nodes
requirements: [ yum, rpm ] requirements: [ yum, rpm ]
@ -91,13 +108,18 @@ def po_to_nevra(po):
else: else:
return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch) return '%s-%s-%s.%s' % (po.name, po.version, po.release, po.arch)
def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf): def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
if not repoq: if not repoq:
pkgs = [] pkgs = []
try: try:
my = yum_base(conf_file) my = yum_base(conf_file)
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
e,m,u = my.rpmdb.matchPackageNames([pkgspec]) e,m,u = my.rpmdb.matchPackageNames([pkgspec])
pkgs = e + m pkgs = e + m
if not pkgs: if not pkgs:
@ -121,13 +143,18 @@ def is_installed(module, repoq, pkgspec, conf_file, qf=def_qf):
return [] return []
def is_available(module, repoq, pkgspec, conf_file, qf=def_qf): def is_available(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
if not repoq: if not repoq:
pkgs = [] pkgs = []
try: try:
my = yum_base(conf_file) my = yum_base(conf_file)
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
e,m,u = my.pkgSack.matchPackageNames([pkgspec]) e,m,u = my.pkgSack.matchPackageNames([pkgspec])
pkgs = e + m pkgs = e + m
if not pkgs: if not pkgs:
@ -148,7 +175,7 @@ def is_available(module, repoq, pkgspec, conf_file, qf=def_qf):
return [] return []
def is_update(module, repoq, pkgspec, conf_file, qf=def_qf): def is_update(module, repoq, pkgspec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
if not repoq: if not repoq:
@ -158,6 +185,11 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
try: try:
my = yum_base(conf_file) my = yum_base(conf_file)
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec) pkgs = my.returnPackagesByDep(pkgspec) + my.returnInstalledPackagesByDep(pkgspec)
if not pkgs: if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([pkgspec]) e,m,u = my.pkgSack.matchPackageNames([pkgspec])
@ -184,13 +216,18 @@ def is_update(module, repoq, pkgspec, conf_file, qf=def_qf):
return [] return []
def what_provides(module, repoq, req_spec, conf_file, qf=def_qf): def what_provides(module, repoq, req_spec, conf_file, qf=def_qf, en_repos=[], dis_repos=[]):
if not repoq: if not repoq:
pkgs = [] pkgs = []
try: try:
my = yum_base(conf_file) my = yum_base(conf_file)
for rid in en_repos:
my.repos.enableRepo(rid)
for rid in dis_repos:
my.repos.disableRepo(rid)
pkgs = my.returnPackagesByDep(req_spec) + my.returnInstalledPackagesByDep(req_spec) pkgs = my.returnPackagesByDep(req_spec) + my.returnInstalledPackagesByDep(req_spec)
if not pkgs: if not pkgs:
e,m,u = my.pkgSack.matchPackageNames([req_spec]) e,m,u = my.pkgSack.matchPackageNames([req_spec])
@ -307,7 +344,7 @@ def run(command):
return rc, out, err return rc, out, err
def install(module, items, repoq, yum_basecmd, conf_file): def install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
res = {} res = {}
res['results'] = [] res['results'] = []
@ -328,7 +365,7 @@ def install(module, items, repoq, yum_basecmd, conf_file):
nvra = local_nvra(spec) nvra = local_nvra(spec)
# look for them in the rpmdb # look for them in the rpmdb
if is_installed(module, repoq, nvra, conf_file): if is_installed(module, repoq, nvra, conf_file, en_repos, dis_repos):
# if they are there, skip it # if they are there, skip it
continue continue
pkg = spec pkg = spec
@ -340,7 +377,7 @@ def install(module, items, repoq, yum_basecmd, conf_file):
# range requires or file-requires or pkgname :( # range requires or file-requires or pkgname :(
else: else:
# look up what pkgs provide this # look up what pkgs provide this
pkglist = what_provides(module, repoq, spec, conf_file) pkglist = what_provides(module, repoq, spec, conf_file, en_repos, dis_repos)
if not pkglist: if not pkglist:
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
module.fail_json(**res) module.fail_json(**res)
@ -350,7 +387,7 @@ def install(module, items, repoq, yum_basecmd, conf_file):
found = False found = False
for this in pkglist: for this in pkglist:
if is_installed(module, repoq, this, conf_file): if is_installed(module, repoq, this, conf_file, en_repos, dis_repos):
found = True found = True
res['results'].append('%s providing %s is already installed' % (this, spec)) res['results'].append('%s providing %s is already installed' % (this, spec))
break break
@ -381,7 +418,7 @@ def install(module, items, repoq, yum_basecmd, conf_file):
module.exit_json(**res) module.exit_json(**res)
def remove(module, items, repoq, yum_basecmd, conf_file): def remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
res = {} res = {}
res['results'] = [] res['results'] = []
@ -397,14 +434,14 @@ def remove(module, items, repoq, yum_basecmd, conf_file):
pkg = spec pkg = spec
# req or pkgname remove # req or pkgname remove
else: else:
pkglist = is_installed(module, repoq, spec, conf_file) pkglist = is_installed(module, repoq, spec, conf_file, en_repos, dis_repos)
if not pkglist: if not pkglist:
res['msg'] += "No Package matching '%s' found installed" % spec res['msg'] += "No Package matching '%s' found installed" % spec
module.exit_json(**res) module.exit_json(**res)
found = False found = False
for this in pkglist: for this in pkglist:
if is_installed(module, repoq, this, conf_file): if is_installed(module, repoq, this, conf_file, en_repos, dis_repos):
found = True found = True
if not found: if not found:
@ -431,7 +468,7 @@ def remove(module, items, repoq, yum_basecmd, conf_file):
module.exit_json(**res) module.exit_json(**res)
def latest(module, items, repoq, yum_basecmd, conf_file): def latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos):
res = {} res = {}
res['results'] = [] res['results'] = []
@ -448,12 +485,12 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
pkg = spec pkg = spec
# dep/pkgname - find it # dep/pkgname - find it
else: else:
if is_installed(module, repoq, spec, conf_file): if is_installed(module, repoq, spec, conf_file, en_repos, dis_repos):
basecmd = 'update' basecmd = 'update'
else: else:
basecmd = 'install' basecmd = 'install'
pkglist = what_provides(module, repoq, spec, conf_file) pkglist = what_provides(module, repoq, spec, conf_file, en_repos, dis_repos)
if not pkglist: if not pkglist:
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
res['failed']=True res['failed']=True
@ -461,11 +498,11 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
nothing_to_do = True nothing_to_do = True
for this in pkglist: for this in pkglist:
if basecmd == 'install' and is_available(module, repoq, this, conf_file): if basecmd == 'install' and is_available(module, repoq, this, conf_file, en_repos, dis_repos):
nothing_to_do = False nothing_to_do = False
break break
if basecmd == 'update' and is_update(module, repoq, this, conf_file): if basecmd == 'update' and is_update(module, repoq, this, conf_file, en_repos, dis_repos):
nothing_to_do = False nothing_to_do = False
break break
@ -501,13 +538,14 @@ def latest(module, items, repoq, yum_basecmd, conf_file):
module.exit_json(**res) module.exit_json(**res)
def ensure(module, state, pkgspec, conf_file): def ensure(module, state, pkgspec, conf_file, enablerepo, disablerepo):
# take multiple args comma separated # take multiple args comma separated
items = pkgspec.split(',') items = pkgspec.split(',')
yum_basecmd = [yumbin, '-d', '1', '-y'] yum_basecmd = [yumbin, '-d', '1', '-y']
if not repoquery: if not repoquery:
repoq = None repoq = None
else: else:
@ -518,12 +556,33 @@ def ensure(module, state, pkgspec, conf_file):
if repoq: if repoq:
repoq += ['-c', conf_file] repoq += ['-c', conf_file]
dis_repos =[]
en_repos = []
if disablerepo:
dis_repos = disablerepo.split(',')
if enablerepo:
en_repos = enablerepo.split(',')
for repoid in en_repos:
r_cmd = ['--enablerepo', repoid]
yum_basecmd.extend(r_cmd)
if repoq:
repoq.extend(r_cmd)
for repoid in dis_repos:
r_cmd = ['--disablerepo', repoid]
yum_basecmd.extend(r_cmd)
if repoq:
repoq.extend(r_cmd)
if state in ['installed', 'present']: if state in ['installed', 'present']:
install(module, items, repoq, yum_basecmd, conf_file) install(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
elif state in ['removed', 'absent']: elif state in ['removed', 'absent']:
remove(module, items, repoq, yum_basecmd, conf_file) remove(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
elif state == 'latest': elif state == 'latest':
latest(module, items, repoq, yum_basecmd, conf_file) latest(module, items, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
# should be caught by AnsibleModule argument_spec # should be caught by AnsibleModule argument_spec
return dict(changed=False, failed=True, results='', errors='unexpected state') return dict(changed=False, failed=True, results='', errors='unexpected state')
@ -546,6 +605,8 @@ def main():
name=dict(aliases=['pkg']), name=dict(aliases=['pkg']),
# removed==absent, installed==present, these are accepted as aliases # removed==absent, installed==present, these are accepted as aliases
state=dict(default='installed', choices=['absent','present','installed','removed','latest']), state=dict(default='installed', choices=['absent','present','installed','removed','latest']),
enablerepo=dict(),
disablerepo=dict(),
list=dict(), list=dict(),
conf_file=dict(default=None), conf_file=dict(default=None),
), ),
@ -564,7 +625,9 @@ def main():
else: else:
pkg = params['name'] pkg = params['name']
state = params['state'] state = params['state']
res = ensure(module, state, pkg, params['conf_file']) enablerepo = params.get('enablerepo', '')
disablerepo = params.get('disablerepo', '')
res = ensure(module, state, pkg, params['conf_file'], enablerepo, disablerepo)
module.fail_json(msg="we should never get here unless this all failed", **res) module.fail_json(msg="we should never get here unless this all failed", **res)
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py