2012-03-10 00:33:58 +01: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/>.
|
|
|
|
#
|
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
|
2012-03-10 00:33:58 +01:00
|
|
|
import traceback
|
2012-08-02 07:47:48 +02:00
|
|
|
import os
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def_qf = "%{name}-%{version}-%{release}.%{arch}"
|
|
|
|
|
|
|
|
def is_installed(repoq, pkgspec, qf=def_qf):
|
|
|
|
cmd = repoq + "--disablerepo=\* --pkgnarrow=installed --qf '%s' %s " % (qf, pkgspec)
|
|
|
|
rc,out,err = run(cmd)
|
|
|
|
if rc == 0:
|
|
|
|
return [ p for p in out.split('\n') if p.strip() ]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def is_available(repoq, pkgspec, qf=def_qf):
|
|
|
|
cmd = repoq + "--qf '%s' %s " % (qf, pkgspec)
|
|
|
|
rc,out,err = run(cmd)
|
|
|
|
if rc == 0:
|
|
|
|
return [ p for p in out.split('\n') if p.strip() ]
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def is_update(repoq, pkgspec, qf=def_qf):
|
|
|
|
cmd = repoq + "--pkgnarrow=updates --qf '%s' %s " % (qf, pkgspec)
|
|
|
|
rc,out,err = run(cmd)
|
|
|
|
if rc == 0:
|
|
|
|
return set([ p for p in out.split('\n') if p.strip() ])
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def what_provides(repoq, req_spec, qf=def_qf):
|
|
|
|
cmd = repoq + "--qf '%s' --whatprovides %s" % (qf, req_spec)
|
|
|
|
rc,out,err = run(cmd)
|
|
|
|
ret = []
|
|
|
|
if rc == 0:
|
|
|
|
ret = set([ p for p in out.split('\n') if p.strip() ])
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
return ret
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def local_nvra(path):
|
|
|
|
"""return nvra of a local rpm passed in"""
|
|
|
|
|
|
|
|
cmd = "/bin/rpm -qp --qf='%%{name}-%%{version}-%%{release}.%%{arch}\n' %s'" % path
|
|
|
|
rc, out, err = run(cmd)
|
|
|
|
if rc != 0:
|
|
|
|
return None
|
|
|
|
nvra = out.split('\n')[0]
|
|
|
|
return nvra
|
|
|
|
|
|
|
|
|
|
|
|
def pkg_to_dict(pkgstr):
|
|
|
|
if pkgstr.strip():
|
|
|
|
n,e,v,r,a,repo = pkgstr.split('|')
|
|
|
|
else:
|
|
|
|
return {'error_parsing': pkgstr}
|
|
|
|
|
2012-03-10 00:33:58 +01:00
|
|
|
d = {
|
2012-08-02 07:47:48 +02:00
|
|
|
'name':n,
|
|
|
|
'arch':a,
|
|
|
|
'epoch':e,
|
|
|
|
'release':r,
|
|
|
|
'version':v,
|
|
|
|
'repo':repo,
|
|
|
|
'nevra': '%s:%s-%s-%s.%s' % (e,n,v,r,a)
|
2012-03-10 00:33:58 +01:00
|
|
|
}
|
2012-04-27 23:38:46 +02:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
if repo == 'installed':
|
2012-03-10 04:06:44 +01:00
|
|
|
d['yumstate'] = 'installed'
|
2012-03-10 00:33:58 +01:00
|
|
|
else:
|
2012-03-10 04:06:44 +01:00
|
|
|
d['yumstate'] = 'available'
|
2012-08-02 07:47:48 +02:00
|
|
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
def repolist(repoq, qf="%{repoid}"):
|
|
|
|
cmd = repoq + "--qf '%s' -a" % (qf)
|
|
|
|
rc,out,err = run(cmd)
|
|
|
|
ret = []
|
|
|
|
if rc == 0:
|
|
|
|
ret = set([ p for p in out.split('\n') if p.strip() ])
|
2012-04-27 23:38:46 +02:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
return ret
|
2012-04-27 23:38:46 +02:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def list_stuff(conf_file, stuff):
|
|
|
|
qf = "%{name}|%{epoch}|%{version}|%{release}|%{arch}|%{repoid}"
|
|
|
|
repoq = '/usr/bin/repoquery --plugins --quiet -q '
|
|
|
|
if conf_file and os.path.exists(conf_file):
|
|
|
|
repoq = '/usr/bin/repoquery -c %s --plugins --quiet -q ' % conf_file
|
2012-03-10 00:33:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
if stuff == 'installed':
|
2012-08-02 07:47:48 +02:00
|
|
|
return [ pkg_to_dict(p) for p in is_installed(repoq, '-a', qf=qf) if p.strip() ]
|
2012-03-10 00:33:58 +01:00
|
|
|
elif stuff == 'updates':
|
2012-08-02 07:47:48 +02:00
|
|
|
return [ pkg_to_dict(p) for p in is_update(repoq, '-a', qf=qf) if p.strip() ]
|
2012-03-10 00:33:58 +01:00
|
|
|
elif stuff == 'available':
|
2012-08-02 07:47:48 +02:00
|
|
|
return [ pkg_to_dict(p) for p in is_available(repoq, '-a', qf=qf) if p.strip() ]
|
2012-03-10 00:33:58 +01:00
|
|
|
elif stuff == 'repos':
|
2012-08-02 07:47:48 +02:00
|
|
|
return [ dict(repoid=name, state='enabled') for name in repolist(repoq) if name.strip() ]
|
2012-03-10 00:33:58 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
return [ pkg_to_dict(p) for p in is_installed(repoq, stuff, qf=qf) + is_available(repoq, stuff, qf=qf) if p.strip() ]
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def run(command):
|
2012-03-10 00:33:58 +01:00
|
|
|
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 = ''
|
2012-08-02 07:47:48 +02:00
|
|
|
|
2012-03-10 00:33:58 +01:00
|
|
|
if out is None:
|
|
|
|
out = ''
|
|
|
|
if err is None:
|
|
|
|
err = ''
|
|
|
|
else:
|
|
|
|
rc = cmd.returncode
|
|
|
|
|
|
|
|
return rc, out, err
|
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
|
|
|
|
def install(module, items, repoq, yum_basecmd):
|
2012-03-12 21:31:13 +01:00
|
|
|
res = {}
|
2012-08-02 07:47:48 +02:00
|
|
|
res['results'] = ''
|
|
|
|
res['msg'] = ''
|
|
|
|
res['rc'] = 0
|
|
|
|
res['changed'] = False
|
|
|
|
|
|
|
|
for spec in items:
|
|
|
|
pkg = None
|
|
|
|
|
|
|
|
# check if pkgspec is installed (if possible for idempotence)
|
|
|
|
# localpkg
|
|
|
|
if spec.endswith('.rpm'):
|
|
|
|
# get the pkg name-v-r.arch
|
|
|
|
nvra = local_nvra(spec)
|
|
|
|
# look for them in the rpmdb
|
|
|
|
if is_installed(repoq, nvra):
|
|
|
|
# if they are there, skip it
|
|
|
|
continue
|
|
|
|
pkg = spec
|
|
|
|
#groups :(
|
|
|
|
elif spec.startswith('@'):
|
|
|
|
# complete wild ass guess b/c it's a group
|
|
|
|
pkg = spec
|
|
|
|
|
|
|
|
# range requires or file-requires or pkgname :(
|
2012-03-14 05:38:38 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
# look up what pkgs provide this
|
|
|
|
pkglist = what_provides(repoq, spec)
|
|
|
|
if not pkglist:
|
|
|
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
|
|
|
res['failed'] = True
|
|
|
|
module.exit_json(**res)
|
|
|
|
|
|
|
|
# if any of them are installed
|
|
|
|
# then nothing to do
|
|
|
|
|
|
|
|
found = False
|
|
|
|
for this in pkglist:
|
|
|
|
if is_installed(repoq, this):
|
|
|
|
found = True
|
|
|
|
res['results'] += '%s providing %s is already installed\n' % (this, spec)
|
|
|
|
|
|
|
|
if found:
|
|
|
|
continue
|
|
|
|
# if not - then pass in the spec as what to install
|
|
|
|
# we could get here if nothing provides it but that's not
|
|
|
|
# the error we're catching here
|
|
|
|
pkg = spec
|
2012-03-14 05:38:38 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
cmd = "%s install '%s'" % (yum_basecmd, pkg)
|
|
|
|
res['results'] += "\nInstalling %s\n" % pkg
|
|
|
|
rc, out, err = run(cmd)
|
2012-03-12 21:31:13 +01: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-10 00:33:58 +01:00
|
|
|
if rc:
|
2012-08-02 07:47:48 +02:00
|
|
|
res['changed'] = False
|
|
|
|
res['rc'] = rc
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
2012-03-10 00:33:58 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
res['changed'] = True
|
|
|
|
res['rc'] = 0
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
|
|
|
|
|
|
|
module.exit_json(**res)
|
2012-03-10 00:33:58 +01:00
|
|
|
|
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def remove(module, items, repoq, yum_basecmd):
|
|
|
|
res = {}
|
|
|
|
res['results'] = ''
|
|
|
|
res['msg'] = ''
|
|
|
|
res['changed'] = False
|
|
|
|
res['rc'] = 0
|
|
|
|
|
|
|
|
for spec in items:
|
|
|
|
pkg = None
|
|
|
|
|
|
|
|
# group remove - hope you like things dying!
|
|
|
|
if spec.startswith('@'):
|
|
|
|
pkg = spec
|
|
|
|
# req or pkgname remove
|
2012-03-10 00:33:58 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
pkglist = what_provides(repoq, spec)
|
|
|
|
if not pkglist:
|
|
|
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
|
|
|
res['failed']=True
|
|
|
|
module.exit_json(**res)
|
|
|
|
|
|
|
|
found = False
|
|
|
|
for this in pkglist:
|
|
|
|
if is_installed(repoq, this):
|
|
|
|
found = True
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
continue
|
|
|
|
pkg = spec
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
cmd = "%s remove '%s'" % (yum_basecmd, pkg)
|
|
|
|
rc, out, err = run(cmd)
|
|
|
|
|
2012-03-12 21:31:13 +01:00
|
|
|
# FIXME if we ran the remove - check to make sure it actually removed :(
|
2012-08-02 07:47:48 +02:00
|
|
|
# look for the pkg in the rpmdb - this is notoriously hard for groups :(
|
|
|
|
if rc != 0:
|
|
|
|
res['changed'] = False
|
|
|
|
res['failed'] = True
|
|
|
|
res['rc'] = rc
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
2012-03-10 00:33:58 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
res['changed'] = True
|
|
|
|
res['rc'] = 0
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
|
|
|
|
|
|
|
module.exit_json(**res)
|
2012-03-12 21:31:13 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
def latest(module, items, repoq, yum_basecmd):
|
|
|
|
res = {}
|
|
|
|
res['results'] = ''
|
|
|
|
res['msg'] = ''
|
|
|
|
res['changed'] = False
|
|
|
|
res['rc'] = 0
|
|
|
|
|
|
|
|
for spec in items:
|
|
|
|
pkg = None
|
2012-03-12 21:31:13 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
# groups, again
|
|
|
|
if spec.startswith('@'):
|
|
|
|
pkg = spec
|
|
|
|
# dep/pkgname - find it
|
2012-03-27 19:58:49 +02:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
pkglist = what_provides(repoq, spec)
|
|
|
|
if not pkglist:
|
|
|
|
res['msg'] += "No Package matching '%s' found available, installed or updated" % spec
|
|
|
|
res['failed']=True
|
|
|
|
module.exit_json(**res)
|
|
|
|
found = False
|
|
|
|
nothing_to_do = False
|
|
|
|
can_be_installed = True
|
|
|
|
for this in pkglist:
|
|
|
|
if is_installed(repoq, this):
|
|
|
|
if is_update(repoq, this):
|
|
|
|
found = True
|
|
|
|
else:
|
|
|
|
nothing_to_do = True
|
|
|
|
|
|
|
|
if nothing_to_do:
|
|
|
|
res['results'] += "All packages providing %s are up to date" % spec
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
basecmd = 'install'
|
|
|
|
else:
|
|
|
|
basecmd = 'update'
|
|
|
|
|
|
|
|
|
|
|
|
pkg = spec
|
|
|
|
|
|
|
|
cmd = "%s %s '%s'" % (yum_basecmd, basecmd, pkg)
|
|
|
|
rc, out, err = run(cmd)
|
2012-03-27 20:15:48 +02:00
|
|
|
|
2012-03-12 21:31:13 +01: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
|
|
|
|
|
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
if rc:
|
|
|
|
res['changed'] = False
|
|
|
|
res['failed'] = True
|
|
|
|
res['rc'] = rc
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
|
|
|
else:
|
|
|
|
res['changed'] = True
|
|
|
|
res['failed'] = False
|
|
|
|
res['rc'] = 0
|
|
|
|
res['results'] += out
|
|
|
|
res['msg'] += err
|
|
|
|
|
|
|
|
module.exit_json(**res)
|
2012-03-12 21:31:13 +01:00
|
|
|
|
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
|
|
|
|
def ensure(module, state, pkgspec, conf_file):
|
|
|
|
res = {}
|
|
|
|
stdout = ""
|
|
|
|
stderr = ""
|
|
|
|
|
|
|
|
# take multiple args comma separated
|
|
|
|
items = [pkgspec]
|
|
|
|
if pkgspec.find(',') != -1:
|
|
|
|
items = pkgspec.split(',')
|
2012-07-26 02:14:15 +02:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
yum_basecmd = '/usr/bin/yum -d1 -y '
|
|
|
|
repoq = '/usr/bin/repoquery --plugins --quiet -q '
|
|
|
|
if conf_file and os.path.exists(conf_file):
|
|
|
|
yum_basecmd = '/usr/bin/yum -c %s -d1 -y' % conf_file
|
|
|
|
repoq = '/usr/bin/repoquery -c %s --plugins --quiet -q ' % conf_file
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
if state in ['installed', 'present']:
|
|
|
|
install(module, items, repoq, yum_basecmd)
|
|
|
|
elif state in ['removed', 'absent']:
|
|
|
|
remove(module, items, repoq, yum_basecmd)
|
|
|
|
elif state == 'latest':
|
|
|
|
latest(module, items, repoq, yum_basecmd)
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
# should be caught by AnsibleModule argument_spec
|
|
|
|
return dict(changed=False, failed=True, results='', errors='unexpected state')
|
|
|
|
|
2012-03-10 00:33:58 +01:00
|
|
|
|
|
|
|
def remove_only(pkgspec):
|
|
|
|
# remove this pkg and only this pkg - fail if it will require more to remove
|
|
|
|
pass
|
|
|
|
|
|
|
|
def main():
|
2012-03-10 00:50:34 +01:00
|
|
|
# state=installed pkg=pkgspec
|
|
|
|
# state=removed pkg=pkgspec
|
2012-04-01 08:35:19 +02:00
|
|
|
# state=latest pkg=pkgspec
|
2012-03-10 00:33:58 +01:00
|
|
|
#
|
2012-04-01 08:35:19 +02:00
|
|
|
# informational commands:
|
|
|
|
# list=installed
|
|
|
|
# list=updates
|
|
|
|
# list=available
|
|
|
|
# list=repos
|
|
|
|
# list=pkgspec
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-07-25 16:15:41 +02:00
|
|
|
module = AnsibleModule(
|
2012-07-26 02:14:15 +02:00
|
|
|
argument_spec = dict(
|
2012-08-02 07:47:48 +02:00
|
|
|
pkg=dict(aliases=['name']),
|
2012-07-26 02:14:15 +02: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-08-02 07:47:48 +02:00
|
|
|
conf_file=dict(default=None),
|
2012-07-26 02:14:15 +02:00
|
|
|
)
|
2012-07-25 16:15:41 +02:00
|
|
|
)
|
2012-03-10 08:19:57 +01:00
|
|
|
|
2012-07-25 16:15:41 +02:00
|
|
|
params = module.params
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 07:47:48 +02:00
|
|
|
|
2012-08-01 03:23:34 +02:00
|
|
|
if params['list'] and params['pkg']:
|
2012-07-26 02:14:15 +02:00
|
|
|
module.fail_json(msg="expected 'list=' or 'name=', but not both")
|
2012-03-10 00:33:58 +01:00
|
|
|
|
2012-08-02 02:33:13 +02:00
|
|
|
if params['list']:
|
2012-08-02 07:47:48 +02:00
|
|
|
results = dict(results=list_stuff(params['conf_file'], params['list']))
|
2012-07-26 02:14:15 +02:00
|
|
|
module.exit_json(**results)
|
2012-04-02 18:33:46 +02:00
|
|
|
|
2012-04-12 02:47:38 +02:00
|
|
|
else:
|
2012-07-26 02:14:15 +02:00
|
|
|
pkg = params['pkg']
|
2012-04-12 02:44:15 +02:00
|
|
|
if 'pkg' is None:
|
2012-07-26 02:14:15 +02:00
|
|
|
module.fail_json(msg="expected 'list=' or 'name='")
|
2012-03-12 21:31:13 +01:00
|
|
|
else:
|
2012-08-02 07:47:48 +02:00
|
|
|
state = params['state']
|
|
|
|
res = ensure(module, state, pkg, params['conf_file'])
|
|
|
|
module.fail_json(msg="we should never get here unless this all failed", **res)
|
2012-03-14 05:38:38 +01:00
|
|
|
|
2012-07-25 16:15:41 +02:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
|
|
|
|
main()
|
|
|
|
|