comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:0f44ee073173
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 """
5 compare package versions across nodes:
6
7 $ sudo rpm -q --qf "%{VERSION}" python-requests
8 2.6.0
9 """
10
11
12 # imports
13 import argparse
14 import json
15 import kcl
16 import sys
17
18
19 def main(args=sys.argv[1:]):
20 """CLI"""
21
22 # parse command line
23 parser = argparse.ArgumentParser(description=__doc__)
24 parser.add_argument('salt_master')
25 parser.add_argument('package')
26 parser.add_argument('--group', dest='group',
27 action='store_true', default=False,
28 help="group hosts by versions")
29 options = parser.parse_args(args)
30
31 # instantiate API
32 salt = kcl.KCl(options.salt_master)
33
34 # get versions
35 command = 'rpm -q --qf "%{VERSION}" ' + options.package
36 code, versions = salt.run(command)
37
38 # output
39 if options.group:
40 # group host by package versions
41 group = {}
42 for host, version in versions.items():
43 group.setdefault(version, []).append(host)
44 print (json.dumps(group, indent=2, sort_keys=True))
45 else:
46 print (json.dumps(versions, indent=2, sort_keys=True))
47
48 sys.exit(code)
49
50 if __name__ == '__main__':
51 main()