view kcl/versions.py @ 0:0f44ee073173 default tip

fake salt, initial commit
author Jeff Hammel <k0scist@gmail.com>
date Mon, 06 Feb 2017 01:10:22 +0000
parents
children
line wrap: on
line source

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
compare package versions across nodes:

$ sudo rpm -q --qf "%{VERSION}" python-requests
2.6.0
"""


# imports
import argparse
import json
import kcl
import sys


def main(args=sys.argv[1:]):
    """CLI"""

    # parse command line
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('salt_master')
    parser.add_argument('package')
    parser.add_argument('--group', dest='group',
                        action='store_true', default=False,
                        help="group hosts by versions")
    options = parser.parse_args(args)

    # instantiate API
    salt = kcl.KCl(options.salt_master)

    # get versions
    command = 'rpm -q --qf "%{VERSION}" ' + options.package
    code, versions = salt.run(command)

    # output
    if options.group:
        # group host by package versions
        group = {}
        for host, version in versions.items():
            group.setdefault(version, []).append(host)
        print (json.dumps(group, indent=2, sort_keys=True))
    else:
        print (json.dumps(versions, indent=2, sort_keys=True))

    sys.exit(code)

if __name__ == '__main__':
    main()