From 4ed7ede1a12e58a6d4f66e0db8ced10f45a0f459 Mon Sep 17 00:00:00 2001 From: Merlin Mathesius Date: Oct 03 2017 20:14:02 +0000 Subject: standard-inventory-rpm updated to recognize and configure RPM repositories as test subjects --- diff --git a/inventory/standard-inventory-rpm b/inventory/standard-inventory-rpm index 31ca4a6..c007387 100755 --- a/inventory/standard-inventory-rpm +++ b/inventory/standard-inventory-rpm @@ -7,6 +7,7 @@ import shlex import subprocess import sys + def main(argv): parser = argparse.ArgumentParser(description="Inventory for local RPM installed host") parser.add_argument("--list", action="store_true", help="Verbose output") @@ -16,9 +17,9 @@ def main(argv): try: if opts.host: - data = host(opts.host) + data = gethost(opts.host) else: - data = list(opts.subjects) + data = getlist(opts.subjects) sys.stdout.write(json.dumps(data, indent=4, separators=(',', ': '))) except RuntimeError as ex: sys.stderr.write("{0}: {1}\n".format(os.path.basename(sys.argv[0]), str(ex))) @@ -26,21 +27,29 @@ def main(argv): return 0 -def list(subjects): - rpms = [ ] - hosts = [ ] - variables = { } + +def getlist(subjects): + repos = [] + rpms = [] + hosts = [] + variables = {} + for subject in subjects: if subject.endswith(".rpm"): rpms.append(subject) - if rpms: - vars = host(rpms) + elif isrepo(subject): + repos.append(subject) + + if repos or rpms: + vars = gethost(repos, rpms) if vars: hosts.append("rpms") variables["rpms"] = vars + return { "subjects": { "hosts": hosts, "vars": { } }, "localhost": { "hosts": hosts, "vars": { } }, "_meta": { "hostvars": variables } } -def host(rpms): + +def gethost(repos, rpms): null = open(os.devnull, 'w') # The variables @@ -55,14 +64,27 @@ def host(rpms): tty = None pass - install = [ "/usr/bin/yum", "-y", "install" ] + rpms + # enable any provided repos first so RPMs can pull dependencies from them if needed + for repo in repos: + addrepo = ["/usr/bin/yum", "config-manager", "--add-repo", repo] + try: + subprocess.check_call(addrepo, stdout=sys.stderr.fileno()) + except subprocess.CalledProcessError: + raise RuntimeError("could not add repo: {0}".format(repo)) - try: - subprocess.check_call(install, stdout=sys.stderr.fileno()) - except subprocess.CalledProcessError: - raise RuntimeError("could not install rpms: {0}".format(rpms)) + if rpms: + install = ["/usr/bin/yum", "-y", "install"] + rpms + try: + subprocess.check_call(install, stdout=sys.stderr.fileno()) + except subprocess.CalledProcessError: + raise RuntimeError("could not install rpms: {0}".format(rpms)) return variables + +def isrepo(subject): + return os.path.isfile(os.path.join(subject, "repodata", "repomd.xml")) + + if __name__ == '__main__': sys.exit(main(sys.argv))