#!/usr/bin/python -tt # (c) 2012, Red Hat, Inc # Written by Seth Vidal # # 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 . # import os import sys import yum import subprocess import datetime import shlex import re import traceback try: import json except ImportError: import simplejson as json 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: my.setCacheDir() return my def pkg_to_dict(po): d = { 'name':po.name, 'arch':po.arch, 'epoch':po.epoch, 'release':po.release, 'version':po.version, 'repo':po.ui_from_repo, '_nevra':po.ui_nevra, } if type(po) == yum.rpmsack.RPMInstalledPackage: d['yumstate'] = 'installed' else: d['yumstate'] = 'available' return d def list_stuff(my, stuff): 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' r[repo.id] = s 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 if state == 'installed': pkg = None # check if pkgspec is installed if re.search('[>